Revert "tevent zero..."
[metze/samba/wip.git] / lib / tevent / tevent.c
index b8178b2d855388fd5dcbb77ce26f231b9480f0e6..99cd074e655aa6adec1a1b69d5c62b4a1adb5853 100644 (file)
@@ -66,6 +66,9 @@
 #include "tevent.h"
 #include "tevent_internal.h"
 #include "tevent_util.h"
+#ifdef HAVE_EVENTFD
+#include <sys/eventfd.h>
+#endif
 
 static void tevent_abort(struct tevent_context *ev, const char *reason);
 
@@ -133,7 +136,9 @@ static void tevent_backend_init(void)
 #elif defined(HAVE_SOLARIS_PORTS)
        tevent_port_init();
 #endif
-
+#ifdef HAVE_KQUEUE
+       tevent_kqueue_init();
+#endif
        tevent_standard_init();
 }
 
@@ -178,6 +183,8 @@ const char **tevent_backend_list(TALLOC_CTX *mem_ctx)
        return list;
 }
 
+static void tevent_common_wakeup_fini(struct tevent_context *ev);
+
 #ifdef HAVE_PTHREAD
 
 static pthread_mutex_t tevent_contexts_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -297,12 +304,7 @@ int tevent_common_context_destructor(struct tevent_context *ev)
                tevent_abort(ev, "threaded contexts exist");
        }
 
-       if (ev->pipe_fde) {
-               talloc_free(ev->pipe_fde);
-               close(ev->pipe_fds[0]);
-               close(ev->pipe_fds[1]);
-               ev->pipe_fde = NULL;
-       }
+       tevent_common_wakeup_fini(ev);
 
        for (fd = ev->fd_events; fd; fd = fn) {
                fn = fd->next;
@@ -770,7 +772,7 @@ done:
 bool tevent_common_have_events(struct tevent_context *ev)
 {
        if (ev->fd_events != NULL) {
-               if (ev->fd_events != ev->pipe_fde) {
+               if (ev->fd_events != ev->wakeup_fde) {
                        return true;
                }
                if (ev->fd_events->next != NULL) {
@@ -843,10 +845,14 @@ static void wakeup_pipe_handler(struct tevent_context *ev,
 {
        ssize_t ret;
 
-       char c[16];
-       /* its non-blocking, doesn't matter if we read too much */
        do {
-               ret = read(fde->fd, c, sizeof(c));
+               /*
+                * This is the boilerplate for eventfd, but it works
+                * for pipes too. And as we don't care about the data
+                * we read, we're fine.
+                */
+               uint64_t val;
+               ret = read(fde->fd, &val, sizeof(val));
        } while (ret == -1 && errno == EINTR);
 }
 
@@ -858,23 +864,39 @@ int tevent_common_wakeup_init(struct tevent_context *ev)
 {
        int ret;
 
-       if (ev->pipe_fde != NULL) {
+       if (ev->wakeup_fde != NULL) {
                return 0;
        }
 
-       ret = pipe(ev->pipe_fds);
+#ifdef HAVE_EVENTFD
+       ret = eventfd(0, EFD_NONBLOCK);
        if (ret == -1) {
                return errno;
        }
-       ev_set_blocking(ev->pipe_fds[0], false);
-       ev_set_blocking(ev->pipe_fds[1], false);
+       ev->wakeup_fd = ret;
+#else
+       {
+               int pipe_fds[2];
+               ret = pipe(pipe_fds);
+               if (ret == -1) {
+                       return errno;
+               }
+               ev->wakeup_fd = pipe_fds[0];
+               ev->wakeup_write_fd = pipe_fds[1];
+
+               ev_set_blocking(ev->wakeup_fd, false);
+               ev_set_blocking(ev->wakeup_write_fd, false);
+       }
+#endif
 
-       ev->pipe_fde = tevent_add_fd(ev, ev, ev->pipe_fds[0],
+       ev->wakeup_fde = tevent_add_fd(ev, ev, ev->wakeup_fd,
                                     TEVENT_FD_READ,
                                     wakeup_pipe_handler, NULL);
-       if (ev->pipe_fde == NULL) {
-               close(ev->pipe_fds[0]);
-               close(ev->pipe_fds[1]);
+       if (ev->wakeup_fde == NULL) {
+               close(ev->wakeup_fd);
+#ifndef HAVE_EVENTFD
+               close(ev->wakeup_write_fd);
+#endif
                return ENOMEM;
        }
 
@@ -885,14 +907,33 @@ int tevent_common_wakeup(struct tevent_context *ev)
 {
        ssize_t ret;
 
-       if (ev->pipe_fds[1] == -1) {
+       if (ev->wakeup_fde == NULL) {
                return ENOTCONN;
        }
 
        do {
+#ifdef HAVE_EVENTFD
+               uint64_t val = 1;
+               ret = write(ev->wakeup_fd, &val, sizeof(val));
+#else
                char c = '\0';
-               ret = write(ev->pipe_fds[1], &c, 1);
+               ret = write(ev->wakeup_write_fd, &c, 1);
+#endif
        } while ((ret == -1) && (errno == EINTR));
 
        return 0;
 }
+
+static void tevent_common_wakeup_fini(struct tevent_context *ev)
+{
+       if (ev->wakeup_fde == NULL) {
+               return;
+       }
+
+       TALLOC_FREE(ev->wakeup_fde);
+
+       close(ev->wakeup_fd);
+#ifndef HAVE_EVENTFD
+       close(ev->wakeup_write_fd);
+#endif
+}