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