Rizin
unix-like reverse engineering framework and cli tools
async.c
Go to the documentation of this file.
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 "atomic-ops.h"
28 
29 #include <errno.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 static void uv__async_send(uv_loop_t* loop);
42 static int uv__async_start(uv_loop_t* loop);
43 
44 
46  int err;
47 
49  if (err)
50  return err;
51 
52  uv__handle_init(loop, (uv_handle_t*)handle, UV_ASYNC);
53  handle->async_cb = async_cb;
54  handle->pending = 0;
55 
56  QUEUE_INSERT_TAIL(&loop->async_handles, &handle->queue);
58 
59  return 0;
60 }
61 
62 
64  /* Do a cheap read first. */
65  if (ACCESS_ONCE(int, handle->pending) != 0)
66  return 0;
67 
68  /* Tell the other thread we're busy with the handle. */
69  if (cmpxchgi(&handle->pending, 0, 1) != 0)
70  return 0;
71 
72  /* Wake up the other thread's event loop. */
73  uv__async_send(handle->loop);
74 
75  /* Tell the other thread we're done. */
76  if (cmpxchgi(&handle->pending, 1, 2) != 1)
77  abort();
78 
79  return 0;
80 }
81 
82 
83 /* Only call this from the event loop thread. */
85  int i;
86  int rc;
87 
88  for (;;) {
89  /* 997 is not completely chosen at random. It's a prime number, acyclical
90  * by nature, and should therefore hopefully dampen sympathetic resonance.
91  */
92  for (i = 0; i < 997; i++) {
93  /* rc=0 -- handle is not pending.
94  * rc=1 -- handle is pending, other thread is still working with it.
95  * rc=2 -- handle is pending, other thread is done.
96  */
97  rc = cmpxchgi(&handle->pending, 2, 0);
98 
99  if (rc != 1)
100  return rc;
101 
102  /* Other thread is busy with this handle, spin until it's done. */
103  cpu_relax();
104  }
105 
106  /* Yield the CPU. We may have preempted the other thread while it's
107  * inside the critical section and if it's running on the same CPU
108  * as us, we'll just burn CPU cycles until the end of our time slice.
109  */
110  sched_yield();
111  }
112 }
113 
114 
117  QUEUE_REMOVE(&handle->queue);
119 }
120 
121 
122 static void uv__async_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
123  char buf[1024];
124  ssize_t r;
125  QUEUE queue;
126  QUEUE* q;
127  uv_async_t* h;
128 
129  assert(w == &loop->async_io_watcher);
130 
131  for (;;) {
132  r = read(w->fd, buf, sizeof(buf));
133 
134  if (r == sizeof(buf))
135  continue;
136 
137  if (r != -1)
138  break;
139 
140  if (errno == EAGAIN || errno == EWOULDBLOCK)
141  break;
142 
143  if (errno == EINTR)
144  continue;
145 
146  abort();
147  }
148 
149  QUEUE_MOVE(&loop->async_handles, &queue);
150  while (!QUEUE_EMPTY(&queue)) {
151  q = QUEUE_HEAD(&queue);
152  h = QUEUE_DATA(q, uv_async_t, queue);
153 
154  QUEUE_REMOVE(q);
155  QUEUE_INSERT_TAIL(&loop->async_handles, q);
156 
157  if (0 == uv__async_spin(h))
158  continue; /* Not pending. */
159 
160  if (h->async_cb == NULL)
161  continue;
162 
163  h->async_cb(h);
164  }
165 }
166 
167 
169  const void* buf;
170  ssize_t len;
171  int fd;
172  int r;
173 
174  buf = "";
175  len = 1;
176  fd = loop->async_wfd;
177 
178 #if defined(__linux__)
179  if (fd == -1) {
180  static const uint64_t val = 1;
181  buf = &val;
182  len = sizeof(val);
183  fd = loop->async_io_watcher.fd; /* eventfd */
184  }
185 #endif
186 
187  do
188  r = write(fd, buf, len);
189  while (r == -1 && errno == EINTR);
190 
191  if (r == len)
192  return;
193 
194  if (r == -1)
195  if (errno == EAGAIN || errno == EWOULDBLOCK)
196  return;
197 
198  abort();
199 }
200 
201 
203  int pipefd[2];
204  int err;
205 
206  if (loop->async_io_watcher.fd != -1)
207  return 0;
208 
209 #ifdef __linux__
210  err = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
211  if (err < 0)
212  return UV__ERR(errno);
213 
214  pipefd[0] = err;
215  pipefd[1] = -1;
216 #else
217  err = uv__make_pipe(pipefd, UV__F_NONBLOCK);
218  if (err < 0)
219  return err;
220 #endif
221 
222  uv__io_init(&loop->async_io_watcher, uv__async_io, pipefd[0]);
223  uv__io_start(loop, &loop->async_io_watcher, POLLIN);
224  loop->async_wfd = pipefd[1];
225 
226  return 0;
227 }
228 
229 
231  if (loop->async_io_watcher.fd == -1) /* never started */
232  return 0;
233 
235 
236  return uv__async_start(loop);
237 }
238 
239 
241  if (loop->async_io_watcher.fd == -1)
242  return;
243 
244  if (loop->async_wfd != -1) {
245  if (loop->async_wfd != loop->async_io_watcher.fd)
246  uv__close(loop->async_wfd);
247  loop->async_wfd = -1;
248  }
249 
250  uv__io_stop(loop, &loop->async_io_watcher, POLLIN);
251  uv__close(loop->async_io_watcher.fd);
252  loop->async_io_watcher.fd = -1;
253 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
ut16 val
Definition: armass64_const.h:6
static bool err
Definition: armass.c:435
static mcore_handle handle
Definition: asm_mcore.c:8
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
#define w
Definition: crypto_rc6.c:13
static static fork write
Definition: sflib.h:33
#define UV__ERR(x)
Definition: errno.h:29
voidpf void * buf
Definition: ioapi.h:138
assert(limit<=UINT32_MAX/2)
#define QUEUE_DATA(ptr, type, field)
Definition: queue.h:30
#define QUEUE_EMPTY(q)
Definition: queue.h:39
#define QUEUE_HEAD(q)
Definition: queue.h:42
#define QUEUE_INSERT_TAIL(h, q)
Definition: queue.h:92
#define QUEUE_MOVE(h, n)
Definition: queue.h:72
void * QUEUE[2]
Definition: queue.h:21
#define QUEUE_REMOVE(q)
Definition: queue.h:101
#define EINTR
Definition: sftypes.h:114
unsigned long uint64_t
Definition: sftypes.h:28
#define EAGAIN
Definition: sftypes.h:121
int ssize_t
Definition: sftypes.h:39
#define h(i)
Definition: sha256.c:48
Definition: unix.h:96
Definition: uv.h:844
Definition: uv.h:1780
uv_loop_t * loop
Definition: main.c:7
void uv__io_stop(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: core.c:910
void uv__io_start(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: core.c:882
void uv__io_init(uv__io_t *w, uv__io_cb cb, int fd)
Definition: core.c:865
int uv__close(int fd)
Definition: core.c:569
#define UV__F_NONBLOCK
Definition: internal.h:288
int uv__make_pipe(int fds[2], int flags)
Definition: process.c:142
#define ACCESS_ONCE(type, var)
Definition: internal.h:79
int uv__async_fork(uv_loop_t *loop)
Definition: async.c:230
void uv__async_stop(uv_loop_t *loop)
Definition: async.c:240
int uv_async_init(uv_loop_t *loop, uv_async_t *handle, uv_async_cb async_cb)
Definition: async.c:45
static int uv__async_start(uv_loop_t *loop)
Definition: async.c:202
static void uv__async_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: async.c:122
static void uv__async_send(uv_loop_t *loop)
Definition: async.c:168
int uv_async_send(uv_async_t *handle)
Definition: async.c:63
void uv__async_close(uv_async_t *handle)
Definition: async.c:115
static int uv__async_spin(uv_async_t *handle)
Definition: async.c:84
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:301
#define uv__handle_stop(h)
Definition: uv-common.h:266
#define uv__handle_start(h)
Definition: uv-common.h:258
void(* uv_async_cb)(uv_async_t *handle)
Definition: uv.h:322
uv_pipe_t queue
Definition: worker.c:9
static const z80_opcode fd[]
Definition: z80_tab.h:997
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115