diff options
author | Lennart Poettering <lennart@poettering.net> | 2013-11-11 22:00:48 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2013-11-12 00:12:43 +0100 |
commit | 76b543756ef69ce69784d571aefe8de65eaeb331 (patch) | |
tree | 092b117494d53fe09a9de227009d60d94d781659 /src/libsystemd-bus | |
parent | afc6adb5ec7e73bc13156c43f52fb015cd80cc68 (diff) |
bus: introduce concept of a default bus for each thread and make use of it everywhere
We want to emphasize bus connections as per-thread communication
primitives, hence introduce a concept of a per-thread default bus, and
make use of it everywhere.
Diffstat (limited to 'src/libsystemd-bus')
-rw-r--r-- | src/libsystemd-bus/bus-internal.h | 3 | ||||
-rw-r--r-- | src/libsystemd-bus/bus-util.c | 4 | ||||
-rw-r--r-- | src/libsystemd-bus/libsystemd-bus.sym | 3 | ||||
-rw-r--r-- | src/libsystemd-bus/sd-bus.c | 74 | ||||
-rw-r--r-- | src/libsystemd-bus/sd-event.c | 10 |
5 files changed, 82 insertions, 12 deletions
diff --git a/src/libsystemd-bus/bus-internal.h b/src/libsystemd-bus/bus-internal.h index cd9f37edd7..4f9d941c63 100644 --- a/src/libsystemd-bus/bus-internal.h +++ b/src/libsystemd-bus/bus-internal.h @@ -249,6 +249,9 @@ struct sd_bus { sd_event *event; sd_bus_message *current; + + sd_bus **default_bus_ptr; + pid_t tid; }; #define BUS_DEFAULT_TIMEOUT ((usec_t) (25 * USEC_PER_SEC)) diff --git a/src/libsystemd-bus/bus-util.c b/src/libsystemd-bus/bus-util.c index d277e1e0f0..33bdf38171 100644 --- a/src/libsystemd-bus/bus-util.c +++ b/src/libsystemd-bus/bus-util.c @@ -944,9 +944,9 @@ int bus_open_transport(BusTransport transport, const char *host, bool user, sd_b case BUS_TRANSPORT_LOCAL: if (user) - r = sd_bus_open_user(bus); + r = sd_bus_default_user(bus); else - r = sd_bus_open_system(bus); + r = sd_bus_default_system(bus); break; diff --git a/src/libsystemd-bus/libsystemd-bus.sym b/src/libsystemd-bus/libsystemd-bus.sym index 796a395a17..b6ad84b98b 100644 --- a/src/libsystemd-bus/libsystemd-bus.sym +++ b/src/libsystemd-bus/libsystemd-bus.sym @@ -12,6 +12,8 @@ global: /* Same order as in sd-bus.h should be used */ /* Connections */ + sd_bus_default_user; + sd_bus_default_system; sd_bus_open_user; sd_bus_open_system; sd_bus_open_system_remote; @@ -49,6 +51,7 @@ global: sd_bus_wait; sd_bus_flush; sd_bus_get_current; + sd_bus_get_tid; sd_bus_attach_event; sd_bus_detach_event; sd_bus_add_filter; diff --git a/src/libsystemd-bus/sd-bus.c b/src/libsystemd-bus/sd-bus.c index fdfbbeb2be..3ecda17fce 100644 --- a/src/libsystemd-bus/sd-bus.c +++ b/src/libsystemd-bus/sd-bus.c @@ -2370,16 +2370,21 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) { int r; assert_return(bus, -EINVAL); - assert_return(event, -EINVAL); assert_return(!bus->event, -EBUSY); assert(!bus->input_io_event_source); assert(!bus->output_io_event_source); assert(!bus->time_event_source); - bus->event = sd_event_ref(event); + if (event) + bus->event = sd_event_ref(event); + else { + r = sd_event_default(&bus->event); + if (r < 0) + return r; + } - r = sd_event_add_io(event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source); + r = sd_event_add_io(bus->event, bus->input_fd, 0, io_callback, bus, &bus->input_io_event_source); if (r < 0) goto fail; @@ -2388,7 +2393,7 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) { goto fail; if (bus->output_fd != bus->input_fd) { - r = sd_event_add_io(event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source); + r = sd_event_add_io(bus->event, bus->output_fd, 0, io_callback, bus, &bus->output_io_event_source); if (r < 0) goto fail; @@ -2401,7 +2406,7 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) { if (r < 0) goto fail; - r = sd_event_add_monotonic(event, 0, 0, time_callback, bus, &bus->time_event_source); + r = sd_event_add_monotonic(bus->event, 0, 0, time_callback, bus, &bus->time_event_source); if (r < 0) goto fail; @@ -2409,7 +2414,7 @@ _public_ int sd_bus_attach_event(sd_bus *bus, sd_event *event, int priority) { if (r < 0) goto fail; - r = sd_event_add_quit(event, quit_callback, bus, &bus->quit_event_source); + r = sd_event_add_quit(bus->event, quit_callback, bus, &bus->quit_event_source); if (r < 0) goto fail; @@ -2442,8 +2447,63 @@ _public_ int sd_bus_detach_event(sd_bus *bus) { return 0; } -sd_bus_message* sd_bus_get_current(sd_bus *bus) { +_public_ sd_bus_message* sd_bus_get_current(sd_bus *bus) { assert_return(bus, NULL); return bus->current; } + +static int bus_default(int (*bus_open)(sd_bus **), sd_bus **default_bus, sd_bus **ret) { + sd_bus *b = NULL; + int r; + + assert(bus_open); + assert(default_bus); + + if (!ret) + return !!*default_bus; + + if (*default_bus) { + *ret = sd_bus_ref(*default_bus); + return 0; + } + + r = bus_open(&b); + if (r < 0) + return r; + + b->default_bus_ptr = default_bus; + b->tid = gettid(); + *default_bus = b; + + *ret = b; + return 1; +} + +_public_ int sd_bus_default_system(sd_bus **ret) { + static __thread sd_bus *default_system_bus = NULL; + + return bus_default(sd_bus_open_system, &default_system_bus, ret); +} + +_public_ int sd_bus_default_user(sd_bus **ret) { + static __thread sd_bus *default_user_bus = NULL; + + return bus_default(sd_bus_open_user, &default_user_bus, ret); +} + +_public_ int sd_bus_get_tid(sd_bus *b, pid_t *tid) { + assert_return(b, -EINVAL); + assert_return(tid, -EINVAL); + assert_return(!bus_pid_changed(b), -ECHILD); + + if (b->tid != 0) { + *tid = b->tid; + return 0; + } + + if (b->event) + return sd_event_get_tid(b->event, tid); + + return -ENXIO; +} diff --git a/src/libsystemd-bus/sd-event.c b/src/libsystemd-bus/sd-event.c index 8025d24bb7..0996316986 100644 --- a/src/libsystemd-bus/sd-event.c +++ b/src/libsystemd-bus/sd-event.c @@ -1906,8 +1906,12 @@ _public_ int sd_event_default(sd_event **ret) { _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); + assert_return(!event_pid_changed(e), -ECHILD); - *tid = e->tid; - return 0; + if (e->tid != 0) { + *tid = e->tid; + return 0; + } + + return -ENXIO; } |