Home | History | Annotate | Line # | Download | only in test
      1      1.1  christos /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
      2      1.1  christos  *
      3      1.1  christos  * Permission is hereby granted, free of charge, to any person obtaining a copy
      4      1.1  christos  * of this software and associated documentation files (the "Software"), to
      5      1.1  christos  * deal in the Software without restriction, including without limitation the
      6      1.1  christos  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
      7      1.1  christos  * sell copies of the Software, and to permit persons to whom the Software is
      8      1.1  christos  * furnished to do so, subject to the following conditions:
      9      1.1  christos  *
     10      1.1  christos  * The above copyright notice and this permission notice shall be included in
     11      1.1  christos  * all copies or substantial portions of the Software.
     12      1.1  christos  *
     13      1.1  christos  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     14      1.1  christos  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     15      1.1  christos  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
     16      1.1  christos  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
     17      1.1  christos  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
     18      1.1  christos  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
     19      1.1  christos  * IN THE SOFTWARE.
     20      1.1  christos  */
     21      1.1  christos 
     22      1.1  christos #include "uv.h"
     23      1.1  christos #include "task.h"
     24      1.1  christos 
     25      1.1  christos #include <string.h>
     26      1.1  christos #include <errno.h>
     27      1.1  christos 
     28      1.1  christos typedef struct {
     29      1.1  christos   uv_barrier_t barrier;
     30  1.1.1.2  christos   unsigned delay;
     31  1.1.1.2  christos   unsigned niter;
     32  1.1.1.2  christos   unsigned main_barrier_wait_rval;
     33  1.1.1.2  christos   unsigned worker_barrier_wait_rval;
     34      1.1  christos } worker_config;
     35      1.1  christos 
     36      1.1  christos 
     37      1.1  christos static void worker(void* arg) {
     38      1.1  christos   worker_config* c = arg;
     39  1.1.1.2  christos   unsigned i;
     40      1.1  christos 
     41      1.1  christos   if (c->delay)
     42      1.1  christos     uv_sleep(c->delay);
     43      1.1  christos 
     44  1.1.1.2  christos   for (i = 0; i < c->niter; i++)
     45  1.1.1.2  christos     c->worker_barrier_wait_rval += uv_barrier_wait(&c->barrier);
     46      1.1  christos }
     47      1.1  christos 
     48      1.1  christos 
     49      1.1  christos TEST_IMPL(barrier_1) {
     50      1.1  christos   uv_thread_t thread;
     51      1.1  christos   worker_config wc;
     52      1.1  christos 
     53      1.1  christos   memset(&wc, 0, sizeof(wc));
     54  1.1.1.2  christos   wc.niter = 1;
     55      1.1  christos 
     56  1.1.1.2  christos   ASSERT_OK(uv_barrier_init(&wc.barrier, 2));
     57  1.1.1.2  christos   ASSERT_OK(uv_thread_create(&thread, worker, &wc));
     58      1.1  christos 
     59      1.1  christos   uv_sleep(100);
     60      1.1  christos   wc.main_barrier_wait_rval = uv_barrier_wait(&wc.barrier);
     61      1.1  christos 
     62  1.1.1.2  christos   ASSERT_OK(uv_thread_join(&thread));
     63      1.1  christos   uv_barrier_destroy(&wc.barrier);
     64      1.1  christos 
     65  1.1.1.2  christos   ASSERT_EQ(1, (wc.main_barrier_wait_rval ^ wc.worker_barrier_wait_rval));
     66      1.1  christos 
     67      1.1  christos   return 0;
     68      1.1  christos }
     69      1.1  christos 
     70      1.1  christos 
     71      1.1  christos TEST_IMPL(barrier_2) {
     72      1.1  christos   uv_thread_t thread;
     73      1.1  christos   worker_config wc;
     74      1.1  christos 
     75      1.1  christos   memset(&wc, 0, sizeof(wc));
     76      1.1  christos   wc.delay = 100;
     77  1.1.1.2  christos   wc.niter = 1;
     78      1.1  christos 
     79  1.1.1.2  christos   ASSERT_OK(uv_barrier_init(&wc.barrier, 2));
     80  1.1.1.2  christos   ASSERT_OK(uv_thread_create(&thread, worker, &wc));
     81      1.1  christos 
     82      1.1  christos   wc.main_barrier_wait_rval = uv_barrier_wait(&wc.barrier);
     83      1.1  christos 
     84  1.1.1.2  christos   ASSERT_OK(uv_thread_join(&thread));
     85      1.1  christos   uv_barrier_destroy(&wc.barrier);
     86      1.1  christos 
     87  1.1.1.2  christos   ASSERT_EQ(1, (wc.main_barrier_wait_rval ^ wc.worker_barrier_wait_rval));
     88      1.1  christos 
     89      1.1  christos   return 0;
     90      1.1  christos }
     91      1.1  christos 
     92      1.1  christos 
     93      1.1  christos TEST_IMPL(barrier_3) {
     94      1.1  christos   uv_thread_t thread;
     95      1.1  christos   worker_config wc;
     96  1.1.1.2  christos   unsigned i;
     97      1.1  christos 
     98      1.1  christos   memset(&wc, 0, sizeof(wc));
     99  1.1.1.2  christos   wc.niter = 5;
    100      1.1  christos 
    101  1.1.1.2  christos   ASSERT_OK(uv_barrier_init(&wc.barrier, 2));
    102  1.1.1.2  christos   ASSERT_OK(uv_thread_create(&thread, worker, &wc));
    103      1.1  christos 
    104  1.1.1.2  christos   for (i = 0; i < wc.niter; i++)
    105  1.1.1.2  christos     wc.main_barrier_wait_rval += uv_barrier_wait(&wc.barrier);
    106      1.1  christos 
    107  1.1.1.2  christos   ASSERT_OK(uv_thread_join(&thread));
    108      1.1  christos   uv_barrier_destroy(&wc.barrier);
    109      1.1  christos 
    110  1.1.1.2  christos   ASSERT_EQ(wc.niter, wc.main_barrier_wait_rval + wc.worker_barrier_wait_rval);
    111      1.1  christos 
    112      1.1  christos   return 0;
    113      1.1  christos }
    114      1.1  christos 
    115      1.1  christos static void serial_worker(void* data) {
    116      1.1  christos   uv_barrier_t* barrier;
    117  1.1.1.2  christos   unsigned i;
    118      1.1  christos 
    119      1.1  christos   barrier = data;
    120  1.1.1.2  christos   for (i = 0; i < 5; i++)
    121  1.1.1.2  christos     uv_barrier_wait(barrier);
    122      1.1  christos   if (uv_barrier_wait(barrier) > 0)
    123      1.1  christos     uv_barrier_destroy(barrier);
    124      1.1  christos 
    125      1.1  christos   uv_sleep(100);  /* Wait a bit before terminating. */
    126      1.1  christos }
    127      1.1  christos 
    128      1.1  christos /* Ensure that uv_barrier_wait returns positive only after all threads have
    129      1.1  christos  * exited the barrier. If this value is returned too early and the barrier is
    130      1.1  christos  * destroyed prematurely, then this test may see a crash. */
    131      1.1  christos TEST_IMPL(barrier_serial_thread) {
    132      1.1  christos   uv_thread_t threads[4];
    133      1.1  christos   uv_barrier_t barrier;
    134      1.1  christos   unsigned i;
    135      1.1  christos 
    136  1.1.1.2  christos   ASSERT_OK(uv_barrier_init(&barrier, ARRAY_SIZE(threads) + 1));
    137      1.1  christos 
    138      1.1  christos   for (i = 0; i < ARRAY_SIZE(threads); ++i)
    139  1.1.1.2  christos     ASSERT_OK(uv_thread_create(&threads[i], serial_worker, &barrier));
    140      1.1  christos 
    141  1.1.1.2  christos   for (i = 0; i < 5; i++)
    142  1.1.1.2  christos     uv_barrier_wait(&barrier);
    143      1.1  christos   if (uv_barrier_wait(&barrier) > 0)
    144      1.1  christos     uv_barrier_destroy(&barrier);
    145      1.1  christos 
    146      1.1  christos   for (i = 0; i < ARRAY_SIZE(threads); ++i)
    147  1.1.1.2  christos     ASSERT_OK(uv_thread_join(&threads[i]));
    148      1.1  christos 
    149      1.1  christos   return 0;
    150      1.1  christos }
    151      1.1  christos 
    152      1.1  christos /* Single thread uv_barrier_wait should return correct return value. */
    153      1.1  christos TEST_IMPL(barrier_serial_thread_single) {
    154      1.1  christos   uv_barrier_t barrier;
    155      1.1  christos 
    156  1.1.1.2  christos   ASSERT_OK(uv_barrier_init(&barrier, 1));
    157  1.1.1.2  christos   ASSERT_LT(0, uv_barrier_wait(&barrier));
    158      1.1  christos   uv_barrier_destroy(&barrier);
    159      1.1  christos   return 0;
    160      1.1  christos }
    161