aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2019-04-06 13:46:40 +0200
committerHarald Welte <laforge@gnumonks.org>2019-07-31 11:50:27 +0200
commit0a27aa263e6dc20da060aea925ef64aba15dabb0 (patch)
tree9d4fa501f299107bce35f8571d8f48be9ce1dcb1
parent6079675bdb652045eff48880b0d9938b3e7e79ad (diff)
select: Make file descriptor lists per-thread
In a multi-threaded environemnt, it's likely that each thread will have its own, distinct set of file descriptors that it wants to watch. Hence, let's make the osmo_fd_* functions configure not one global list of file descriptors, but a thread-local list of file descriptors. Change-Id: I5082ed3e500ad1a7516e1785bc57e008da2fac9a
-rw-r--r--include/osmocom/core/select.h1
-rw-r--r--src/select.c20
2 files changed, 18 insertions, 3 deletions
diff --git a/include/osmocom/core/select.h b/include/osmocom/core/select.h
index a200b6f3..92904e2f 100644
--- a/include/osmocom/core/select.h
+++ b/include/osmocom/core/select.h
@@ -52,6 +52,7 @@ void osmo_fd_unregister(struct osmo_fd *fd);
void osmo_fd_close(struct osmo_fd *fd);
int osmo_select_main(int polling);
int osmo_select_main_ctx(int polling);
+void osmo_select_init(void);
struct osmo_fd *osmo_fd_get_by_fd(int fd);
diff --git a/src/select.c b/src/select.c
index 34a133c0..3d4803f7 100644
--- a/src/select.c
+++ b/src/select.c
@@ -50,9 +50,11 @@
*
* \file select.c */
-static int maxfd = 0;
-static LLIST_HEAD(osmo_fds);
-static int unregistered_count;
+/* keep a set of file descriptors per-thread, so that each thread can have its own
+ * distinct set of file descriptors to interact with */
+static __thread int maxfd = 0;
+static __thread struct llist_head osmo_fds; /* TLS cannot use LLIST_HEAD() */
+static __thread int unregistered_count;
/*! Set up an osmo-fd. Will not register it.
* \param[inout] ofd Osmo FD to be set-up
@@ -308,6 +310,18 @@ struct osmo_fd *osmo_fd_get_by_fd(int fd)
return NULL;
}
+/*! initialize the osmocom select abstraction for the current thread */
+void osmo_select_init(void)
+{
+ INIT_LLIST_HEAD(&osmo_fds);
+}
+
+/* ensure main thread always has pre-initialized osmo_fds */
+static __attribute__((constructor)) void on_dso_load_select(void)
+{
+ osmo_select_init();
+}
+
#ifdef HAVE_SYS_TIMERFD_H
#include <sys/timerfd.h>