Coverage Report

Created: 2026-06-15 07:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmlibuv/src/unix/async.c
Line
Count
Source
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
28
#include <errno.h>
29
#include <stdatomic.h>
30
#include <stdio.h>  /* snprintf() */
31
#include <assert.h>
32
#include <stdlib.h>
33
#include <string.h>
34
#include <unistd.h>
35
#include <sched.h>  /* sched_yield() */
36
37
#ifdef __linux__
38
#include <sys/eventfd.h>
39
#endif
40
41
#if UV__KQUEUE_EVFILT_USER
42
static uv_once_t kqueue_runtime_detection_guard = UV_ONCE_INIT;
43
static int kqueue_evfilt_user_support = 1;
44
45
46
static void uv__kqueue_runtime_detection(void) {
47
  int kq;
48
  struct kevent ev[2];
49
  struct timespec timeout = {0, 0};
50
51
  /* Perform the runtime detection to ensure that kqueue with
52
   * EVFILT_USER actually works. */
53
  kq = kqueue();
54
  EV_SET(ev, UV__KQUEUE_EVFILT_USER_IDENT, EVFILT_USER,
55
         EV_ADD | EV_CLEAR, 0, 0, 0);
56
  EV_SET(ev + 1, UV__KQUEUE_EVFILT_USER_IDENT, EVFILT_USER,
57
         0, NOTE_TRIGGER, 0, 0);
58
  if (kevent(kq, ev, 2, ev, 1, &timeout) < 1 ||
59
      ev[0].filter != EVFILT_USER ||
60
      ev[0].ident != UV__KQUEUE_EVFILT_USER_IDENT ||
61
      ev[0].flags & EV_ERROR)
62
    /* If we wind up here, we can assume that EVFILT_USER is defined but
63
     * broken on the current system. */
64
    kqueue_evfilt_user_support = 0;
65
  uv__close(kq);
66
}
67
#endif
68
69
static void uv__async_send(uv_loop_t* loop);
70
static int uv__async_start(uv_loop_t* loop);
71
static void uv__cpu_relax(void);
72
73
74
0
int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
75
0
  int err;
76
77
0
  err = uv__async_start(loop);
78
0
  if (err)
79
0
    return err;
80
81
0
  uv__handle_init(loop, (uv_handle_t*)handle, UV_ASYNC);
82
0
  handle->async_cb = async_cb;
83
0
  handle->pending = 0;
84
0
  handle->u.fd = 0; /* This will be used as a busy flag. */
85
86
0
  uv__queue_insert_tail(&loop->async_handles, &handle->queue);
87
0
  uv__handle_start(handle);
88
89
0
  return 0;
90
0
}
91
92
93
0
int uv_async_send(uv_async_t* handle) {
94
0
  _Atomic int* pending;
95
0
  _Atomic int* busy;
96
97
0
  pending = (_Atomic int*) &handle->pending;
98
0
  busy = (_Atomic int*) &handle->u.fd;
99
100
  /* Do a cheap read first. */
101
0
  if (atomic_load_explicit(pending, memory_order_relaxed) != 0)
102
0
    return 0;
103
104
  /* Set the loop to busy. */
105
0
  atomic_fetch_add(busy, 1);
106
107
  /* Wake up the other thread's event loop. */
108
0
  if (atomic_exchange(pending, 1) == 0)
109
0
    uv__async_send(handle->loop);
110
111
  /* Set the loop to not-busy. */
112
0
  atomic_fetch_add(busy, -1);
113
114
0
  return 0;
115
0
}
116
117
118
/* Wait for the busy flag to clear before closing.
119
 * Only call this from the event loop thread. */
120
0
static void uv__async_spin(uv_async_t* handle) {
121
0
  _Atomic int* pending;
122
0
  _Atomic int* busy;
123
0
  int i;
124
125
0
  pending = (_Atomic int*) &handle->pending;
126
0
  busy = (_Atomic int*) &handle->u.fd;
127
128
  /* Set the pending flag first, so no new events will be added by other
129
   * threads after this function returns. */
130
0
  atomic_store(pending, 1);
131
132
0
  for (;;) {
133
    /* 997 is not completely chosen at random. It's a prime number, acyclic by
134
     * nature, and should therefore hopefully dampen sympathetic resonance.
135
     */
136
0
    for (i = 0; i < 997; i++) {
137
0
      if (atomic_load(busy) == 0)
138
0
        return;
139
140
      /* Other thread is busy with this handle, spin until it's done. */
141
0
      uv__cpu_relax();
142
0
    }
143
144
    /* Yield the CPU. We may have preempted the other thread while it's
145
     * inside the critical section and if it's running on the same CPU
146
     * as us, we'll just burn CPU cycles until the end of our time slice.
147
     */
148
0
    sched_yield();
149
0
  }
150
0
}
151
152
153
0
void uv__async_close(uv_async_t* handle) {
154
0
  uv__async_spin(handle);
155
0
  uv__queue_remove(&handle->queue);
156
0
  uv__handle_stop(handle);
157
0
}
158
159
160
0
void uv__async_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
161
0
  char buf[1024];
