1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/ipc/chromium/src/third_party/libevent/test/regress_thread.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,511 @@ 1.4 +/* 1.5 + * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1. Redistributions of source code must retain the above copyright 1.11 + * notice, this list of conditions and the following disclaimer. 1.12 + * 2. Redistributions in binary form must reproduce the above copyright 1.13 + * notice, this list of conditions and the following disclaimer in the 1.14 + * documentation and/or other materials provided with the distribution. 1.15 + * 3. The name of the author may not be used to endorse or promote products 1.16 + * derived from this software without specific prior written permission. 1.17 + * 1.18 + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 1.19 + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 1.20 + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 1.21 + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 1.22 + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 1.23 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.24 + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.25 + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.26 + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 1.27 + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.28 + */ 1.29 + 1.30 +/* The old tests here need assertions to work. */ 1.31 +#undef NDEBUG 1.32 + 1.33 +#include "event2/event-config.h" 1.34 + 1.35 +#include <sys/types.h> 1.36 +#include <stdio.h> 1.37 +#include <stdlib.h> 1.38 +#include <string.h> 1.39 +#ifdef _EVENT_HAVE_UNISTD_H 1.40 +#include <unistd.h> 1.41 +#endif 1.42 +#ifdef _EVENT_HAVE_SYS_WAIT_H 1.43 +#include <sys/wait.h> 1.44 +#endif 1.45 + 1.46 +#ifdef _EVENT_HAVE_PTHREADS 1.47 +#include <pthread.h> 1.48 +#elif defined(WIN32) 1.49 +#include <process.h> 1.50 +#endif 1.51 +#include <assert.h> 1.52 +#ifdef _EVENT_HAVE_UNISTD_H 1.53 +#include <unistd.h> 1.54 +#endif 1.55 +#include <time.h> 1.56 + 1.57 +#include "sys/queue.h" 1.58 + 1.59 +#include "event2/util.h" 1.60 +#include "event2/event.h" 1.61 +#include "event2/event_struct.h" 1.62 +#include "event2/thread.h" 1.63 +#include "evthread-internal.h" 1.64 +#include "event-internal.h" 1.65 +#include "defer-internal.h" 1.66 +#include "regress.h" 1.67 +#include "tinytest_macros.h" 1.68 + 1.69 +#ifdef _EVENT_HAVE_PTHREADS 1.70 +#define THREAD_T pthread_t 1.71 +#define THREAD_FN void * 1.72 +#define THREAD_RETURN() return (NULL) 1.73 +#define THREAD_START(threadvar, fn, arg) \ 1.74 + pthread_create(&(threadvar), NULL, fn, arg) 1.75 +#define THREAD_JOIN(th) pthread_join(th, NULL) 1.76 +#else 1.77 +#define THREAD_T HANDLE 1.78 +#define THREAD_FN unsigned __stdcall 1.79 +#define THREAD_RETURN() return (0) 1.80 +#define THREAD_START(threadvar, fn, arg) do { \ 1.81 + uintptr_t threadhandle = _beginthreadex(NULL,0,fn,(arg),0,NULL); \ 1.82 + (threadvar) = (HANDLE) threadhandle; \ 1.83 + } while (0) 1.84 +#define THREAD_JOIN(th) WaitForSingleObject(th, INFINITE) 1.85 +#endif 1.86 + 1.87 +struct cond_wait { 1.88 + void *lock; 1.89 + void *cond; 1.90 +}; 1.91 + 1.92 +static void 1.93 +wake_all_timeout(evutil_socket_t fd, short what, void *arg) 1.94 +{ 1.95 + struct cond_wait *cw = arg; 1.96 + EVLOCK_LOCK(cw->lock, 0); 1.97 + EVTHREAD_COND_BROADCAST(cw->cond); 1.98 + EVLOCK_UNLOCK(cw->lock, 0); 1.99 + 1.100 +} 1.101 + 1.102 +static void 1.103 +wake_one_timeout(evutil_socket_t fd, short what, void *arg) 1.104 +{ 1.105 + struct cond_wait *cw = arg; 1.106 + EVLOCK_LOCK(cw->lock, 0); 1.107 + EVTHREAD_COND_SIGNAL(cw->cond); 1.108 + EVLOCK_UNLOCK(cw->lock, 0); 1.109 +} 1.110 + 1.111 +#define NUM_THREADS 100 1.112 +#define NUM_ITERATIONS 100 1.113 +void *count_lock; 1.114 +static int count; 1.115 + 1.116 +static THREAD_FN 1.117 +basic_thread(void *arg) 1.118 +{ 1.119 + struct cond_wait cw; 1.120 + struct event_base *base = arg; 1.121 + struct event ev; 1.122 + int i = 0; 1.123 + 1.124 + EVTHREAD_ALLOC_LOCK(cw.lock, 0); 1.125 + EVTHREAD_ALLOC_COND(cw.cond); 1.126 + assert(cw.lock); 1.127 + assert(cw.cond); 1.128 + 1.129 + evtimer_assign(&ev, base, wake_all_timeout, &cw); 1.130 + for (i = 0; i < NUM_ITERATIONS; i++) { 1.131 + struct timeval tv; 1.132 + evutil_timerclear(&tv); 1.133 + tv.tv_sec = 0; 1.134 + tv.tv_usec = 3000; 1.135 + 1.136 + EVLOCK_LOCK(cw.lock, 0); 1.137 + /* we need to make sure that event does not happen before 1.138 + * we get to wait on the conditional variable */ 1.139 + assert(evtimer_add(&ev, &tv) == 0); 1.140 + 1.141 + assert(EVTHREAD_COND_WAIT(cw.cond, cw.lock) == 0); 1.142 + EVLOCK_UNLOCK(cw.lock, 0); 1.143 + 1.144 + EVLOCK_LOCK(count_lock, 0); 1.145 + ++count; 1.146 + EVLOCK_UNLOCK(count_lock, 0); 1.147 + } 1.148 + 1.149 + /* exit the loop only if all threads fired all timeouts */ 1.150 + EVLOCK_LOCK(count_lock, 0); 1.151 + if (count >= NUM_THREADS * NUM_ITERATIONS) 1.152 + event_base_loopexit(base, NULL); 1.153 + EVLOCK_UNLOCK(count_lock, 0); 1.154 + 1.155 + EVTHREAD_FREE_LOCK(cw.lock, 0); 1.156 + EVTHREAD_FREE_COND(cw.cond); 1.157 + 1.158 + THREAD_RETURN(); 1.159 +} 1.160 + 1.161 +static int notification_fd_used = 0; 1.162 +#ifndef WIN32 1.163 +static int got_sigchld = 0; 1.164 +static void 1.165 +sigchld_cb(evutil_socket_t fd, short event, void *arg) 1.166 +{ 1.167 + struct timeval tv; 1.168 + struct event_base *base = arg; 1.169 + 1.170 + got_sigchld++; 1.171 + tv.tv_usec = 100000; 1.172 + tv.tv_sec = 0; 1.173 + event_base_loopexit(base, &tv); 1.174 +} 1.175 + 1.176 + 1.177 +static void 1.178 +notify_fd_cb(evutil_socket_t fd, short event, void *arg) 1.179 +{ 1.180 + ++notification_fd_used; 1.181 +} 1.182 +#endif 1.183 + 1.184 +static void 1.185 +thread_basic(void *arg) 1.186 +{ 1.187 + THREAD_T threads[NUM_THREADS]; 1.188 + struct event ev; 1.189 + struct timeval tv; 1.190 + int i; 1.191 + struct basic_test_data *data = arg; 1.192 + struct event_base *base = data->base; 1.193 + 1.194 + struct event *notification_event = NULL; 1.195 + struct event *sigchld_event = NULL; 1.196 + 1.197 + EVTHREAD_ALLOC_LOCK(count_lock, 0); 1.198 + tt_assert(count_lock); 1.199 + 1.200 + tt_assert(base); 1.201 + if (evthread_make_base_notifiable(base)<0) { 1.202 + tt_abort_msg("Couldn't make base notifiable!"); 1.203 + } 1.204 + 1.205 +#ifndef WIN32 1.206 + if (data->setup_data && !strcmp(data->setup_data, "forking")) { 1.207 + pid_t pid; 1.208 + int status; 1.209 + sigchld_event = evsignal_new(base, SIGCHLD, sigchld_cb, base); 1.210 + /* This piggybacks on the th_notify_fd weirdly, and looks 1.211 + * inside libevent internals. Not a good idea in non-testing 1.212 + * code! */ 1.213 + notification_event = event_new(base, 1.214 + base->th_notify_fd[0], EV_READ|EV_PERSIST, notify_fd_cb, 1.215 + NULL); 1.216 + event_add(sigchld_event, NULL); 1.217 + event_add(notification_event, NULL); 1.218 + 1.219 + if ((pid = fork()) == 0) { 1.220 + event_del(notification_event); 1.221 + if (event_reinit(base) < 0) { 1.222 + TT_FAIL(("reinit")); 1.223 + exit(1); 1.224 + } 1.225 + event_assign(notification_event, base, 1.226 + base->th_notify_fd[0], EV_READ|EV_PERSIST, 1.227 + notify_fd_cb, NULL); 1.228 + event_add(notification_event, NULL); 1.229 + goto child; 1.230 + } 1.231 + 1.232 + event_base_dispatch(base); 1.233 + 1.234 + if (waitpid(pid, &status, 0) == -1) 1.235 + tt_abort_perror("waitpid"); 1.236 + TT_BLATHER(("Waitpid okay\n")); 1.237 + 1.238 + tt_assert(got_sigchld); 1.239 + tt_int_op(notification_fd_used, ==, 0); 1.240 + 1.241 + goto end; 1.242 + } 1.243 + 1.244 +child: 1.245 +#endif 1.246 + for (i = 0; i < NUM_THREADS; ++i) 1.247 + THREAD_START(threads[i], basic_thread, base); 1.248 + 1.249 + evtimer_assign(&ev, base, NULL, NULL); 1.250 + evutil_timerclear(&tv); 1.251 + tv.tv_sec = 1000; 1.252 + event_add(&ev, &tv); 1.253 + 1.254 + event_base_dispatch(base); 1.255 + 1.256 + for (i = 0; i < NUM_THREADS; ++i) 1.257 + THREAD_JOIN(threads[i]); 1.258 + 1.259 + event_del(&ev); 1.260 + 1.261 + tt_int_op(count, ==, NUM_THREADS * NUM_ITERATIONS); 1.262 + 1.263 + EVTHREAD_FREE_LOCK(count_lock, 0); 1.264 + 1.265 + TT_BLATHER(("notifiations==%d", notification_fd_used)); 1.266 + 1.267 +end: 1.268 + 1.269 + if (notification_event) 1.270 + event_free(notification_event); 1.271 + if (sigchld_event) 1.272 + event_free(sigchld_event); 1.273 +} 1.274 + 1.275 +#undef NUM_THREADS 1.276 +#define NUM_THREADS 10 1.277 + 1.278 +struct alerted_record { 1.279 + struct cond_wait *cond; 1.280 + struct timeval delay; 1.281 + struct timeval alerted_at; 1.282 + int timed_out; 1.283 +}; 1.284 + 1.285 +static THREAD_FN 1.286 +wait_for_condition(void *arg) 1.287 +{ 1.288 + struct alerted_record *rec = arg; 1.289 + int r; 1.290 + 1.291 + EVLOCK_LOCK(rec->cond->lock, 0); 1.292 + if (rec->delay.tv_sec || rec->delay.tv_usec) { 1.293 + r = EVTHREAD_COND_WAIT_TIMED(rec->cond->cond, rec->cond->lock, 1.294 + &rec->delay); 1.295 + } else { 1.296 + r = EVTHREAD_COND_WAIT(rec->cond->cond, rec->cond->lock); 1.297 + } 1.298 + EVLOCK_UNLOCK(rec->cond->lock, 0); 1.299 + 1.300 + evutil_gettimeofday(&rec->alerted_at, NULL); 1.301 + if (r == 1) 1.302 + rec->timed_out = 1; 1.303 + 1.304 + THREAD_RETURN(); 1.305 +} 1.306 + 1.307 +static void 1.308 +thread_conditions_simple(void *arg) 1.309 +{ 1.310 + struct timeval tv_signal, tv_timeout, tv_broadcast; 1.311 + struct alerted_record alerted[NUM_THREADS]; 1.312 + THREAD_T threads[NUM_THREADS]; 1.313 + struct cond_wait cond; 1.314 + int i; 1.315 + struct timeval launched_at; 1.316 + struct event wake_one; 1.317 + struct event wake_all; 1.318 + struct basic_test_data *data = arg; 1.319 + struct event_base *base = data->base; 1.320 + int n_timed_out=0, n_signal=0, n_broadcast=0; 1.321 + 1.322 + tv_signal.tv_sec = tv_timeout.tv_sec = tv_broadcast.tv_sec = 0; 1.323 + tv_signal.tv_usec = 30*1000; 1.324 + tv_timeout.tv_usec = 150*1000; 1.325 + tv_broadcast.tv_usec = 500*1000; 1.326 + 1.327 + EVTHREAD_ALLOC_LOCK(cond.lock, EVTHREAD_LOCKTYPE_RECURSIVE); 1.328 + EVTHREAD_ALLOC_COND(cond.cond); 1.329 + tt_assert(cond.lock); 1.330 + tt_assert(cond.cond); 1.331 + for (i = 0; i < NUM_THREADS; ++i) { 1.332 + memset(&alerted[i], 0, sizeof(struct alerted_record)); 1.333 + alerted[i].cond = &cond; 1.334 + } 1.335 + 1.336 + /* Threads 5 and 6 will be allowed to time out */ 1.337 + memcpy(&alerted[5].delay, &tv_timeout, sizeof(tv_timeout)); 1.338 + memcpy(&alerted[6].delay, &tv_timeout, sizeof(tv_timeout)); 1.339 + 1.340 + evtimer_assign(&wake_one, base, wake_one_timeout, &cond); 1.341 + evtimer_assign(&wake_all, base, wake_all_timeout, &cond); 1.342 + 1.343 + evutil_gettimeofday(&launched_at, NULL); 1.344 + 1.345 + /* Launch the threads... */ 1.346 + for (i = 0; i < NUM_THREADS; ++i) { 1.347 + THREAD_START(threads[i], wait_for_condition, &alerted[i]); 1.348 + } 1.349 + 1.350 + /* Start the timers... */ 1.351 + tt_int_op(event_add(&wake_one, &tv_signal), ==, 0); 1.352 + tt_int_op(event_add(&wake_all, &tv_broadcast), ==, 0); 1.353 + 1.354 + /* And run for a bit... */ 1.355 + event_base_dispatch(base); 1.356 + 1.357 + /* And wait till the threads are done. */ 1.358 + for (i = 0; i < NUM_THREADS; ++i) 1.359 + THREAD_JOIN(threads[i]); 1.360 + 1.361 + /* Now, let's see what happened. At least one of 5 or 6 should 1.362 + * have timed out. */ 1.363 + n_timed_out = alerted[5].timed_out + alerted[6].timed_out; 1.364 + tt_int_op(n_timed_out, >=, 1); 1.365 + tt_int_op(n_timed_out, <=, 2); 1.366 + 1.367 + for (i = 0; i < NUM_THREADS; ++i) { 1.368 + const struct timeval *target_delay; 1.369 + struct timeval target_time, actual_delay; 1.370 + if (alerted[i].timed_out) { 1.371 + TT_BLATHER(("%d looks like a timeout\n", i)); 1.372 + target_delay = &tv_timeout; 1.373 + tt_assert(i == 5 || i == 6); 1.374 + } else if (evutil_timerisset(&alerted[i].alerted_at)) { 1.375 + long diff1,diff2; 1.376 + evutil_timersub(&alerted[i].alerted_at, 1.377 + &launched_at, &actual_delay); 1.378 + diff1 = timeval_msec_diff(&actual_delay, 1.379 + &tv_signal); 1.380 + diff2 = timeval_msec_diff(&actual_delay, 1.381 + &tv_broadcast); 1.382 + if (abs(diff1) < abs(diff2)) { 1.383 + TT_BLATHER(("%d looks like a signal\n", i)); 1.384 + target_delay = &tv_signal; 1.385 + ++n_signal; 1.386 + } else { 1.387 + TT_BLATHER(("%d looks like a broadcast\n", i)); 1.388 + target_delay = &tv_broadcast; 1.389 + ++n_broadcast; 1.390 + } 1.391 + } else { 1.392 + TT_FAIL(("Thread %d never got woken", i)); 1.393 + continue; 1.394 + } 1.395 + evutil_timeradd(target_delay, &launched_at, &target_time); 1.396 + test_timeval_diff_leq(&target_time, &alerted[i].alerted_at, 1.397 + 0, 50); 1.398 + } 1.399 + tt_int_op(n_broadcast + n_signal + n_timed_out, ==, NUM_THREADS); 1.400 + tt_int_op(n_signal, ==, 1); 1.401 + 1.402 +end: 1.403 + ; 1.404 +} 1.405 + 1.406 +#define CB_COUNT 128 1.407 +#define QUEUE_THREAD_COUNT 8 1.408 + 1.409 +#ifdef WIN32 1.410 +#define SLEEP_MS(ms) Sleep(ms) 1.411 +#else 1.412 +#define SLEEP_MS(ms) usleep((ms) * 1000) 1.413 +#endif 1.414 + 1.415 +struct deferred_test_data { 1.416 + struct deferred_cb cbs[CB_COUNT]; 1.417 + struct deferred_cb_queue *queue; 1.418 +}; 1.419 + 1.420 +static time_t timer_start = 0; 1.421 +static time_t timer_end = 0; 1.422 +static unsigned callback_count = 0; 1.423 +static THREAD_T load_threads[QUEUE_THREAD_COUNT]; 1.424 +static struct deferred_test_data deferred_data[QUEUE_THREAD_COUNT]; 1.425 + 1.426 +static void 1.427 +deferred_callback(struct deferred_cb *cb, void *arg) 1.428 +{ 1.429 + SLEEP_MS(1); 1.430 + callback_count += 1; 1.431 +} 1.432 + 1.433 +static THREAD_FN 1.434 +load_deferred_queue(void *arg) 1.435 +{ 1.436 + struct deferred_test_data *data = arg; 1.437 + size_t i; 1.438 + 1.439 + for (i = 0; i < CB_COUNT; ++i) { 1.440 + event_deferred_cb_init(&data->cbs[i], deferred_callback, NULL); 1.441 + event_deferred_cb_schedule(data->queue, &data->cbs[i]); 1.442 + SLEEP_MS(1); 1.443 + } 1.444 + 1.445 + THREAD_RETURN(); 1.446 +} 1.447 + 1.448 +static void 1.449 +timer_callback(evutil_socket_t fd, short what, void *arg) 1.450 +{ 1.451 + timer_end = time(NULL); 1.452 +} 1.453 + 1.454 +static void 1.455 +start_threads_callback(evutil_socket_t fd, short what, void *arg) 1.456 +{ 1.457 + int i; 1.458 + 1.459 + for (i = 0; i < QUEUE_THREAD_COUNT; ++i) { 1.460 + THREAD_START(load_threads[i], load_deferred_queue, 1.461 + &deferred_data[i]); 1.462 + } 1.463 +} 1.464 + 1.465 +static void 1.466 +thread_deferred_cb_skew(void *arg) 1.467 +{ 1.468 + struct basic_test_data *data = arg; 1.469 + struct timeval tv_timer = {4, 0}; 1.470 + struct deferred_cb_queue *queue; 1.471 + time_t elapsed; 1.472 + int i; 1.473 + 1.474 + queue = event_base_get_deferred_cb_queue(data->base); 1.475 + tt_assert(queue); 1.476 + 1.477 + for (i = 0; i < QUEUE_THREAD_COUNT; ++i) 1.478 + deferred_data[i].queue = queue; 1.479 + 1.480 + timer_start = time(NULL); 1.481 + event_base_once(data->base, -1, EV_TIMEOUT, timer_callback, NULL, 1.482 + &tv_timer); 1.483 + event_base_once(data->base, -1, EV_TIMEOUT, start_threads_callback, 1.484 + NULL, NULL); 1.485 + event_base_dispatch(data->base); 1.486 + 1.487 + elapsed = timer_end - timer_start; 1.488 + TT_BLATHER(("callback count, %u", callback_count)); 1.489 + TT_BLATHER(("elapsed time, %u", (unsigned)elapsed)); 1.490 + /* XXX be more intelligent here. just make sure skew is 1.491 + * within 2 seconds for now. */ 1.492 + tt_assert(elapsed >= 4 && elapsed <= 6); 1.493 + 1.494 +end: 1.495 + for (i = 0; i < QUEUE_THREAD_COUNT; ++i) 1.496 + THREAD_JOIN(load_threads[i]); 1.497 +} 1.498 + 1.499 +#define TEST(name) \ 1.500 + { #name, thread_##name, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE, \ 1.501 + &basic_setup, NULL } 1.502 + 1.503 +struct testcase_t thread_testcases[] = { 1.504 + { "basic", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE, 1.505 + &basic_setup, NULL }, 1.506 +#ifndef WIN32 1.507 + { "forking", thread_basic, TT_FORK|TT_NEED_THREADS|TT_NEED_BASE, 1.508 + &basic_setup, (char*)"forking" }, 1.509 +#endif 1.510 + TEST(conditions_simple), 1.511 + TEST(deferred_cb_skew), 1.512 + END_OF_TESTCASES 1.513 +}; 1.514 +