1*9034ec65Schristos /*	$NetBSD: regress_thread.c,v 1.6 2020/05/25 20:47:34 christos Exp $	*/
22b3787f6Schristos 
32b3787f6Schristos /*
42b3787f6Schristos  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
52b3787f6Schristos  *
62b3787f6Schristos  * Redistribution and use in source and binary forms, with or without
72b3787f6Schristos  * modification, are permitted provided that the following conditions
82b3787f6Schristos  * are met:
92b3787f6Schristos  * 1. Redistributions of source code must retain the above copyright
102b3787f6Schristos  *    notice, this list of conditions and the following disclaimer.
112b3787f6Schristos  * 2. Redistributions in binary form must reproduce the above copyright
122b3787f6Schristos  *    notice, this list of conditions and the following disclaimer in the
132b3787f6Schristos  *    documentation and/or other materials provided with the distribution.
142b3787f6Schristos  * 3. The name of the author may not be used to endorse or promote products
152b3787f6Schristos  *    derived from this software without specific prior written permission.
162b3787f6Schristos  *
172b3787f6Schristos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182b3787f6Schristos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
192b3787f6Schristos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
202b3787f6Schristos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
212b3787f6Schristos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
222b3787f6Schristos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232b3787f6Schristos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242b3787f6Schristos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252b3787f6Schristos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
262b3787f6Schristos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272b3787f6Schristos  */
282b3787f6Schristos #include "util-internal.h"
292b3787f6Schristos 
302b3787f6Schristos /* The old tests here need assertions to work. */
312b3787f6Schristos #undef NDEBUG
322b3787f6Schristos 
332b3787f6Schristos #include "event2/event-config.h"
342b3787f6Schristos 
352b3787f6Schristos #include <sys/types.h>
362b3787f6Schristos #include <stdio.h>
372b3787f6Schristos #include <stdlib.h>
382b3787f6Schristos #include <string.h>
392b3787f6Schristos #ifdef EVENT__HAVE_UNISTD_H
402b3787f6Schristos #include <unistd.h>
412b3787f6Schristos #endif
422b3787f6Schristos #ifdef EVENT__HAVE_SYS_WAIT_H
432b3787f6Schristos #include <sys/wait.h>
442b3787f6Schristos #endif
452b3787f6Schristos 
462b3787f6Schristos #ifdef EVENT__HAVE_PTHREADS
472b3787f6Schristos #include <pthread.h>
482b3787f6Schristos #elif defined(_WIN32)
492b3787f6Schristos #include <process.h>
502b3787f6Schristos #endif
512b3787f6Schristos #include <assert.h>
522b3787f6Schristos #ifdef EVENT__HAVE_UNISTD_H
532b3787f6Schristos #include <unistd.h>
542b3787f6Schristos #endif
552b3787f6Schristos #include <time.h>
562b3787f6Schristos 
572b3787f6Schristos #include "sys/queue.h"
582b3787f6Schristos 
592b3787f6Schristos #include "event2/event.h"
602b3787f6Schristos #include "event2/event_struct.h"
612b3787f6Schristos #include "event2/thread.h"
622b3787f6Schristos #include "event2/util.h"
632b3787f6Schristos #include "evthread-internal.h"
642b3787f6Schristos #include "event-internal.h"
652b3787f6Schristos #include "defer-internal.h"
662b3787f6Schristos #include "regress.h"
672b3787f6Schristos #include "tinytest_macros.h"
682b3787f6Schristos #include "time-internal.h"
691b6f2cd4Schristos #include "regress_thread.h"
702b3787f6Schristos 
712b3787f6Schristos struct cond_wait {
722b3787f6Schristos 	void *lock;
732b3787f6Schristos 	void *cond;
742b3787f6Schristos };
752b3787f6Schristos 
762b3787f6Schristos static void
wake_all_timeout(evutil_socket_t fd,short what,void * arg)772b3787f6Schristos wake_all_timeout(evutil_socket_t fd, short what, void *arg)
782b3787f6Schristos {
792b3787f6Schristos 	struct cond_wait *cw = arg;
802b3787f6Schristos 	EVLOCK_LOCK(cw->lock, 0);
812b3787f6Schristos 	EVTHREAD_COND_BROADCAST(cw->cond);
822b3787f6Schristos 	EVLOCK_UNLOCK(cw->lock, 0);
832b3787f6Schristos 
842b3787f6Schristos }
852b3787f6Schristos 
862b3787f6Schristos static void
wake_one_timeout(evutil_socket_t fd,short what,void * arg)872b3787f6Schristos wake_one_timeout(evutil_socket_t fd, short what, void *arg)
882b3787f6Schristos {
892b3787f6Schristos 	struct cond_wait *cw = arg;
902b3787f6Schristos 	EVLOCK_LOCK(cw->lock, 0);
912b3787f6Schristos 	EVTHREAD_COND_SIGNAL(cw->cond);
922b3787f6Schristos 	EVLOCK_UNLOCK(cw->lock, 0);
932b3787f6Schristos }
942b3787f6Schristos 
952b3787f6Schristos #define NUM_THREADS	100
962b3787f6Schristos #define NUM_ITERATIONS  100
972b3787f6Schristos void *count_lock;
982b3787f6Schristos static int count;
992b3787f6Schristos 
1002b3787f6Schristos static THREAD_FN
basic_thread(void * arg)1012b3787f6Schristos basic_thread(void *arg)
1022b3787f6Schristos {
1032b3787f6Schristos 	struct cond_wait cw;
1042b3787f6Schristos 	struct event_base *base = arg;
1052b3787f6Schristos 	struct event ev;
1062b3787f6Schristos 	int i = 0;
1072b3787f6Schristos 
1082b3787f6Schristos 	EVTHREAD_ALLOC_LOCK(cw.lock, 0);
1092b3787f6Schristos 	EVTHREAD_ALLOC_COND(cw.cond);
1102b3787f6Schristos 	assert(cw.lock);
1112b3787f6Schristos 	assert(cw.cond);
1122b3787f6Schristos 
1132b3787f6Schristos 	evtimer_assign(&ev, base, wake_all_timeout, &cw);
1142b3787f6Schristos 	for (i = 0; i < NUM_ITERATIONS; i++) {
1152b3787f6Schristos 		struct timeval tv;
1162b3787f6Schristos 		evutil_timerclear(&tv);
1172b3787f6Schristos 		tv.tv_sec = 0;
1182b3787f6Schristos 		tv.tv_usec = 3000;
1192b3787f6Schristos 
1202b3787f6Schristos 		EVLOCK_LOCK(cw.lock, 0);
1212b3787f6Schristos 		/* we need to make sure that event does not happen before
1222b3787f6Schristos 		 * we get to wait on the conditional variable */
1232b3787f6Schristos 		assert(evtimer_add(&ev, &tv) == 0);
1242b3787f6Schristos 
1252b3787f6Schristos 		assert(EVTHREAD_COND_WAIT(cw.cond, cw.lock) == 0);
1262b3787f6Schristos 		EVLOCK_UNLOCK(cw.lock, 0);
1272b3787f6Schristos 
1282b3787f6Schristos 		EVLOCK_LOCK(count_lock, 0);
1292b3787f6Schristos 		++count;
1302b3787f6Schristos 		EVLOCK_UNLOCK(count_lock, 0);
1312b3787f6Schristos 	}
1322b3787f6Schristos 
1332b3787f6Schristos 	/* exit the loop only if all threads fired all timeouts */
1342b3787f6Schristos 	EVLOCK_LOCK(count_lock, 0);
1352b3787f6Schristos 	if (count >= NUM_THREADS * NUM_ITERATIONS)
1362b3787f6Schristos 		event_base_loopexit(base, NULL);
1372b3787f6Schristos 	EVLOCK_UNLOCK(count_lock, 0);
1382b3787f6Schristos 
1392b3787f6Schristos 	EVTHREAD_FREE_LOCK(cw.lock, 0);
1402b3787f6Schristos 	EVTHREAD_FREE_COND(cw.cond);
1412b3787f6Schristos 
1422b3787f6Schristos 	THREAD_RETURN();
1432b3787f6Schristos }
1442b3787f6Schristos 
1452b3787f6Schristos static int notification_fd_used = 0;
1462b3787f6Schristos #ifndef _WIN32
1472b3787f6Schristos static int got_sigchld = 0;
1482b3787f6Schristos static void
sigchld_cb(evutil_socket_t fd,short event,void * arg)1492b3787f6Schristos sigchld_cb(evutil_socket_t fd, short event, void *arg)
1502b3787f6Schristos {
1512b3787f6Schristos 	struct timeval tv;
1522b3787f6Schristos 	struct event_base *base = arg;
1532b3787f6Schristos 
1542b3787f6Schristos 	got_sigchld++;
1552b3787f6Schristos 	tv.tv_usec = 100000;
1562b3787f6Schristos 	tv.tv_sec = 0;
1572b3787f6Schristos 	event_base_loopexit(base, &tv);
1582b3787f6Schristos }
1592b3787f6Schristos 
1602b3787f6Schristos 
1612b3787f6Schristos static void
notify_fd_cb(evutil_socket_t fd,short event,void * arg)1622b3787f6Schristos notify_fd_cb(evutil_socket_t fd, short event, void *arg)
1632b3787f6Schristos {
1642b3787f6Schristos 	++notification_fd_used;
1652b3787f6Schristos }
1662b3787f6Schristos #endif
1672b3787f6Schristos 
1682b3787f6Schristos static void
thread_basic(void * arg)1692b3787f6Schristos thread_basic(void *arg)
1702b3787f6Schristos {
1712b3787f6Schristos 	THREAD_T threads[NUM_THREADS];
1722b3787f6Schristos 	struct event ev;
1732b3787f6Schristos 	struct timeval tv;
1742b3787f6Schristos 	int i;
1752b3787f6Schristos 	struct basic_test_data *data = arg;
1762b3787f6Schristos 	struct event_base *base = data->base;
1772b3787f6Schristos 
1782b3787f6Schristos 	struct event *notification_event = NULL;
1792b3787f6Schristos 	struct event *sigchld_event = NULL;
1802b3787f6Schristos 
1812b3787f6Schristos 	EVTHREAD_ALLOC_LOCK(count_lock, 0);
1822b3787f6Schristos 	tt_assert(count_lock);
1832b3787f6Schristos 
1842b3787f6Schristos 	tt_assert(base);
1852b3787f6Schristos 	if (evthread_make_base_notifiable(base)<0) {
1862b3787f6Schristos 		tt_abort_msg("Couldn't make base notifiable!");
1872b3787f6Schristos 	}
1882b3787f6Schristos 
1892b3787f6Schristos #ifndef _WIN32
1902b3787f6Schristos 	if (data->setup_data && !strcmp(data->setup_data, "forking")) {
1912b3787f6Schristos 		pid_t pid;
1922b3787f6Schristos 		int status;
1932b3787f6Schristos 		sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base);
1942b3787f6Schristos 		/* This piggybacks on the th_notify_fd weirdly, and looks
1952b3787f6Schristos 		 * inside libevent internals.  Not a good idea in non-testing
1962b3787f6Schristos 		 * code! */
1972b3787f6Schristos 		notification_event = event_new(base,
1982b3787f6Schristos 		    base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb,
1992b3787f6Schristos 		    NULL);
2002b3787f6Schristos 		event_add(sigchld_event, NULL);
2012b3787f6Schristos 		event_add(notification_event, NULL);
2022b3787f6Schristos 
2032b3787f6Schristos 		if ((pid = fork()) == 0) {
2042b3787f6Schristos 			event_del(notification_event);
2052b3787f6Schristos 			if (event_reinit(base) < 0) {
2062b3787f6Schristos 				TT_FAIL(("reinit"));
2072b3787f6Schristos 				exit(1);
2082b3787f6Schristos 			}
2092b3787f6Schristos 			event_assign(notification_event, base,
2102b3787f6Schristos 			    base->th_notify_fd[0], EV_READ|EV_PERSIST,
2112b3787f6Schristos 			    notify_fd_cb, NULL);
2122b3787f6Schristos 			event_add(notification_event, NULL);
2132b3787f6Schristos 	 		goto child;
2142b3787f6Schristos 		}
2152b3787f6Schristos 
2162b3787f6Schristos 		event_base_dispatch(base);
2172b3787f6Schristos 
2182b3787f6Schristos 		if (waitpid(pid, &status, 0) == -1)
2192b3787f6Schristos 			tt_abort_perror("waitpid");
2202b3787f6Schristos 		TT_BLATHER(("Waitpid okay\n"));
2212b3787f6Schristos 
2222b3787f6Schristos 		tt_assert(got_sigchld);
2232b3787f6Schristos 		tt_int_op(notification_fd_used, ==, 0);
2242b3787f6Schristos 
2252b3787f6Schristos 		goto end;
2262b3787f6Schristos 	}
2272b3787f6Schristos 
2282b3787f6Schristos child:
2292b3787f6Schristos #endif
2302b3787f6Schristos 	for (i = 0; i < NUM_THREADS; ++i)
2312b3787f6Schristos 		THREAD_START(threads[i], basic_thread, base);
2322b3787f6Schristos 
2332b3787f6Schristos 	evtimer_assign(&ev, base, NULL, NULL);
2342b3787f6Schristos 	evutil_timerclear(&tv);
2352b3787f6Schristos 	tv.tv_sec = 1000;
2362b3787f6Schristos 	event_add(&ev, &tv);
2372b3787f6Schristos 
2382b3787f6Schristos 	event_base_dispatch(base);
2392b3787f6Schristos 
2402b3787f6Schristos 	for (i = 0; i < NUM_THREADS; ++i)
2412b3787f6Schristos 		THREAD_JOIN(threads[i]);
2422b3787f6Schristos 
2432b3787f6Schristos 	event_del(&ev);
2442b3787f6Schristos 
2452b3787f6Schristos 	tt_int_op(count, ==, NUM_THREADS * NUM_ITERATIONS);
2462b3787f6Schristos 
2472b3787f6Schristos 	EVTHREAD_FREE_LOCK(count_lock, 0);
2482b3787f6Schristos 
2492b3787f6Schristos 	TT_BLATHER(("notifiations==%d", notification_fd_used));
2502b3787f6Schristos 
2512b3787f6Schristos end:
2522b3787f6Schristos 
2532b3787f6Schristos 	if (notification_event)
2542b3787f6Schristos 		event_free(notification_event);
2552b3787f6Schristos 	if (sigchld_event)
2562b3787f6Schristos 		event_free(sigchld_event);
2572b3787f6Schristos }
2582b3787f6Schristos 
2592b3787f6Schristos #undef NUM_THREADS
2602b3787f6Schristos #define NUM_THREADS 10
2612b3787f6Schristos 
2622b3787f6Schristos struct alerted_record {
2632b3787f6Schristos 	struct cond_wait *cond;
2642b3787f6Schristos 	struct timeval delay;
2652b3787f6Schristos 	struct timeval alerted_at;
2662b3787f6Schristos 	int timed_out;
2672b3787f6Schristos };
2682b3787f6Schristos 
2692b3787f6Schristos static THREAD_FN
wait_for_condition(void * arg)2702b3787f6Schristos wait_for_condition(void *arg)
2712b3787f6Schristos {
2722b3787f6Schristos 	struct alerted_record *rec = arg;
2732b3787f6Schristos 	int r;
2742b3787f6Schristos 
2752b3787f6Schristos 	EVLOCK_LOCK(rec->cond->lock, 0);
2762b3787f6Schristos 	if (rec->delay.tv_sec || rec->delay.tv_usec) {
2772b3787f6Schristos 		r = EVTHREAD_COND_WAIT_TIMED(rec->cond->cond, rec->cond->lock,
2782b3787f6Schristos 		    &rec->delay);
2792b3787f6Schristos 	} else {
2802b3787f6Schristos 		r = EVTHREAD_COND_WAIT(rec->cond->cond, rec->cond->lock);
2812b3787f6Schristos 	}
2822b3787f6Schristos 	EVLOCK_UNLOCK(rec->cond->lock, 0);
2832b3787f6Schristos 
2842b3787f6Schristos 	evutil_gettimeofday(&rec->alerted_at, NULL);
2852b3787f6Schristos 	if (r == 1)
2862b3787f6Schristos 		rec->timed_out = 1;
2872b3787f6Schristos 
2882b3787f6Schristos 	THREAD_RETURN();
2892b3787f6Schristos }
2902b3787f6Schristos 
2912b3787f6Schristos static void
thread_conditions_simple(void * arg)2922b3787f6Schristos thread_conditions_simple(void *arg)
2932b3787f6Schristos {
2942b3787f6Schristos 	struct timeval tv_signal, tv_timeout, tv_broadcast;
2952b3787f6Schristos 	struct alerted_record alerted[NUM_THREADS];
2962b3787f6Schristos 	THREAD_T threads[NUM_THREADS];
2972b3787f6Schristos 	struct cond_wait cond;
2982b3787f6Schristos 	int i;
2992b3787f6Schristos 	struct timeval launched_at;
3002b3787f6Schristos 	struct event wake_one;
3012b3787f6Schristos 	struct event wake_all;
3022b3787f6Schristos 	struct basic_test_data *data = arg;
3032b3787f6Schristos 	struct event_base *base = data->base;
3042b3787f6Schristos 	int n_timed_out=0, n_signal=0, n_broadcast=0;
3052b3787f6Schristos 
3062b3787f6Schristos 	tv_signal.tv_sec = tv_timeout.tv_sec = tv_broadcast.tv_sec = 0;
3072b3787f6Schristos 	tv_signal.tv_usec = 30*1000;
3082b3787f6Schristos 	tv_timeout.tv_usec = 150*1000;
3092b3787f6Schristos 	tv_broadcast.tv_usec = 500*1000;
3102b3787f6Schristos 
3112b3787f6Schristos 	EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
3122b3787f6Schristos 	EVTHREAD_ALLOC_COND(cond.cond);
3132b3787f6Schristos 	tt_assert(cond.lock);
3142b3787f6Schristos 	tt_assert(cond.cond);
3152b3787f6Schristos 	for (i = 0; i < NUM_THREADS; ++i) {
3162b3787f6Schristos 		memset(&alerted[i], 0, sizeof(struct alerted_record));
3172b3787f6Schristos 		alerted[i].cond = &cond;
3182b3787f6Schristos 	}
3192b3787f6Schristos 
3202b3787f6Schristos 	/* Threads 5 and 6 will be allowed to time out */
3212b3787f6Schristos 	memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout));
3222b3787f6Schristos 	memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout));
3232b3787f6Schristos 
3242b3787f6Schristos 	evtimer_assign(&wake_one, base, wake_one_timeout, &cond);
3252b3787f6Schristos 	evtimer_assign(&wake_all, base, wake_all_timeout, &cond);
3262b3787f6Schristos 
3272b3787f6Schristos 	evutil_gettimeofday(&launched_at, NULL);
3282b3787f6Schristos 
3292b3787f6Schristos 	/* Launch the threads... */
3302b3787f6Schristos 	for (i = 0; i < NUM_THREADS; ++i) {
3312b3787f6Schristos 		THREAD_START(threads[i], wait_for_condition, &alerted[i]);
3322b3787f6Schristos 	}
3332b3787f6Schristos 
3342b3787f6Schristos 	/* Start the timers... */
3352b3787f6Schristos 	tt_int_op(event_add(&wake_one, &tv_signal), ==, 0);
3362b3787f6Schristos 	tt_int_op(event_add(&wake_all, &tv_broadcast), ==, 0);
3372b3787f6Schristos 
3382b3787f6Schristos 	/* And run for a bit... */
3392b3787f6Schristos 	event_base_dispatch(base);
3402b3787f6Schristos 
3412b3787f6Schristos 	/* And wait till the threads are done. */
3422b3787f6Schristos 	for (i = 0; i < NUM_THREADS; ++i)
3432b3787f6Schristos 		THREAD_JOIN(threads[i]);
3442b3787f6Schristos 
3452b3787f6Schristos 	/* Now, let's see what happened. At least one of 5 or 6 should
3462b3787f6Schristos 	 * have timed out. */
3472b3787f6Schristos 	n_timed_out = alerted[5].timed_out + alerted[6].timed_out;
3482b3787f6Schristos 	tt_int_op(n_timed_out, >=, 1);
3492b3787f6Schristos 	tt_int_op(n_timed_out, <=, 2);
3502b3787f6Schristos 
3512b3787f6Schristos 	for (i = 0; i < NUM_THREADS; ++i) {
3522b3787f6Schristos 		const struct timeval *target_delay;
3532b3787f6Schristos 		struct timeval target_time, actual_delay;
3542b3787f6Schristos 		if (alerted[i].timed_out) {
3552b3787f6Schristos 			TT_BLATHER(("%d looks like a timeout\n", i));
3562b3787f6Schristos 			target_delay = &tv_timeout;
3572b3787f6Schristos 			tt_assert(i == 5 || i == 6);
3582b3787f6Schristos 		} else if (evutil_timerisset(&alerted[i].alerted_at)) {
3592b3787f6Schristos 			long diff1,diff2;
3602b3787f6Schristos 			evutil_timersub(&alerted[i].alerted_at,
3612b3787f6Schristos 			    &launched_at, &actual_delay);
3622b3787f6Schristos 			diff1 = timeval_msec_diff(&actual_delay,
3632b3787f6Schristos 			    &tv_signal);
3642b3787f6Schristos 			diff2 = timeval_msec_diff(&actual_delay,
3652b3787f6Schristos 			    &tv_broadcast);
36650cc4415Schristos 			if (labs(diff1) < labs(diff2)) {
3672b3787f6Schristos 				TT_BLATHER(("%d looks like a signal\n", i));
3682b3787f6Schristos 				target_delay = &tv_signal;
3692b3787f6Schristos 				++n_signal;
3702b3787f6Schristos 			} else {
3712b3787f6Schristos 				TT_BLATHER(("%d looks like a broadcast\n", i));
3722b3787f6Schristos 				target_delay = &tv_broadcast;
3732b3787f6Schristos 				++n_broadcast;
3742b3787f6Schristos 			}
3752b3787f6Schristos 		} else {
3762b3787f6Schristos 			TT_FAIL(("Thread %d never got woken", i));
3772b3787f6Schristos 			continue;
3782b3787f6Schristos 		}
3792b3787f6Schristos 		evutil_timeradd(target_delay, &launched_at, &target_time);
3802b3787f6Schristos 		test_timeval_diff_leq(&target_time, &alerted[i].alerted_at,
3812b3787f6Schristos 		    0, 50);
3822b3787f6Schristos 	}
3832b3787f6Schristos 	tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS);
3842b3787f6Schristos 	tt_int_op(n_signal, ==, 1);
3852b3787f6Schristos 
3862b3787f6Schristos end:
38750cc4415Schristos 	EVTHREAD_FREE_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE);
38850cc4415Schristos 	EVTHREAD_FREE_COND(cond.cond);
3892b3787f6Schristos }
3902b3787f6Schristos 
3912b3787f6Schristos #define CB_COUNT 128
3922b3787f6Schristos #define QUEUE_THREAD_COUNT 8
3932b3787f6Schristos 
3942b3787f6Schristos static void
SLEEP_MS(int ms)3952b3787f6Schristos SLEEP_MS(int ms)
3962b3787f6Schristos {
3972b3787f6Schristos 	struct timeval tv;
3982b3787f6Schristos 	tv.tv_sec = ms/1000;
3992b3787f6Schristos 	tv.tv_usec = (ms%1000)*1000;
4002b3787f6Schristos 	evutil_usleep_(&tv);
4012b3787f6Schristos }
4022b3787f6Schristos 
4032b3787f6Schristos struct deferred_test_data {
4042b3787f6Schristos 	struct event_callback cbs[CB_COUNT];
4052b3787f6Schristos 	struct event_base *queue;
4062b3787f6Schristos };
4072b3787f6Schristos 
4082b3787f6Schristos static struct timeval timer_start = {0,0};
4092b3787f6Schristos static struct timeval timer_end = {0,0};
4102b3787f6Schristos static unsigned callback_count = 0;
4112b3787f6Schristos static THREAD_T load_threads[QUEUE_THREAD_COUNT];
4122b3787f6Schristos static struct deferred_test_data deferred_data[QUEUE_THREAD_COUNT];
4132b3787f6Schristos 
4142b3787f6Schristos static void
deferred_callback(struct event_callback * cb,void * arg)4152b3787f6Schristos deferred_callback(struct event_callback *cb, void *arg)
4162b3787f6Schristos {
4172b3787f6Schristos 	SLEEP_MS(1);
4182b3787f6Schristos 	callback_count += 1;
4192b3787f6Schristos }
4202b3787f6Schristos 
4212b3787f6Schristos static THREAD_FN
load_deferred_queue(void * arg)4222b3787f6Schristos load_deferred_queue(void *arg)
4232b3787f6Schristos {
4242b3787f6Schristos 	struct deferred_test_data *data = arg;
4252b3787f6Schristos 	size_t i;
4262b3787f6Schristos 
4272b3787f6Schristos 	for (i = 0; i < CB_COUNT; ++i) {
4282b3787f6Schristos 		event_deferred_cb_init_(&data->cbs[i], 0, deferred_callback,
4292b3787f6Schristos 		    NULL);
4302b3787f6Schristos 		event_deferred_cb_schedule_(data->queue, &data->cbs[i]);
4312b3787f6Schristos 		SLEEP_MS(1);
4322b3787f6Schristos 	}
4332b3787f6Schristos 
4342b3787f6Schristos 	THREAD_RETURN();
4352b3787f6Schristos }
4362b3787f6Schristos 
4372b3787f6Schristos static void
timer_callback(evutil_socket_t fd,short what,void * arg)4382b3787f6Schristos timer_callback(evutil_socket_t fd, short what, void *arg)
4392b3787f6Schristos {
4402b3787f6Schristos 	evutil_gettimeofday(&timer_end, NULL);
4412b3787f6Schristos }
4422b3787f6Schristos 
4432b3787f6Schristos static void
start_threads_callback(evutil_socket_t fd,short what,void * arg)4442b3787f6Schristos start_threads_callback(evutil_socket_t fd, short what, void *arg)
4452b3787f6Schristos {
4462b3787f6Schristos 	int i;
4472b3787f6Schristos 
4482b3787f6Schristos 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i) {
4492b3787f6Schristos 		THREAD_START(load_threads[i], load_deferred_queue,
4502b3787f6Schristos 				&deferred_data[i]);
4512b3787f6Schristos 	}
4522b3787f6Schristos }
4532b3787f6Schristos 
4542b3787f6Schristos static void
thread_deferred_cb_skew(void * arg)4552b3787f6Schristos thread_deferred_cb_skew(void *arg)
4562b3787f6Schristos {
4572b3787f6Schristos 	struct timeval tv_timer = {1, 0};
4582b3787f6Schristos 	struct event_base *base = NULL;
4592b3787f6Schristos 	struct event_config *cfg = NULL;
4602b3787f6Schristos 	struct timeval elapsed;
4612b3787f6Schristos 	int elapsed_usec;
4622b3787f6Schristos 	int i;
4632b3787f6Schristos 
4642b3787f6Schristos 	cfg = event_config_new();
4652b3787f6Schristos 	tt_assert(cfg);
4662b3787f6Schristos 	event_config_set_max_dispatch_interval(cfg, NULL, 16, 0);
4672b3787f6Schristos 
4682b3787f6Schristos 	base = event_base_new_with_config(cfg);
4692b3787f6Schristos 	tt_assert(base);
4702b3787f6Schristos 
4712b3787f6Schristos 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
4722b3787f6Schristos 		deferred_data[i].queue = base;
4732b3787f6Schristos 
4742b3787f6Schristos 	evutil_gettimeofday(&timer_start, NULL);
4752b3787f6Schristos 	event_base_once(base, -1, EV_TIMEOUT, timer_callback, NULL,
4762b3787f6Schristos 			&tv_timer);
4772b3787f6Schristos 	event_base_once(base, -1, EV_TIMEOUT, start_threads_callback,
4782b3787f6Schristos 			NULL, NULL);
4792b3787f6Schristos 	event_base_dispatch(base);
4802b3787f6Schristos 
4812b3787f6Schristos 	evutil_timersub(&timer_end, &timer_start, &elapsed);
4822b3787f6Schristos 	TT_BLATHER(("callback count, %u", callback_count));
4832b3787f6Schristos 	elapsed_usec =
4842b3787f6Schristos 	    (unsigned)(elapsed.tv_sec*1000000 + elapsed.tv_usec);
4852b3787f6Schristos 	TT_BLATHER(("elapsed time, %u usec", elapsed_usec));
4862b3787f6Schristos 
4872b3787f6Schristos 	/* XXX be more intelligent here.  just make sure skew is
4882b3787f6Schristos 	 * within .4 seconds for now. */
4892b3787f6Schristos 	tt_assert(elapsed_usec >= 600000 && elapsed_usec <= 1400000);
4902b3787f6Schristos 
4912b3787f6Schristos end:
4922b3787f6Schristos 	for (i = 0; i < QUEUE_THREAD_COUNT; ++i)
4932b3787f6Schristos 		THREAD_JOIN(load_threads[i]);
4942b3787f6Schristos 	if (base)
4952b3787f6Schristos 		event_base_free(base);
4962b3787f6Schristos 	if (cfg)
4972b3787f6Schristos 		event_config_free(cfg);
4982b3787f6Schristos }
4992b3787f6Schristos 
5002b3787f6Schristos static struct event time_events[5];
5012b3787f6Schristos static struct timeval times[5];
5022b3787f6Schristos static struct event_base *exit_base = NULL;
5032b3787f6Schristos static void
note_time_cb(evutil_socket_t fd,short what,void * arg)5042b3787f6Schristos note_time_cb(evutil_socket_t fd, short what, void *arg)
5052b3787f6Schristos {
5062b3787f6Schristos 	evutil_gettimeofday(arg, NULL);
5072b3787f6Schristos 	if (arg == &times[4]) {
5082b3787f6Schristos 		event_base_loopbreak(exit_base);
5092b3787f6Schristos 	}
5102b3787f6Schristos }
5112b3787f6Schristos static THREAD_FN
register_events_subthread(void * arg)5122b3787f6Schristos register_events_subthread(void *arg)
5132b3787f6Schristos {
5142b3787f6Schristos 	struct timeval tv = {0,0};
5152b3787f6Schristos 	SLEEP_MS(100);
5162b3787f6Schristos 	event_active(&time_events[0], EV_TIMEOUT, 1);
5172b3787f6Schristos 	SLEEP_MS(100);
5182b3787f6Schristos 	event_active(&time_events[1], EV_TIMEOUT, 1);
5192b3787f6Schristos 	SLEEP_MS(100);
5202b3787f6Schristos 	tv.tv_usec = 100*1000;
5212b3787f6Schristos 	event_add(&time_events[2], &tv);
5222b3787f6Schristos 	tv.tv_usec = 150*1000;
5232b3787f6Schristos 	event_add(&time_events[3], &tv);
5242b3787f6Schristos 	SLEEP_MS(200);
5252b3787f6Schristos 	event_active(&time_events[4], EV_TIMEOUT, 1);
5262b3787f6Schristos 
5272b3787f6Schristos 	THREAD_RETURN();
5282b3787f6Schristos }
5292b3787f6Schristos 
5302b3787f6Schristos static void
thread_no_events(void * arg)5312b3787f6Schristos thread_no_events(void *arg)
5322b3787f6Schristos {
5332b3787f6Schristos 	THREAD_T thread;
5342b3787f6Schristos 	struct basic_test_data *data = arg;
5352b3787f6Schristos 	struct timeval starttime, endtime;
5362b3787f6Schristos 	int i;
5372b3787f6Schristos 	exit_base = data->base;
5382b3787f6Schristos 
5392b3787f6Schristos 	memset(times,0,sizeof(times));
5402b3787f6Schristos 	for (i=0;i<5;++i) {
5412b3787f6Schristos 		event_assign(&time_events[i], data->base,
5422b3787f6Schristos 		    -1, 0, note_time_cb, &times[i]);
5432b3787f6Schristos 	}
5442b3787f6Schristos 
5452b3787f6Schristos 	evutil_gettimeofday(&starttime, NULL);
5462b3787f6Schristos 	THREAD_START(thread, register_events_subthread, data->base);
5472b3787f6Schristos 	event_base_loop(data->base, EVLOOP_NO_EXIT_ON_EMPTY);
5482b3787f6Schristos 	evutil_gettimeofday(&endtime, NULL);
5492b3787f6Schristos 	tt_assert(event_base_got_break(data->base));
5502b3787f6Schristos 	THREAD_JOIN(thread);
5512b3787f6Schristos 	for (i=0; i<5; ++i) {
5522b3787f6Schristos 		struct timeval diff;
5532b3787f6Schristos 		double sec;
5542b3787f6Schristos 		evutil_timersub(&times[i], &starttime, &diff);
5552b3787f6Schristos 		sec = diff.tv_sec + diff.tv_usec/1.0e6;
5562b3787f6Schristos 		TT_BLATHER(("event %d at %.4f seconds", i, sec));
5572b3787f6Schristos 	}
5582b3787f6Schristos 	test_timeval_diff_eq(&starttime, &times[0], 100);
5592b3787f6Schristos 	test_timeval_diff_eq(&starttime, &times[1], 200);
5602b3787f6Schristos 	test_timeval_diff_eq(&starttime, &times[2], 400);
5612b3787f6Schristos 	test_timeval_diff_eq(&starttime, &times[3], 450);
5622b3787f6Schristos 	test_timeval_diff_eq(&starttime, &times[4], 500);
5632b3787f6Schristos 	test_timeval_diff_eq(&starttime, &endtime,  500);
5642b3787f6Schristos 
5652b3787f6Schristos end:
5662b3787f6Schristos 	;
5672b3787f6Schristos }
5682b3787f6Schristos 
5692b3787f6Schristos #define TEST(name)							\
5702b3787f6Schristos 	{ #name, thread_##name, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,	\
5712b3787f6Schristos 	  &basic_setup, NULL }
5722b3787f6Schristos 
5732b3787f6Schristos struct testcase_t thread_testcases[] = {
5742b3787f6Schristos 	{ "basic", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
5752b3787f6Schristos 	  &basic_setup, NULL },
5762b3787f6Schristos #ifndef _WIN32
5772b3787f6Schristos 	{ "forking", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE,
5782b3787f6Schristos 	  &basic_setup, (char*)"forking" },
5792b3787f6Schristos #endif
5802b3787f6Schristos 	TEST(conditions_simple),
5812b3787f6Schristos 	{ "deferred_cb_skew", thread_deferred_cb_skew,
5822b3787f6Schristos 	  TT_FORK|TT_NEED_THREADS|TT_OFF_BY_DEFAULT,
5832b3787f6Schristos 	  &basic_setup, NULL },
5842b3787f6Schristos 	TEST(no_events),
5852b3787f6Schristos 	END_OF_TESTCASES
5862b3787f6Schristos };
5872b3787f6Schristos 
588