test-barrier.c
Go to the documentation of this file.
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 
28 typedef struct {
30  int delay;
31  volatile int posted;
35 
36 
37 static 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 
47 TEST_IMPL(barrier_1) {
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);
58 
61 
63 
64  return 0;
65 }
66 
67 
68 TEST_IMPL(barrier_2) {
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 
79 
82 
84 
85  return 0;
86 }
87 
88 
89 TEST_IMPL(barrier_3) {
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 
99 
100  ASSERT(0 == uv_thread_join(&thread));
102 
104 
105  return 0;
106 }
107 
108 static 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. */
121 TEST_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. */
141 TEST_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 }
ARRAY_SIZE
#define ARRAY_SIZE(array)
Definition: bloaty.cc:101
task.h
memset
return memset(p, 0, total)
string.h
uv_barrier_destroy
UV_EXTERN void uv_barrier_destroy(uv_barrier_t *barrier)
Definition: libuv/src/unix/thread.c:118
worker_config::worker_barrier_wait_rval
int worker_barrier_wait_rval
Definition: test-barrier.c:33
ASSERT
#define ASSERT(expr)
Definition: task.h:102
uv_thread_join
UV_EXTERN int uv_thread_join(uv_thread_t *tid)
Definition: libuv/src/unix/thread.c:271
threads
static uv_thread_t * threads
Definition: threadpool.c:38
worker
static void worker(void *arg)
Definition: test-barrier.c:37
worker_config
struct worker_config worker_config
c
void c(T a)
Definition: miscompile_with_no_unique_address_test.cc:40
uv_barrier_init
UV_EXTERN int uv_barrier_init(uv_barrier_t *barrier, unsigned int count)
Definition: libuv/src/unix/thread.c:55
worker
Definition: worker.py:1
worker_config::barrier
uv_barrier_t barrier
Definition: test-barrier.c:29
arg
Definition: cmdline.cc:40
worker_config::posted
volatile int posted
Definition: test-barrier.c:31
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
uv_thread_create
UV_EXTERN int uv_thread_create(uv_thread_t *tid, uv_thread_cb entry, void *arg)
Definition: libuv/src/unix/thread.c:209
uv_sleep
UV_EXTERN void uv_sleep(unsigned int msec)
Definition: unix/core.c:1521
uv.h
uv_barrier_t
Definition: unix.h:154
arg
struct arg arg
worker_config::delay
int delay
Definition: test-barrier.c:30
uv_thread_t
pthread_t uv_thread_t
Definition: unix.h:134
serial_worker
static void serial_worker(void *data)
Definition: test-barrier.c:108
uv_barrier_wait
UV_EXTERN int uv_barrier_wait(uv_barrier_t *barrier)
Definition: libuv/src/unix/thread.c:89
worker_config::main_barrier_wait_rval
int main_barrier_wait_rval
Definition: test-barrier.c:32
worker_config
Definition: test-barrier.c:28
thread
static uv_thread_t thread
Definition: test-async-null-cb.c:29
TEST_IMPL
TEST_IMPL(barrier_1)
Definition: test-barrier.c:47
errno.h
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:26