1 | /* |
1 | /* |
|
|
2 | * libev event processing core, watcher management |
|
|
3 | * |
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
4 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
3 | * All rights reserved. |
5 | * All rights reserved. |
4 | * |
6 | * |
5 | * Redistribution and use in source and binary forms, with or without |
7 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions are |
8 | * modification, are permitted provided that the following conditions are |
… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
26 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
27 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
28 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
29 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
30 | */ |
|
|
31 | #if EV_USE_CONFIG_H |
|
|
32 | # include "config.h" |
|
|
33 | #endif |
29 | |
34 | |
30 | #include <math.h> |
35 | #include <math.h> |
31 | #include <stdlib.h> |
36 | #include <stdlib.h> |
32 | #include <unistd.h> |
37 | #include <unistd.h> |
33 | #include <fcntl.h> |
38 | #include <fcntl.h> |
… | |
… | |
37 | #include <stdio.h> |
42 | #include <stdio.h> |
38 | |
43 | |
39 | #include <assert.h> |
44 | #include <assert.h> |
40 | #include <errno.h> |
45 | #include <errno.h> |
41 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
42 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
43 | #include <sys/time.h> |
50 | #include <sys/time.h> |
44 | #include <time.h> |
51 | #include <time.h> |
45 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
46 | #ifndef HAVE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
|
|
56 | # define EV_USE_MONOTONIC 1 |
|
|
57 | #endif |
|
|
58 | |
|
|
59 | #ifndef EV_USE_SELECT |
|
|
60 | # define EV_USE_SELECT 1 |
|
|
61 | #endif |
|
|
62 | |
|
|
63 | #ifndef EV_USE_POLL |
|
|
64 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
65 | #endif |
|
|
66 | |
|
|
67 | #ifndef EV_USE_EPOLL |
|
|
68 | # define EV_USE_EPOLL 0 |
|
|
69 | #endif |
|
|
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
|
|
73 | #endif |
|
|
74 | |
|
|
75 | #ifndef EV_USE_REALTIME |
|
|
76 | # define EV_USE_REALTIME 1 |
|
|
77 | #endif |
|
|
78 | |
|
|
79 | /**/ |
|
|
80 | |
47 | # ifdef CLOCK_MONOTONIC |
81 | #ifndef CLOCK_MONOTONIC |
|
|
82 | # undef EV_USE_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
83 | # define EV_USE_MONOTONIC 0 |
49 | # endif |
84 | #endif |
50 | #endif |
|
|
51 | |
85 | |
52 | #ifndef HAVE_SELECT |
|
|
53 | # define HAVE_SELECT 1 |
|
|
54 | #endif |
|
|
55 | |
|
|
56 | #ifndef HAVE_EPOLL |
|
|
57 | # define HAVE_EPOLL 0 |
|
|
58 | #endif |
|
|
59 | |
|
|
60 | #ifndef HAVE_REALTIME |
86 | #ifndef CLOCK_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
87 | # undef EV_USE_REALTIME |
|
|
88 | # define EV_USE_REALTIME 0 |
62 | #endif |
89 | #endif |
|
|
90 | |
|
|
91 | /**/ |
63 | |
92 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
93 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
95 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
96 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
67 | |
97 | |
68 | #include "ev.h" |
98 | #include "ev.h" |
|
|
99 | |
|
|
100 | #if __GNUC__ >= 3 |
|
|
101 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
102 | # define inline inline |
|
|
103 | #else |
|
|
104 | # define expect(expr,value) (expr) |
|
|
105 | # define inline static |
|
|
106 | #endif |
|
|
107 | |
|
|
108 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
109 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
110 | |
|
|
111 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
112 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
69 | |
113 | |
70 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
72 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
73 | |
117 | |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
75 | ev_tstamp ev_now; |
119 | ev_tstamp ev_now; |
76 | int ev_method; |
120 | int ev_method; |
77 | |
121 | |
78 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
79 | |
123 | |
… | |
… | |
84 | /*****************************************************************************/ |
128 | /*****************************************************************************/ |
85 | |
129 | |
86 | ev_tstamp |
130 | ev_tstamp |
87 | ev_time (void) |
131 | ev_time (void) |
88 | { |
132 | { |
89 | #if HAVE_REALTIME |
133 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
134 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
135 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
136 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
137 | #else |
94 | struct timeval tv; |
138 | struct timeval tv; |
… | |
… | |
98 | } |
142 | } |
99 | |
143 | |
100 | static ev_tstamp |
144 | static ev_tstamp |
101 | get_clock (void) |
145 | get_clock (void) |
102 | { |
146 | { |
103 | #if HAVE_MONOTONIC |
147 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
148 | if (expect_true (have_monotonic)) |
105 | { |
149 | { |
106 | struct timespec ts; |
150 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
151 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
153 | } |
110 | #endif |
154 | #endif |
111 | |
155 | |
112 | return ev_time (); |
156 | return ev_time (); |
113 | } |
157 | } |
114 | |
158 | |
|
|
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
160 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
161 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
162 | if (expect_false ((cnt) > cur)) \ |
117 | { \ |
163 | { \ |
118 | int newcnt = cur; \ |
164 | int newcnt = cur; \ |
119 | do \ |
165 | do \ |
120 | { \ |
166 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
168 | } \ |
123 | while ((cnt) > newcnt); \ |
169 | while ((cnt) > newcnt); \ |
124 | \ |
170 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
171 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
172 | init (base + cur, newcnt - cur); \ |
… | |
… | |
130 | /*****************************************************************************/ |
176 | /*****************************************************************************/ |
131 | |
177 | |
132 | typedef struct |
178 | typedef struct |
133 | { |
179 | { |
134 | struct ev_io *head; |
180 | struct ev_io *head; |
135 | int events; |
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
136 | } ANFD; |
183 | } ANFD; |
137 | |
184 | |
138 | static ANFD *anfds; |
185 | static ANFD *anfds; |
139 | static int anfdmax; |
186 | static int anfdmax; |
140 | |
187 | |
… | |
… | |
143 | { |
190 | { |
144 | while (count--) |
191 | while (count--) |
145 | { |
192 | { |
146 | base->head = 0; |
193 | base->head = 0; |
147 | base->events = EV_NONE; |
194 | base->events = EV_NONE; |
|
|
195 | base->reify = 0; |
|
|
196 | |
148 | ++base; |
197 | ++base; |
149 | } |
198 | } |
150 | } |
199 | } |
151 | |
200 | |
152 | typedef struct |
201 | typedef struct |
153 | { |
202 | { |
154 | W w; |
203 | W w; |
155 | int events; |
204 | int events; |
156 | } ANPENDING; |
205 | } ANPENDING; |
157 | |
206 | |
158 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
159 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
160 | |
209 | |
161 | static void |
210 | static void |
162 | event (W w, int events) |
211 | event (W w, int events) |
163 | { |
212 | { |
164 | if (w->active) |
213 | if (w->pending) |
165 | { |
214 | { |
166 | w->pending = ++pendingcnt; |
|
|
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
|
|
168 | pendings [pendingcnt - 1].w = w; |
|
|
169 | pendings [pendingcnt - 1].events = events; |
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
216 | return; |
170 | } |
217 | } |
|
|
218 | |
|
|
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
|
|
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
|
|
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
|
|
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
171 | } |
223 | } |
172 | |
224 | |
173 | static void |
225 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
175 | { |
227 | { |
… | |
… | |
213 | int events = 0; |
265 | int events = 0; |
214 | |
266 | |
215 | for (w = anfd->head; w; w = w->next) |
267 | for (w = anfd->head; w; w = w->next) |
216 | events |= w->events; |
268 | events |= w->events; |
217 | |
269 | |
218 | anfd->events &= ~EV_REIFY; |
270 | anfd->reify = 0; |
219 | |
271 | |
220 | if (anfd->events != events) |
272 | if (anfd->events != events) |
221 | { |
273 | { |
222 | method_modify (fd, anfd->events, events); |
274 | method_modify (fd, anfd->events, events); |
223 | anfd->events = events; |
275 | anfd->events = events; |
… | |
… | |
228 | } |
280 | } |
229 | |
281 | |
230 | static void |
282 | static void |
231 | fd_change (int fd) |
283 | fd_change (int fd) |
232 | { |
284 | { |
233 | if (anfds [fd].events & EV_REIFY) |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
234 | return; |
286 | return; |
235 | |
287 | |
236 | anfds [fd].events |= EV_REIFY; |
288 | anfds [fd].reify = 1; |
237 | |
289 | |
238 | ++fdchangecnt; |
290 | ++fdchangecnt; |
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
240 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
241 | } |
293 | } |
242 | |
294 | |
|
|
295 | static void |
|
|
296 | fd_kill (int fd) |
|
|
297 | { |
|
|
298 | struct ev_io *w; |
|
|
299 | |
|
|
300 | printf ("killing fd %d\n", fd);//D |
|
|
301 | while ((w = anfds [fd].head)) |
|
|
302 | { |
|
|
303 | ev_io_stop (w); |
|
|
304 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
305 | } |
|
|
306 | } |
|
|
307 | |
243 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
244 | static void |
309 | static void |
245 | fd_recheck (void) |
310 | fd_ebadf (void) |
246 | { |
311 | { |
247 | int fd; |
312 | int fd; |
248 | |
313 | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
317 | fd_kill (fd); |
|
|
318 | } |
|
|
319 | |
|
|
320 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
321 | static void |
|
|
322 | fd_enomem (void) |
|
|
323 | { |
|
|
324 | int fd = anfdmax; |
|
|
325 | |
|
|
326 | while (fd--) |
|
|
327 | if (anfds [fd].events) |
253 | { |
328 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
329 | close (fd); |
255 | ev_io_stop (anfds [fd].head); |
330 | fd_kill (fd); |
|
|
331 | return; |
256 | } |
332 | } |
257 | } |
333 | } |
258 | |
334 | |
259 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
260 | |
336 | |
261 | static struct ev_timer **timers; |
337 | static struct ev_timer **timers; |
… | |
… | |
308 | /*****************************************************************************/ |
384 | /*****************************************************************************/ |
309 | |
385 | |
310 | typedef struct |
386 | typedef struct |
311 | { |
387 | { |
312 | struct ev_signal *head; |
388 | struct ev_signal *head; |
313 | sig_atomic_t gotsig; |
389 | sig_atomic_t volatile gotsig; |
314 | } ANSIG; |
390 | } ANSIG; |
315 | |
391 | |
316 | static ANSIG *signals; |
392 | static ANSIG *signals; |
317 | static int signalmax; |
393 | static int signalmax; |
318 | |
394 | |
319 | static int sigpipe [2]; |
395 | static int sigpipe [2]; |
320 | static sig_atomic_t gotsig; |
396 | static sig_atomic_t volatile gotsig; |
321 | static struct ev_io sigev; |
397 | static struct ev_io sigev; |
322 | |
398 | |
323 | static void |
399 | static void |
324 | signals_init (ANSIG *base, int count) |
400 | signals_init (ANSIG *base, int count) |
325 | { |
401 | { |
326 | while (count--) |
402 | while (count--) |
327 | { |
403 | { |
328 | base->head = 0; |
404 | base->head = 0; |
329 | base->gotsig = 0; |
405 | base->gotsig = 0; |
|
|
406 | |
330 | ++base; |
407 | ++base; |
331 | } |
408 | } |
332 | } |
409 | } |
333 | |
410 | |
334 | static void |
411 | static void |
… | |
… | |
337 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
338 | |
415 | |
339 | if (!gotsig) |
416 | if (!gotsig) |
340 | { |
417 | { |
341 | gotsig = 1; |
418 | gotsig = 1; |
342 | write (sigpipe [1], &gotsig, 1); |
419 | write (sigpipe [1], &signum, 1); |
343 | } |
420 | } |
344 | } |
421 | } |
345 | |
422 | |
346 | static void |
423 | static void |
347 | sigcb (struct ev_io *iow, int revents) |
424 | sigcb (struct ev_io *iow, int revents) |
348 | { |
425 | { |
349 | struct ev_signal *w; |
426 | struct ev_signal *w; |
350 | int sig; |
427 | int signum; |
351 | |
428 | |
|
|
429 | read (sigpipe [0], &revents, 1); |
352 | gotsig = 0; |
430 | gotsig = 0; |
353 | read (sigpipe [0], &revents, 1); |
|
|
354 | |
431 | |
355 | for (sig = signalmax; sig--; ) |
432 | for (signum = signalmax; signum--; ) |
356 | if (signals [sig].gotsig) |
433 | if (signals [signum].gotsig) |
357 | { |
434 | { |
358 | signals [sig].gotsig = 0; |
435 | signals [signum].gotsig = 0; |
359 | |
436 | |
360 | for (w = signals [sig].head; w; w = w->next) |
437 | for (w = signals [signum].head; w; w = w->next) |
361 | event ((W)w, EV_SIGNAL); |
438 | event ((W)w, EV_SIGNAL); |
362 | } |
439 | } |
363 | } |
440 | } |
364 | |
441 | |
365 | static void |
442 | static void |
366 | siginit (void) |
443 | siginit (void) |
367 | { |
444 | { |
|
|
445 | #ifndef WIN32 |
368 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
369 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
370 | |
448 | |
371 | /* rather than sort out wether we really need nb, set it */ |
449 | /* rather than sort out wether we really need nb, set it */ |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
452 | #endif |
374 | |
453 | |
375 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
376 | ev_io_start (&sigev); |
455 | ev_io_start (&sigev); |
377 | } |
456 | } |
378 | |
457 | |
… | |
… | |
390 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
391 | |
470 | |
392 | static struct ev_child *childs [PID_HASHSIZE]; |
471 | static struct ev_child *childs [PID_HASHSIZE]; |
393 | static struct ev_signal childev; |
472 | static struct ev_signal childev; |
394 | |
473 | |
|
|
474 | #ifndef WIN32 |
|
|
475 | |
395 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
396 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
397 | #endif |
478 | #endif |
398 | |
479 | |
399 | static void |
480 | static void |
|
|
481 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
482 | { |
|
|
483 | struct ev_child *w; |
|
|
484 | |
|
|
485 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
486 | if (w->pid == pid || !w->pid) |
|
|
487 | { |
|
|
488 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
489 | w->rpid = pid; |
|
|
490 | w->rstatus = status; |
|
|
491 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
492 | event ((W)w, EV_CHILD); |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | static void |
400 | childcb (struct ev_signal *sw, int revents) |
497 | childcb (struct ev_signal *sw, int revents) |
401 | { |
498 | { |
402 | struct ev_child *w; |
|
|
403 | int pid, status; |
499 | int pid, status; |
404 | |
500 | |
|
|
501 | printf ("chld %x\n", revents);//D |
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
502 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
503 | { |
407 | if (w->pid == pid || w->pid == -1) |
504 | /* make sure we are called again until all childs have been reaped */ |
408 | { |
|
|
409 | w->status = status; |
|
|
410 | event ((W)w, EV_CHILD); |
505 | event ((W)sw, EV_SIGNAL); |
411 | } |
506 | |
|
|
507 | child_reap (sw, pid, pid, status); |
|
|
508 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
509 | } |
412 | } |
510 | } |
|
|
511 | |
|
|
512 | #endif |
413 | |
513 | |
414 | /*****************************************************************************/ |
514 | /*****************************************************************************/ |
415 | |
515 | |
|
|
516 | #if EV_USE_KQUEUE |
|
|
517 | # include "ev_kqueue.c" |
|
|
518 | #endif |
416 | #if HAVE_EPOLL |
519 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
520 | # include "ev_epoll.c" |
418 | #endif |
521 | #endif |
|
|
522 | #if EV_USE_POLL |
|
|
523 | # include "ev_poll.c" |
|
|
524 | #endif |
419 | #if HAVE_SELECT |
525 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
526 | # include "ev_select.c" |
421 | #endif |
527 | #endif |
422 | |
528 | |
423 | int |
529 | int |
424 | ev_version_major (void) |
530 | ev_version_major (void) |
… | |
… | |
430 | ev_version_minor (void) |
536 | ev_version_minor (void) |
431 | { |
537 | { |
432 | return EV_VERSION_MINOR; |
538 | return EV_VERSION_MINOR; |
433 | } |
539 | } |
434 | |
540 | |
|
|
541 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
542 | static int |
|
|
543 | enable_secure () |
|
|
544 | { |
|
|
545 | return getuid () != geteuid () |
|
|
546 | || getgid () != getegid (); |
|
|
547 | } |
|
|
548 | |
435 | int ev_init (int flags) |
549 | int ev_init (int methods) |
436 | { |
550 | { |
437 | if (!ev_method) |
551 | if (!ev_method) |
438 | { |
552 | { |
439 | #if HAVE_MONOTONIC |
553 | #if EV_USE_MONOTONIC |
440 | { |
554 | { |
441 | struct timespec ts; |
555 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
556 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
557 | have_monotonic = 1; |
444 | } |
558 | } |
445 | #endif |
559 | #endif |
446 | |
560 | |
447 | ev_now = ev_time (); |
561 | ev_now = ev_time (); |
448 | now = get_clock (); |
562 | now = get_clock (); |
|
|
563 | now_floor = now; |
449 | diff = ev_now - now; |
564 | diff = ev_now - now; |
450 | |
565 | |
451 | if (pipe (sigpipe)) |
566 | if (pipe (sigpipe)) |
452 | return 0; |
567 | return 0; |
453 | |
568 | |
|
|
569 | if (methods == EVMETHOD_AUTO) |
|
|
570 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
571 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
572 | else |
454 | ev_method = EVMETHOD_NONE; |
573 | methods = EVMETHOD_ANY; |
|
|
574 | |
|
|
575 | ev_method = 0; |
|
|
576 | #if EV_USE_KQUEUE |
|
|
577 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
578 | #endif |
455 | #if HAVE_EPOLL |
579 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
580 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
457 | #endif |
581 | #endif |
|
|
582 | #if EV_USE_POLL |
|
|
583 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
|
|
584 | #endif |
458 | #if HAVE_SELECT |
585 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
586 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
460 | #endif |
587 | #endif |
461 | |
588 | |
462 | if (ev_method) |
589 | if (ev_method) |
463 | { |
590 | { |
464 | ev_watcher_init (&sigev, sigcb); |
591 | ev_watcher_init (&sigev, sigcb); |
|
|
592 | ev_set_priority (&sigev, EV_MAXPRI); |
465 | siginit (); |
593 | siginit (); |
466 | |
594 | |
|
|
595 | #ifndef WIN32 |
467 | ev_signal_init (&childev, childcb, SIGCHLD); |
596 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
597 | ev_set_priority (&childev, EV_MAXPRI); |
468 | ev_signal_start (&childev); |
598 | ev_signal_start (&childev); |
|
|
599 | #endif |
469 | } |
600 | } |
470 | } |
601 | } |
471 | |
602 | |
472 | return ev_method; |
603 | return ev_method; |
473 | } |
604 | } |
474 | |
605 | |
475 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
476 | |
607 | |
477 | void |
608 | void |
478 | ev_prefork (void) |
609 | ev_fork_prepare (void) |
479 | { |
610 | { |
480 | /* nop */ |
611 | /* nop */ |
481 | } |
612 | } |
482 | |
613 | |
483 | void |
614 | void |
484 | ev_postfork_parent (void) |
615 | ev_fork_parent (void) |
485 | { |
616 | { |
486 | /* nop */ |
617 | /* nop */ |
487 | } |
618 | } |
488 | |
619 | |
489 | void |
620 | void |
490 | ev_postfork_child (void) |
621 | ev_fork_child (void) |
491 | { |
622 | { |
492 | #if HAVE_EPOLL |
623 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
624 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
625 | epoll_postfork_child (); |
495 | #endif |
626 | #endif |
496 | |
627 | |
497 | ev_io_stop (&sigev); |
628 | ev_io_stop (&sigev); |
… | |
… | |
504 | /*****************************************************************************/ |
635 | /*****************************************************************************/ |
505 | |
636 | |
506 | static void |
637 | static void |
507 | call_pending (void) |
638 | call_pending (void) |
508 | { |
639 | { |
|
|
640 | int pri; |
|
|
641 | |
|
|
642 | for (pri = NUMPRI; pri--; ) |
509 | while (pendingcnt) |
643 | while (pendingcnt [pri]) |
510 | { |
644 | { |
511 | ANPENDING *p = pendings + --pendingcnt; |
645 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
512 | |
646 | |
513 | if (p->w) |
647 | if (p->w) |
514 | { |
648 | { |
515 | p->w->pending = 0; |
649 | p->w->pending = 0; |
516 | p->w->cb (p->w, p->events); |
650 | p->w->cb (p->w, p->events); |
517 | } |
651 | } |
518 | } |
652 | } |
519 | } |
653 | } |
520 | |
654 | |
521 | static void |
655 | static void |
522 | timers_reify (void) |
656 | timers_reify (void) |
523 | { |
657 | { |
524 | while (timercnt && timers [0]->at <= now) |
658 | while (timercnt && timers [0]->at <= now) |
525 | { |
659 | { |
526 | struct ev_timer *w = timers [0]; |
660 | struct ev_timer *w = timers [0]; |
527 | |
|
|
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
661 | |
530 | /* first reschedule or stop timer */ |
662 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
663 | if (w->repeat) |
532 | { |
664 | { |
|
|
665 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
666 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
667 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
668 | } |
537 | else |
669 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
670 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
671 | |
|
|
672 | event ((W)w, EV_TIMEOUT); |
539 | } |
673 | } |
540 | } |
674 | } |
541 | |
675 | |
542 | static void |
676 | static void |
543 | periodics_reify (void) |
677 | periodics_reify (void) |
… | |
… | |
548 | |
682 | |
549 | /* first reschedule or stop timer */ |
683 | /* first reschedule or stop timer */ |
550 | if (w->interval) |
684 | if (w->interval) |
551 | { |
685 | { |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
686 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
553 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
687 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
688 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
689 | } |
556 | else |
690 | else |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
691 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
692 | |
559 | event ((W)w, EV_TIMEOUT); |
693 | event ((W)w, EV_PERIODIC); |
560 | } |
694 | } |
561 | } |
695 | } |
562 | |
696 | |
563 | static void |
697 | static void |
564 | periodics_reschedule (ev_tstamp diff) |
698 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
583 | } |
717 | } |
584 | } |
718 | } |
585 | } |
719 | } |
586 | } |
720 | } |
587 | |
721 | |
|
|
722 | static int |
|
|
723 | time_update_monotonic (void) |
|
|
724 | { |
|
|
725 | now = get_clock (); |
|
|
726 | |
|
|
727 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
728 | { |
|
|
729 | ev_now = now + diff; |
|
|
730 | return 0; |
|
|
731 | } |
|
|
732 | else |
|
|
733 | { |
|
|
734 | now_floor = now; |
|
|
735 | ev_now = ev_time (); |
|
|
736 | return 1; |
|
|
737 | } |
|
|
738 | } |
|
|
739 | |
588 | static void |
740 | static void |
589 | time_update (void) |
741 | time_update (void) |
590 | { |
742 | { |
591 | int i; |
743 | int i; |
592 | |
744 | |
593 | ev_now = ev_time (); |
745 | #if EV_USE_MONOTONIC |
594 | |
|
|
595 | if (have_monotonic) |
746 | if (expect_true (have_monotonic)) |
596 | { |
747 | { |
597 | ev_tstamp odiff = diff; |
748 | if (time_update_monotonic ()) |
598 | |
|
|
599 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
600 | { |
749 | { |
601 | now = get_clock (); |
750 | ev_tstamp odiff = diff; |
|
|
751 | |
|
|
752 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
753 | { |
602 | diff = ev_now - now; |
754 | diff = ev_now - now; |
603 | |
755 | |
604 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
756 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
605 | return; /* all is well */ |
757 | return; /* all is well */ |
606 | |
758 | |
607 | ev_now = ev_time (); |
759 | ev_now = ev_time (); |
|
|
760 | now = get_clock (); |
|
|
761 | now_floor = now; |
|
|
762 | } |
|
|
763 | |
|
|
764 | periodics_reschedule (diff - odiff); |
|
|
765 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
608 | } |
766 | } |
609 | |
|
|
610 | periodics_reschedule (diff - odiff); |
|
|
611 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
612 | } |
767 | } |
613 | else |
768 | else |
|
|
769 | #endif |
614 | { |
770 | { |
|
|
771 | ev_now = ev_time (); |
|
|
772 | |
615 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
773 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
616 | { |
774 | { |
617 | periodics_reschedule (ev_now - now); |
775 | periodics_reschedule (ev_now - now); |
618 | |
776 | |
619 | /* adjust timers. this is easy, as the offset is the same for all */ |
777 | /* adjust timers. this is easy, as the offset is the same for all */ |
620 | for (i = 0; i < timercnt; ++i) |
778 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
791 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
634 | |
792 | |
635 | do |
793 | do |
636 | { |
794 | { |
637 | /* queue check watchers (and execute them) */ |
795 | /* queue check watchers (and execute them) */ |
638 | if (preparecnt) |
796 | if (expect_false (preparecnt)) |
639 | { |
797 | { |
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
798 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
641 | call_pending (); |
799 | call_pending (); |
642 | } |
800 | } |
643 | |
801 | |
… | |
… | |
646 | |
804 | |
647 | /* calculate blocking time */ |
805 | /* calculate blocking time */ |
648 | |
806 | |
649 | /* we only need this for !monotonic clockor timers, but as we basically |
807 | /* we only need this for !monotonic clockor timers, but as we basically |
650 | always have timers, we just calculate it always */ |
808 | always have timers, we just calculate it always */ |
|
|
809 | #if EV_USE_MONOTONIC |
|
|
810 | if (expect_true (have_monotonic)) |
|
|
811 | time_update_monotonic (); |
|
|
812 | else |
|
|
813 | #endif |
|
|
814 | { |
651 | ev_now = ev_time (); |
815 | ev_now = ev_time (); |
|
|
816 | now = ev_now; |
|
|
817 | } |
652 | |
818 | |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
819 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
654 | block = 0.; |
820 | block = 0.; |
655 | else |
821 | else |
656 | { |
822 | { |
657 | block = MAX_BLOCKTIME; |
823 | block = MAX_BLOCKTIME; |
658 | |
824 | |
659 | if (timercnt) |
825 | if (timercnt) |
660 | { |
826 | { |
661 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
827 | ev_tstamp to = timers [0]->at - now + method_fudge; |
662 | if (block > to) block = to; |
828 | if (block > to) block = to; |
663 | } |
829 | } |
664 | |
830 | |
665 | if (periodiccnt) |
831 | if (periodiccnt) |
666 | { |
832 | { |
… | |
… | |
719 | head = &(*head)->next; |
885 | head = &(*head)->next; |
720 | } |
886 | } |
721 | } |
887 | } |
722 | |
888 | |
723 | static void |
889 | static void |
724 | ev_clear (W w) |
890 | ev_clear_pending (W w) |
725 | { |
891 | { |
726 | if (w->pending) |
892 | if (w->pending) |
727 | { |
893 | { |
728 | pendings [w->pending - 1].w = 0; |
894 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
729 | w->pending = 0; |
895 | w->pending = 0; |
730 | } |
896 | } |
731 | } |
897 | } |
732 | |
898 | |
733 | static void |
899 | static void |
734 | ev_start (W w, int active) |
900 | ev_start (W w, int active) |
735 | { |
901 | { |
|
|
902 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
903 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
904 | |
736 | w->active = active; |
905 | w->active = active; |
737 | } |
906 | } |
738 | |
907 | |
739 | static void |
908 | static void |
740 | ev_stop (W w) |
909 | ev_stop (W w) |
… | |
… | |
745 | /*****************************************************************************/ |
914 | /*****************************************************************************/ |
746 | |
915 | |
747 | void |
916 | void |
748 | ev_io_start (struct ev_io *w) |
917 | ev_io_start (struct ev_io *w) |
749 | { |
918 | { |
|
|
919 | int fd = w->fd; |
|
|
920 | |
750 | if (ev_is_active (w)) |
921 | if (ev_is_active (w)) |
751 | return; |
922 | return; |
752 | |
923 | |
753 | int fd = w->fd; |
924 | assert (("ev_io_start called with negative fd", fd >= 0)); |
754 | |
925 | |
755 | ev_start ((W)w, 1); |
926 | ev_start ((W)w, 1); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
927 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
928 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
758 | |
929 | |
… | |
… | |
760 | } |
931 | } |
761 | |
932 | |
762 | void |
933 | void |
763 | ev_io_stop (struct ev_io *w) |
934 | ev_io_stop (struct ev_io *w) |
764 | { |
935 | { |
765 | ev_clear ((W)w); |
936 | ev_clear_pending ((W)w); |
766 | if (!ev_is_active (w)) |
937 | if (!ev_is_active (w)) |
767 | return; |
938 | return; |
768 | |
939 | |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
940 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
770 | ev_stop ((W)w); |
941 | ev_stop ((W)w); |
… | |
… | |
778 | if (ev_is_active (w)) |
949 | if (ev_is_active (w)) |
779 | return; |
950 | return; |
780 | |
951 | |
781 | w->at += now; |
952 | w->at += now; |
782 | |
953 | |
783 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
954 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
784 | |
955 | |
785 | ev_start ((W)w, ++timercnt); |
956 | ev_start ((W)w, ++timercnt); |
786 | array_needsize (timers, timermax, timercnt, ); |
957 | array_needsize (timers, timermax, timercnt, ); |
787 | timers [timercnt - 1] = w; |
958 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
959 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
960 | } |
790 | |
961 | |
791 | void |
962 | void |
792 | ev_timer_stop (struct ev_timer *w) |
963 | ev_timer_stop (struct ev_timer *w) |
793 | { |
964 | { |
794 | ev_clear ((W)w); |
965 | ev_clear_pending ((W)w); |
795 | if (!ev_is_active (w)) |
966 | if (!ev_is_active (w)) |
796 | return; |
967 | return; |
797 | |
968 | |
798 | if (w->active < timercnt--) |
969 | if (w->active < timercnt--) |
799 | { |
970 | { |
… | |
… | |
827 | ev_periodic_start (struct ev_periodic *w) |
998 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
999 | { |
829 | if (ev_is_active (w)) |
1000 | if (ev_is_active (w)) |
830 | return; |
1001 | return; |
831 | |
1002 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
1003 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
833 | |
1004 | |
834 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1005 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
835 | if (w->interval) |
1006 | if (w->interval) |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1007 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
837 | |
1008 | |
… | |
… | |
842 | } |
1013 | } |
843 | |
1014 | |
844 | void |
1015 | void |
845 | ev_periodic_stop (struct ev_periodic *w) |
1016 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
1017 | { |
847 | ev_clear ((W)w); |
1018 | ev_clear_pending ((W)w); |
848 | if (!ev_is_active (w)) |
1019 | if (!ev_is_active (w)) |
849 | return; |
1020 | return; |
850 | |
1021 | |
851 | if (w->active < periodiccnt--) |
1022 | if (w->active < periodiccnt--) |
852 | { |
1023 | { |
… | |
… | |
855 | } |
1026 | } |
856 | |
1027 | |
857 | ev_stop ((W)w); |
1028 | ev_stop ((W)w); |
858 | } |
1029 | } |
859 | |
1030 | |
|
|
1031 | #ifndef SA_RESTART |
|
|
1032 | # define SA_RESTART 0 |
|
|
1033 | #endif |
|
|
1034 | |
860 | void |
1035 | void |
861 | ev_signal_start (struct ev_signal *w) |
1036 | ev_signal_start (struct ev_signal *w) |
862 | { |
1037 | { |
863 | if (ev_is_active (w)) |
1038 | if (ev_is_active (w)) |
864 | return; |
1039 | return; |
|
|
1040 | |
|
|
1041 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
865 | |
1042 | |
866 | ev_start ((W)w, 1); |
1043 | ev_start ((W)w, 1); |
867 | array_needsize (signals, signalmax, w->signum, signals_init); |
1044 | array_needsize (signals, signalmax, w->signum, signals_init); |
868 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1045 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
869 | |
1046 | |
870 | if (!w->next) |
1047 | if (!w->next) |
871 | { |
1048 | { |
872 | struct sigaction sa; |
1049 | struct sigaction sa; |
873 | sa.sa_handler = sighandler; |
1050 | sa.sa_handler = sighandler; |
874 | sigfillset (&sa.sa_mask); |
1051 | sigfillset (&sa.sa_mask); |
875 | sa.sa_flags = 0; |
1052 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
876 | sigaction (w->signum, &sa, 0); |
1053 | sigaction (w->signum, &sa, 0); |
877 | } |
1054 | } |
878 | } |
1055 | } |
879 | |
1056 | |
880 | void |
1057 | void |
881 | ev_signal_stop (struct ev_signal *w) |
1058 | ev_signal_stop (struct ev_signal *w) |
882 | { |
1059 | { |
883 | ev_clear ((W)w); |
1060 | ev_clear_pending ((W)w); |
884 | if (!ev_is_active (w)) |
1061 | if (!ev_is_active (w)) |
885 | return; |
1062 | return; |
886 | |
1063 | |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1064 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | ev_stop ((W)w); |
1065 | ev_stop ((W)w); |
… | |
… | |
903 | } |
1080 | } |
904 | |
1081 | |
905 | void |
1082 | void |
906 | ev_idle_stop (struct ev_idle *w) |
1083 | ev_idle_stop (struct ev_idle *w) |
907 | { |
1084 | { |
908 | ev_clear ((W)w); |
1085 | ev_clear_pending ((W)w); |
909 | if (ev_is_active (w)) |
1086 | if (ev_is_active (w)) |
910 | return; |
1087 | return; |
911 | |
1088 | |
912 | idles [w->active - 1] = idles [--idlecnt]; |
1089 | idles [w->active - 1] = idles [--idlecnt]; |
913 | ev_stop ((W)w); |
1090 | ev_stop ((W)w); |
… | |
… | |
925 | } |
1102 | } |
926 | |
1103 | |
927 | void |
1104 | void |
928 | ev_prepare_stop (struct ev_prepare *w) |
1105 | ev_prepare_stop (struct ev_prepare *w) |
929 | { |
1106 | { |
930 | ev_clear ((W)w); |
1107 | ev_clear_pending ((W)w); |
931 | if (ev_is_active (w)) |
1108 | if (ev_is_active (w)) |
932 | return; |
1109 | return; |
933 | |
1110 | |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
1111 | prepares [w->active - 1] = prepares [--preparecnt]; |
935 | ev_stop ((W)w); |
1112 | ev_stop ((W)w); |
… | |
… | |
947 | } |
1124 | } |
948 | |
1125 | |
949 | void |
1126 | void |
950 | ev_check_stop (struct ev_check *w) |
1127 | ev_check_stop (struct ev_check *w) |
951 | { |
1128 | { |
952 | ev_clear ((W)w); |
1129 | ev_clear_pending ((W)w); |
953 | if (ev_is_active (w)) |
1130 | if (ev_is_active (w)) |
954 | return; |
1131 | return; |
955 | |
1132 | |
956 | checks [w->active - 1] = checks [--checkcnt]; |
1133 | checks [w->active - 1] = checks [--checkcnt]; |
957 | ev_stop ((W)w); |
1134 | ev_stop ((W)w); |
… | |
… | |
968 | } |
1145 | } |
969 | |
1146 | |
970 | void |
1147 | void |
971 | ev_child_stop (struct ev_child *w) |
1148 | ev_child_stop (struct ev_child *w) |
972 | { |
1149 | { |
973 | ev_clear ((W)w); |
1150 | ev_clear_pending ((W)w); |
974 | if (ev_is_active (w)) |
1151 | if (ev_is_active (w)) |
975 | return; |
1152 | return; |
976 | |
1153 | |
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1154 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
978 | ev_stop ((W)w); |
1155 | ev_stop ((W)w); |
… | |
… | |
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1194 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1018 | { |
1195 | { |
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1196 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1020 | |
1197 | |
1021 | if (!once) |
1198 | if (!once) |
1022 | cb (EV_ERROR, arg); |
1199 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1023 | else |
1200 | else |
1024 | { |
1201 | { |
1025 | once->cb = cb; |
1202 | once->cb = cb; |
1026 | once->arg = arg; |
1203 | once->arg = arg; |
1027 | |
1204 | |
1028 | ev_watcher_init (&once->io, once_cb_io); |
1205 | ev_watcher_init (&once->io, once_cb_io); |
1029 | |
|
|
1030 | if (fd >= 0) |
1206 | if (fd >= 0) |
1031 | { |
1207 | { |
1032 | ev_io_set (&once->io, fd, events); |
1208 | ev_io_set (&once->io, fd, events); |
1033 | ev_io_start (&once->io); |
1209 | ev_io_start (&once->io); |
1034 | } |
1210 | } |
1035 | |
1211 | |
1036 | ev_watcher_init (&once->to, once_cb_to); |
1212 | ev_watcher_init (&once->to, once_cb_to); |
1037 | |
|
|
1038 | if (timeout >= 0.) |
1213 | if (timeout >= 0.) |
1039 | { |
1214 | { |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1215 | ev_timer_set (&once->to, timeout, 0.); |
1041 | ev_timer_start (&once->to); |
1216 | ev_timer_start (&once->to); |
1042 | } |
1217 | } |