• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2  * Permission is hereby granted, free of charge, to any person obtaining a copy
3  * of this software and associated documentation files (the "Software"), to
4  * deal in the Software without restriction, including without limitation the
5  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6  * sell copies of the Software, and to permit persons to whom the Software is
7  * furnished to do so, subject to the following conditions:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18  * IN THE SOFTWARE.
19  */
20 
21 /* This file contains both the uv__async internal infrastructure and the
22  * user-facing uv_async_t functions.
23  */
24 
25 #include "uv.h"
26 #include "internal.h"
27 #include "uv_log.h"
28 
29 #include <errno.h>
30 #include <stdatomic.h>
31 #include <stdio.h>  /* snprintf() */
32 #include <assert.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <sched.h>  /* sched_yield() */
37 
38 #ifdef __linux__
39 #include <sys/eventfd.h>
40 #endif
41 
42 #ifdef USE_FFRT
43 #include "ffrt.h"
44 #include "c/executor_task.h"
45 #endif
46 
47 static void uv__async_send(uv_async_t* handle);
48 static int uv__async_start(uv_loop_t* loop);
49 
50 
uv_async_init(uv_loop_t * loop,uv_async_t * handle,uv_async_cb async_cb)51 int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
52   int err;
53 
54 #if defined(USE_OHOS_DFX) && defined(__aarch64__)
55   uv__multi_thread_check_unify(loop, __func__);
56 #endif
57   err = uv__async_start(loop);
58   if (err)
59     return err;
60 
61   uv__handle_init(loop, (uv_handle_t*)handle, UV_ASYNC);
62   handle->async_cb = async_cb;
63   handle->pending = 0;
64 
65   uv__queue_insert_tail(&loop->async_handles, &handle->queue);
66   uv__handle_start(handle);
67 
68   return 0;
69 }
70 
71 
uv_async_send(uv_async_t * handle)72 int uv_async_send(uv_async_t* handle) {
73   _Atomic int* pending;
74 
75 #ifdef USE_OHOS_DFX
76   if (handle == NULL) {
77     UV_LOGF("handle is NULL");
78     return -1;
79   }
80 #endif
81 
82   pending = (_Atomic int*) &handle->pending;
83 
84 
85   /* Do a cheap read first. */
86   if (atomic_load_explicit(pending, memory_order_relaxed) != 0)
87     return 0;
88 
89   /* Wake up the other thread's event loop. */
90   if (atomic_exchange(pending, 1) != 0)
91     return 0;
92 
93   /* Wake up the other thread's event loop. */
94   uv__async_send(handle);
95   return 0;
96 }
97 
98 
99 
uv__async_close(uv_async_t * handle)100 void uv__async_close(uv_async_t* handle) {
101   atomic_exchange((_Atomic int*) &handle->pending, 0);
102   uv__queue_remove(&handle->queue);
103   uv__handle_stop(handle);
104 }
105 
106 
uv__async_io(uv_loop_t * loop,uv__io_t * w,unsigned int events)107 static void uv__async_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
108   char buf[1024];
109   ssize_t r;
110   struct uv__queue queue;
111   struct uv__queue* q;
112   uv_async_t* h;
113   _Atomic int *pending;
114 
115   assert(w == &loop->async_io_watcher);
116 
117   for (;;) {
118     r = read(w->fd, buf, sizeof(buf));
119 
120     if (r == sizeof(buf))
121       continue;
122 
123     if (r != -1)
124       break;
125 
126     if (errno == EAGAIN || errno == EWOULDBLOCK)
127       break;
128 
129     if (errno == EINTR)
130       continue;
131 
132 #ifdef USE_OHOS_DFX
133     UV_ERRNO_ABORT("errno is %d, loop addr is %zu, fd is %d (%s:%s:%d)",
134       errno, (size_t)loop, w->fd, __FILE__, __func__, __LINE__);
135 #else
136     abort();
137 #endif
138   }
139 
140   uv__queue_move(&loop->async_handles, &queue);
141   while (!uv__queue_empty(&queue)) {
142     q = uv__queue_head(&queue);
143     h = uv__queue_data(q, uv_async_t, queue);
144 
145     uv__queue_remove(q);
146     uv__queue_insert_tail(&loop->async_handles, q);
147 
148     /* Atomically fetch and clear pending flag */
149     pending = (_Atomic int*) &h->pending;
150     if (atomic_exchange(pending, 0) == 0)
151       continue;
152 
153     if (h->async_cb == NULL)
154       continue;
155 
156     h->async_cb(h);
157   }
158 }
159 
160 
uv__async_send(uv_async_t * handle)161 static void uv__async_send(uv_async_t* handle) {
162   const void* buf;
163   ssize_t len;
164   int fd;
165   int r;
166 
167   uv_loop_t* loop = handle->loop;
168   if (loop == NULL) {
169     UV_LOGF("loop is NULL");
170     return;
171   }
172 
173   buf = "";
174   len = 1;
175   fd = loop->async_wfd;
176 
177 #if defined(__linux__)
178   if (fd == -1) {
179     static const uint64_t val = 1;
180     buf = &val;
181     len = sizeof(val);
182     fd = loop->async_io_watcher.fd;  /* eventfd */
183   }
184 #endif
185 
186   do
187     r = write(fd, buf, len);
188   while (r == -1 && errno == EINTR && atomic_load_explicit((_Atomic int*) &handle->pending, memory_order_relaxed) == 1);
189 
190   if (r == len)
191     return;
192 
193   if (r == -1)
194     if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
195       return;
196 
197 #ifdef USE_OHOS_DFX
198     UV_ERRNO_ABORT("errno is %d, loop addr is %zu, fd is %d (%s:%s:%d)",
199       errno, (size_t)loop, fd, __FILE__, __func__, __LINE__);
200 #else
201     abort();
202 #endif
203 }
204 
205 
uv__async_start(uv_loop_t * loop)206 static int uv__async_start(uv_loop_t* loop) {
207   int pipefd[2];
208   int err;
209 
210   if (loop->async_io_watcher.fd != -1)
211     return 0;
212 
213 #ifdef __linux__
214   err = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
215   if (err < 0)
216     return UV__ERR(errno);
217 
218   pipefd[0] = err;
219   pipefd[1] = -1;
220 #ifdef USE_OHOS_DFX
221   fdsan_exchange_owner_tag(pipefd[0], 0, uv__get_addr_tag((void *)&loop->async_io_watcher));
222 #endif
223 #else
224   err = uv__make_pipe(pipefd, UV_NONBLOCK_PIPE);
225   if (err < 0)
226     return err;
227 #endif
228 
229   uv__io_init(&loop->async_io_watcher, uv__async_io, pipefd[0]);
230   uv__io_start(loop, &loop->async_io_watcher, POLLIN);
231   loop->async_wfd = pipefd[1];
232   UV_LOGI("open:%{public}zu, pipefd[0]:%{public}d", (size_t)loop, pipefd[0]);
233   return 0;
234 }
235 
236 
uv__async_fork(uv_loop_t * loop)237 int uv__async_fork(uv_loop_t* loop) {
238   if (loop->async_io_watcher.fd == -1) /* never started */
239     return 0;
240 
241   uv__async_stop(loop);
242 
243   return uv__async_start(loop);
244 }
245 
246 
uv__async_stop(uv_loop_t * loop)247 void uv__async_stop(uv_loop_t* loop) {
248   if (loop->async_io_watcher.fd == -1)
249     return;
250 
251   if (loop->async_wfd != -1) {
252     if (loop->async_wfd != loop->async_io_watcher.fd) {
253       UV_LOGI("close:%{public}zu, async_wfd:%{public}d", (size_t)loop, loop->async_wfd);
254       uv__close(loop->async_wfd);
255     }
256     loop->async_wfd = -1;
257   }
258 
259   uv__io_stop(loop, &loop->async_io_watcher, POLLIN);
260 #ifdef USE_FFRT
261   if (ffrt_get_cur_task() != NULL) {
262     uv__epoll_ctl(loop->backend_fd, EPOLL_CTL_DEL, loop->async_io_watcher.fd, NULL);
263   }
264 #endif
265 
266 #if defined(__linux__) && defined(USE_OHOS_DFX)
267   fdsan_close_with_tag(loop->async_io_watcher.fd, uv__get_addr_tag((void *)&loop->async_io_watcher));
268 #else
269   uv__close(loop->async_io_watcher.fd);
270 #endif
271   UV_LOGI("close:%{public}zu, async_io_wfd:%{public}d", (size_t)loop, loop->async_io_watcher.fd);
272   loop->async_io_watcher.fd = -1;
273 }
274 
275