Rizin
unix-like reverse engineering framework and cli tools
async.c File Reference
#include "uv.h"
#include "internal.h"
#include "atomic-ops.h"
#include <errno.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sched.h>

Go to the source code of this file.

Functions

static void uv__async_send (uv_loop_t *loop)
 
static int uv__async_start (uv_loop_t *loop)
 
int uv_async_init (uv_loop_t *loop, uv_async_t *handle, uv_async_cb async_cb)
 
int uv_async_send (uv_async_t *handle)
 
static int uv__async_spin (uv_async_t *handle)
 
void uv__async_close (uv_async_t *handle)
 
static void uv__async_io (uv_loop_t *loop, uv__io_t *w, unsigned int events)
 
int uv__async_fork (uv_loop_t *loop)
 
void uv__async_stop (uv_loop_t *loop)
 

Function Documentation

◆ uv__async_close()

void uv__async_close ( uv_async_t handle)

Definition at line 115 of file async.c.

115  {
117  QUEUE_REMOVE(&handle->queue);
119 }
static mcore_handle handle
Definition: asm_mcore.c:8
#define QUEUE_REMOVE(q)
Definition: queue.h:101
static int uv__async_spin(uv_async_t *handle)
Definition: async.c:84
#define uv__handle_stop(h)
Definition: uv-common.h:266

References handle, QUEUE_REMOVE, uv__async_spin(), and uv__handle_stop.

Referenced by uv_close().

◆ uv__async_fork()

int uv__async_fork ( uv_loop_t loop)

Definition at line 230 of file async.c.

230  {
231  if (loop->async_io_watcher.fd == -1) /* never started */
232  return 0;
233 
235 
236  return uv__async_start(loop);
237 }
uv_loop_t * loop
Definition: main.c:7
void uv__async_stop(uv_loop_t *loop)
Definition: async.c:240
static int uv__async_start(uv_loop_t *loop)
Definition: async.c:202

References loop, uv__async_start(), and uv__async_stop().

Referenced by uv_loop_fork().

◆ uv__async_io()

static void uv__async_io ( uv_loop_t loop,
uv__io_t w,
unsigned int  events 
)
static

Definition at line 122 of file async.c.

122  {
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 }
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
#define w
Definition: crypto_rc6.c:13
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 EINTR
Definition: sftypes.h:114
#define EAGAIN
Definition: sftypes.h:121
int ssize_t
Definition: sftypes.h:39
#define h(i)
Definition: sha256.c:48
Definition: uv.h:844
uv_pipe_t queue
Definition: worker.c:9
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References assert(), EAGAIN, EINTR, h, loop, NULL, queue, QUEUE_DATA, QUEUE_EMPTY, QUEUE_HEAD, QUEUE_INSERT_TAIL, QUEUE_MOVE, QUEUE_REMOVE, r, read(), uv__async_spin(), and w.

Referenced by uv__async_start().

◆ uv__async_send()

static void uv__async_send ( uv_loop_t loop)
static

Definition at line 168 of file async.c.

168  {
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 }
size_t len
Definition: 6502dis.c:15
ut16 val
Definition: armass64_const.h:6
static static fork write
Definition: sflib.h:33
unsigned long uint64_t
Definition: sftypes.h:28
static const z80_opcode fd[]
Definition: z80_tab.h:997

References EAGAIN, EINTR, fd, len, loop, r, val, and write.

Referenced by uv_async_send().

◆ uv__async_spin()

static int uv__async_spin ( uv_async_t handle)
static

Definition at line 84 of file async.c.

84  {
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 }
lzma_index ** i
Definition: index.h:629

References handle, and i.

Referenced by uv__async_close(), and uv__async_io().

◆ uv__async_start()

static int uv__async_start ( uv_loop_t loop)
static

Definition at line 202 of file async.c.

202  {
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 }
static bool err
Definition: armass.c:435
#define UV__ERR(x)
Definition: errno.h:29
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
#define UV__F_NONBLOCK
Definition: internal.h:288
int uv__make_pipe(int fds[2], int flags)
Definition: process.c:142
static void uv__async_io(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: async.c:122

References err, loop, uv__async_io(), UV__ERR, UV__F_NONBLOCK, uv__io_init(), uv__io_start(), and uv__make_pipe().

Referenced by uv__async_fork(), and uv_async_init().

◆ uv__async_stop()

void uv__async_stop ( uv_loop_t loop)

Definition at line 240 of file async.c.

240  {
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 }
void uv__io_stop(uv_loop_t *loop, uv__io_t *w, unsigned int events)
Definition: core.c:910
int uv__close(int fd)
Definition: core.c:569

References loop, uv__close(), and uv__io_stop().

Referenced by uv__async_fork(), and uv__loop_close().

◆ uv_async_init()

int uv_async_init ( uv_loop_t loop,
uv_async_t handle,
uv_async_cb  async_cb 
)

Definition at line 45 of file async.c.

45  {
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 }
#define uv__handle_init(loop_, h, type_)
Definition: uv-common.h:301
#define uv__handle_start(h)
Definition: uv-common.h:258

◆ uv_async_send()

int uv_async_send ( uv_async_t handle)

Definition at line 63 of file async.c.

63  {
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 }
#define ACCESS_ONCE(type, var)
Definition: internal.h:79
static void uv__async_send(uv_loop_t *loop)
Definition: async.c:168