From afc6adb5ec7e73bc13156c43f52fb015cd80cc68 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Mon, 11 Nov 2013 19:34:13 +0100 Subject: bus: introduce concept of a "default" event loop per-thread and make use of it everywhere Try to emphasize a bit that there should be a mapping between event loops and threads, hence introduce a logic that there's one "default" event loop for each thread, that can be queried via "sd_event_default()". --- src/libsystemd-bus/libsystemd-bus.sym | 6 ++++- src/libsystemd-bus/sd-event.c | 44 ++++++++++++++++++++++++++++++++++- src/libsystemd-bus/test-event.c | 2 +- 3 files changed, 49 insertions(+), 3 deletions(-) (limited to 'src/libsystemd-bus') diff --git a/src/libsystemd-bus/libsystemd-bus.sym b/src/libsystemd-bus/libsystemd-bus.sym index 2c4f07d753..796a395a17 100644 --- a/src/libsystemd-bus/libsystemd-bus.sym +++ b/src/libsystemd-bus/libsystemd-bus.sym @@ -197,6 +197,8 @@ global: sd_memfd_set_size; /* sd-event functions */ + sd_event_default; + sd_event_new; sd_event_ref; sd_event_unref; @@ -213,15 +215,17 @@ global: sd_event_loop; sd_event_get_state; + sd_event_get_tid; sd_event_get_quit; sd_event_request_quit; sd_event_get_now_realtime; sd_event_get_now_monotonic; - sd_event_get; sd_event_source_ref; sd_event_source_unref; + sd_event_get; + sd_event_source_set_prepare; sd_event_source_get_pending; sd_event_source_get_priority; diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 97e6db289b..8025d24bb7 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -23,12 +23,13 @@ #include #include +#include "sd-id128.h" #include "macro.h" #include "prioq.h" #include "hashmap.h" #include "util.h" #include "time-util.h" -#include "sd-id128.h" +#include "missing.h" #include "sd-event.h" @@ -138,6 +139,9 @@ struct sd_event { bool quit_requested:1; bool need_process_child:1; + + pid_t tid; + sd_event **default_event_ptr; }; static int pending_prioq_compare(const void *a, const void *b) { @@ -304,6 +308,9 @@ static int quit_prioq_compare(const void *a, const void *b) { static void event_free(sd_event *e) { assert(e); + if (e->default_event_ptr) + *(e->default_event_ptr) = NULL; + if (e->epoll_fd >= 0) close_nointr_nofail(e->epoll_fd); @@ -1869,3 +1876,38 @@ _public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) { *usec = e->timestamp.monotonic; return 0; } + +_public_ int sd_event_default(sd_event **ret) { + + static __thread sd_event *default_event = NULL; + sd_event *e; + int r; + + if (!ret) + return !!default_event; + + if (default_event) { + *ret = sd_event_ref(default_event); + return 0; + } + + r = sd_event_new(&e); + if (r < 0) + return r; + + e->default_event_ptr = &default_event; + e->tid = gettid(); + default_event = e; + + *ret = e; + return 1; +} + +_public_ int sd_event_get_tid(sd_event *e, pid_t *tid) { + assert_return(e, -EINVAL); + assert_return(tid, -EINVAL); + assert_return(e->tid != 0, -ENXIO); + + *tid = e->tid; + return 0; +} diff --git a/src/libsystemd-bus/test-event.c b/src/libsystemd-bus/test-event.c index 65c1c296f8..ba60a47a34 100644 --- a/src/libsystemd-bus/test-event.c +++ b/src/libsystemd-bus/test-event.c @@ -163,7 +163,7 @@ int main(int argc, char *argv[]) { assert_se(pipe(b) >= 0); assert_se(pipe(d) >= 0); - assert_se(sd_event_new(&e) >= 0); + assert_se(sd_event_default(&e) >= 0); got_a = false, got_b = false, got_c = false, got_d = 0; -- cgit v1.2.3-54-g00ecf