test-async.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 #include <stdio.h>
25 #include <stdlib.h>
26 
29 
32 
33 static volatile int async_cb_called;
34 static int prepare_cb_called;
35 static int close_cb_called;
36 
37 
38 static void thread_cb(void *arg) {
39  int n;
40  int r;
41 
42  for (;;) {
46 
47  if (n == 3) {
48  break;
49  }
50 
51  r = uv_async_send(&async);
52  ASSERT(r == 0);
53 
54  /* Work around a bug in Valgrind.
55  *
56  * Valgrind runs threads not in parallel but sequentially, i.e. one after
57  * the other. It also doesn't preempt them, instead it depends on threads
58  * yielding voluntarily by making a syscall.
59  *
60  * That never happens here: the pipe that is associated with the async
61  * handle is written to once but that's too early for Valgrind's scheduler
62  * to kick in. Afterwards, the thread busy-loops, starving the main thread.
63  * Therefore, we yield.
64  *
65  * This behavior has been observed with Valgrind 3.7.0 and 3.9.0.
66  */
67  uv_sleep(0);
68  }
69 }
70 
71 
72 static void close_cb(uv_handle_t* handle) {
73  ASSERT(handle != NULL);
75 }
76 
77 
78 static void async_cb(uv_async_t* handle) {
79  int n;
80 
81  ASSERT(handle == &async);
82 
84  n = ++async_cb_called;
86 
87  if (n == 3) {
90  }
91 }
92 
93 
95  int r;
96 
97  ASSERT(handle == &prepare);
98 
99  if (prepare_cb_called++)
100  return;
101 
102  r = uv_thread_create(&thread, thread_cb, NULL);
103  ASSERT(r == 0);
105 }
106 
107 
109  int r;
110 
111  r = uv_mutex_init(&mutex);
112  ASSERT(r == 0);
114 
116  ASSERT(r == 0);
118  ASSERT(r == 0);
119 
121  ASSERT(r == 0);
122 
124  ASSERT(r == 0);
125 
127  ASSERT(async_cb_called == 3);
128  ASSERT(close_cb_called == 2);
129 
130  ASSERT(0 == uv_thread_join(&thread));
131 
133  return 0;
134 }
mutex
static uv_mutex_t mutex
Definition: test-async.c:28
uv_prepare_init
UV_EXTERN int uv_prepare_init(uv_loop_t *, uv_prepare_t *prepare)
task.h
uv_prepare_s
Definition: uv.h:804
uv_mutex_init
UV_EXTERN int uv_mutex_init(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:281
ASSERT
#define ASSERT(expr)
Definition: task.h:102
prepare_cb_called
static int prepare_cb_called
Definition: test-async.c:34
uv_thread_join
UV_EXTERN int uv_thread_join(uv_thread_t *tid)
Definition: libuv/src/unix/thread.c:271
close_cb
static void close_cb(uv_handle_t *handle)
Definition: test-async.c:72
uv_run
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: unix/core.c:361
async
static uv_async_t async
Definition: test-async.c:31
async_cb
static void async_cb(uv_async_t *handle)
Definition: test-async.c:78
uv_close
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: unix/core.c:112
uv_default_loop
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:733
uv_async_s
Definition: uv.h:834
async_cb_called
static volatile int async_cb_called
Definition: test-async.c:33
UV_RUN_DEFAULT
@ UV_RUN_DEFAULT
Definition: uv.h:254
close_cb_called
static int close_cb_called
Definition: test-async.c:35
uv_prepare_start
UV_EXTERN int uv_prepare_start(uv_prepare_t *prepare, uv_prepare_cb cb)
arg
Definition: cmdline.cc:40
uv_mutex_t
pthread_mutex_t uv_mutex_t
Definition: unix.h:135
prepare
static uv_prepare_t prepare
Definition: test-async.c:30
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_mutex_unlock
UV_EXTERN void uv_mutex_unlock(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:349
n
int n
Definition: abseil-cpp/absl/container/btree_test.cc:1080
uv.h
MAKE_VALGRIND_HAPPY
#define MAKE_VALGRIND_HAPPY()
Definition: task.h:229
uv_async_init
UV_EXTERN int uv_async_init(uv_loop_t *, uv_async_t *async, uv_async_cb async_cb)
Definition: unix/async.c:44
uv_mutex_lock
UV_EXTERN void uv_mutex_lock(uv_mutex_t *handle)
Definition: libuv/src/unix/thread.c:329
thread
static uv_thread_t thread
Definition: test-async.c:27
fix_build_deps.r
r
Definition: fix_build_deps.py:491
thread_cb
static void thread_cb(void *arg)
Definition: test-async.c:38
TEST_IMPL
TEST_IMPL(async)
Definition: test-async.c:108
handle
static csh handle
Definition: test_arm_regression.c:16
uv_handle_s
Definition: uv.h:441
uv_thread_t
pthread_t uv_thread_t
Definition: unix.h:134
prepare_cb
static void prepare_cb(uv_prepare_t *handle)
Definition: test-async.c:94
uv_async_send
UV_EXTERN int uv_async_send(uv_async_t *async)
Definition: unix/async.c:62


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:29