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