mirror of
https://github.com/cuberite/libevent.git
synced 2025-09-09 04:19:10 -04:00
On Linux, use CLOCK_MONOTONIC_COARSE by default
You can make it use CLOCK_MONOTONIC again by setting the EVENT_BASE_FLAG_PRECISE_TIMER flag in the event_config.
This commit is contained in:
parent
ddd69d391e
commit
55780a70e2
@ -258,6 +258,10 @@ struct event_base {
|
|||||||
#if defined(EVENT__HAVE_MACH_ABSOLUTE_TIME)
|
#if defined(EVENT__HAVE_MACH_ABSOLUTE_TIME)
|
||||||
struct mach_timebase_info mach_timebase_units;
|
struct mach_timebase_info mach_timebase_units;
|
||||||
#endif
|
#endif
|
||||||
|
#if defined(EVENT__HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC) && defined(CLOCK_MONOTONIC_COARSE)
|
||||||
|
#define CLOCK_IS_SELECTED
|
||||||
|
int monotonic_clock;
|
||||||
|
#endif
|
||||||
#if defined(HAVE_ANY_MONOTONIC)
|
#if defined(HAVE_ANY_MONOTONIC)
|
||||||
/** True iff we should use our system's monotonic time implementation */
|
/** True iff we should use our system's monotonic time implementation */
|
||||||
int use_monotonic;
|
int use_monotonic;
|
||||||
|
20
event.c
20
event.c
@ -341,7 +341,7 @@ HT_GENERATE(event_debug_map, event_debug_entry, node, hash_debug_entry,
|
|||||||
/* Set base->use_monotonic to 1 if we have a clock function that supports
|
/* Set base->use_monotonic to 1 if we have a clock function that supports
|
||||||
* monotonic time */
|
* monotonic time */
|
||||||
static void
|
static void
|
||||||
detect_monotonic(struct event_base *base)
|
detect_monotonic(struct event_base *base, const struct event_config *cfg)
|
||||||
{
|
{
|
||||||
#if defined(EVENT__HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
#if defined(EVENT__HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
||||||
{
|
{
|
||||||
@ -350,8 +350,17 @@ detect_monotonic(struct event_base *base)
|
|||||||
* versions won't have it working. */
|
* versions won't have it working. */
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
|
||||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0)
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) {
|
||||||
base->use_monotonic = 1;
|
base->use_monotonic = 1;
|
||||||
|
#ifdef CLOCK_IS_SELECTED
|
||||||
|
base->monotonic_clock = CLOCK_MONOTONIC;
|
||||||
|
if (cfg == NULL ||
|
||||||
|
!(cfg->flags & EVENT_BASE_FLAG_PRECISE_TIMER)) {
|
||||||
|
if (clock_gettime(CLOCK_MONOTONIC_COARSE, &ts) == 0)
|
||||||
|
base->monotonic_clock = CLOCK_MONOTONIC_COARSE;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
#elif defined(EVENT__HAVE_MACH_ABSOLUTE_TIME)
|
#elif defined(EVENT__HAVE_MACH_ABSOLUTE_TIME)
|
||||||
{
|
{
|
||||||
@ -392,8 +401,13 @@ gettime(struct event_base *base, struct timeval *tp)
|
|||||||
if (base->use_monotonic) {
|
if (base->use_monotonic) {
|
||||||
#if defined(EVENT__HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
#if defined(EVENT__HAVE_CLOCK_GETTIME) && defined(CLOCK_MONOTONIC)
|
||||||
struct timespec ts;
|
struct timespec ts;
|
||||||
|
#ifdef CLOCK_IS_SELECTED
|
||||||
|
if (clock_gettime(base->monotonic_clock, &ts) == -1)
|
||||||
|
return (-1);
|
||||||
|
#else
|
||||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
|
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
|
||||||
return (-1);
|
return (-1);
|
||||||
|
#endif
|
||||||
|
|
||||||
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;
|
||||||
@ -614,7 +628,7 @@ event_base_new_with_config(const struct event_config *cfg)
|
|||||||
event_warn("%s: calloc", __func__);
|
event_warn("%s: calloc", __func__);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
detect_monotonic(base);
|
detect_monotonic(base, cfg);
|
||||||
gettime(base, &base->event_tv);
|
gettime(base, &base->event_tv);
|
||||||
|
|
||||||
min_heap_ctor_(&base->timeheap);
|
min_heap_ctor_(&base->timeheap);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user