1c43e99fdSEd Maste /*
2c43e99fdSEd Maste  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3c43e99fdSEd Maste  *
4c43e99fdSEd Maste  * Redistribution and use in source and binary forms, with or without
5c43e99fdSEd Maste  * modification, are permitted provided that the following conditions
6c43e99fdSEd Maste  * are met:
7c43e99fdSEd Maste  * 1. Redistributions of source code must retain the above copyright
8c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer.
9c43e99fdSEd Maste  * 2. Redistributions in binary form must reproduce the above copyright
10c43e99fdSEd Maste  *    notice, this list of conditions and the following disclaimer in the
11c43e99fdSEd Maste  *    documentation and/or other materials provided with the distribution.
12c43e99fdSEd Maste  * 3. The name of the author may not be used to endorse or promote products
13c43e99fdSEd Maste  *    derived from this software without specific prior written permission.
14c43e99fdSEd Maste  *
15c43e99fdSEd Maste  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16c43e99fdSEd Maste  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17c43e99fdSEd Maste  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18c43e99fdSEd Maste  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19c43e99fdSEd Maste  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20c43e99fdSEd Maste  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21c43e99fdSEd Maste  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22c43e99fdSEd Maste  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23c43e99fdSEd Maste  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24c43e99fdSEd Maste  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25c43e99fdSEd Maste  */
26c43e99fdSEd Maste #include "util-internal.h"
27c43e99fdSEd Maste 
28c43e99fdSEd Maste /* The old tests here need assertions to work. */
29c43e99fdSEd Maste #undef NDEBUG
30c43e99fdSEd Maste 
31c43e99fdSEd Maste #include "event2/event-config.h"
32c43e99fdSEd Maste 
33c43e99fdSEd Maste #include <sys/types.h>
34c43e99fdSEd Maste #include <stdio.h>
35c43e99fdSEd Maste #include <stdlib.h>
36c43e99fdSEd Maste #include <string.h>
37c43e99fdSEd Maste #ifdef EVENT__HAVE_UNISTD_H
38c43e99fdSEd Maste #include <unistd.h>
39c43e99fdSEd Maste #endif
40c43e99fdSEd Maste #ifdef EVENT__HAVE_SYS_WAIT_H
41c43e99fdSEd Maste #include <sys/wait.h>
42c43e99fdSEd Maste #endif
43c43e99fdSEd Maste 
44c43e99fdSEd Maste #ifdef EVENT__HAVE_PTHREADS
45c43e99fdSEd Maste #include <pthread.h>
46c43e99fdSEd Maste #elif defined(_WIN32)
47c43e99fdSEd Maste #include <process.h>
48c43e99fdSEd Maste #endif
49c43e99fdSEd Maste #include <assert.h>
50c43e99fdSEd Maste #ifdef EVENT__HAVE_UNISTD_H
51c43e99fdSEd Maste #include <unistd.h>
52c43e99fdSEd Maste #endif
53c43e99fdSEd Maste #include <time.h>
54c43e99fdSEd Maste 
55c43e99fdSEd Maste #include "sys/queue.h"
56c43e99fdSEd Maste 
57c43e99fdSEd Maste #include "event2/event.h"
58c43e99fdSEd Maste #include "event2/event_struct.h"
59c43e99fdSEd Maste #include "event2/thread.h"
60c43e99fdSEd Maste #include "event2/util.h"
61c43e99fdSEd Maste #include "evthread-internal.h"
62c43e99fdSEd Maste #include "event-internal.h"
63c43e99fdSEd Maste #include "defer-internal.h"
64c43e99fdSEd Maste #include "regress.h"
65c43e99fdSEd Maste #include "tinytest_macros.h"
66c43e99fdSEd Maste #include "time-internal.h"
67c43e99fdSEd Maste #include "regress_thread.h"
68c43e99fdSEd Maste 
69c43e99fdSEd Maste struct cond_wait {
70c43e99fdSEd Maste 	void *lock;
71c43e99fdSEd Maste 	void *cond;
72c43e99fdSEd Maste };
73c43e99fdSEd Maste 
74c43e99fdSEd Maste static void
wake_all_timeout(evutil_socket_t fd,short what,void * arg)75c43e99fdSEd Maste wake_all_timeout(evutil_socket_t fd, short what, void *arg)
76c43e99fdSEd Maste {
77c43e99fdSEd Maste 	struct cond_wait *cw = arg;
78c43e99fdSEd Maste 	EVLOCK_LOCK(cw->lock, 0);
79c43e99fdSEd Maste 	EVTHREAD_COND_BROADCAST(cw->cond);
80c43e99fdSEd Maste 	EVLOCK_UNLOCK(cw->lock, 0);
81c43e99fdSEd Maste 
82c43e99fdSEd Maste }
83c43e99fdSEd Maste 
84c43e99fdSEd Maste static void
wake_one_timeout(evutil_socket_t fd,short what,void * arg)85c43e99fdSEd Maste wake_one_timeout(evutil_socket_t fd, short what, void *arg)
86c43e99fdSEd Maste {
87c43e99fdSEd Maste 	struct cond_wait *cw = arg;
88c43e99fdSEd Maste 	EVLOCK_LOCK(cw->lock, 0);
89c43e99fdSEd Maste 	EVTHREAD_COND_SIGNAL(cw->cond);
90c43e99fdSEd Maste 	EVLOCK_UNLOCK(cw->lock, 0);
91c43e99fdSEd Maste }
92c43e99fdSEd Maste 
93c43e99fdSEd Maste #define NUM_THREADS	100
94c43e99fdSEd Maste #define NUM_ITERATIONS  100
95c43e99fdSEd Maste void *count_lock;
96c43e99fdSEd Maste static int count;
97c43e99fdSEd Maste 
98c43e99fdSEd Maste static THREAD_FN
basic_thread(void * arg)99c43e99fdSEd Maste basic_thread(void *arg)
100c43e99fdSEd Maste {
101c43e99fdSEd Maste 	struct cond_wait cw;
102c43e99fdSEd Maste 	struct event_base *base = arg;
103c43e99fdSEd Maste 	struct event ev;
104c43e99fdSEd Maste 	int i = 0;
105c43e99fdSEd Maste 
106c43e99fdSEd Maste 	EVTHREAD_ALLOC_LOCK(cw.lock, 0);
107c43e99fdSEd Maste 	EVTHREAD_ALLOC_COND(cw.cond);
108c43e99fdSEd Maste 	assert(cw.lock);
109c43e99fdSEd Maste 	assert(cw.cond);
110c43e99fdSEd Maste 
111c43e99fdSEd Maste 	evtimer_assign(&ev, base, wake_all_timeout, &cw);
112c43e99fdSEd Maste 	for (i = 0; i < NUM_ITERATIONS; i++) {
113c43e99fdSEd Maste 		struct timeval tv;
114c43e99fdSEd Maste 		evutil_timerclear(&tv);
115c43e99fdSEd Maste 		tv.tv_sec = 0;
116c43e99fdSEd Maste 		tv.tv_usec = 3000;
117c43e99fdSEd Maste 
118c43e99fdSEd Maste 		EVLOCK_LOCK(cw.lock, 0);
119c43e99fdSEd Maste 		/* we need to make sure that event does not happen before
120c43e99fdSEd Maste 		 * we get to wait on the conditional variable */
121c43e99fdSEd Maste 		assert(evtimer_add(&ev, &tv) == 0);
122c43e99fdSEd Maste 
123c43e99fdSEd Maste 		assert(EVTHREAD_COND_WAIT(cw.cond, cw.lock) == 0);
124c43e99fdSEd Maste 		EVLOCK_UNLOCK(cw.lock, 0);
125c43e99fdSEd Maste 
126c43e99fdSEd Maste 		EVLOCK_LOCK(count_lock, 0);
127c43e99fdSEd Maste 		++count;
128c43e99fdSEd Maste 		EVLOCK_UNLOCK(count_lock, 0);
129c43e99fdSEd Maste 	}
130c43e99fdSEd Maste 
131c43e99fdSEd Maste 	/* exit the loop only if all threads fired all timeouts */
132c43e99fdSEd Maste 	EVLOCK_LOCK(count_lock, 0);
133c43e99fdSEd Maste 	if (count >= NUM_THREADS * NUM_ITERATIONS)
134c43e99fdSEd Maste 		event_base_loopexit(base, NULL);
135c43e99fdSEd Maste 	EVLOCK_UNLOCK(count_lock, 0);
136c43e99fdSEd Maste 
137c43e99fdSEd Maste 	EVTHREAD_FREE_LOCK(cw.lock, 0);
138c43e99fdSEd Maste 	EVTHREAD_FREE_COND(cw.cond);
139c43e99fdSEd Maste 
140c43e99fdSEd Maste 	THREAD_RETURN();
141c43e99fdSEd Maste }
142c43e99fdSEd Maste 
143c43e99fdSEd Maste static int notification_fd_used = 0;
144c43e99fdSEd Maste #ifndef _WIN32
145c43e99fdSEd Maste static int got_sigchld = 0;
146c43e99fdSEd Maste static void
sigchld_cb(evutil_socket_t fd,short event,void * arg)147c43e99fdSEd Maste sigchld_cb(evutil_socket_t fd, short event, void *arg)
148c43e99fdSEd Maste {
149c43e99fdSEd Maste 	struct timeval tv;
150c43e99fdSEd Maste 	struct event_base *base = arg;
151c43e99fdSEd Maste 
152c43e99fdSEd Maste 	got_sigchld++;
153c43e99fdSEd Maste 	tv.tv_usec = 100000;
154c43e99fdSEd Maste 	tv.tv_sec = 0;
155c43e99fdSEd Maste 	event_base_loopexit(base, &tv);
156c43e99fdSEd Maste }
157c43e99fdSEd Maste 
158c43e99fdSEd Maste 
159c43e99fdSEd Maste static void
notify_fd_cb(evutil_socket_t fd,short event,void * arg)160c43e99fdSEd Maste notify_fd_cb(evutil_socket_t fd, short event, void *arg)
161c43e99fdSEd Maste {
162c43e99fdSEd Maste 	++notification_fd_used;
163c43e99fdSEd Maste }
164c43e99fdSEd Maste #endif
165c43e99fdSEd Maste 
166c43e99fdSEd Maste static void
thread_basic(void * arg)167c43e99fdSEd Maste thread_basic(void *arg)
168c43e99fdSEd Maste {
169c43e99fdSEd Maste 	THREAD_T threads[NUM_THREADS];
170c43e99fdSEd Maste 	struct event ev;
171c43e99fdSEd Maste 	struct timeval tv;
172c43e99fdSEd Maste 	int i;
173c43e99fdSEd Maste 	struct basic_test_data *data = arg;
174c43e99fdSEd Maste 	struct event_base *base = data->base;
175c43e99fdSEd Maste 
176c43e99fdSEd Maste 	struct event *notification_event = NULL;
177c43e99fdSEd Maste 	struct event *sigchld_event = NULL;
178c43e99fdSEd Maste 
179c43e99fdSEd Maste 	EVTHREAD_ALLOC_LOCK(count_lock, 0);
180c43e99fdSEd Maste 	tt_assert(count_lock);
181c43e99fdSEd Maste 
182c43e99fdSEd Maste 	tt_assert(base);
183c43e99fdSEd Maste 	if (evthread_make_base_notifiable(base)<0) {
184c43e99fdSEd Maste 		tt_abort_msg("Couldn't make base notifiable!");
185c43e99fdSEd Maste 	}
186c43e99fdSEd Maste 
187c43e99fdSEd Maste #ifndef _WIN32
188c43e99fdSEd Maste 	if (data->setup_data && !strcmp(data->setup_data, "forking")) {
189c43e99fdSEd Maste 		pid_t pid;
190c43e99fdSEd Maste 		int status;
191c43e99fdSEd Maste 		sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base);
192c43e99fdSEd Maste 		/* This piggybacks on the th_notify_fd weirdly, and looks
193c43e99fdSEd Maste 		 * inside libevent internals.  Not a good idea in non-testing
194c43e99fdSEd Maste 		 * code! */
195c43e99fdSEd Maste 		notification_event = event_new(base,
196c43e99fdSEd Maste 		    base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb,
197c43e99fdSEd Maste 		    NULL);
198c43e99fdSEd Maste 		event_add(sigchld_event, NULL);
199c43e99fdSEd Maste 		event_add(notification_event, NULL);
200c43e99fdSEd Maste 
201c43e99fdSEd Maste 		if ((pid = fork()) == 0) {
202c43e99fdSEd Maste 			event_del(notification_event);
203c43e99fdSEd Maste 			if (event_reinit(base) < 0) {
204c43e99fdSEd Maste 				TT_FAIL(("reinit"));
205c43e99fdSEd Maste 				exit(1);
206c43e99fdSEd Maste 			}
207c43e99fdSEd Maste 			event_assign(notification_event, base,
208c43e99fdSEd Maste 			    base->th_notify_fd[0], EV_READ|EV_PERSIST,
209c43e99fdSEd Maste 			    notify_fd_cb, NULL);
210c43e99fdSEd Maste 			event_add(notification_event, NULL);
211c43e99fdSEd Maste 	 		goto child;
212c43e99fdSEd Maste 		}
213c43e99fdSEd Maste 
214c43e99fdSEd Maste 		event_base_dispatch(base);
215c43e99fdSEd Maste 
216c43e99fdSEd Maste 		if (waitpid(pid, &status, 0) == -1)
217c43e99fdSEd Maste 			tt_abort_perror("waitpid");
218c43e99fdSEd Maste 		TT_BLATHER(("Waitpid okay\n"));
219c43e99fdSEd Maste 
220c43e99fdSEd Maste 		tt_assert(got_sigchld);
221c43e99fdSEd Maste 		tt_int_op(notification_fd_used, ==, 0);
222c43e99fdSEd Maste 
223c43e99fdSEd Maste 		goto end;
224c43e99fdSEd Maste 	}
225c43e99fdSEd Maste 
226c43e99fdSEd Maste child:
227c43e99fdSEd Maste #endif
228c43e99fdSEd Maste 	for (i = 0; i < NUM_THREADS; ++i)
229c43e99fdSEd Maste 		THREAD_START(threads[i], basic_thread, base);
230c43e99fdSEd Maste 
231c43e99fdSEd Maste 	evtimer_assign(&ev, base, NULL, NULL);
232c43e99fdSEd Maste 	evutil_timerclear(&tv);
233c43e99fdSEd Maste 	tv.tv_sec = 1000;
234c43e99fdSEd Maste 	event_add(&ev, &tv);
235c43e99fdSEd Maste 
236c43e99fdSEd Maste 	event_base_dispatch(base);
237c43e99fdSEd Maste 
238c43e99fdSEd Maste 	for (i = 0; i < NUM_THREADS; ++i)
239c43e99fdSEd Maste 		THREAD_JOIN(threads[i]);
240c43e99fdSEd Maste 
241c43e99fdSEd Maste 	event_del(&ev);
242c43e99fdSEd Maste 
243c43e99fdSEd Maste 	tt_int_op(count, ==, NUM_THREADS * NUM_ITERATIONS);
244c43e99fdSEd Maste 
245c43e99fdSEd Maste 	EVTHREAD_FREE_LOCK(count_lock, 0);
246c43e99fdSEd Maste 
247c43e99fdSEd Maste 	TT_BLATHER(("notifiations==%d", notification_fd_used));
248c43e99fdSEd Maste 
249c43e99fdSEd Maste end:
250c43e99fdSEd Maste 
251c43e99fdSEd Maste 	if (notification_event)
252c43e99fdSEd Maste 		event_free(notification_event);
253c43e99fdSEd Maste 	if (sigchld_event)
254c43e99fdSEd Maste 		event_free(sigchld_event);
255c43e99fdSEd Maste }
256c43e99fdSEd Maste 
257c43e99fdSEd Maste #undef NUM_THREADS
258c43e99fdSEd Maste #define NUM_THREADS 10
259c43e99fdSEd Maste 
260c43e99fdSEd Maste struct alerted_record {
261c43e99fdSEd Maste 	struct cond_wait *cond;
262c43e99fdSEd Maste 	struct timeval delay;
263c43e99fdSEd Maste 	struct timeval alerted_at;
264c43e99fdSEd Maste 	int timed_out;
265c43e99fdSEd Maste };
266c43e99fdSEd Maste 
267c43e99fdSEd Maste static THREAD_FN
wait_for_condition(void * arg)268c43e99fdSEd Maste wait_for_condition(void *arg)
269c43e99fdSEd Maste {
270c43e99fdSEd Maste 	struct alerted_record *rec = arg;
271c43e99fdSEd Maste 	int r;
272c43e99fdSEd Maste 
273c43e99fdSEd Maste 	EVLOCK_LOCK(rec->cond->lock, 0);
274c43e99fdSEd Maste 	if (rec->delay.tv_sec || rec->delay.tv_usec) {
275c43e99fdSEd Maste 		r = EVTHREAD_COND_WAIT_TIMED(rec->cond->cond, rec->cond->lock,
276c43e99fdSEd Maste 		    &rec->delay);
277c43e99fdSEd Maste 	} else {
278c43e99fdSEd Maste 		r = EVTHREAD_COND_WAIT(rec->cond->cond, rec->cond->lock);
279c43e99fdSEd Maste 	}
280c43e99fdSEd Maste 	EVLOCK_UNLOCK(rec->cond->lock, 0);
281c43e99fdSEd Maste 
282c43e99fdSEd Maste 	evutil_gettimeofday(&rec->alerted_at, NULL);
283c43e99fdSEd Maste 	if (r == 1)
284c43e99fdSEd Maste 		rec->timed_out = 1;
285c43e99fdSEd Maste 
286c43e99fdSEd Maste 	THREAD_RETURN();
287c43e99fdSEd Maste }
288c43e99fdSEd Maste 
289c43e99fdSEd Maste static void
thread_conditions_simple(void * arg)290c43e99fdSEd Maste thread_conditions_simple(void *arg)
291c43e99fdSEd Maste {
292c43e99fdSEd Maste 	struct timeval tv_signal, tv_timeout, tv_broadcast;
293c43e99fdSEd Maste 	struct alerted_record alerted[NUM_THREADS];
294c43e99fdSEd Maste 	THREAD_T threads[NUM_THREADS];
295c43e99fdSEd Maste 	struct cond_wait cond;
296c43e99fdSEd Maste 	int i;
297c43e99fdSEd Maste 	struct timeval launched_at;
298c43e99fdSEd Maste 	struct event wake_one;
299c43e99fdSEd Maste 	struct event wake_all;
300c43e99fdSEd Maste 	struct basic_test_data *data = arg;
301c43e99fdSEd Maste 	struct event_base *base = data->base;
302c43e99fdSEd Maste 	int n_timed_out=0, n_signal=0, n_broadcast=0;
303c43e99fdSEd Maste 
304c43e99fdSEd Maste 	tv_signal.tv_sec = tv_timeout.tv_sec = tv_broadcast.tv_sec = 0;
305c43e99fdSEd Maste 	tv_signal.tv_usec = 30*1000;
306c43e99fdSEd Maste 	tv_timeout.tv_usec = 150*1000;
307c43e99fdSEd Maste 	tv_broadcast.tv_usec = 500*1000;
308c43e99fdSEd Maste 
309c43e99fdSEd Maste 	EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
310c43e99fdSEd Maste 	EVTHREAD_ALLOC_COND(cond.cond);
311c43e99fdSEd Maste 	tt_assert(cond.lock);
312c43e99fdSEd Maste 	tt_assert(cond.cond);
313c43e99fdSEd Maste 	for (i = 0; i < NUM_THREADS; ++i) {
314c43e99fdSEd Maste 		memset(&alerted[i], 0, sizeof(struct alerted_record));
315c43e99fdSEd Maste 		alerted[i].cond = &cond;
316c43e99fdSEd Maste 	}
317c43e99fdSEd Maste 
318c43e99fdSEd Maste 	/* Threads 5 and 6 will be allowed to time out */
319c43e99fdSEd Maste 	memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout));
320c43e99fdSEd Maste 	memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout));
321c43e99fdSEd Maste 
322c43e99fdSEd Maste 	evtimer_assign(&wake_one, base, wake_one_timeout, &cond);
323c43e99fdSEd Maste 	evtimer_assign(&wake_all, base, wake_all_timeout, &cond);
324c43e99fdSEd Maste 
325c43e99fdSEd Maste 	evutil_gettimeofday(&launched_at, NULL);
326c43e99fdSEd Maste 
327c43e99fdSEd Maste 	/* Launch the threads... */
328c43e99fdSEd Maste 	for (i = 0; i < NUM_THREADS; ++i) {
329c43e99fdSEd Maste 		THREAD_START(threads[i], wait_for_condition, &alerted[i]);
330c43e99fdSEd Maste 	}
331c43e99fdSEd Maste 
332c43e99fdSEd Maste 	/* Start the timers... */
333c43e99fdSEd Maste 	tt_int_op(event_add(&wake_one, &tv_signal), ==, 0);
334c43e99fdSEd Maste 	tt_int_op(event_add(&wake_all, &tv_broadcast), ==, 0);
335c43e99fdSEd Maste 
336c43e99fdSEd Maste 	/* And run for a bit... */
337c43e99fdSEd Maste 	event_base_dispatch(base);
338c43e99fdSEd Maste 
339c43e99fdSEd Maste 	/* And wait till the threads are done. */
340c43e99fdSEd Maste 	for (i = 0; i < NUM_THREADS; ++i)
341c43e99fdSEd Maste 		THREAD_JOIN(threads[i]);
342c43e99fdSEd Maste 
343c43e99fdSEd Maste 	/* Now, let's see what happened. At least one of 5 or 6 should
344c43e99fdSEd Maste 	 * have timed out. */
345c43e99fdSEd Maste 	n_timed_out = alerted[5].timed_out + alerted[6].timed_out;
346c43e99fdSEd Maste 	tt_int_op(n_timed_out, >=, 1);
347c43e99fdSEd Maste 	tt_int_op(n_timed_out, <=, 2);
348c43e99fdSEd Maste 
349c43e99fdSEd Maste 	for (i = 0; i < NUM_THREADS; ++i) {
350c43e99fdSEd Maste 		const struct timeval *target_delay;
351c43e99fdSEd Maste 		struct timeval target_time, actual_delay;
352c43e99fdSEd Maste 		if (alerted[i].timed_out) {
353c43e99fdSEd Maste 			TT_BLATHER(("%d looks like a timeout\n", i));
354c43e99fdSEd Maste 			target_delay = &tv_timeout;
355c43e99fdSEd Maste 			tt_assert(i == 5 || i == 6);
356c43e99fdSEd Maste 		} else if (evutil_timerisset(&alerted[i].alerted_at)) {
357c43e99fdSEd Maste 			long diff1,diff2;
358c43e99fdSEd Maste 			evutil_timersub(&alerted[i].alerted_at,
359c43e99fdSEd Maste 			    &launched_at, &actual_delay);
360c43e99fdSEd Maste 			diff1 = timeval_msec_diff(&actual_delay,
361c43e99fdSEd Maste 			    &tv_signal);
362c43e99fdSEd Maste 			diff2 = timeval_msec_diff(&actual_delay,
363c43e99fdSEd Maste 			    &tv_broadcast);
364c43e99fdSEd Maste 			if (labs(diff1) < labs(diff2)) {
365c43e99fdSEd Maste 				TT_BLATHER(("%d looks like a signal\n", i));
366c43e99fdSEd Maste 				target_delay = &tv_signal;
367c43e99fdSEd Maste 				++n_signal;
368c43e99fdSEd Maste 			} else {
369c43e99fdSEd Maste 				TT_BLATHER(("%d looks like a broadcast\n", i));
370c43e99fdSEd Maste 				target_delay = &tv_broadcast;
371c43e99fdSEd Maste 				++n_broadcast;
372c43e99fdSEd Maste 			}
373c43e99fdSEd Maste 		} else {
374c43e99fdSEd Maste 			TT_FAIL(("Thread %d never got woken", i));
375c43e99fdSEd Maste 			continue;
376c43e99fdSEd Maste 		}
377c43e99fdSEd Maste 		evutil_timeradd(target_delay, &launched_at, &target_time);
378c43e99fdSEd Maste 		test_timeval_diff_leq(&target_time, &alerted[i].alerted_at,
379c43e99fdSEd Maste 		    0, 200);
380c43e99fdSEd Maste 	}
381c43e99fdSEd Maste 	tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS);
382c43e99fdSEd Maste 	tt_int_op(n_signal, ==, 1);
383c43e99fdSEd Maste 
384c43e99fdSEd Maste end:
385c43e99fdSEd Maste 	EVTHREAD_FREE_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
386c43e99fdSEd Maste 	EVTHREAD_FREE_COND(cond.cond);
387c43e99fdSEd Maste }
388c43e99fdSEd Maste 
389c43e99fdSEd Maste #define CB_COUNT 128
390c43e99fdSEd Maste #define QUEUE_THREAD_COUNT 8
391c43e99fdSEd Maste 
392c43e99fdSEd Maste static void
SLEEP_MS(int ms)393c43e99fdSEd Maste SLEEP_MS(int ms)
394c43e99fdSEd Maste {
395c43e99fdSEd Maste 	struct timeval tv;
396c43e99fdSEd Maste 	tv.tv_sec = ms/1000;
397c43e99fdSEd Maste 	tv.tv_usec = (ms%1000)*1000;
398c43e99fdSEd Maste 	evutil_usleep_(&tv);
399c43e99fdSEd Maste }
400c43e99fdSEd Maste 
401c43e99fdSEd Maste struct deferred_test_data {
402c43e99fdSEd Maste 	struct event_callback cbs[CB_COUNT];
403c43e99fdSEd Maste 	struct event_base *queue;
404c43e99fdSEd Maste };
405c43e99fdSEd Maste 
406c43e99fdSEd Maste static struct timeval timer_start = {0,0};
407c43e99fdSEd Maste static struct timeval timer_end = {0,0};
408c43e99fdSEd Maste static unsigned callback_count = 0;
409c43e99fdSEd Maste static THREAD_T load_threads[QUEUE_THREAD_COUNT];
410c43e99fdSEd Maste static struct deferred_test_data deferred_data[QUEUE_THREAD_COUNT];
411c43e99fdSEd Maste 
412c43e99fdSEd Maste static void
deferred_callback(struct event_callback * cb,void * arg)413c43e99fdSEd Maste deferred_callback(struct event_callback *cb, void *arg)
414c43e99fdSEd Maste {
415c43e99fdSEd Maste 	SLEEP_MS(1);
416c43e99fdSEd Maste 	callback_count += 1;
417c43e99fdSEd Maste }
418c43e99fdSEd Maste 
419c43e99fdSEd Maste static THREAD_FN
load_deferred_queue(void * arg)420c43e99fdSEd Maste load_deferred_queue(void *arg)
421c43e99fdSEd Maste {
422c43e99fdSEd Maste 	struct deferred_test_data *data = arg;
423c43e99fdSEd Maste 	size_t i;
424c43e99fdSEd Maste 
425c43e99fdSEd Maste 	for (i = 0; i < CB_COUNT; ++i) {
426c43e99fdSEd Maste 		event_deferred_cb_init_(&data->cbs[i], 0, deferred_callback,
427c43e99fdSEd Maste 		    NULL);
428c43e99fdSEd Maste 		event_deferred_cb_schedule_(data->queue, &data->cbs[i]);
429c43e99fdSEd Maste 		SLEEP_MS(1);
430c43e99fdSEd Maste 	}
431c43e99fdSEd Maste 
432c43e99fdSEd Maste 	THREAD_RETURN();
433c43e99fdSEd Maste }
434c43e99fdSEd Maste 
435c43e99fdSEd Maste static void
timer_callback(evutil_socket_t fd,short what,void * arg)436c43e99fdSEd Maste timer_callback(evutil_socket_t fd, short what, void *arg)
437c43e99fdSEd Maste {
438c43e99fdSEd Maste 	evutil_gettimeofday(&timer_end, NULL);
439c43e99fdSEd Maste }
440c43e99fdSEd Maste 
441c43e99fdSEd Maste static void
start_threads_callback(evutil_socket_t fd,short what,void * arg)442c43e99fdSEd Maste start_threads_callback(evutil_socket_t fd, short what, void *arg)
443c43e99fdSEd Maste {
444c43e99fdSEd Maste 	int i;
445c43e99fdSEd Maste 
446c43e99fdSEd Maste 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i) {
447c43e99fdSEd Maste 		THREAD_START(load_threads[i], load_deferred_queue,
448c43e99fdSEd Maste 				&deferred_data[i]);
449c43e99fdSEd Maste 	}
450c43e99fdSEd Maste }
451c43e99fdSEd Maste 
452c43e99fdSEd Maste static void
thread_deferred_cb_skew(void * arg)453c43e99fdSEd Maste thread_deferred_cb_skew(void *arg)
454c43e99fdSEd Maste {
455c43e99fdSEd Maste 	struct timeval tv_timer = {1, 0};
456c43e99fdSEd Maste 	struct event_base *base = NULL;
457c43e99fdSEd Maste 	struct event_config *cfg = NULL;
458c43e99fdSEd Maste 	struct timeval elapsed;
459c43e99fdSEd Maste 	int elapsed_usec;
460c43e99fdSEd Maste 	int i;
461c43e99fdSEd Maste 
462c43e99fdSEd Maste 	cfg = event_config_new();
463c43e99fdSEd Maste 	tt_assert(cfg);
464c43e99fdSEd Maste 	event_config_set_max_dispatch_interval(cfg, NULL, 16, 0);
465c43e99fdSEd Maste 
466c43e99fdSEd Maste 	base = event_base_new_with_config(cfg);
467c43e99fdSEd Maste 	tt_assert(base);
468c43e99fdSEd Maste 
469c43e99fdSEd Maste 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
470c43e99fdSEd Maste 		deferred_data[i].queue = base;
471c43e99fdSEd Maste 
472c43e99fdSEd Maste 	evutil_gettimeofday(&timer_start, NULL);
473c43e99fdSEd Maste 	event_base_once(base, -1, EV_TIMEOUT, timer_callback, NULL,
474c43e99fdSEd Maste 			&tv_timer);
475c43e99fdSEd Maste 	event_base_once(base, -1, EV_TIMEOUT, start_threads_callback,
476c43e99fdSEd Maste 			NULL, NULL);
477c43e99fdSEd Maste 	event_base_dispatch(base);
478c43e99fdSEd Maste 
479c43e99fdSEd Maste 	evutil_timersub(&timer_end, &timer_start, &elapsed);
480c43e99fdSEd Maste 	TT_BLATHER(("callback count, %u", callback_count));
481c43e99fdSEd Maste 	elapsed_usec =
482c43e99fdSEd Maste 	    (unsigned)(elapsed.tv_sec*1000000 + elapsed.tv_usec);
483c43e99fdSEd Maste 	TT_BLATHER(("elapsed time, %u usec", elapsed_usec));
484c43e99fdSEd Maste 
485c43e99fdSEd Maste 	/* XXX be more intelligent here.  just make sure skew is
486c43e99fdSEd Maste 	 * within .4 seconds for now. */
487c43e99fdSEd Maste 	tt_assert(elapsed_usec >= 600000 && elapsed_usec <= 1400000);
488c43e99fdSEd Maste 
489c43e99fdSEd Maste end:
490c43e99fdSEd Maste 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
491c43e99fdSEd Maste 		THREAD_JOIN(load_threads[i]);
492c43e99fdSEd Maste 	if (base)
493c43e99fdSEd Maste 		event_base_free(base);
494c43e99fdSEd Maste 	if (cfg)
495c43e99fdSEd Maste 		event_config_free(cfg);
496c43e99fdSEd Maste }
497c43e99fdSEd Maste 
498c43e99fdSEd Maste static struct event time_events[5];
499c43e99fdSEd Maste static struct timeval times[5];
500c43e99fdSEd Maste static struct event_base *exit_base = NULL;
501c43e99fdSEd Maste static void
note_time_cb(evutil_socket_t fd,short what,void * arg)502c43e99fdSEd Maste note_time_cb(evutil_socket_t fd, short what, void *arg)
503c43e99fdSEd Maste {
504c43e99fdSEd Maste 	evutil_gettimeofday(arg, NULL);
505c43e99fdSEd Maste 	if (arg == &times[4]) {
506c43e99fdSEd Maste 		event_base_loopbreak(exit_base);
507c43e99fdSEd Maste 	}
508c43e99fdSEd Maste }
509c43e99fdSEd Maste static THREAD_FN
register_events_subthread(void * arg)510c43e99fdSEd Maste register_events_subthread(void *arg)
511c43e99fdSEd Maste {
512c43e99fdSEd Maste 	struct timeval tv = {0,0};
513c43e99fdSEd Maste 	SLEEP_MS(100);
514c43e99fdSEd Maste 	event_active(&time_events[0], EV_TIMEOUT, 1);
515c43e99fdSEd Maste 	SLEEP_MS(100);
516c43e99fdSEd Maste 	event_active(&time_events[1], EV_TIMEOUT, 1);
517c43e99fdSEd Maste 	SLEEP_MS(100);
518c43e99fdSEd Maste 	tv.tv_usec = 100*1000;
519c43e99fdSEd Maste 	event_add(&time_events[2], &tv);
520c43e99fdSEd Maste 	tv.tv_usec = 150*1000;
521c43e99fdSEd Maste 	event_add(&time_events[3], &tv);
522c43e99fdSEd Maste 	SLEEP_MS(200);
523c43e99fdSEd Maste 	event_active(&time_events[4], EV_TIMEOUT, 1);
524c43e99fdSEd Maste 
525c43e99fdSEd Maste 	THREAD_RETURN();
526c43e99fdSEd Maste }
527c43e99fdSEd Maste 
528c43e99fdSEd Maste static void
thread_no_events(void * arg)529c43e99fdSEd Maste thread_no_events(void *arg)
530c43e99fdSEd Maste {
531c43e99fdSEd Maste 	THREAD_T thread;
532c43e99fdSEd Maste 	struct basic_test_data *data = arg;
533c43e99fdSEd Maste 	struct timeval starttime, endtime;
534c43e99fdSEd Maste 	int i;
535c43e99fdSEd Maste 	exit_base = data->base;
536c43e99fdSEd Maste 
537c43e99fdSEd Maste 	memset(times,0,sizeof(times));
538c43e99fdSEd Maste 	for (i=0;i<5;++i) {
539c43e99fdSEd Maste 		event_assign(&time_events[i], data->base,
540c43e99fdSEd Maste 		    -1, 0, note_time_cb, &times[i]);
541c43e99fdSEd Maste 	}
542c43e99fdSEd Maste 
543c43e99fdSEd Maste 	evutil_gettimeofday(&starttime, NULL);
544c43e99fdSEd Maste 	THREAD_START(thread, register_events_subthread, data->base);
545c43e99fdSEd Maste 	event_base_loop(data->base, EVLOOP_NO_EXIT_ON_EMPTY);
546c43e99fdSEd Maste 	evutil_gettimeofday(&endtime, NULL);
547c43e99fdSEd Maste 	tt_assert(event_base_got_break(data->base));
548c43e99fdSEd Maste 	THREAD_JOIN(thread);
549c43e99fdSEd Maste 	for (i=0; i<5; ++i) {
550c43e99fdSEd Maste 		struct timeval diff;
551c43e99fdSEd Maste 		double sec;
552c43e99fdSEd Maste 		evutil_timersub(&times[i], &starttime, &diff);
553c43e99fdSEd Maste 		sec = diff.tv_sec + diff.tv_usec/1.0e6;
554c43e99fdSEd Maste 		TT_BLATHER(("event %d at %.4f seconds", i, sec));
555c43e99fdSEd Maste 	}
556c43e99fdSEd Maste 	test_timeval_diff_eq(&starttime, &times[0], 100);
557c43e99fdSEd Maste 	test_timeval_diff_eq(&starttime, &times[1], 200);
558c43e99fdSEd Maste 	test_timeval_diff_eq(&starttime, &times[2], 400);
559c43e99fdSEd Maste 	test_timeval_diff_eq(&starttime, &times[3], 450);
560c43e99fdSEd Maste 	test_timeval_diff_eq(&starttime, &times[4], 500);
561c43e99fdSEd Maste 	test_timeval_diff_eq(&starttime, &endtime,  500);
562c43e99fdSEd Maste 
563c43e99fdSEd Maste end:
564c43e99fdSEd Maste 	;
565c43e99fdSEd Maste }
566c43e99fdSEd Maste 
567*b50261e2SCy Schubert #define TEST(name, f)							\
568*b50261e2SCy Schubert 	{ #name, thread_##name, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE|(f),	\
569c43e99fdSEd Maste 	  &basic_setup, NULL }
570c43e99fdSEd Maste 
571c43e99fdSEd Maste struct testcase_t thread_testcases[] = {
572c43e99fdSEd Maste 	{ "basic", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
573c43e99fdSEd Maste 	  &basic_setup, NULL },
574c43e99fdSEd Maste #ifndef _WIN32
575c43e99fdSEd Maste 	{ "forking", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
576c43e99fdSEd Maste 	  &basic_setup, (char*)"forking" },
577c43e99fdSEd Maste #endif
578*b50261e2SCy Schubert 	TEST(conditions_simple, TT_RETRIABLE),
579c43e99fdSEd Maste 	{ "deferred_cb_skew", thread_deferred_cb_skew,
580c43e99fdSEd Maste 	  TT_FORK|TT_NEED_THREADS|TT_OFF_BY_DEFAULT,
581c43e99fdSEd Maste 	  &basic_setup, NULL },
582c43e99fdSEd Maste #ifndef _WIN32
583c43e99fdSEd Maste 	/****** XXX TODO FIXME windows seems to be having some timing trouble,
584c43e99fdSEd Maste 	 * looking into it now. / ellzey
585c43e99fdSEd Maste 	 ******/
586*b50261e2SCy Schubert 	TEST(no_events, TT_RETRIABLE),
587c43e99fdSEd Maste #endif
588c43e99fdSEd Maste 	END_OF_TESTCASES
589c43e99fdSEd Maste };
590c43e99fdSEd Maste 
591