pespin submitted this change.

View Change

Approvals: laforge: Looks good to me, but someone else must approve osmith: Looks good to me, but someone else must approve pespin: Looks good to me, approved Jenkins Builder: Verified
osmo_io: Lazy-initialize during osmo_iofd_setup()

This way new threads (!=main) don't need to explicitly call
osmo_iofd_init() explicitly before using osmo_io. This is specially
useful for threads using osmo_io indirectly, eg, when logging.

The osmo_iofd_setup() API is really the only main API which requires
initialization. All other APIs requiring it come after osmo_iofd_setup()
since they use an osmo_iofd pointer.

Change-Id: Ia543c1b9d111c929cd9dbed266b6d21b74e47e4b
---
M src/core/osmo_io.c
1 file changed, 7 insertions(+), 0 deletions(-)

diff --git a/src/core/osmo_io.c b/src/core/osmo_io.c
index 9e159a1..7c59a61 100644
--- a/src/core/osmo_io.c
+++ b/src/core/osmo_io.c
@@ -69,9 +69,14 @@
/* Used by some tests, can't be static */
struct iofd_backend_ops osmo_iofd_ops;

+static __thread bool g_thread_initialized = false;
+
/*! initialize osmo_io for the current thread */
void osmo_iofd_init(void)
{
+ if (g_thread_initialized)
+ return;
+
switch (g_io_backend) {
case OSMO_IO_BACKEND_POLL:
break;
@@ -84,6 +89,7 @@
OSMO_ASSERT(0);
break;
}
+ g_thread_initialized = true;
}

/* ensure main thread always has pre-initialized osmo_io
@@ -833,6 +839,7 @@
const struct osmo_io_ops *ioops, void *data)
{
struct osmo_io_fd *iofd;
+ osmo_iofd_init();

/* reject unsupported/unknown modes */
switch (mode) {

To view, visit change 42084. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-MessageType: merged
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: Ia543c1b9d111c929cd9dbed266b6d21b74e47e4b
Gerrit-Change-Number: 42084
Gerrit-PatchSet: 1
Gerrit-Owner: pespin <pespin@sysmocom.de>
Gerrit-Reviewer: Jenkins Builder
Gerrit-Reviewer: laforge <laforge@osmocom.org>
Gerrit-Reviewer: osmith <osmith@sysmocom.de>
Gerrit-Reviewer: pespin <pespin@sysmocom.de>