mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-10 04:50:37 -04:00
Merge remote branch 'origin/patches-2.0'
Conflicts: event-internal.h
This commit is contained in:
commit
4560b31bdf
@ -34,6 +34,7 @@ extern "C" {
|
|||||||
#include "event2/event-config.h"
|
#include "event2/event-config.h"
|
||||||
#include "evconfig-private.h"
|
#include "evconfig-private.h"
|
||||||
|
|
||||||
|
#include <time.h>
|
||||||
#include <sys/queue.h>
|
#include <sys/queue.h>
|
||||||
#include "event2/event_struct.h"
|
#include "event2/event_struct.h"
|
||||||
#include "minheap-internal.h"
|
#include "minheap-internal.h"
|
||||||
@ -237,9 +238,18 @@ struct event_base {
|
|||||||
/** Priority queue of events with timeouts. */
|
/** Priority queue of events with timeouts. */
|
||||||
struct min_heap timeheap;
|
struct min_heap timeheap;
|
||||||
|
|
||||||
/** Stored timeval: used to avoid calling gettimeofday too often. */
|
/** Stored timeval: used to avoid calling gettimeofday/clock_gettime
|
||||||
|
* too often. */
|
||||||
struct timeval tv_cache;
|
struct timeval tv_cache;
|
||||||
|
|
||||||
|
#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
||||||
|
/** Difference between internal time (maybe from clock_gettime) and
|
||||||
|
* gettimeofday. */
|
||||||
|
struct timeval tv_clock_diff;
|
||||||
|
/** Second in which we last updated tv_clock_diff, in monotonic time. */
|
||||||
|
time_t last_updated_clock_diff;
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
#ifndef _EVENT_DISABLE_THREAD_SUPPORT
|
||||||
/* threading support */
|
/* threading support */
|
||||||
/** The thread currently running the event_loop for this base */
|
/** The thread currently running the event_loop for this base */
|
||||||
|
35
event.c
35
event.c
@ -331,6 +331,10 @@ detect_monotonic(void)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* How often (in seconds) do we check for changes in wall clock time relative
|
||||||
|
* to monotonic time? Set this to -1 for 'never.' */
|
||||||
|
#define CLOCK_SYNC_INTERVAL -1
|
||||||
|
|
||||||
/** Set 'tp' to the current time according to 'base'. We must hold the lock
|
/** Set 'tp' to the current time according to 'base'. We must hold the lock
|
||||||
* on 'base'. If there is a cached time, return it. Otherwise, use
|
* on 'base'. If there is a cached time, return it. Otherwise, use
|
||||||
* clock_gettime or gettimeofday as appropriate to find out the right time.
|
* clock_gettime or gettimeofday as appropriate to find out the right time.
|
||||||
@ -355,6 +359,14 @@ gettime(struct event_base *base, struct timeval *tp)
|
|||||||
|
|
||||||
tp->tv_sec = ts.tv_sec;
|
tp->tv_sec = ts.tv_sec;
|
||||||
tp->tv_usec = ts.tv_nsec / 1000;
|
tp->tv_usec = ts.tv_nsec / 1000;
|
||||||
|
if (base->last_updated_clock_diff + CLOCK_SYNC_INTERVAL
|
||||||
|
< ts.tv_sec) {
|
||||||
|
struct timeval tv;
|
||||||
|
evutil_gettimeofday(&tv,NULL);
|
||||||
|
evutil_timersub(&tv, tp, &base->tv_clock_diff);
|
||||||
|
base->last_updated_clock_diff = ts.tv_sec;
|
||||||
|
}
|
||||||
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -373,7 +385,16 @@ event_base_gettimeofday_cached(struct event_base *base, struct timeval *tv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
EVBASE_ACQUIRE_LOCK(base, th_base_lock);
|
||||||
r = gettime(base, tv);
|
if (base->tv_cache.tv_sec == 0) {
|
||||||
|
r = evutil_gettimeofday(tv, NULL);
|
||||||
|
} else {
|
||||||
|
#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
||||||
|
evutil_timeradd(&base->tv_cache, &base->tv_clock_diff, tv);
|
||||||
|
#else
|
||||||
|
*tv = base->tv_cache;
|
||||||
|
#endif
|
||||||
|
r = 0;
|
||||||
|
}
|
||||||
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
EVBASE_RELEASE_LOCK(base, th_base_lock);
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
@ -1779,7 +1800,6 @@ event_priority_set(struct event *ev, int pri)
|
|||||||
int
|
int
|
||||||
event_pending(const struct event *ev, short event, struct timeval *tv)
|
event_pending(const struct event *ev, short event, struct timeval *tv)
|
||||||
{
|
{
|
||||||
struct timeval now, res;
|
|
||||||
int flags = 0;
|
int flags = 0;
|
||||||
|
|
||||||
_event_debug_assert_is_setup(ev);
|
_event_debug_assert_is_setup(ev);
|
||||||
@ -1796,12 +1816,13 @@ event_pending(const struct event *ev, short event, struct timeval *tv)
|
|||||||
/* See if there is a timeout that we should report */
|
/* See if there is a timeout that we should report */
|
||||||
if (tv != NULL && (flags & event & EV_TIMEOUT)) {
|
if (tv != NULL && (flags & event & EV_TIMEOUT)) {
|
||||||
struct timeval tmp = ev->ev_timeout;
|
struct timeval tmp = ev->ev_timeout;
|
||||||
event_base_gettimeofday_cached(ev->ev_base, &now);
|
|
||||||
tmp.tv_usec &= MICROSECONDS_MASK;
|
tmp.tv_usec &= MICROSECONDS_MASK;
|
||||||
evutil_timersub(&tmp, &now, &res);
|
#if defined(_EVENT_HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
||||||
/* correctly remap to real time */
|
/* correctly remamp to real time */
|
||||||
evutil_gettimeofday(&now, NULL);
|
evutil_timeradd(&ev->ev_base->tv_clock_diff, &tmp, tv);
|
||||||
evutil_timeradd(&now, &res, tv);
|
#else
|
||||||
|
*tv = tmp;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
return (flags & event);
|
return (flags & event);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user