dovecot-2.2: lib: Added support for setting timeouts at absolute...

dovecot at dovecot.org dovecot at dovecot.org
Tue Sep 30 21:04:01 UTC 2014


details:   http://hg.dovecot.org/dovecot-2.2/rev/d80395660f27
changeset: 17853:d80395660f27
user:      Stephan Bosch <stephan at rename-it.nl>
date:      Wed Oct 01 00:03:17 2014 +0300
description:
lib: Added support for setting timeouts at absolute time, rather than relative to current time.

diffstat:

 src/lib/ioloop-private.h |   2 ++
 src/lib/ioloop.c         |  37 ++++++++++++++++++++++++++++++++-----
 src/lib/ioloop.h         |   7 +++++++
 3 files changed, 41 insertions(+), 5 deletions(-)

diffs (103 lines):

diff -r b7678ce561c1 -r d80395660f27 src/lib/ioloop-private.h
--- a/src/lib/ioloop-private.h	Tue Sep 30 23:49:52 2014 +0300
+++ b/src/lib/ioloop-private.h	Wed Oct 01 00:03:17 2014 +0300
@@ -69,6 +69,8 @@
 
 	struct ioloop *ioloop;
 	struct ioloop_context *ctx;
+
+	unsigned int one_shot:1;
 };
 
 struct ioloop_context_callback {
diff -r b7678ce561c1 -r d80395660f27 src/lib/ioloop.c
--- a/src/lib/ioloop.c	Tue Sep 30 23:49:52 2014 +0300
+++ b/src/lib/ioloop.c	Wed Oct 01 00:03:17 2014 +0300
@@ -194,15 +194,14 @@
 	}
 }
 
-#undef timeout_add
-struct timeout *timeout_add(unsigned int msecs, unsigned int source_linenum,
+static struct timeout *
+timeout_add_common(unsigned int source_linenum,
 			    timeout_callback_t *callback, void *context)
 {
 	struct timeout *timeout;
 
 	timeout = i_new(struct timeout, 1);
         timeout->source_linenum = source_linenum;
-        timeout->msecs = msecs;
 	timeout->ioloop = current_ioloop;
 
 	timeout->callback = callback;
@@ -213,6 +212,18 @@
 		io_loop_context_ref(timeout->ctx);
 	}
 
+	return timeout;
+}
+
+#undef timeout_add
+struct timeout *timeout_add(unsigned int msecs, unsigned int source_linenum,
+			    timeout_callback_t *callback, void *context)
+{
+	struct timeout *timeout;
+
+	timeout = timeout_add_common(source_linenum, callback, context);
+	timeout->msecs = msecs;
+
 	timeout_update_next(timeout, timeout->ioloop->running ?
 			    NULL : &ioloop_timeval);
 	priorityq_add(timeout->ioloop->timeouts, &timeout->item);
@@ -227,6 +238,20 @@
 	return timeout_add(msecs, source_linenum, callback, context);
 }
 
+#undef timeout_add_absolute
+struct timeout *timeout_add_absolute(const struct timeval *time,
+			    unsigned int source_linenum,
+			    timeout_callback_t *callback, void *context)
+{
+	struct timeout *timeout;
+
+	timeout = timeout_add_common(source_linenum, callback, context);
+	timeout->one_shot = TRUE;
+
+	priorityq_add(timeout->ioloop->timeouts, &timeout->item);
+	return timeout;
+}
+
 static void timeout_free(struct timeout *timeout)
 {
 	if (timeout->ctx != NULL)
@@ -407,8 +432,10 @@
 		if (timeout_get_wait_time(timeout, &tv, &tv_call) > 0)
 			break;
 
-		/* update timeout's next_run and reposition it in the queue */
-		timeout_reset_timeval(timeout, &tv_call);
+		if (!timeout->one_shot) {
+			/* update timeout's next_run and reposition it in the queue */
+			timeout_reset_timeval(timeout, &tv_call);
+		}
 
 		if (timeout->ctx != NULL)
 			io_loop_context_activate(timeout->ctx);
diff -r b7678ce561c1 -r d80395660f27 src/lib/ioloop.h
--- a/src/lib/ioloop.h	Tue Sep 30 23:49:52 2014 +0300
+++ b/src/lib/ioloop.h	Wed Oct 01 00:03:17 2014 +0300
@@ -97,6 +97,13 @@
 	timeout_add_short(msecs, __LINE__ + \
 		CALLBACK_TYPECHECK(callback, void (*)(typeof(context))), \
 		(io_callback_t *)callback, context)
+struct timeout *timeout_add_absolute(const struct timeval *time,
+			    unsigned int source_linenum,
+			    timeout_callback_t *callback, void *context) ATTR_NULL(4);
+#define timeout_add_absolute(time, callback, context) \
+	timeout_add_absolute(time, __LINE__ + \
+		CALLBACK_TYPECHECK(callback, void (*)(typeof(context))), \
+		(io_callback_t *)callback, context)
 /* Remove timeout handler, and set timeout pointer to NULL. */
 void timeout_remove(struct timeout **timeout);
 /* Reset timeout so it's next run after now+msecs. */


More information about the dovecot-cvs mailing list