chiark / gitweb /
event: clear pending-state when re-arming timers
[elogind.git] / src / libsystemd-bus / sd-event.c
index c52e4cd..d01e82d 100644 (file)
 #include <sys/timerfd.h>
 #include <sys/wait.h>
 
+#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"
 
@@ -53,7 +54,7 @@ struct sd_event_source {
         sd_prepare_handler_t prepare;
 
         EventSourceType type:4;
-        int mute:3;
+        int enabled:3;
         bool pending:1;
 
         int priority;
@@ -126,17 +127,21 @@ struct sd_event {
         sd_event_source **signal_sources;
 
         Hashmap *child_sources;
-        unsigned n_unmuted_child_sources;
+        unsigned n_enabled_child_sources;
 
         Prioq *quit;
 
         pid_t original_pid;
 
         unsigned iteration;
+        dual_timestamp timestamp;
         int state;
 
         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) {
@@ -145,10 +150,10 @@ static int pending_prioq_compare(const void *a, const void *b) {
         assert(x->pending);
         assert(y->pending);
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Lower priority values first */
@@ -186,10 +191,10 @@ static int prepare_prioq_compare(const void *a, const void *b) {
         if (x->prepare_iteration > y->prepare_iteration)
                 return 1;
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Lower priority values first */
@@ -213,10 +218,10 @@ static int earliest_time_prioq_compare(const void *a, const void *b) {
         assert(x->type == SOURCE_MONOTONIC || x->type == SOURCE_REALTIME);
         assert(y->type == SOURCE_MONOTONIC || y->type == SOURCE_REALTIME);
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Move the pending ones to the end */
@@ -246,10 +251,10 @@ static int latest_time_prioq_compare(const void *a, const void *b) {
         assert((x->type == SOURCE_MONOTONIC && y->type == SOURCE_MONOTONIC) ||
                (x->type == SOURCE_REALTIME && y->type == SOURCE_REALTIME));
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Move the pending ones to the end */
@@ -279,10 +284,10 @@ static int quit_prioq_compare(const void *a, const void *b) {
         assert(x->type == SOURCE_QUIT);
         assert(y->type == SOURCE_QUIT);
 
-        /* Unmuted ones first */
-        if (x->mute != SD_EVENT_MUTED && y->mute == SD_EVENT_MUTED)
+        /* Enabled ones first */
+        if (x->enabled != SD_EVENT_OFF && y->enabled == SD_EVENT_OFF)
                 return -1;
-        if (x->mute == SD_EVENT_MUTED && y->mute != SD_EVENT_MUTED)
+        if (x->enabled == SD_EVENT_OFF && y->enabled != SD_EVENT_OFF)
                 return 1;
 
         /* Lower priority values first */
@@ -303,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);
 
@@ -329,12 +337,11 @@ static void event_free(sd_event *e) {
         free(e);
 }
 
-int sd_event_new(sd_event** ret) {
+_public_ int sd_event_new(sd_event** ret) {
         sd_event *e;
         int r;
 
-        if (!ret)
-                return -EINVAL;
+        assert_return(ret, -EINVAL);
 
         e = new0(sd_event, 1);
         if (!e)
@@ -367,9 +374,8 @@ fail:
         return r;
 }
 
-sd_event* sd_event_ref(sd_event *e) {
-        if (!e)
-                return NULL;
+_public_ sd_event* sd_event_ref(sd_event *e) {
+        assert_return(e, NULL);
 
         assert(e->n_ref >= 1);
         e->n_ref++;
@@ -377,9 +383,8 @@ sd_event* sd_event_ref(sd_event *e) {
         return e;
 }
 
-sd_event* sd_event_unref(sd_event *e) {
-        if (!e)
-                return NULL;
+_public_ sd_event* sd_event_unref(sd_event *e) {
+        assert_return(e, NULL);
 
         assert(e->n_ref >= 1);
         e->n_ref--;
@@ -416,18 +421,22 @@ static int source_io_unregister(sd_event_source *s) {
         return 0;
 }
 
-static int source_io_register(sd_event_source *s, int mute, uint32_t events) {
+static int source_io_register(
+                sd_event_source *s,
+                int enabled,
+                uint32_t events) {
+
         struct epoll_event ev = {};
         int r;
 
         assert(s);
         assert(s->type == SOURCE_IO);
-        assert(mute != SD_EVENT_MUTED);
+        assert(enabled != SD_EVENT_OFF);
 
         ev.events = events;
         ev.data.ptr = s;
 
-        if (mute == SD_EVENT_ONESHOT)
+        if (enabled == SD_EVENT_ONESHOT)
                 ev.events |= EPOLLONESHOT;
 
         if (s->io.registered)
@@ -467,7 +476,7 @@ static void source_free(sd_event_source *s) {
 
                 case SOURCE_SIGNAL:
                         if (s->signal.sig > 0) {
-                                if (s->signal.sig != SIGCHLD || s->event->n_unmuted_child_sources == 0)
+                                if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)
                                         assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
 
                                 if (s->event->signal_sources)
@@ -478,9 +487,9 @@ static void source_free(sd_event_source *s) {
 
                 case SOURCE_CHILD:
                         if (s->child.pid > 0) {
-                                if (s->mute != SD_EVENT_MUTED) {
-                                        assert(s->event->n_unmuted_child_sources > 0);
-                                        s->event->n_unmuted_child_sources--;
+                                if (s->enabled != SD_EVENT_OFF) {
+                                        assert(s->event->n_enabled_child_sources > 0);
+                                        s->event->n_enabled_child_sources--;
                                 }
 
                                 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD])
@@ -491,6 +500,10 @@ static void source_free(sd_event_source *s) {
 
                         break;
 
+                case SOURCE_DEFER:
+                        /* nothing */
+                        break;
+
                 case SOURCE_QUIT:
                         prioq_remove(s->event->quit, s, &s->quit.prioq_index);
                         break;
@@ -550,7 +563,7 @@ static sd_event_source *source_new(sd_event *e, EventSourceType type) {
         return s;
 }
 
-int sd_event_add_io(
+_public_ int sd_event_add_io(
                 sd_event *e,
                 int fd,
                 uint32_t events,
@@ -561,19 +574,13 @@ int sd_event_add_io(
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (fd < 0)
-                return -EINVAL;
-        if (events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP))
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(fd >= 0, -EINVAL);
+        assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP)), -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         s = source_new(e, SOURCE_IO);
         if (!s)
@@ -583,9 +590,9 @@ int sd_event_add_io(
         s->io.events = events;
         s->io.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_UNMUTED;
+        s->enabled = SD_EVENT_ON;
 
-        r = source_io_register(s, s->mute, events);
+        r = source_io_register(s, s->enabled, events);
         if (r < 0) {
                 source_free(s);
                 return -errno;
@@ -655,19 +662,13 @@ static int event_add_time_internal(
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
-        if (usec == (uint64_t) -1)
-                return -EINVAL;
-        if (accuracy == (uint64_t) -1)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
+        assert_return(usec != (uint64_t) -1, -EINVAL);
+        assert_return(accuracy != (uint64_t) -1, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         assert(timer_fd);
         assert(earliest);
@@ -700,7 +701,7 @@ static int event_add_time_internal(
         s->time.callback = callback;
         s->time.earliest_index = s->time.latest_index = PRIOQ_IDX_NULL;
         s->userdata = userdata;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = prioq_put(*earliest, s, &s->time.earliest_index);
         if (r < 0)
@@ -718,11 +719,23 @@ fail:
         return r;
 }
 
-int sd_event_add_monotonic(sd_event *e, uint64_t usec, uint64_t accuracy, sd_time_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_monotonic(sd_event *e,
+                                    uint64_t usec,
+                                    uint64_t accuracy,
+                                    sd_time_handler_t callback,
+                                    void *userdata,
+                                    sd_event_source **ret) {
+
         return event_add_time_internal(e, SOURCE_MONOTONIC, &e->monotonic_fd, CLOCK_MONOTONIC, &e->monotonic_earliest, &e->monotonic_latest, usec, accuracy, callback, userdata, ret);
 }
 
-int sd_event_add_realtime(sd_event *e, uint64_t usec, uint64_t accuracy, sd_time_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_realtime(sd_event *e,
+                                   uint64_t usec,
+                                   uint64_t accuracy,
+                                   sd_time_handler_t callback,
+                                   void *userdata,
+                                   sd_event_source **ret) {
+
         return event_add_time_internal(e, SOURCE_REALTIME, &e->realtime_fd, CLOCK_REALTIME, &e->realtime_earliest, &e->monotonic_latest, usec, accuracy, callback, userdata, ret);
 }
 
@@ -758,23 +771,23 @@ static int event_update_signal_fd(sd_event *e) {
         return 0;
 }
 
-int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_signal(
+                sd_event *e,
+                int sig,
+                sd_signal_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (sig <= 0)
-                return -EINVAL;
-        if (sig >= _NSIG)
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(sig > 0, -EINVAL);
+        assert_return(sig < _NSIG, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         if (!e->signal_sources) {
                 e->signal_sources = new0(sd_event_source*, _NSIG);
@@ -790,12 +803,12 @@ int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void
         s->signal.sig = sig;
         s->signal.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_UNMUTED;
+        s->enabled = SD_EVENT_ON;
 
         e->signal_sources[sig] = s;
         assert_se(sigaddset(&e->sigset, sig) == 0);
 
-        if (sig != SIGCHLD || e->n_unmuted_child_sources == 0) {
+        if (sig != SIGCHLD || e->n_enabled_child_sources == 0) {
                 r = event_update_signal_fd(e);
                 if (r < 0) {
                         source_free(s);
@@ -807,23 +820,25 @@ int sd_event_add_signal(sd_event *e, int sig, sd_signal_handler_t callback, void
         return 0;
 }
 
-int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_child(
+                sd_event *e,
+                pid_t pid,
+                int options,
+                sd_child_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (pid <= 1)
-                return -EINVAL;
-        if (options & ~(WEXITED|WSTOPPED|WCONTINUED))
-                return -EINVAL;
-        if (!callback)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(pid > 1, -EINVAL);
+        assert_return(!(options & ~(WEXITED|WSTOPPED|WCONTINUED)), -EINVAL);
+        assert_return(options != 0, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         r = hashmap_ensure_allocated(&e->child_sources, trivial_hash_func, trivial_compare_func);
         if (r < 0)
@@ -840,7 +855,7 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
         s->child.options = options;
         s->child.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = hashmap_put(e->child_sources, INT_TO_PTR(pid), s);
         if (r < 0) {
@@ -848,7 +863,7 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
                 return r;
         }
 
-        e->n_unmuted_child_sources ++;
+        e->n_enabled_child_sources ++;
 
         assert_se(sigaddset(&e->sigset, SIGCHLD) == 0);
 
@@ -866,17 +881,20 @@ int sd_event_add_child(sd_event *e, pid_t pid, int options, sd_child_handler_t c
         return 0;
 }
 
-int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_defer(
+                sd_event *e,
+                sd_defer_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
-        if (!e)
-                return -EINVAL;
-        if (!ret)
-                return -EINVAL;
+        assert_return(e, -EINVAL);
+        assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(e))
-                return -ECHILD;
+        assert_return(!event_pid_changed(e), -ECHILD);
 
         s = source_new(e, SOURCE_DEFER);
         if (!s)
@@ -884,7 +902,7 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
 
         s->defer.callback = callback;
         s->userdata = userdata;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = source_set_pending(s, true);
         if (r < 0) {
@@ -896,12 +914,18 @@ int sd_event_add_defer(sd_event *e, sd_defer_handler_t callback, void *userdata,
         return 0;
 }
 
-int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, sd_event_source **ret) {
+_public_ int sd_event_add_quit(
+                sd_event *e,
+                sd_quit_handler_t callback,
+                void *userdata,
+                sd_event_source **ret) {
+
         sd_event_source *s;
         int r;
 
         assert_return(e, -EINVAL);
         assert_return(callback, -EINVAL);
+        assert_return(ret, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         assert_return(!event_pid_changed(e), -ECHILD);
 
@@ -918,7 +942,7 @@ int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, s
         s->quit.callback = callback;
         s->userdata = userdata;
         s->quit.prioq_index = PRIOQ_IDX_NULL;
-        s->mute = SD_EVENT_ONESHOT;
+        s->enabled = SD_EVENT_ONESHOT;
 
         r = prioq_put(s->event->quit, s, &s->quit.prioq_index);
         if (r < 0) {
@@ -930,7 +954,7 @@ int sd_event_add_quit(sd_event *e, sd_quit_handler_t callback, void *userdata, s
         return 0;
 }
 
-sd_event_source* sd_event_source_ref(sd_event_source *s) {
+_public_ sd_event_source* sd_event_source_ref(sd_event_source *s) {
         assert_return(s, NULL);
 
         assert(s->n_ref >= 1);
@@ -939,7 +963,7 @@ sd_event_source* sd_event_source_ref(sd_event_source *s) {
         return s;
 }
 
-sd_event_source* sd_event_source_unref(sd_event_source *s) {
+_public_ sd_event_source* sd_event_source_unref(sd_event_source *s) {
         assert_return(s, NULL);
 
         assert(s->n_ref >= 1);
@@ -951,66 +975,53 @@ sd_event_source* sd_event_source_unref(sd_event_source *s) {
         return NULL;
 }
 
-sd_event *sd_event_get(sd_event_source *s) {
-        if (!s)
-                return NULL;
+_public_ sd_event *sd_event_get(sd_event_source *s) {
+        assert_return(s, NULL);
 
         return s->event;
 }
 
-int sd_event_source_get_pending(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
+_public_ int sd_event_source_get_pending(sd_event_source *s) {
+        assert_return(s, -EINVAL);
+        assert_return(s->type != SOURCE_QUIT, -EDOM);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->pending;
 }
 
-int sd_event_source_get_io_fd(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_IO)
-                return -EDOM;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_io_fd(sd_event_source *s) {
+        assert_return(s, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->io.fd;
 }
 
-int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_IO)
-                return -EDOM;
-        if (!events)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_io_events(sd_event_source *s, uint32_t* events) {
+        assert_return(s, -EINVAL);
+        assert_return(events, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *events = s->io.events;
         return 0;
 }
 
-int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
+_public_ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
         int r;
 
-        if (!s)
-                return -EINVAL;
-        if (!s->type != SOURCE_IO)
-                return -EDOM;
-        if (events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP))
-                return -EINVAL;
+        assert_return(s, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(!(events & ~(EPOLLIN|EPOLLOUT|EPOLLRDHUP|EPOLLPRI|EPOLLERR|EPOLLHUP)), -EINVAL);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->io.events == events)
                 return 0;
 
-        if (s->mute != SD_EVENT_MUTED) {
-                r = source_io_register(s, s->io.events, events);
+        if (s->enabled != SD_EVENT_OFF) {
+                r = source_io_register(s, s->enabled, events);
                 if (r < 0)
                         return r;
         }
@@ -1020,49 +1031,36 @@ int sd_event_source_set_io_events(sd_event_source *s, uint32_t events) {
         return 0;
 }
 
-int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_IO)
-                return -EDOM;
-        if (!revents)
-                return -EINVAL;
-        if (!s->pending)
-                return -ENODATA;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_io_revents(sd_event_source *s, uint32_t* revents) {
+        assert_return(s, -EINVAL);
+        assert_return(revents, -EINVAL);
+        assert_return(s->type == SOURCE_IO, -EDOM);
+        assert_return(s->pending, -ENODATA);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *revents = s->io.revents;
         return 0;
 }
 
-int sd_event_source_get_signal(sd_event_source *s) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_SIGNAL)
-                return -EDOM;
-        assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_signal(sd_event_source *s) {
+        assert_return(s, -EINVAL);
+        assert_return(s->type == SOURCE_SIGNAL, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->signal.sig;
 }
 
-int sd_event_source_get_priority(sd_event_source *s, int *priority) {
-        if (!s)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_priority(sd_event_source *s, int *priority) {
+        assert_return(s, -EINVAL);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         return s->priority;
 }
 
-int sd_event_source_set_priority(sd_event_source *s, int priority) {
-        if (!s)
-                return -EINVAL;
+_public_ int sd_event_source_set_priority(sd_event_source *s, int priority) {
+        assert_return(s, -EINVAL);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->priority == priority)
                 return 0;
@@ -1075,36 +1073,33 @@ int sd_event_source_set_priority(sd_event_source *s, int priority) {
         if (s->prepare)
                 prioq_reshuffle(s->event->prepare, s, &s->prepare_index);
 
+        if (s->type == SOURCE_QUIT)
+                prioq_reshuffle(s->event->quit, s, &s->quit.prioq_index);
+
         return 0;
 }
 
-int sd_event_source_get_mute(sd_event_source *s, int *m) {
-        if (!s)
-                return -EINVAL;
-        if (!m)
-                return -EINVAL;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_enabled(sd_event_source *s, int *m) {
+        assert_return(s, -EINVAL);
+        assert_return(m, -EINVAL);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
-        *m = s->mute;
+        *m = s->enabled;
         return 0;
 }
 
-int sd_event_source_set_mute(sd_event_source *s, int m) {
+_public_ int sd_event_source_set_enabled(sd_event_source *s, int m) {
         int r;
 
-        if (!s)
-                return -EINVAL;
-        if (m != SD_EVENT_MUTED && m != SD_EVENT_UNMUTED && !SD_EVENT_ONESHOT)
-                return -EINVAL;
+        assert_return(s, -EINVAL);
+        assert_return(m == SD_EVENT_OFF || m == SD_EVENT_ON || m == SD_EVENT_ONESHOT, -EINVAL);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
-        if (s->mute == m)
+        if (s->enabled == m)
                 return 0;
 
-        if (m == SD_EVENT_MUTED) {
+        if (m == SD_EVENT_OFF) {
 
                 switch (s->type) {
 
@@ -1113,24 +1108,24 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         if (r < 0)
                                 return r;
 
-                        s->mute = m;
+                        s->enabled = m;
                         break;
 
                 case SOURCE_MONOTONIC:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_REALTIME:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_SIGNAL:
-                        s->mute = m;
-                        if (s->signal.sig != SIGCHLD || s->event->n_unmuted_child_sources == 0) {
+                        s->enabled = m;
+                        if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0) {
                                 assert_se(sigdelset(&s->event->sigset, s->signal.sig) == 0);
                                 event_update_signal_fd(s->event);
                         }
@@ -1138,10 +1133,10 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         break;
 
                 case SOURCE_CHILD:
-                        s->mute = m;
+                        s->enabled = m;
 
-                        assert(s->event->n_unmuted_child_sources > 0);
-                        s->event->n_unmuted_child_sources--;
+                        assert(s->event->n_enabled_child_sources > 0);
+                        s->event->n_enabled_child_sources--;
 
                         if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
                                 assert_se(sigdelset(&s->event->sigset, SIGCHLD) == 0);
@@ -1150,9 +1145,13 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
 
                         break;
 
-                case SOURCE_DEFER:
                 case SOURCE_QUIT:
-                        s->mute = m;
+                        s->enabled = m;
+                        prioq_reshuffle(s->event->quit, s, &s->quit.prioq_index);
+                        break;
+
+                case SOURCE_DEFER:
+                        s->enabled = m;
                         break;
                 }
 
@@ -1164,35 +1163,35 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         if (r < 0)
                                 return r;
 
-                        s->mute = m;
+                        s->enabled = m;
                         break;
 
                 case SOURCE_MONOTONIC:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->monotonic_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->monotonic_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_REALTIME:
-                        s->mute = m;
+                        s->enabled = m;
                         prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
                         prioq_reshuffle(s->event->realtime_latest, s, &s->time.latest_index);
                         break;
 
                 case SOURCE_SIGNAL:
-                        s->mute = m;
+                        s->enabled = m;
 
-                        if (s->signal.sig != SIGCHLD || s->event->n_unmuted_child_sources == 0)  {
+                        if (s->signal.sig != SIGCHLD || s->event->n_enabled_child_sources == 0)  {
                                 assert_se(sigaddset(&s->event->sigset, s->signal.sig) == 0);
                                 event_update_signal_fd(s->event);
                         }
                         break;
 
                 case SOURCE_CHILD:
-                        s->mute = m;
+                        s->enabled = m;
 
-                        if (s->mute == SD_EVENT_MUTED) {
-                                s->event->n_unmuted_child_sources++;
+                        if (s->enabled == SD_EVENT_OFF) {
+                                s->event->n_enabled_child_sources++;
 
                                 if (!s->event->signal_sources || !s->event->signal_sources[SIGCHLD]) {
                                         assert_se(sigaddset(&s->event->sigset, SIGCHLD) == 0);
@@ -1201,9 +1200,13 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
                         }
                         break;
 
-                case SOURCE_DEFER:
                 case SOURCE_QUIT:
-                        s->mute = m;
+                        s->enabled = m;
+                        prioq_reshuffle(s->event->quit, s, &s->quit.prioq_index);
+                        break;
+
+                case SOURCE_DEFER:
+                        s->enabled = m;
                         break;
                 }
         }
@@ -1217,35 +1220,28 @@ int sd_event_source_set_mute(sd_event_source *s, int m) {
         return 0;
 }
 
-int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
-        if (!s)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (s->type != SOURCE_REALTIME && s->type != SOURCE_MONOTONIC)
-                return -EDOM;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_time(sd_event_source *s, uint64_t *usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         *usec = s->time.next;
         return 0;
 }
 
-int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
-        if (!s)
-                return -EINVAL;
-        if (usec == (uint64_t) -1)
-                return -EINVAL;
-        if (s->type != SOURCE_REALTIME && s->type != SOURCE_MONOTONIC)
-                return -EDOM;
+_public_ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec != (uint64_t) -1, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (s->time.next == usec)
                 return 0;
 
         s->time.next = usec;
+        source_set_pending(s, false);
 
         if (s->type == SOURCE_REALTIME) {
                 prioq_reshuffle(s->event->realtime_earliest, s, &s->time.earliest_index);
@@ -1258,14 +1254,22 @@ int sd_event_source_set_time(sd_event_source *s, uint64_t usec) {
         return 0;
 }
 
-int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
-        if (!s)
-                return -EINVAL;
-        if (s->type != SOURCE_MONOTONIC && s->type != SOURCE_REALTIME)
-                return -EDOM;
+_public_ int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
+
+        *usec = s->time.accuracy;
+        return 0;
+}
+
+_public_ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
+        assert_return(s, -EINVAL);
+        assert_return(usec != (uint64_t) -1, -EINVAL);
+        assert_return(s->type == SOURCE_REALTIME || s->type == SOURCE_MONOTONIC, -EDOM);
         assert_return(s->event->state != SD_EVENT_FINISHED, -ESTALE);
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
         if (usec == 0)
                 usec = DEFAULT_ACCURACY_USEC;
@@ -1283,21 +1287,17 @@ int sd_event_source_set_time_accuracy(sd_event_source *s, uint64_t usec) {
         return 0;
 }
 
-int sd_event_source_get_time_accuracy(sd_event_source *s, uint64_t *usec) {
-        if (!s)
-                return -EINVAL;
-        if (!usec)
-                return -EINVAL;
-        if (s->type != SOURCE_MONOTONIC && s->type != SOURCE_REALTIME)
-                return -EDOM;
-        if (event_pid_changed(s->event))
-                return -ECHILD;
+_public_ int sd_event_source_get_child_pid(sd_event_source *s, pid_t *pid) {
+        assert_return(s, -EINVAL);
+        assert_return(pid, -EINVAL);
+        assert_return(s->type == SOURCE_CHILD, -EDOM);
+        assert_return(!event_pid_changed(s->event), -ECHILD);
 
-        *usec = s->time.accuracy;
+        *pid = s->child.pid;
         return 0;
 }
 
-int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback) {
+_public_ int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callback) {
         int r;
 
         assert_return(s, -EINVAL);
@@ -1329,7 +1329,7 @@ int sd_event_source_set_prepare(sd_event_source *s, sd_prepare_handler_t callbac
         return 0;
 }
 
-void* sd_event_source_get_userdata(sd_event_source *s) {
+_public_ void* sd_event_source_get_userdata(sd_event_source *s) {
         assert_return(s, NULL);
 
         return s->userdata;
@@ -1404,11 +1404,23 @@ static int event_arm_timer(
         assert_se(next);
 
         a = prioq_peek(earliest);
-        if (!a || a->mute == SD_EVENT_MUTED)
+        if (!a || a->enabled == SD_EVENT_OFF) {
+
+                if (*next == (usec_t) -1)
+                        return 0;
+
+                /* disarm */
+                r = timerfd_settime(timer_fd, TFD_TIMER_ABSTIME, &its, NULL);
+                if (r < 0)
+                        return r;
+
+                *next = (usec_t) -1;
+
                 return 0;
+        }
 
         b = prioq_peek(latest);
-        assert_se(b && b->mute != SD_EVENT_MUTED);
+        assert_se(b && b->enabled != SD_EVENT_OFF);
 
         t = sleep_between(e, a->time.next, b->time.next + b->time.accuracy);
         if (*next == t)
@@ -1438,27 +1450,18 @@ static int process_io(sd_event *e, sd_event_source *s, uint32_t events) {
 
         s->io.revents = events;
 
-        /*
-           If this is a oneshot event source, then we added it to the
-           epoll with EPOLLONESHOT, hence we know it's not registered
-           anymore. We can save a syscall here...
-        */
-
-        if (s->mute == SD_EVENT_ONESHOT)
-                s->io.registered = false;
-
         return source_set_pending(s, true);
 }
 
-static int flush_timer(sd_event *e, int fd, uint32_t events) {
+static int flush_timer(sd_event *e, int fd, uint32_t events, usec_t *next) {
         uint64_t x;
         ssize_t ss;
 
         assert(e);
         assert(fd >= 0);
+        assert(next);
 
-        if (events != EPOLLIN)
-                return -EIO;
+        assert_return(events == EPOLLIN, -EIO);
 
         ss = read(fd, &x, sizeof(x));
         if (ss < 0) {
@@ -1471,10 +1474,17 @@ static int flush_timer(sd_event *e, int fd, uint32_t events) {
         if (ss != sizeof(x))
                 return -EIO;
 
+        *next = (usec_t) -1;
+
         return 0;
 }
 
-static int process_timer(sd_event *e, usec_t n, Prioq *earliest, Prioq *latest) {
+static int process_timer(
+                sd_event *e,
+                usec_t n,
+                Prioq *earliest,
+                Prioq *latest) {
+
         sd_event_source *s;
         int r;
 
@@ -1484,7 +1494,7 @@ static int process_timer(sd_event *e, usec_t n, Prioq *earliest, Prioq *latest)
                 s = prioq_peek(earliest);
                 if (!s ||
                     s->time.next > n ||
-                    s->mute == SD_EVENT_MUTED ||
+                    s->enabled == SD_EVENT_OFF ||
                     s->pending)
                         break;
 
@@ -1528,7 +1538,7 @@ static int process_child(sd_event *e) {
                 if (s->pending)
                         continue;
 
-                if (s->mute == SD_EVENT_MUTED)
+                if (s->enabled == SD_EVENT_OFF)
                         continue;
 
                 zero(s->child.siginfo);
@@ -1547,17 +1557,17 @@ static int process_child(sd_event *e) {
 }
 
 static int process_signal(sd_event *e, uint32_t events) {
-        struct signalfd_siginfo si;
         bool read_one = false;
-        ssize_t ss;
         int r;
 
         assert(e);
+        assert(e->signal_sources);
 
-        if (events != EPOLLIN)
-                return -EIO;
+        assert_return(events == EPOLLIN, -EIO);
 
         for (;;) {
+                struct signalfd_siginfo si;
+                ssize_t ss;
                 sd_event_source *s;
 
                 ss = read(e->signal_fd, &si, sizeof(si));
@@ -1573,17 +1583,16 @@ static int process_signal(sd_event *e, uint32_t events) {
 
                 read_one = true;
 
+                s = e->signal_sources[si.ssi_signo];
                 if (si.ssi_signo == SIGCHLD) {
                         r = process_child(e);
                         if (r < 0)
                                 return r;
-                        if (r > 0 || !e->signal_sources[si.ssi_signo])
+                        if (r > 0 || !s)
                                 continue;
-                } else {
-                        s = e->signal_sources[si.ssi_signo];
+                } else
                         if (!s)
                                 return -EIO;
-                }
 
                 s->signal.siginfo = si;
                 r = source_set_pending(s, true);
@@ -1596,7 +1605,7 @@ static int process_signal(sd_event *e, uint32_t events) {
 }
 
 static int source_dispatch(sd_event_source *s) {
-        int r;
+        int r = 0;
 
         assert(s);
         assert(s->pending || s->type == SOURCE_QUIT);
@@ -1607,12 +1616,14 @@ static int source_dispatch(sd_event_source *s) {
                         return r;
         }
 
-        if (s->mute == SD_EVENT_ONESHOT) {
-                r = sd_event_source_set_mute(s, SD_EVENT_MUTED);
+        if (s->enabled == SD_EVENT_ONESHOT) {
+                r = sd_event_source_set_enabled(s, SD_EVENT_OFF);
                 if (r < 0)
                         return r;
         }
 
+        sd_event_source_ref(s);
+
         switch (s->type) {
 
         case SOURCE_IO:
@@ -1644,6 +1655,8 @@ static int source_dispatch(sd_event_source *s) {
                 break;
         }
 
+        sd_event_source_unref(s);
+
         return r;
 }
 
@@ -1656,7 +1669,7 @@ static int event_prepare(sd_event *e) {
                 sd_event_source *s;
 
                 s = prioq_peek(e->prepare);
-                if (!s || s->prepare_iteration == e->iteration || s->mute == SD_EVENT_MUTED)
+                if (!s || s->prepare_iteration == e->iteration || s->enabled == SD_EVENT_OFF)
                         break;
 
                 s->prepare_iteration = e->iteration;
@@ -1681,7 +1694,7 @@ static int dispatch_quit(sd_event *e) {
         assert(e);
 
         p = prioq_peek(e->quit);
-        if (!p || p->mute == SD_EVENT_MUTED) {
+        if (!p || p->enabled == SD_EVENT_OFF) {
                 e->state = SD_EVENT_FINISHED;
                 return 0;
         }
@@ -1707,17 +1720,16 @@ static sd_event_source* event_next_pending(sd_event *e) {
         if (!p)
                 return NULL;
 
-        if (p->mute == SD_EVENT_MUTED)
+        if (p->enabled == SD_EVENT_OFF)
                 return NULL;
 
         return p;
 }
 
-int sd_event_run(sd_event *e, uint64_t timeout) {
+_public_ int sd_event_run(sd_event *e, uint64_t timeout) {
         struct epoll_event ev_queue[EPOLL_QUEUE_MAX];
         sd_event_source *p;
         int r, i, m;
-        dual_timestamp n;
 
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
@@ -1751,18 +1763,18 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
         m = epoll_wait(e->epoll_fd, ev_queue, EPOLL_QUEUE_MAX,
                        timeout == (uint64_t) -1 ? -1 : (int) ((timeout + USEC_PER_MSEC - 1) / USEC_PER_MSEC));
         if (m < 0) {
-                r = m;
+                r = errno == EAGAIN || errno == EINTR ? 0 : -errno;
                 goto finish;
         }
 
-        dual_timestamp_get(&n);
+        dual_timestamp_get(&e->timestamp);
 
         for (i = 0; i < m; i++) {
 
                 if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_MONOTONIC))
-                        r = flush_timer(e, e->monotonic_fd, ev_queue[i].events);
+                        r = flush_timer(e, e->monotonic_fd, ev_queue[i].events, &e->monotonic_next);
                 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_REALTIME))
-                        r = flush_timer(e, e->realtime_fd, ev_queue[i].events);
+                        r = flush_timer(e, e->realtime_fd, ev_queue[i].events, &e->realtime_next);
                 else if (ev_queue[i].data.ptr == INT_TO_PTR(SOURCE_SIGNAL))
                         r = process_signal(e, ev_queue[i].events);
                 else
@@ -1772,11 +1784,11 @@ int sd_event_run(sd_event *e, uint64_t timeout) {
                         goto finish;
         }
 
-        r = process_timer(e, n.monotonic, e->monotonic_earliest, e->monotonic_latest);
+        r = process_timer(e, e->timestamp.monotonic, e->monotonic_earliest, e->monotonic_latest);
         if (r < 0)
                 goto finish;
 
-        r = process_timer(e, n.realtime, e->realtime_earliest, e->realtime_latest);
+        r = process_timer(e, e->timestamp.realtime, e->realtime_earliest, e->realtime_latest);
         if (r < 0)
                 goto finish;
 
@@ -1801,7 +1813,7 @@ finish:
         return r;
 }
 
-int sd_event_loop(sd_event *e) {
+_public_ int sd_event_loop(sd_event *e) {
         int r;
 
         assert_return(e, -EINVAL);
@@ -1823,21 +1835,21 @@ finish:
         return r;
 }
 
-int sd_event_get_state(sd_event *e) {
+_public_ int sd_event_get_state(sd_event *e) {
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
 
         return e->state;
 }
 
-int sd_event_get_quit(sd_event *e) {
+_public_ int sd_event_get_quit(sd_event *e) {
         assert_return(e, -EINVAL);
         assert_return(!event_pid_changed(e), -ECHILD);
 
         return e->quit_requested;
 }
 
-int sd_event_request_quit(sd_event *e) {
+_public_ int sd_event_request_quit(sd_event *e) {
         assert_return(e, -EINVAL);
         assert_return(e->state != SD_EVENT_FINISHED, -ESTALE);
         assert_return(!event_pid_changed(e), -ECHILD);
@@ -1845,3 +1857,62 @@ int sd_event_request_quit(sd_event *e) {
         e->quit_requested = true;
         return 0;
 }
+
+_public_ int sd_event_get_now_realtime(sd_event *e, uint64_t *usec) {
+        assert_return(e, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(dual_timestamp_is_set(&e->timestamp), -ENODATA);
+        assert_return(!event_pid_changed(e), -ECHILD);
+
+        *usec = e->timestamp.realtime;
+        return 0;
+}
+
+_public_ int sd_event_get_now_monotonic(sd_event *e, uint64_t *usec) {
+        assert_return(e, -EINVAL);
+        assert_return(usec, -EINVAL);
+        assert_return(dual_timestamp_is_set(&e->timestamp), -ENODATA);
+        assert_return(!event_pid_changed(e), -ECHILD);
+
+        *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(!event_pid_changed(e), -ECHILD);
+
+        if (e->tid != 0) {
+                *tid = e->tid;
+                return 0;
+        }
+
+        return -ENXIO;
+}