]> git.ozlabs.org Git - ccan/blobdiff - ccan/io/poll.c
io: io_time_override to insert fake times.
[ccan] / ccan / io / poll.c
index 3266bdb2f5c737baecde62870445f90597895cfe..cddc3cacd266e5b80392d91af367d689da093bd6 100644 (file)
@@ -16,6 +16,14 @@ static struct pollfd *pollfds = NULL;
 static struct fd **fds = NULL;
 static LIST_HEAD(closing);
 static LIST_HEAD(always);
+static struct timeabs (*nowfn)(void) = time_now;
+
+struct timeabs (*io_time_override(struct timeabs (*now)(void)))(void)
+{
+       struct timeabs (*old)(void) = nowfn;
+       nowfn = now;
+       return old;
+}
 
 static bool add_fd(struct fd *fd, short events)
 {
@@ -222,16 +230,16 @@ static bool handle_always(void)
 }
 
 /* This is the main loop. */
-void *io_loop(struct timers *timers, struct list_head *expired)
+void *io_loop(struct timers *timers, struct timer **expired)
 {
        void *ret;
 
        /* if timers is NULL, expired must be.  If not, not. */
        assert(!timers == !expired);
 
-       /* Make sure this is empty if we exit for some other reason. */
+       /* Make sure this is NULL if we exit for some other reason. */
        if (expired)
-               list_head_init(expired);
+               *expired = NULL;
 
        while (!io_loop_return) {
                int i, r, ms_timeout = -1;
@@ -256,11 +264,11 @@ void *io_loop(struct timers *timers, struct list_head *expired)
                if (timers) {
                        struct timeabs now, first;
 
-                       now = time_now();
+                       now = nowfn();
 
                        /* Call functions for expired timers. */
-                       timers_expire(timers, now, expired);
-                       if (!list_empty(expired))
+                       *expired = timers_expire(timers, now);
+                       if (*expired)
                                break;
 
                        /* Now figure out how long to wait for the next one. */