]> granicus.if.org Git - libevent/commitdiff
Expose our cached gettimeofday value with a new interface
authorNick Mathewson <nickm@torproject.org>
Mon, 28 Dec 2009 06:40:37 +0000 (01:40 -0500)
committerNick Mathewson <nickm@torproject.org>
Mon, 28 Dec 2009 21:11:18 +0000 (16:11 -0500)
I've got a two use case that wants this for a fairly sensible purpose:
one external and on internal.

event.c
include/event2/event.h

diff --git a/event.c b/event.c
index aa8d5ce50df86dd69d70be123e1aa4579340fec1..2c830cee16101ef01657b90dd3764f1a46a789d3 100644 (file)
--- a/event.c
+++ b/event.c
@@ -180,6 +180,22 @@ gettime(struct event_base *base, struct timeval *tp)
        return (evutil_gettimeofday(tp, NULL));
 }
 
+int
+event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
+{
+       int r;
+       if (!base) {
+               base = current_base;
+               if (!current_base)
+                       return evutil_gettimeofday(tv, NULL);
+       }
+
+       EVBASE_ACQUIRE_LOCK(base, th_base_lock);
+       r = gettime(base, tv);
+       EVBASE_RELEASE_LOCK(base, th_base_lock);
+       return r;
+}
+
 static inline void
 clear_time_cache(struct event_base *base)
 {
index 6cc83d496f8144b0c152650ed23e6481d6727165..489026a661688695b91552b5098f38262c9912e6 100644 (file)
@@ -671,6 +671,20 @@ void event_set_mem_functions(void *(*malloc_fn)(size_t sz),
 
 void event_base_dump_events(struct event_base *, FILE *);
 
+/** Sets 'tv' to the current time (as returned by gettimeofday()),
+    looking at the cached value in 'base' if possible, and calling
+    gettimeofday() or clock_gettime() as appropriate if there is no
+    cached time.
+
+    Generally, this value will only be cached while actually
+    processing event callbacks, and may be very inaccuate if your
+    callbacks take a long time to execute.
+
+    Returns 0 on success, negative on failure.
+ */
+int event_base_gettimeofday_cached(struct event_base *base,
+    struct timeval *tv);
+
 #ifdef __cplusplus
 }
 #endif