summaryrefslogtreecommitdiff
path: root/src/common/evsched.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/evsched.c')
-rwxr-xr-x[-rw-r--r--]src/common/evsched.c25
1 files changed, 14 insertions, 11 deletions
diff --git a/src/common/evsched.c b/src/common/evsched.c
index 8b6f721..9bfdef0 100644..100755
--- a/src/common/evsched.c
+++ b/src/common/evsched.c
@@ -26,15 +26,18 @@
#define OPENBSD_SLAB_BROKEN
#endif
-/* Heap only cares about x<y. */
-static int compare_event_heap_nodes(event_t **e1, event_t **e2)
+/*! \brief Some implementations of timercmp >= are broken, this is for compat.*/
+static inline int timercmp_ge(struct timeval *a, struct timeval *b) {
+ return timercmp(a, b, >) || timercmp(a, b, ==);
+}
+
+static int compare_event_heap_nodes(event_t *e1, event_t *e2)
{
- if (timercmp(&(*e1)->tv, &(*e2)->tv, <)) return -1;
- if (timercmp(&(*e1)->tv, &(*e2)->tv, >)) return 1;
+ if (timercmp(&e1->tv, &e2->tv, <)) return -1;
+ if (timercmp(&e1->tv, &e2->tv, >)) return 1;
return 0;
}
-
/*!
* \brief Set event timer to T (now) + dt miliseconds.
*/
@@ -78,7 +81,7 @@ evsched_t *evsched_new()
#ifndef OPENBSD_SLAB_BROKEN
slab_cache_init(&s->cache.alloc, sizeof(event_t));
#endif
- heap_init(&s->heap, sizeof(event_t *), compare_event_heap_nodes, 0, NULL);
+ heap_init(&s->heap, compare_event_heap_nodes, 0);
return s;
}
@@ -179,7 +182,7 @@ event_t* evsched_next(evsched_t *s)
event_t *next_ev = *((event_t**)HHEAD(&s->heap));
/* Immediately return. */
- if (timercmp(&dt, &next_ev->tv, >=)) {
+ if (timercmp_ge(&dt, &next_ev->tv)) {
s->current = next_ev;
heap_delmin(&s->heap);
pthread_mutex_unlock(&s->mx);
@@ -235,10 +238,10 @@ int evsched_schedule(evsched_t *s, event_t *ev, uint32_t dt)
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
- heap_insert(&s->heap, &ev);
+ heap_insert(&s->heap, ev);
/* Unlock calendar. */
- pthread_cond_signal(&s->notify);
+ pthread_cond_broadcast(&s->notify);
pthread_mutex_unlock(&s->mx);
return 0;
@@ -302,12 +305,12 @@ int evsched_cancel(evsched_t *s, event_t *ev)
/* Lock calendar. */
pthread_mutex_lock(&s->mx);
- if ((found = heap_find(&s->heap, &ev))) {
+ if ((found = heap_find(&s->heap, ev))) {
heap_delete(&s->heap, found);
}
/* Unlock calendar. */
- pthread_cond_signal(&s->notify);
+ pthread_cond_broadcast(&s->notify);
pthread_mutex_unlock(&s->mx);
/* Enable running events. */