162
0
  ssize_t r;
163
0
  struct uv__queue queue;
164
0
  struct uv__queue* q;
165
0
  uv_async_t* h;
166
0
  _Atomic int *pending;
167
168
0
  assert(w == &loop->async_io_watcher);
169
170
#if UV__KQUEUE_EVFILT_USER
171
  for (;!kqueue_evfilt_user_support;) {
172
#else
173
0
  for (;;) {
174
0
#endif
175
0
    r = read(w->fd, buf, sizeof(buf));
176
177
0
    if (r == sizeof(buf))
178
0
      continue;
179
180
0
    if (r != -1)
181
0
      break;
182
183
0
    if (errno == EAGAIN || errno == EWOULDBLOCK)
184
0
      break;
185
186
0
    if (errno == EINTR)
187
0
      continue;
188
189
0
    abort();
190
0
  }
191
192
0
  uv__queue_move(&loop->async_handles, &queue);
193
0
  while (!uv__queue_empty(&queue)) {
194
0
    q = uv__queue_head(&queue);
195
0
    h = uv__queue_data(q, uv_async_t, queue);
196
197
0
    uv__queue_remove(q);
198
0
    uv__queue_insert_tail(&loop->async_handles, q);
199
200
    /* Atomically fetch and clear pending flag */
201
0
    pending = (_Atomic int*) &h->pending;
202
0
    if (atomic_exchange(pending, 0) == 0)
203
0
      continue;
204
205
0
    if (h->async_cb == NULL)
206
0
      continue;
207
208
0
    h->async_cb(h);
209
0
  }
210
0
}
211
212
213
0
static void uv__async_send(uv_loop_t* loop) {
214
0
  const void* buf;
215
0
  ssize_t len;
216
0
  int fd;
217
0
  int r;
218
219
0
  buf = "";
220
0
  len = 1;
221
0
  fd = loop->async_wfd;
222
223
0
#if defined(__linux__)
224
0
  if (fd == -1) {
225
0
    static const uint64_t val = 1;
226
0
    buf = &val;
227
0
    len = sizeof(val);
228
0
    fd = loop->async_io_watcher.fd;  /* eventfd */
229
0
  }
230
#elif UV__KQUEUE_EVFILT_USER
231
  struct kevent ev;
232
233
  if (kqueue_evfilt_user_support) {
234
    fd = loop->async_io_watcher.fd; /* magic number for EVFILT_USER */
235
    EV_SET(&ev, fd, EVFILT_USER, 0, NOTE_TRIGGER, 0, 0);
236
    r = kevent(loop->backend_fd, &ev, 1, NULL, 0, NULL);
237
    if (r == 0)
238
      return;
239
    abort();
240
  }
241
#endif
242
243
0
  do
244
0
    r = write(fd, buf, len);
245
0
  while (r == -1 && errno == EINTR);
246
247
0
  if (r == len)
248
0
    return;
249
250
0
  if (r == -1)
251
0
    if (errno == EAGAIN || errno == EWOULDBLOCK)
252
0
      return;
253
254
0
  abort();
255
0
}
256
257
258
0
static int uv__async_start(uv_loop_t* loop) {
259
0
  int pipefd[2];
260
0
  int err;
261
#if UV__KQUEUE_EVFILT_USER
262
  struct kevent ev;
263
#endif
264
265
0
  if (loop->async_io_watcher.fd != -1)
266
0
    return 0;
267
268
0
#ifdef __linux__
269
0
  err = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
270
0
  if (err < 0)
271
0
    return UV__ERR(errno);
272
273
0
  pipefd[0] = err;
274
0
  pipefd[1] = -1;
275
#elif UV__KQUEUE_EVFILT_USER
276
  uv_once(&kqueue_runtime_detection_guard, uv__kqueue_runtime_detection);
277
  if (kqueue_evfilt_user_support) {
278
    /* In order not to break the generic pattern of I/O polling, a valid
279
     * file descriptor is required to take up a room in loop->watchers,
280
     * thus we create one for that, but this fd will not be actually used,
281
     * it's just a placeholder and magic number which is going to be closed
282
     * during the cleanup, as other FDs. */
283
    err = uv__open_cloexec("/", O_RDONLY);
284
    if (err < 0)
285
      return err;
286
287
    pipefd[0] = err;
288
    pipefd[1] = -1;
289
290
    /* When using EVFILT_USER event to wake up the kqueue, this event must be
291
     * registered beforehand. Otherwise, calling kevent() to issue an
292
     * unregistered EVFILT_USER event will get an ENOENT.
293
     * Since uv__async_send() may happen before uv__io_poll() with multi-threads,
294
     * we can't defer this registration of EVFILT_USER event as we did for other
295
     * events, but must perform it right away. */
296
    EV_SET(&ev, err, EVFILT_USER, EV_ADD | EV_CLEAR, 0, 0, 0);
297
    err = kevent(loop->backend_fd, &ev, 1, NULL, 0, NULL);
298
    if (err < 0)
299
      return UV__ERR(errno);
300
  } else {
301
    err = uv__make_pipe(pipefd, UV_NONBLOCK_PIPE);
302
    if (err < 0)
303
      return err;
304
  }
305
#else
306
  err = uv__make_pipe(pipefd, UV_NONBLOCK_PIPE);
307
  if (err < 0)
308
    return err;
309
#endif
310
311
0
  err = uv__io_init_start(loop, &loop->async_io_watcher, UV__ASYNC_IO,
312
0
                          pipefd[0], POLLIN);
313
0
  if (err < 0) {
314
0
    uv__close(pipefd[0]);
315
0
    if (pipefd[1] != -1)
316
0
      uv__close(pipefd[1]);
317
0
    return err;
318
0
  }
319
0
  loop->async_wfd = pipefd[1];
320
321
#if UV__KQUEUE_EVFILT_USER
322
  /* Prevent the EVFILT_USER event from being added to kqueue redundantly
323
   * and mistakenly later in uv__io_poll(). */
324
  if (kqueue_evfilt_user_support)
325
    loop->async_io_watcher.events = loop->async_io_watcher.pevents;
326
#endif
327
328
0
  return 0;
329
0
}
330
331
332
0
void uv__async_stop(uv_loop_t* loop) {
333
0
  struct uv__queue queue;
334
0
  struct uv__queue* q;
335
0
  uv_async_t* h;
336
337
0
  if (loop->async_io_watcher.fd == -1)
338
0
    return;
339
340
  /* Make sure no other thread is accessing the async handle fd after the loop
341
   * cleanup.
342
   */
343
0
  uv__queue_move(&loop->async_handles, &queue);
344
0
  while (!uv__queue_empty(&queue)) {
345
0
    q = uv__queue_head(&queue);
346
0
    h = uv__queue_data(q, uv_async_t, queue);
347
348
0
    uv__queue_remove(q);
349
0
    uv__queue_insert_tail(&loop->async_handles, q);
350
351
0
    uv__async_spin(h);
352
0
  }
353
354
0
  if (loop->async_wfd != -1) {
355
0
    if (loop->async_wfd != loop->async_io_watcher.fd)
356
0
      uv__close(loop->async_wfd);
357
0
    loop->async_wfd = -1;
358
0
  }
359
360
0
  uv__io_stop(loop, &loop->async_io_watcher, POLLIN);
361
0
  uv__close(loop->async_io_watcher.fd);
362
0
  loop->async_io_watcher.fd = -1;
363
0
}
364
365
366
0
int uv__async_fork(uv_loop_t* loop) {
367
0
  struct uv__queue queue;
368
0
  struct uv__queue* q;
369
0
  uv_async_t* h;
370
371
0
  if (loop->async_io_watcher.fd == -1) /* never started */
372
0
    return 0;
373
374
0
  uv__queue_move(&loop->async_handles, &queue);
375
0
  while (!uv__queue_empty(&queue)) {
376
0
    q = uv__queue_head(&queue);
377
0
    h = uv__queue_data(q, uv_async_t, queue);
378
379
0
    uv__queue_remove(q);
380
0
    uv__queue_insert_tail(&loop->async_handles, q);
381
382
    /* The state of any thread that set pending is now likely corrupt in this
383
     * child because the user called fork, so just clear these flags and move
384
     * on. Calling most libc functions after `fork` is declared to be undefined
385
     * behavior anyways, unless async-signal-safe, for multithreaded programs
386
     * like libuv, and nothing interesting in pthreads is async-signal-safe.
387
     */
388
0
    h->pending = 0;
389
    /* This is the busy flag, and we just abruptly lost all other threads. */
390
0
    h->u.fd = 0;
391
0
  }
392
393
  /* Recreate these, since they still exist, but belong to the wrong pid now. */
394
0
  if (loop->async_wfd != -1) {
395
0
    if (loop->async_wfd != loop->async_io_watcher.fd)
396
0
      uv__close(loop->async_wfd);
397
0
    loop->async_wfd = -1;
398
0
  }
399
400
0
  uv__io_stop(loop, &loop->async_io_watcher, POLLIN);
401
0
  uv__close(loop->async_io_watcher.fd);
402
0
  loop->async_io_watcher.fd = -1;
403
404
0
  return uv__async_start(loop);
405
0
}
406
407
408
0
static void uv__cpu_relax(void) {
409
0
#if defined(__i386__) || defined(__x86_64__)
410
0
  __asm__ __volatile__ ("rep; nop" ::: "memory");  /* a.k.a. PAUSE */
411
#elif (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__)
412
  __asm__ __volatile__ ("isb" ::: "memory");
413
#elif (defined(__ppc__) || defined(__ppc64__)) && defined(__APPLE__)
414
  __asm volatile ("" : : : "memory");
415
#elif !defined(__APPLE__) && (defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__))
416
  __asm__ __volatile__ ("or 1,1,1; or 2,2,2" ::: "memory");
417
#elif defined(__riscv) && __riscv_xlen == 64
418
  __asm__ volatile(".insn 0x0100000f" ::: "memory");  /* FENCE */
419
#endif
420
0
}