static void wake_one_timeout(evutil_socket_t fd, short what, void *arg) { struct cond_wait *cw = arg; EVLOCK_LOCK(cw->lock, 0); EVTHREAD_COND_SIGNAL(cw->cond); EVLOCK_UNLOCK(cw->lock, 0); }
static void count_incr(void) { EVLOCK_LOCK(count_lock, 0); count++; EVTHREAD_COND_BROADCAST(count_cond); EVLOCK_UNLOCK(count_lock, 0); }
static THREAD_FN basic_thread(void *arg) { struct cond_wait cw; struct event_base *base = arg; struct event ev; int i = 0; EVTHREAD_ALLOC_LOCK(cw.lock, 0); EVTHREAD_ALLOC_COND(cw.cond); assert(cw.lock); assert(cw.cond); evtimer_assign(&ev, base, wake_all_timeout, &cw); for (i = 0; i < NUM_ITERATIONS; i++) { struct timeval tv; evutil_timerclear(&tv); tv.tv_sec = 0; tv.tv_usec = 3000; EVLOCK_LOCK(cw.lock, 0); /* we need to make sure that event does not happen before * we get to wait on the conditional variable */ assert(evtimer_add(&ev, &tv) == 0); assert(EVTHREAD_COND_WAIT(cw.cond, cw.lock) == 0); EVLOCK_UNLOCK(cw.lock, 0); EVLOCK_LOCK(count_lock, 0); ++count; EVLOCK_UNLOCK(count_lock, 0); } /* exit the loop only if all threads fired all timeouts */ EVLOCK_LOCK(count_lock, 0); if (count >= NUM_THREADS * NUM_ITERATIONS) event_base_loopexit(base, NULL); EVLOCK_UNLOCK(count_lock, 0); EVTHREAD_FREE_LOCK(cw.lock, 0); EVTHREAD_FREE_COND(cw.cond); THREAD_RETURN(); }
static THREAD_FN wait_for_condition(void *arg) { struct alerted_record *rec = arg; int r; EVLOCK_LOCK(rec->cond->lock, 0); if (rec->delay.tv_sec || rec->delay.tv_usec) { r = EVTHREAD_COND_WAIT_TIMED(rec->cond->cond, rec->cond->lock, &rec->delay); } else { r = EVTHREAD_COND_WAIT(rec->cond->cond, rec->cond->lock); } EVLOCK_UNLOCK(rec->cond->lock, 0); evutil_gettimeofday(&rec->alerted_at, NULL); if (r == 1) rec->timed_out = 1; THREAD_RETURN(); }
static int count_wait_for(int i, int ms) { struct timeval tv; DWORD elapsed; int rv = -1; EVLOCK_LOCK(count_lock, 0); while (ms > 0 && count != i) { tv.tv_sec = 0; tv.tv_usec = ms * 1000; elapsed = GetTickCount(); EVTHREAD_COND_WAIT_TIMED(count_cond, count_lock, &tv); elapsed = GetTickCount() - elapsed; ms -= elapsed; } if (count == i) rv = 0; EVLOCK_UNLOCK(count_lock, 0); return rv; }