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_async_t* handle);
42 static int uv__async_start(uv_loop_t* loop);
43
44
uv_async_init(uv_loop_t * loop,uv_async_t * handle,uv_async_cb async_cb)45 int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
46 int err;
47
48 err = uv__async_start(loop);
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);
57 uv__handle_start(handle);
58
59 return 0;
60 }
61
62
uv_async_send(uv_async_t * handle)63 int uv_async_send(uv_async_t* handle) {
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);
74
75 return 0;
76 }
77
uv__async_close(uv_async_t * handle)78 void uv__async_close(uv_async_t* handle) {
79 cmpxchgi(&handle->pending, 1, 0);
80 QUEUE_REMOVE(&handle->queue);
81 uv__handle_stop(handle);
82 }
83
84
uv__async_io(uv_loop_t * loop,uv__io_t * w,unsigned int events)85 static void uv__async_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) {
86 char buf[1024];
87 ssize_t r;
88 QUEUE queue;
89 QUEUE* q;
90 uv_async_t* h;
91
92 assert(w == &loop->async_io_watcher);
93
94 for (;;) {
95 r = read(w->fd, buf, sizeof(buf));
96
97 if (r == sizeof(buf))
98 continue;
99
100 if (r != -1)
101 break;
102
103 if (errno == EAGAIN || errno == EWOULDBLOCK)
104 break;
105
106 if (errno == EINTR)
107 continue;
108
109 abort();
110 }
111
112 QUEUE_MOVE(&loop->async_handles, &queue);
113 while (!QUEUE_EMPTY(&queue)) {
114 q = QUEUE_HEAD(&queue);
115 h = QUEUE_DATA(q, uv_async_t, queue);
116
117 QUEUE_REMOVE(q);
118 QUEUE_INSERT_TAIL(&loop->async_handles, q);
119
120 if (0 == cmpxchgi(&h->pending, 1, 0))
121 continue; /* Not pending. */
122
123 if (h->async_cb == NULL)
124 continue;
125
126 h->async_cb(h);
127 }
128 }
129
130
uv__async_send(uv_async_t * handle)131 static void uv__async_send(uv_async_t* handle) {
132 const void* buf;
133 ssize_t len;
134 int fd;
135 int r;
136 uv_loop_t* loop = handle->loop;
137
138 buf = "";
139 len = 1;
140 fd = loop->async_wfd;
141
142 #if defined(__linux__)
143 if (fd == -1) {
144 static const uint64_t val = 1;
145 buf = &val;
146 len = sizeof(val);
147 fd = loop->async_io_watcher.fd; /* eventfd */
148 }
149 #endif
150
151 do
152 r = write(fd, buf, len);
153 while (r == -1 && errno == EINTR && ACCESS_ONCE(int, handle->pending) == 1);
154
155 if (r == len)
156 return;
157
158 if (r == -1)
159 if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
160 return;
161
162 abort();
163 }
164
165
uv__async_start(uv_loop_t * loop)166 static int uv__async_start(uv_loop_t* loop) {
167 int pipefd[2];
168 int err;
169
170 if (loop->async_io_watcher.fd != -1)
171 return 0;
172
173 #ifdef __linux__
174 err = eventfd(0, EFD_CLOEXEC | EFD_NONBLOCK);
175 if (err < 0)
176 return UV__ERR(errno);
177
178 pipefd[0] = err;
179 pipefd[1] = -1;
180 #else
181 err = uv__make_pipe(pipefd, UV_NONBLOCK_PIPE);
182 if (err < 0)
183 return err;
184 #endif
185
186 uv__io_init(&loop->async_io_watcher, uv__async_io, pipefd[0]);
187 uv__io_start(loop, &loop->async_io_watcher, POLLIN);
188 loop->async_wfd = pipefd[1];
189
190 return 0;
191 }
192
193
uv__async_fork(uv_loop_t * loop)194 int uv__async_fork(uv_loop_t* loop) {
195 if (loop->async_io_watcher.fd == -1) /* never started */
196 return 0;
197
198 uv__async_stop(loop);
199
200 return uv__async_start(loop);
201 }
202
203
uv__async_stop(uv_loop_t * loop)204 void uv__async_stop(uv_loop_t* loop) {
205 if (loop->async_io_watcher.fd == -1)
206 return;
207
208 if (loop->async_wfd != -1) {
209 if (loop->async_wfd != loop->async_io_watcher.fd)
210 uv__close(loop->async_wfd);
211 loop->async_wfd = -1;
212 }
213
214 uv__io_stop(loop, &loop->async_io_watcher, POLLIN);
215 uv__close(loop->async_io_watcher.fd);
216 loop->async_io_watcher.fd = -1;
217 }
218