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 | |
36 | static void uv__async_send(uv_loop_t* loop); |
37 | static int uv__async_start(uv_loop_t* loop); |
38 | static int uv__async_eventfd(void); |
39 | |
40 | |
41 | int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) { |
42 | int err; |
43 | |
44 | err = uv__async_start(loop); |
45 | if (err) |
46 | return err; |
47 | |
48 | uv__handle_init(loop, (uv_handle_t*)handle, UV_ASYNC); |
49 | handle->async_cb = async_cb; |
50 | handle->pending = 0; |
51 | |
52 | QUEUE_INSERT_TAIL(&loop->async_handles, &handle->queue); |
53 | uv__handle_start(handle); |
54 | |
55 | return 0; |
56 | } |
57 | |
58 | |
59 | int uv_async_send(uv_async_t* handle) { |
60 | /* Do a cheap read first. */ |
61 | if (ACCESS_ONCE(int, handle->pending) != 0) |
62 | return 0; |
63 | |
64 | /* Tell the other thread we're busy with the handle. */ |
65 | if (cmpxchgi(&handle->pending, 0, 1) != 0) |
66 | return 0; |
67 | |
68 | /* Wake up the other thread's event loop. */ |
69 | uv__async_send(handle->loop); |
70 | |
71 | /* Tell the other thread we're done. */ |
72 | if (cmpxchgi(&handle->pending, 1, 2) != 1) |
73 | abort(); |
74 | |
75 | return 0; |
76 | } |
77 | |
78 | |
79 | /* Only call this from the event loop thread. */ |
80 | static int uv__async_spin(uv_async_t* handle) { |
81 | int rc; |
82 | |
83 | for (;;) { |
84 | /* rc=0 -- handle is not pending. |
85 | * rc=1 -- handle is pending, other thread is still working with it. |
86 | * rc=2 -- handle is pending, other thread is done. |
87 | */ |
88 | rc = cmpxchgi(&handle->pending, 2, 0); |
89 | |
90 | if (rc != 1) |
91 | return rc; |
92 | |
93 | /* Other thread is busy with this handle, spin until it's done. */ |
94 | cpu_relax(); |
95 | } |
96 | } |
97 | |
98 | |
99 | void uv__async_close(uv_async_t* handle) { |
100 | uv__async_spin(handle); |
101 | QUEUE_REMOVE(&handle->queue); |
102 | uv__handle_stop(handle); |
103 | } |
104 | |
105 | |
106 | static void uv__async_io(uv_loop_t* loop, uv__io_t* w, unsigned int events) { |
107 | char buf[1024]; |
108 | ssize_t r; |
109 | QUEUE queue; |
110 | QUEUE* q; |
111 | uv_async_t* h; |
112 | |
113 | assert(w == &loop->async_io_watcher); |
114 | |
115 | for (;;) { |
116 | r = read(w->fd, buf, sizeof(buf)); |
117 | |
118 | if (r == sizeof(buf)) |
119 | continue; |
120 | |
121 | if (r != -1) |
122 | break; |
123 | |
124 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
125 | break; |
126 | |
127 | if (errno == EINTR) |
128 | continue; |
129 | |
130 | abort(); |
131 | } |
132 | |
133 | QUEUE_MOVE(&loop->async_handles, &queue); |
134 | while (!QUEUE_EMPTY(&queue)) { |
135 | q = QUEUE_HEAD(&queue); |
136 | h = QUEUE_DATA(q, uv_async_t, queue); |
137 | |
138 | QUEUE_REMOVE(q); |
139 | QUEUE_INSERT_TAIL(&loop->async_handles, q); |
140 | |
141 | if (0 == uv__async_spin(h)) |
142 | continue; /* Not pending. */ |
143 | |
144 | if (h->async_cb == NULL) |
145 | continue; |
146 | |
147 | h->async_cb(h); |
148 | } |
149 | } |
150 | |
151 | |
152 | static void uv__async_send(uv_loop_t* loop) { |
153 | const void* buf; |
154 | ssize_t len; |
155 | int fd; |
156 | int r; |
157 | |
158 | buf = "" ; |
159 | len = 1; |
160 | fd = loop->async_wfd; |
161 | |
162 | #if defined(__linux__) |
163 | if (fd == -1) { |
164 | static const uint64_t val = 1; |
165 | buf = &val; |
166 | len = sizeof(val); |
167 | fd = loop->async_io_watcher.fd; /* eventfd */ |
168 | } |
169 | #endif |
170 | |
171 | do |
172 | r = write(fd, buf, len); |
173 | while (r == -1 && errno == EINTR); |
174 | |
175 | if (r == len) |
176 | return; |
177 | |
178 | if (r == -1) |
179 | if (errno == EAGAIN || errno == EWOULDBLOCK) |
180 | return; |
181 | |
182 | abort(); |
183 | } |
184 | |
185 | |
186 | static int uv__async_start(uv_loop_t* loop) { |
187 | int pipefd[2]; |
188 | int err; |
189 | |
190 | if (loop->async_io_watcher.fd != -1) |
191 | return 0; |
192 | |
193 | err = uv__async_eventfd(); |
194 | if (err >= 0) { |
195 | pipefd[0] = err; |
196 | pipefd[1] = -1; |
197 | } |
198 | else if (err == UV_ENOSYS) { |
199 | err = uv__make_pipe(pipefd, UV__F_NONBLOCK); |
200 | #if defined(__linux__) |
201 | /* Save a file descriptor by opening one of the pipe descriptors as |
202 | * read/write through the procfs. That file descriptor can then |
203 | * function as both ends of the pipe. |
204 | */ |
205 | if (err == 0) { |
206 | char buf[32]; |
207 | int fd; |
208 | |
209 | snprintf(buf, sizeof(buf), "/proc/self/fd/%d" , pipefd[0]); |
210 | fd = uv__open_cloexec(buf, O_RDWR); |
211 | if (fd >= 0) { |
212 | uv__close(pipefd[0]); |
213 | uv__close(pipefd[1]); |
214 | pipefd[0] = fd; |
215 | pipefd[1] = fd; |
216 | } |
217 | } |
218 | #endif |
219 | } |
220 | |
221 | if (err < 0) |
222 | return err; |
223 | |
224 | uv__io_init(&loop->async_io_watcher, uv__async_io, pipefd[0]); |
225 | uv__io_start(loop, &loop->async_io_watcher, POLLIN); |
226 | loop->async_wfd = pipefd[1]; |
227 | |
228 | return 0; |
229 | } |
230 | |
231 | |
232 | int uv__async_fork(uv_loop_t* loop) { |
233 | if (loop->async_io_watcher.fd == -1) /* never started */ |
234 | return 0; |
235 | |
236 | uv__async_stop(loop); |
237 | |
238 | return uv__async_start(loop); |
239 | } |
240 | |
241 | |
242 | void uv__async_stop(uv_loop_t* loop) { |
243 | if (loop->async_io_watcher.fd == -1) |
244 | return; |
245 | |
246 | if (loop->async_wfd != -1) { |
247 | if (loop->async_wfd != loop->async_io_watcher.fd) |
248 | uv__close(loop->async_wfd); |
249 | loop->async_wfd = -1; |
250 | } |
251 | |
252 | uv__io_stop(loop, &loop->async_io_watcher, POLLIN); |
253 | uv__close(loop->async_io_watcher.fd); |
254 | loop->async_io_watcher.fd = -1; |
255 | } |
256 | |
257 | |
258 | static int uv__async_eventfd(void) { |
259 | #if defined(__linux__) |
260 | static int no_eventfd2; |
261 | static int no_eventfd; |
262 | int fd; |
263 | |
264 | if (no_eventfd2) |
265 | goto skip_eventfd2; |
266 | |
267 | fd = uv__eventfd2(0, UV__EFD_CLOEXEC | UV__EFD_NONBLOCK); |
268 | if (fd != -1) |
269 | return fd; |
270 | |
271 | if (errno != ENOSYS) |
272 | return UV__ERR(errno); |
273 | |
274 | no_eventfd2 = 1; |
275 | |
276 | skip_eventfd2: |
277 | |
278 | if (no_eventfd) |
279 | goto skip_eventfd; |
280 | |
281 | fd = uv__eventfd(0); |
282 | if (fd != -1) { |
283 | uv__cloexec(fd, 1); |
284 | uv__nonblock(fd, 1); |
285 | return fd; |
286 | } |
287 | |
288 | if (errno != ENOSYS) |
289 | return UV__ERR(errno); |
290 | |
291 | no_eventfd = 1; |
292 | |
293 | skip_eventfd: |
294 | |
295 | #endif |
296 | |
297 | return UV_ENOSYS; |
298 | } |
299 | |