/*
* Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "util-internal.h"
/* The old tests here need assertions to work. */
#undef NDEBUG
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);
/* 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);
tt_assert(base);
if (evthread_make_base_notifiable(base)<0) {
tt_abort_msg("Couldn't make base notifiable!");
}
#ifndef _WIN32
if (data->setup_data && !strcmp(data->setup_data, "forking")) {
pid_t pid;
int status;
sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base);
/* This piggybacks on the th_notify_fd weirdly, and looks
* inside libevent internals. Not a good idea in non-testing
* code! */
notification_event = event_new(base,
base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb,
NULL);
event_add(sigchld_event, NULL);
event_add(notification_event, NULL);
EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
EVTHREAD_ALLOC_COND(cond.cond);
tt_assert(cond.lock);
tt_assert(cond.cond);
for (i = 0; i < NUM_THREADS; ++i) {
memset(&alerted[i], 0, sizeof(struct alerted_record));
alerted[i].cond = &cond;
}
/* Threads 5 and 6 will be allowed to time out */
memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout));
memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout));
/* And run for a bit... */
event_base_dispatch(base);
/* And wait till the threads are done. */
for (i = 0; i < NUM_THREADS; ++i)
THREAD_JOIN(threads[i]);
/* Now, let's see what happened. At least one of 5 or 6 should
* have timed out. */
n_timed_out = alerted[5].timed_out + alerted[6].timed_out;
tt_int_op(n_timed_out, >=, 1);
tt_int_op(n_timed_out, <=, 2);
for (i = 0; i < NUM_THREADS; ++i) {
const struct timeval *target_delay;
struct timeval target_time, actual_delay;
if (alerted[i].timed_out) {
TT_BLATHER(("%d looks like a timeout\n", i));
target_delay = &tv_timeout;
tt_assert(i == 5 || i == 6);
} else if (evutil_timerisset(&alerted[i].alerted_at)) {
long diff1,diff2;
evutil_timersub(&alerted[i].alerted_at,
&launched_at, &actual_delay);
diff1 = timeval_msec_diff(&actual_delay,
&tv_signal);
diff2 = timeval_msec_diff(&actual_delay,
&tv_broadcast);
if (labs(diff1) < labs(diff2)) {
TT_BLATHER(("%d looks like a signal\n", i));
target_delay = &tv_signal;
++n_signal;
} else {
TT_BLATHER(("%d looks like a broadcast\n", i));
target_delay = &tv_broadcast;
++n_broadcast;
}
} else {
TT_FAIL(("Thread %d never got woken", i));
continue;
}
evutil_timeradd(target_delay, &launched_at, &target_time);
test_timeval_diff_leq(&target_time, &alerted[i].alerted_at,
0, 200);
}
tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS);
tt_int_op(n_signal, ==, 1);