michael@0: /* michael@0: * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson michael@0: * michael@0: * Redistribution and use in source and binary forms, with or without michael@0: * modification, are permitted provided that the following conditions michael@0: * are met: michael@0: * 1. Redistributions of source code must retain the above copyright michael@0: * notice, this list of conditions and the following disclaimer. michael@0: * 2. Redistributions in binary form must reproduce the above copyright michael@0: * notice, this list of conditions and the following disclaimer in the michael@0: * documentation and/or other materials provided with the distribution. michael@0: * 3. The name of the author may not be used to endorse or promote products michael@0: * derived from this software without specific prior written permission. michael@0: * michael@0: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR michael@0: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES michael@0: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. michael@0: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, michael@0: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT michael@0: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, michael@0: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY michael@0: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT michael@0: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF michael@0: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. michael@0: */ michael@0: michael@0: /* The old tests here need assertions to work. */ michael@0: #undef NDEBUG michael@0: michael@0: #include "event2/event-config.h" michael@0: michael@0: #include michael@0: #include michael@0: #include michael@0: #include michael@0: #ifdef _EVENT_HAVE_UNISTD_H michael@0: #include michael@0: #endif michael@0: #ifdef _EVENT_HAVE_SYS_WAIT_H michael@0: #include michael@0: #endif michael@0: michael@0: #ifdef _EVENT_HAVE_PTHREADS michael@0: #include michael@0: #elif defined(WIN32) michael@0: #include michael@0: #endif michael@0: #include michael@0: #ifdef _EVENT_HAVE_UNISTD_H michael@0: #include michael@0: #endif michael@0: #include michael@0: michael@0: #include "sys/queue.h" michael@0: michael@0: #include "event2/util.h" michael@0: #include "event2/event.h" michael@0: #include "event2/event_struct.h" michael@0: #include "event2/thread.h" michael@0: #include "evthread-internal.h" michael@0: #include "event-internal.h" michael@0: #include "defer-internal.h" michael@0: #include "regress.h" michael@0: #include "tinytest_macros.h" michael@0: michael@0: #ifdef _EVENT_HAVE_PTHREADS michael@0: #define THREAD_T pthread_t michael@0: #define THREAD_FN void * michael@0: #define THREAD_RETURN() return (NULL) michael@0: #define THREAD_START(threadvar, fn, arg) \ michael@0: pthread_create(&(threadvar), NULL, fn, arg) michael@0: #define THREAD_JOIN(th) pthread_join(th, NULL) michael@0: #else michael@0: #define THREAD_T HANDLE michael@0: #define THREAD_FN unsigned __stdcall michael@0: #define THREAD_RETURN() return (0) michael@0: #define THREAD_START(threadvar, fn, arg) do { \ michael@0: uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL); \ michael@0: (threadvar) = (HANDLE) threadhandle; \ michael@0: } while (0) michael@0: #define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE) michael@0: #endif michael@0: michael@0: struct cond_wait { michael@0: void *lock; michael@0: void *cond; michael@0: }; michael@0: michael@0: static void michael@0: wake_all_timeout(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: struct cond_wait *cw = arg; michael@0: EVLOCK_LOCK(cw->lock, 0); michael@0: EVTHREAD_COND_BROADCAST(cw->cond); michael@0: EVLOCK_UNLOCK(cw->lock, 0); michael@0: michael@0: } michael@0: michael@0: static void michael@0: wake_one_timeout(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: struct cond_wait *cw = arg; michael@0: EVLOCK_LOCK(cw->lock, 0); michael@0: EVTHREAD_COND_SIGNAL(cw->cond); michael@0: EVLOCK_UNLOCK(cw->lock, 0); michael@0: } michael@0: michael@0: #define NUM_THREADS 100 michael@0: #define NUM_ITERATIONS 100 michael@0: void *count_lock; michael@0: static int count; michael@0: michael@0: static THREAD_FN michael@0: basic_thread(void *arg) michael@0: { michael@0: struct cond_wait cw; michael@0: struct event_base *base = arg; michael@0: struct event ev; michael@0: int i = 0; michael@0: michael@0: EVTHREAD_ALLOC_LOCK(cw.lock, 0); michael@0: EVTHREAD_ALLOC_COND(cw.cond); michael@0: assert(cw.lock); michael@0: assert(cw.cond); michael@0: michael@0: evtimer_assign(&ev, base, wake_all_timeout, &cw); michael@0: for (i = 0; i < NUM_ITERATIONS; i++) { michael@0: struct timeval tv; michael@0: evutil_timerclear(&tv); michael@0: tv.tv_sec = 0; michael@0: tv.tv_usec = 3000; michael@0: michael@0: EVLOCK_LOCK(cw.lock, 0); michael@0: /* we need to make sure that event does not happen before michael@0: * we get to wait on the conditional variable */ michael@0: assert(evtimer_add(&ev, &tv) == 0); michael@0: michael@0: assert(EVTHREAD_COND_WAIT(cw.cond, cw.lock) == 0); michael@0: EVLOCK_UNLOCK(cw.lock, 0); michael@0: michael@0: EVLOCK_LOCK(count_lock, 0); michael@0: ++count; michael@0: EVLOCK_UNLOCK(count_lock, 0); michael@0: } michael@0: michael@0: /* exit the loop only if all threads fired all timeouts */ michael@0: EVLOCK_LOCK(count_lock, 0); michael@0: if (count >= NUM_THREADS * NUM_ITERATIONS) michael@0: event_base_loopexit(base, NULL); michael@0: EVLOCK_UNLOCK(count_lock, 0); michael@0: michael@0: EVTHREAD_FREE_LOCK(cw.lock, 0); michael@0: EVTHREAD_FREE_COND(cw.cond); michael@0: michael@0: THREAD_RETURN(); michael@0: } michael@0: michael@0: static int notification_fd_used = 0; michael@0: #ifndef WIN32 michael@0: static int got_sigchld = 0; michael@0: static void michael@0: sigchld_cb(evutil_socket_t fd, short event, void *arg) michael@0: { michael@0: struct timeval tv; michael@0: struct event_base *base = arg; michael@0: michael@0: got_sigchld++; michael@0: tv.tv_usec = 100000; michael@0: tv.tv_sec = 0; michael@0: event_base_loopexit(base, &tv); michael@0: } michael@0: michael@0: michael@0: static void michael@0: notify_fd_cb(evutil_socket_t fd, short event, void *arg) michael@0: { michael@0: ++notification_fd_used; michael@0: } michael@0: #endif michael@0: michael@0: static void michael@0: thread_basic(void *arg) michael@0: { michael@0: THREAD_T threads[NUM_THREADS]; michael@0: struct event ev; michael@0: struct timeval tv; michael@0: int i; michael@0: struct basic_test_data *data = arg; michael@0: struct event_base *base = data->base; michael@0: michael@0: struct event *notification_event = NULL; michael@0: struct event *sigchld_event = NULL; michael@0: michael@0: EVTHREAD_ALLOC_LOCK(count_lock, 0); michael@0: tt_assert(count_lock); michael@0: michael@0: tt_assert(base); michael@0: if (evthread_make_base_notifiable(base)<0) { michael@0: tt_abort_msg("Couldn't make base notifiable!"); michael@0: } michael@0: michael@0: #ifndef WIN32 michael@0: if (data->setup_data && !strcmp(data->setup_data, "forking")) { michael@0: pid_t pid; michael@0: int status; michael@0: sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base); michael@0: /* This piggybacks on the th_notify_fd weirdly, and looks michael@0: * inside libevent internals. Not a good idea in non-testing michael@0: * code! */ michael@0: notification_event = event_new(base, michael@0: base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb, michael@0: NULL); michael@0: event_add(sigchld_event, NULL); michael@0: event_add(notification_event, NULL); michael@0: michael@0: if ((pid = fork()) == 0) { michael@0: event_del(notification_event); michael@0: if (event_reinit(base) < 0) { michael@0: TT_FAIL(("reinit")); michael@0: exit(1); michael@0: } michael@0: event_assign(notification_event, base, michael@0: base->th_notify_fd[0], EV_READ|EV_PERSIST, michael@0: notify_fd_cb, NULL); michael@0: event_add(notification_event, NULL); michael@0: goto child; michael@0: } michael@0: michael@0: event_base_dispatch(base); michael@0: michael@0: if (waitpid(pid, &status, 0) == -1) michael@0: tt_abort_perror("waitpid"); michael@0: TT_BLATHER(("Waitpid okay\n")); michael@0: michael@0: tt_assert(got_sigchld); michael@0: tt_int_op(notification_fd_used, ==, 0); michael@0: michael@0: goto end; michael@0: } michael@0: michael@0: child: michael@0: #endif michael@0: for (i = 0; i < NUM_THREADS; ++i) michael@0: THREAD_START(threads[i], basic_thread, base); michael@0: michael@0: evtimer_assign(&ev, base, NULL, NULL); michael@0: evutil_timerclear(&tv); michael@0: tv.tv_sec = 1000; michael@0: event_add(&ev, &tv); michael@0: michael@0: event_base_dispatch(base); michael@0: michael@0: for (i = 0; i < NUM_THREADS; ++i) michael@0: THREAD_JOIN(threads[i]); michael@0: michael@0: event_del(&ev); michael@0: michael@0: tt_int_op(count, ==, NUM_THREADS * NUM_ITERATIONS); michael@0: michael@0: EVTHREAD_FREE_LOCK(count_lock, 0); michael@0: michael@0: TT_BLATHER(("notifiations==%d", notification_fd_used)); michael@0: michael@0: end: michael@0: michael@0: if (notification_event) michael@0: event_free(notification_event); michael@0: if (sigchld_event) michael@0: event_free(sigchld_event); michael@0: } michael@0: michael@0: #undef NUM_THREADS michael@0: #define NUM_THREADS 10 michael@0: michael@0: struct alerted_record { michael@0: struct cond_wait *cond; michael@0: struct timeval delay; michael@0: struct timeval alerted_at; michael@0: int timed_out; michael@0: }; michael@0: michael@0: static THREAD_FN michael@0: wait_for_condition(void *arg) michael@0: { michael@0: struct alerted_record *rec = arg; michael@0: int r; michael@0: michael@0: EVLOCK_LOCK(rec->cond->lock, 0); michael@0: if (rec->delay.tv_sec || rec->delay.tv_usec) { michael@0: r = EVTHREAD_COND_WAIT_TIMED(rec->cond->cond, rec->cond->lock, michael@0: &rec->delay); michael@0: } else { michael@0: r = EVTHREAD_COND_WAIT(rec->cond->cond, rec->cond->lock); michael@0: } michael@0: EVLOCK_UNLOCK(rec->cond->lock, 0); michael@0: michael@0: evutil_gettimeofday(&rec->alerted_at, NULL); michael@0: if (r == 1) michael@0: rec->timed_out = 1; michael@0: michael@0: THREAD_RETURN(); michael@0: } michael@0: michael@0: static void michael@0: thread_conditions_simple(void *arg) michael@0: { michael@0: struct timeval tv_signal, tv_timeout, tv_broadcast; michael@0: struct alerted_record alerted[NUM_THREADS]; michael@0: THREAD_T threads[NUM_THREADS]; michael@0: struct cond_wait cond; michael@0: int i; michael@0: struct timeval launched_at; michael@0: struct event wake_one; michael@0: struct event wake_all; michael@0: struct basic_test_data *data = arg; michael@0: struct event_base *base = data->base; michael@0: int n_timed_out=0, n_signal=0, n_broadcast=0; michael@0: michael@0: tv_signal.tv_sec = tv_timeout.tv_sec = tv_broadcast.tv_sec = 0; michael@0: tv_signal.tv_usec = 30*1000; michael@0: tv_timeout.tv_usec = 150*1000; michael@0: tv_broadcast.tv_usec = 500*1000; michael@0: michael@0: EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE); michael@0: EVTHREAD_ALLOC_COND(cond.cond); michael@0: tt_assert(cond.lock); michael@0: tt_assert(cond.cond); michael@0: for (i = 0; i < NUM_THREADS; ++i) { michael@0: memset(&alerted[i], 0, sizeof(struct alerted_record)); michael@0: alerted[i].cond = &cond; michael@0: } michael@0: michael@0: /* Threads 5 and 6 will be allowed to time out */ michael@0: memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout)); michael@0: memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout)); michael@0: michael@0: evtimer_assign(&wake_one, base, wake_one_timeout, &cond); michael@0: evtimer_assign(&wake_all, base, wake_all_timeout, &cond); michael@0: michael@0: evutil_gettimeofday(&launched_at, NULL); michael@0: michael@0: /* Launch the threads... */ michael@0: for (i = 0; i < NUM_THREADS; ++i) { michael@0: THREAD_START(threads[i], wait_for_condition, &alerted[i]); michael@0: } michael@0: michael@0: /* Start the timers... */ michael@0: tt_int_op(event_add(&wake_one, &tv_signal), ==, 0); michael@0: tt_int_op(event_add(&wake_all, &tv_broadcast), ==, 0); michael@0: michael@0: /* And run for a bit... */ michael@0: event_base_dispatch(base); michael@0: michael@0: /* And wait till the threads are done. */ michael@0: for (i = 0; i < NUM_THREADS; ++i) michael@0: THREAD_JOIN(threads[i]); michael@0: michael@0: /* Now, let's see what happened. At least one of 5 or 6 should michael@0: * have timed out. */ michael@0: n_timed_out = alerted[5].timed_out + alerted[6].timed_out; michael@0: tt_int_op(n_timed_out, >=, 1); michael@0: tt_int_op(n_timed_out, <=, 2); michael@0: michael@0: for (i = 0; i < NUM_THREADS; ++i) { michael@0: const struct timeval *target_delay; michael@0: struct timeval target_time, actual_delay; michael@0: if (alerted[i].timed_out) { michael@0: TT_BLATHER(("%d looks like a timeout\n", i)); michael@0: target_delay = &tv_timeout; michael@0: tt_assert(i == 5 || i == 6); michael@0: } else if (evutil_timerisset(&alerted[i].alerted_at)) { michael@0: long diff1,diff2; michael@0: evutil_timersub(&alerted[i].alerted_at, michael@0: &launched_at, &actual_delay); michael@0: diff1 = timeval_msec_diff(&actual_delay, michael@0: &tv_signal); michael@0: diff2 = timeval_msec_diff(&actual_delay, michael@0: &tv_broadcast); michael@0: if (abs(diff1) < abs(diff2)) { michael@0: TT_BLATHER(("%d looks like a signal\n", i)); michael@0: target_delay = &tv_signal; michael@0: ++n_signal; michael@0: } else { michael@0: TT_BLATHER(("%d looks like a broadcast\n", i)); michael@0: target_delay = &tv_broadcast; michael@0: ++n_broadcast; michael@0: } michael@0: } else { michael@0: TT_FAIL(("Thread %d never got woken", i)); michael@0: continue; michael@0: } michael@0: evutil_timeradd(target_delay, &launched_at, &target_time); michael@0: test_timeval_diff_leq(&target_time, &alerted[i].alerted_at, michael@0: 0, 50); michael@0: } michael@0: tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS); michael@0: tt_int_op(n_signal, ==, 1); michael@0: michael@0: end: michael@0: ; michael@0: } michael@0: michael@0: #define CB_COUNT 128 michael@0: #define QUEUE_THREAD_COUNT 8 michael@0: michael@0: #ifdef WIN32 michael@0: #define SLEEP_MS(ms) Sleep(ms) michael@0: #else michael@0: #define SLEEP_MS(ms) usleep((ms) * 1000) michael@0: #endif michael@0: michael@0: struct deferred_test_data { michael@0: struct deferred_cb cbs[CB_COUNT]; michael@0: struct deferred_cb_queue *queue; michael@0: }; michael@0: michael@0: static time_t timer_start = 0; michael@0: static time_t timer_end = 0; michael@0: static unsigned callback_count = 0; michael@0: static THREAD_T load_threads[QUEUE_THREAD_COUNT]; michael@0: static struct deferred_test_data deferred_data[QUEUE_THREAD_COUNT]; michael@0: michael@0: static void michael@0: deferred_callback(struct deferred_cb *cb, void *arg) michael@0: { michael@0: SLEEP_MS(1); michael@0: callback_count += 1; michael@0: } michael@0: michael@0: static THREAD_FN michael@0: load_deferred_queue(void *arg) michael@0: { michael@0: struct deferred_test_data *data = arg; michael@0: size_t i; michael@0: michael@0: for (i = 0; i < CB_COUNT; ++i) { michael@0: event_deferred_cb_init(&data->cbs[i], deferred_callback, NULL); michael@0: event_deferred_cb_schedule(data->queue, &data->cbs[i]); michael@0: SLEEP_MS(1); michael@0: } michael@0: michael@0: THREAD_RETURN(); michael@0: } michael@0: michael@0: static void michael@0: timer_callback(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: timer_end = time(NULL); michael@0: } michael@0: michael@0: static void michael@0: start_threads_callback(evutil_socket_t fd, short what, void *arg) michael@0: { michael@0: int i; michael@0: michael@0: for (i = 0; i < QUEUE_THREAD_COUNT; ++i) { michael@0: THREAD_START(load_threads[i], load_deferred_queue, michael@0: &deferred_data[i]); michael@0: } michael@0: } michael@0: michael@0: static void michael@0: thread_deferred_cb_skew(void *arg) michael@0: { michael@0: struct basic_test_data *data = arg; michael@0: struct timeval tv_timer = {4, 0}; michael@0: struct deferred_cb_queue *queue; michael@0: time_t elapsed; michael@0: int i; michael@0: michael@0: queue = event_base_get_deferred_cb_queue(data->base); michael@0: tt_assert(queue); michael@0: michael@0: for (i = 0; i < QUEUE_THREAD_COUNT; ++i) michael@0: deferred_data[i].queue = queue; michael@0: michael@0: timer_start = time(NULL); michael@0: event_base_once(data->base, -1, EV_TIMEOUT, timer_callback, NULL, michael@0: &tv_timer); michael@0: event_base_once(data->base, -1, EV_TIMEOUT, start_threads_callback, michael@0: NULL, NULL); michael@0: event_base_dispatch(data->base); michael@0: michael@0: elapsed = timer_end - timer_start; michael@0: TT_BLATHER(("callback count, %u", callback_count)); michael@0: TT_BLATHER(("elapsed time, %u", (unsigned)elapsed)); michael@0: /* XXX be more intelligent here. just make sure skew is michael@0: * within 2 seconds for now. */ michael@0: tt_assert(elapsed >= 4 && elapsed <= 6); michael@0: michael@0: end: michael@0: for (i = 0; i < QUEUE_THREAD_COUNT; ++i) michael@0: THREAD_JOIN(load_threads[i]); michael@0: } michael@0: michael@0: #define TEST(name) \ michael@0: { #name, thread_##name, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE, \ michael@0: &basic_setup, NULL } michael@0: michael@0: struct testcase_t thread_testcases[] = { michael@0: { "basic", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE, michael@0: &basic_setup, NULL }, michael@0: #ifndef WIN32 michael@0: { "forking", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE, michael@0: &basic_setup, (char*)"forking" }, michael@0: #endif michael@0: TEST(conditions_simple), michael@0: TEST(deferred_cb_skew), michael@0: END_OF_TESTCASES michael@0: }; michael@0: