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 |
… | |
… | |
336 | { |
413 | { |
337 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
338 | |
415 | |
339 | if (!gotsig) |
416 | if (!gotsig) |
340 | { |
417 | { |
|
|
418 | int old_errno = errno; |
341 | gotsig = 1; |
419 | gotsig = 1; |
342 | write (sigpipe [1], &gotsig, 1); |
420 | write (sigpipe [1], &signum, 1); |
|
|
421 | errno = old_errno; |
343 | } |
422 | } |
344 | } |
423 | } |
345 | |
424 | |
346 | static void |
425 | static void |
347 | sigcb (struct ev_io *iow, int revents) |
426 | sigcb (struct ev_io *iow, int revents) |
348 | { |
427 | { |
349 | struct ev_signal *w; |
428 | struct ev_signal *w; |
350 | int sig; |
429 | int signum; |
351 | |
430 | |
|
|
431 | read (sigpipe [0], &revents, 1); |
352 | gotsig = 0; |
432 | gotsig = 0; |
353 | read (sigpipe [0], &revents, 1); |
|
|
354 | |
433 | |
355 | for (sig = signalmax; sig--; ) |
434 | for (signum = signalmax; signum--; ) |
356 | if (signals [sig].gotsig) |
435 | if (signals [signum].gotsig) |
357 | { |
436 | { |
358 | signals [sig].gotsig = 0; |
437 | signals [signum].gotsig = 0; |
359 | |
438 | |
360 | for (w = signals [sig].head; w; w = w->next) |
439 | for (w = signals [signum].head; w; w = w->next) |
361 | event ((W)w, EV_SIGNAL); |
440 | event ((W)w, EV_SIGNAL); |
362 | } |
441 | } |
363 | } |
442 | } |
364 | |
443 | |
365 | static void |
444 | static void |
366 | siginit (void) |
445 | siginit (void) |
367 | { |
446 | { |
|
|
447 | #ifndef WIN32 |
368 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
448 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
369 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
449 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
370 | |
450 | |
371 | /* rather than sort out wether we really need nb, set it */ |
451 | /* rather than sort out wether we really need nb, set it */ |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
452 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
453 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
454 | #endif |
374 | |
455 | |
375 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
456 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
376 | ev_io_start (&sigev); |
457 | ev_io_start (&sigev); |
377 | } |
458 | } |
378 | |
459 | |
… | |
… | |
390 | /*****************************************************************************/ |
471 | /*****************************************************************************/ |
391 | |
472 | |
392 | static struct ev_child *childs [PID_HASHSIZE]; |
473 | static struct ev_child *childs [PID_HASHSIZE]; |
393 | static struct ev_signal childev; |
474 | static struct ev_signal childev; |
394 | |
475 | |
|
|
476 | #ifndef WIN32 |
|
|
477 | |
395 | #ifndef WCONTINUED |
478 | #ifndef WCONTINUED |
396 | # define WCONTINUED 0 |
479 | # define WCONTINUED 0 |
397 | #endif |
480 | #endif |
398 | |
481 | |
399 | static void |
482 | static void |
|
|
483 | child_reap (struct ev_signal *sw, int chain, int pid, int status) |
|
|
484 | { |
|
|
485 | struct ev_child *w; |
|
|
486 | |
|
|
487 | for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
488 | if (w->pid == pid || !w->pid) |
|
|
489 | { |
|
|
490 | w->priority = sw->priority; /* need to do it *now* */ |
|
|
491 | w->rpid = pid; |
|
|
492 | w->rstatus = status; |
|
|
493 | printf ("rpid %p %d %d\n", w, pid, w->pid);//D |
|
|
494 | event ((W)w, EV_CHILD); |
|
|
495 | } |
|
|
496 | } |
|
|
497 | |
|
|
498 | static void |
400 | childcb (struct ev_signal *sw, int revents) |
499 | childcb (struct ev_signal *sw, int revents) |
401 | { |
500 | { |
402 | struct ev_child *w; |
|
|
403 | int pid, status; |
501 | int pid, status; |
404 | |
502 | |
|
|
503 | printf ("chld %x\n", revents);//D |
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
504 | if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED))) |
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
505 | { |
407 | if (w->pid == pid || w->pid == -1) |
506 | /* make sure we are called again until all childs have been reaped */ |
408 | { |
|
|
409 | w->status = status; |
|
|
410 | event ((W)w, EV_CHILD); |
507 | event ((W)sw, EV_SIGNAL); |
411 | } |
508 | |
|
|
509 | child_reap (sw, pid, pid, status); |
|
|
510 | child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */ |
|
|
511 | } |
412 | } |
512 | } |
|
|
513 | |
|
|
514 | #endif |
413 | |
515 | |
414 | /*****************************************************************************/ |
516 | /*****************************************************************************/ |
415 | |
517 | |
|
|
518 | #if EV_USE_KQUEUE |
|
|
519 | # include "ev_kqueue.c" |
|
|
520 | #endif |
416 | #if HAVE_EPOLL |
521 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
522 | # include "ev_epoll.c" |
418 | #endif |
523 | #endif |
|
|
524 | #if EV_USE_POLL |
|
|
525 | # include "ev_poll.c" |
|
|
526 | #endif |
419 | #if HAVE_SELECT |
527 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
528 | # include "ev_select.c" |
421 | #endif |
529 | #endif |
422 | |
530 | |
423 | int |
531 | int |
424 | ev_version_major (void) |
532 | ev_version_major (void) |
… | |
… | |
430 | ev_version_minor (void) |
538 | ev_version_minor (void) |
431 | { |
539 | { |
432 | return EV_VERSION_MINOR; |
540 | return EV_VERSION_MINOR; |
433 | } |
541 | } |
434 | |
542 | |
|
|
543 | /* return true if we are running with elevated privileges and should ignore env variables */ |
|
|
544 | static int |
|
|
545 | enable_secure () |
|
|
546 | { |
|
|
547 | #ifdef WIN32 |
|
|
548 | return 0; |
|
|
549 | #else |
|
|
550 | return getuid () != geteuid () |
|
|
551 | || getgid () != getegid (); |
|
|
552 | #endif |
|
|
553 | } |
|
|
554 | |
435 | int ev_init (int flags) |
555 | int ev_init (int methods) |
436 | { |
556 | { |
437 | if (!ev_method) |
557 | if (!ev_method) |
438 | { |
558 | { |
439 | #if HAVE_MONOTONIC |
559 | #if EV_USE_MONOTONIC |
440 | { |
560 | { |
441 | struct timespec ts; |
561 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
562 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
563 | have_monotonic = 1; |
444 | } |
564 | } |
445 | #endif |
565 | #endif |
446 | |
566 | |
447 | ev_now = ev_time (); |
567 | ev_now = ev_time (); |
448 | now = get_clock (); |
568 | now = get_clock (); |
|
|
569 | now_floor = now; |
449 | diff = ev_now - now; |
570 | diff = ev_now - now; |
450 | |
571 | |
451 | if (pipe (sigpipe)) |
572 | if (pipe (sigpipe)) |
452 | return 0; |
573 | return 0; |
453 | |
574 | |
|
|
575 | if (methods == EVMETHOD_AUTO) |
|
|
576 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
577 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
578 | else |
454 | ev_method = EVMETHOD_NONE; |
579 | methods = EVMETHOD_ANY; |
|
|
580 | |
|
|
581 | ev_method = 0; |
|
|
582 | #if EV_USE_KQUEUE |
|
|
583 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
584 | #endif |
455 | #if HAVE_EPOLL |
585 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
586 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
457 | #endif |
587 | #endif |
|
|
588 | #if EV_USE_POLL |
|
|
589 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
|
|
590 | #endif |
458 | #if HAVE_SELECT |
591 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
592 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
460 | #endif |
593 | #endif |
461 | |
594 | |
462 | if (ev_method) |
595 | if (ev_method) |
463 | { |
596 | { |
464 | ev_watcher_init (&sigev, sigcb); |
597 | ev_watcher_init (&sigev, sigcb); |
|
|
598 | ev_set_priority (&sigev, EV_MAXPRI); |
465 | siginit (); |
599 | siginit (); |
466 | |
600 | |
|
|
601 | #ifndef WIN32 |
467 | ev_signal_init (&childev, childcb, SIGCHLD); |
602 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
603 | ev_set_priority (&childev, EV_MAXPRI); |
468 | ev_signal_start (&childev); |
604 | ev_signal_start (&childev); |
|
|
605 | #endif |
469 | } |
606 | } |
470 | } |
607 | } |
471 | |
608 | |
472 | return ev_method; |
609 | return ev_method; |
473 | } |
610 | } |
474 | |
611 | |
475 | /*****************************************************************************/ |
612 | /*****************************************************************************/ |
476 | |
613 | |
477 | void |
614 | void |
478 | ev_prefork (void) |
615 | ev_fork_prepare (void) |
479 | { |
616 | { |
480 | /* nop */ |
617 | /* nop */ |
481 | } |
618 | } |
482 | |
619 | |
483 | void |
620 | void |
484 | ev_postfork_parent (void) |
621 | ev_fork_parent (void) |
485 | { |
622 | { |
486 | /* nop */ |
623 | /* nop */ |
487 | } |
624 | } |
488 | |
625 | |
489 | void |
626 | void |
490 | ev_postfork_child (void) |
627 | ev_fork_child (void) |
491 | { |
628 | { |
492 | #if HAVE_EPOLL |
629 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
630 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
631 | epoll_postfork_child (); |
495 | #endif |
632 | #endif |
496 | |
633 | |
497 | ev_io_stop (&sigev); |
634 | ev_io_stop (&sigev); |
… | |
… | |
504 | /*****************************************************************************/ |
641 | /*****************************************************************************/ |
505 | |
642 | |
506 | static void |
643 | static void |
507 | call_pending (void) |
644 | call_pending (void) |
508 | { |
645 | { |
|
|
646 | int pri; |
|
|
647 | |
|
|
648 | for (pri = NUMPRI; pri--; ) |
509 | while (pendingcnt) |
649 | while (pendingcnt [pri]) |
510 | { |
650 | { |
511 | ANPENDING *p = pendings + --pendingcnt; |
651 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
512 | |
652 | |
513 | if (p->w) |
653 | if (p->w) |
514 | { |
654 | { |
515 | p->w->pending = 0; |
655 | p->w->pending = 0; |
516 | p->w->cb (p->w, p->events); |
656 | p->w->cb (p->w, p->events); |
517 | } |
657 | } |
518 | } |
658 | } |
519 | } |
659 | } |
520 | |
660 | |
521 | static void |
661 | static void |
522 | timers_reify (void) |
662 | timers_reify (void) |
523 | { |
663 | { |
524 | while (timercnt && timers [0]->at <= now) |
664 | while (timercnt && timers [0]->at <= now) |
525 | { |
665 | { |
526 | struct ev_timer *w = timers [0]; |
666 | struct ev_timer *w = timers [0]; |
527 | |
|
|
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
667 | |
530 | /* first reschedule or stop timer */ |
668 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
669 | if (w->repeat) |
532 | { |
670 | { |
|
|
671 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
672 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
673 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
674 | } |
537 | else |
675 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
676 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
677 | |
|
|
678 | event ((W)w, EV_TIMEOUT); |
539 | } |
679 | } |
540 | } |
680 | } |
541 | |
681 | |
542 | static void |
682 | static void |
543 | periodics_reify (void) |
683 | periodics_reify (void) |
… | |
… | |
548 | |
688 | |
549 | /* first reschedule or stop timer */ |
689 | /* first reschedule or stop timer */ |
550 | if (w->interval) |
690 | if (w->interval) |
551 | { |
691 | { |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
692 | 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)); |
693 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
694 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
695 | } |
556 | else |
696 | else |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
697 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
698 | |
559 | event ((W)w, EV_TIMEOUT); |
699 | event ((W)w, EV_PERIODIC); |
560 | } |
700 | } |
561 | } |
701 | } |
562 | |
702 | |
563 | static void |
703 | static void |
564 | periodics_reschedule (ev_tstamp diff) |
704 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
583 | } |
723 | } |
584 | } |
724 | } |
585 | } |
725 | } |
586 | } |
726 | } |
587 | |
727 | |
|
|
728 | static int |
|
|
729 | time_update_monotonic (void) |
|
|
730 | { |
|
|
731 | now = get_clock (); |
|
|
732 | |
|
|
733 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
734 | { |
|
|
735 | ev_now = now + diff; |
|
|
736 | return 0; |
|
|
737 | } |
|
|
738 | else |
|
|
739 | { |
|
|
740 | now_floor = now; |
|
|
741 | ev_now = ev_time (); |
|
|
742 | return 1; |
|
|
743 | } |
|
|
744 | } |
|
|
745 | |
588 | static void |
746 | static void |
589 | time_update (void) |
747 | time_update (void) |
590 | { |
748 | { |
591 | int i; |
749 | int i; |
592 | |
750 | |
593 | ev_now = ev_time (); |
751 | #if EV_USE_MONOTONIC |
594 | |
|
|
595 | if (have_monotonic) |
752 | if (expect_true (have_monotonic)) |
596 | { |
753 | { |
597 | ev_tstamp odiff = diff; |
754 | if (time_update_monotonic ()) |
598 | |
|
|
599 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
600 | { |
755 | { |
601 | now = get_clock (); |
756 | ev_tstamp odiff = diff; |
|
|
757 | |
|
|
758 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
759 | { |
602 | diff = ev_now - now; |
760 | diff = ev_now - now; |
603 | |
761 | |
604 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
762 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
605 | return; /* all is well */ |
763 | return; /* all is well */ |
606 | |
764 | |
607 | ev_now = ev_time (); |
765 | ev_now = ev_time (); |
|
|
766 | now = get_clock (); |
|
|
767 | now_floor = now; |
|
|
768 | } |
|
|
769 | |
|
|
770 | periodics_reschedule (diff - odiff); |
|
|
771 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
608 | } |
772 | } |
609 | |
|
|
610 | periodics_reschedule (diff - odiff); |
|
|
611 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
612 | } |
773 | } |
613 | else |
774 | else |
|
|
775 | #endif |
614 | { |
776 | { |
|
|
777 | ev_now = ev_time (); |
|
|
778 | |
615 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
779 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
616 | { |
780 | { |
617 | periodics_reschedule (ev_now - now); |
781 | periodics_reschedule (ev_now - now); |
618 | |
782 | |
619 | /* adjust timers. this is easy, as the offset is the same for all */ |
783 | /* adjust timers. this is easy, as the offset is the same for all */ |
620 | for (i = 0; i < timercnt; ++i) |
784 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
797 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
634 | |
798 | |
635 | do |
799 | do |
636 | { |
800 | { |
637 | /* queue check watchers (and execute them) */ |
801 | /* queue check watchers (and execute them) */ |
638 | if (preparecnt) |
802 | if (expect_false (preparecnt)) |
639 | { |
803 | { |
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
804 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
641 | call_pending (); |
805 | call_pending (); |
642 | } |
806 | } |
643 | |
807 | |
… | |
… | |
646 | |
810 | |
647 | /* calculate blocking time */ |
811 | /* calculate blocking time */ |
648 | |
812 | |
649 | /* we only need this for !monotonic clockor timers, but as we basically |
813 | /* we only need this for !monotonic clockor timers, but as we basically |
650 | always have timers, we just calculate it always */ |
814 | always have timers, we just calculate it always */ |
|
|
815 | #if EV_USE_MONOTONIC |
|
|
816 | if (expect_true (have_monotonic)) |
|
|
817 | time_update_monotonic (); |
|
|
818 | else |
|
|
819 | #endif |
|
|
820 | { |
651 | ev_now = ev_time (); |
821 | ev_now = ev_time (); |
|
|
822 | now = ev_now; |
|
|
823 | } |
652 | |
824 | |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
825 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
654 | block = 0.; |
826 | block = 0.; |
655 | else |
827 | else |
656 | { |
828 | { |
657 | block = MAX_BLOCKTIME; |
829 | block = MAX_BLOCKTIME; |
658 | |
830 | |
659 | if (timercnt) |
831 | if (timercnt) |
660 | { |
832 | { |
661 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
833 | ev_tstamp to = timers [0]->at - now + method_fudge; |
662 | if (block > to) block = to; |
834 | if (block > to) block = to; |
663 | } |
835 | } |
664 | |
836 | |
665 | if (periodiccnt) |
837 | if (periodiccnt) |
666 | { |
838 | { |
… | |
… | |
719 | head = &(*head)->next; |
891 | head = &(*head)->next; |
720 | } |
892 | } |
721 | } |
893 | } |
722 | |
894 | |
723 | static void |
895 | static void |
724 | ev_clear (W w) |
896 | ev_clear_pending (W w) |
725 | { |
897 | { |
726 | if (w->pending) |
898 | if (w->pending) |
727 | { |
899 | { |
728 | pendings [w->pending - 1].w = 0; |
900 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
729 | w->pending = 0; |
901 | w->pending = 0; |
730 | } |
902 | } |
731 | } |
903 | } |
732 | |
904 | |
733 | static void |
905 | static void |
734 | ev_start (W w, int active) |
906 | ev_start (W w, int active) |
735 | { |
907 | { |
|
|
908 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
909 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
910 | |
736 | w->active = active; |
911 | w->active = active; |
737 | } |
912 | } |
738 | |
913 | |
739 | static void |
914 | static void |
740 | ev_stop (W w) |
915 | ev_stop (W w) |
… | |
… | |
745 | /*****************************************************************************/ |
920 | /*****************************************************************************/ |
746 | |
921 | |
747 | void |
922 | void |
748 | ev_io_start (struct ev_io *w) |
923 | ev_io_start (struct ev_io *w) |
749 | { |
924 | { |
|
|
925 | int fd = w->fd; |
|
|
926 | |
750 | if (ev_is_active (w)) |
927 | if (ev_is_active (w)) |
751 | return; |
928 | return; |
752 | |
929 | |
753 | int fd = w->fd; |
930 | assert (("ev_io_start called with negative fd", fd >= 0)); |
754 | |
931 | |
755 | ev_start ((W)w, 1); |
932 | ev_start ((W)w, 1); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
933 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
934 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
758 | |
935 | |
… | |
… | |
760 | } |
937 | } |
761 | |
938 | |
762 | void |
939 | void |
763 | ev_io_stop (struct ev_io *w) |
940 | ev_io_stop (struct ev_io *w) |
764 | { |
941 | { |
765 | ev_clear ((W)w); |
942 | ev_clear_pending ((W)w); |
766 | if (!ev_is_active (w)) |
943 | if (!ev_is_active (w)) |
767 | return; |
944 | return; |
768 | |
945 | |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
946 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
770 | ev_stop ((W)w); |
947 | ev_stop ((W)w); |
… | |
… | |
778 | if (ev_is_active (w)) |
955 | if (ev_is_active (w)) |
779 | return; |
956 | return; |
780 | |
957 | |
781 | w->at += now; |
958 | w->at += now; |
782 | |
959 | |
783 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
960 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
784 | |
961 | |
785 | ev_start ((W)w, ++timercnt); |
962 | ev_start ((W)w, ++timercnt); |
786 | array_needsize (timers, timermax, timercnt, ); |
963 | array_needsize (timers, timermax, timercnt, ); |
787 | timers [timercnt - 1] = w; |
964 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
965 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
966 | } |
790 | |
967 | |
791 | void |
968 | void |
792 | ev_timer_stop (struct ev_timer *w) |
969 | ev_timer_stop (struct ev_timer *w) |
793 | { |
970 | { |
794 | ev_clear ((W)w); |
971 | ev_clear_pending ((W)w); |
795 | if (!ev_is_active (w)) |
972 | if (!ev_is_active (w)) |
796 | return; |
973 | return; |
797 | |
974 | |
798 | if (w->active < timercnt--) |
975 | if (w->active < timercnt--) |
799 | { |
976 | { |
… | |
… | |
827 | ev_periodic_start (struct ev_periodic *w) |
1004 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
1005 | { |
829 | if (ev_is_active (w)) |
1006 | if (ev_is_active (w)) |
830 | return; |
1007 | return; |
831 | |
1008 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
1009 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
833 | |
1010 | |
834 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
1011 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
835 | if (w->interval) |
1012 | if (w->interval) |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
1013 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
837 | |
1014 | |
… | |
… | |
842 | } |
1019 | } |
843 | |
1020 | |
844 | void |
1021 | void |
845 | ev_periodic_stop (struct ev_periodic *w) |
1022 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
1023 | { |
847 | ev_clear ((W)w); |
1024 | ev_clear_pending ((W)w); |
848 | if (!ev_is_active (w)) |
1025 | if (!ev_is_active (w)) |
849 | return; |
1026 | return; |
850 | |
1027 | |
851 | if (w->active < periodiccnt--) |
1028 | if (w->active < periodiccnt--) |
852 | { |
1029 | { |
… | |
… | |
855 | } |
1032 | } |
856 | |
1033 | |
857 | ev_stop ((W)w); |
1034 | ev_stop ((W)w); |
858 | } |
1035 | } |
859 | |
1036 | |
|
|
1037 | #ifndef SA_RESTART |
|
|
1038 | # define SA_RESTART 0 |
|
|
1039 | #endif |
|
|
1040 | |
860 | void |
1041 | void |
861 | ev_signal_start (struct ev_signal *w) |
1042 | ev_signal_start (struct ev_signal *w) |
862 | { |
1043 | { |
863 | if (ev_is_active (w)) |
1044 | if (ev_is_active (w)) |
864 | return; |
1045 | return; |
|
|
1046 | |
|
|
1047 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
865 | |
1048 | |
866 | ev_start ((W)w, 1); |
1049 | ev_start ((W)w, 1); |
867 | array_needsize (signals, signalmax, w->signum, signals_init); |
1050 | array_needsize (signals, signalmax, w->signum, signals_init); |
868 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1051 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
869 | |
1052 | |
870 | if (!w->next) |
1053 | if (!w->next) |
871 | { |
1054 | { |
872 | struct sigaction sa; |
1055 | struct sigaction sa; |
873 | sa.sa_handler = sighandler; |
1056 | sa.sa_handler = sighandler; |
874 | sigfillset (&sa.sa_mask); |
1057 | sigfillset (&sa.sa_mask); |
875 | sa.sa_flags = 0; |
1058 | sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ |
876 | sigaction (w->signum, &sa, 0); |
1059 | sigaction (w->signum, &sa, 0); |
877 | } |
1060 | } |
878 | } |
1061 | } |
879 | |
1062 | |
880 | void |
1063 | void |
881 | ev_signal_stop (struct ev_signal *w) |
1064 | ev_signal_stop (struct ev_signal *w) |
882 | { |
1065 | { |
883 | ev_clear ((W)w); |
1066 | ev_clear_pending ((W)w); |
884 | if (!ev_is_active (w)) |
1067 | if (!ev_is_active (w)) |
885 | return; |
1068 | return; |
886 | |
1069 | |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1070 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | ev_stop ((W)w); |
1071 | ev_stop ((W)w); |
… | |
… | |
903 | } |
1086 | } |
904 | |
1087 | |
905 | void |
1088 | void |
906 | ev_idle_stop (struct ev_idle *w) |
1089 | ev_idle_stop (struct ev_idle *w) |
907 | { |
1090 | { |
908 | ev_clear ((W)w); |
1091 | ev_clear_pending ((W)w); |
909 | if (ev_is_active (w)) |
1092 | if (ev_is_active (w)) |
910 | return; |
1093 | return; |
911 | |
1094 | |
912 | idles [w->active - 1] = idles [--idlecnt]; |
1095 | idles [w->active - 1] = idles [--idlecnt]; |
913 | ev_stop ((W)w); |
1096 | ev_stop ((W)w); |
… | |
… | |
925 | } |
1108 | } |
926 | |
1109 | |
927 | void |
1110 | void |
928 | ev_prepare_stop (struct ev_prepare *w) |
1111 | ev_prepare_stop (struct ev_prepare *w) |
929 | { |
1112 | { |
930 | ev_clear ((W)w); |
1113 | ev_clear_pending ((W)w); |
931 | if (ev_is_active (w)) |
1114 | if (ev_is_active (w)) |
932 | return; |
1115 | return; |
933 | |
1116 | |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
1117 | prepares [w->active - 1] = prepares [--preparecnt]; |
935 | ev_stop ((W)w); |
1118 | ev_stop ((W)w); |
… | |
… | |
947 | } |
1130 | } |
948 | |
1131 | |
949 | void |
1132 | void |
950 | ev_check_stop (struct ev_check *w) |
1133 | ev_check_stop (struct ev_check *w) |
951 | { |
1134 | { |
952 | ev_clear ((W)w); |
1135 | ev_clear_pending ((W)w); |
953 | if (ev_is_active (w)) |
1136 | if (ev_is_active (w)) |
954 | return; |
1137 | return; |
955 | |
1138 | |
956 | checks [w->active - 1] = checks [--checkcnt]; |
1139 | checks [w->active - 1] = checks [--checkcnt]; |
957 | ev_stop ((W)w); |
1140 | ev_stop ((W)w); |
… | |
… | |
968 | } |
1151 | } |
969 | |
1152 | |
970 | void |
1153 | void |
971 | ev_child_stop (struct ev_child *w) |
1154 | ev_child_stop (struct ev_child *w) |
972 | { |
1155 | { |
973 | ev_clear ((W)w); |
1156 | ev_clear_pending ((W)w); |
974 | if (ev_is_active (w)) |
1157 | if (ev_is_active (w)) |
975 | return; |
1158 | return; |
976 | |
1159 | |
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1160 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
978 | ev_stop ((W)w); |
1161 | ev_stop ((W)w); |
… | |
… | |
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1200 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1018 | { |
1201 | { |
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1202 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1020 | |
1203 | |
1021 | if (!once) |
1204 | if (!once) |
1022 | cb (EV_ERROR, arg); |
1205 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1023 | else |
1206 | else |
1024 | { |
1207 | { |
1025 | once->cb = cb; |
1208 | once->cb = cb; |
1026 | once->arg = arg; |
1209 | once->arg = arg; |
1027 | |
1210 | |
1028 | ev_watcher_init (&once->io, once_cb_io); |
1211 | ev_watcher_init (&once->io, once_cb_io); |
1029 | |
|
|
1030 | if (fd >= 0) |
1212 | if (fd >= 0) |
1031 | { |
1213 | { |
1032 | ev_io_set (&once->io, fd, events); |
1214 | ev_io_set (&once->io, fd, events); |
1033 | ev_io_start (&once->io); |
1215 | ev_io_start (&once->io); |
1034 | } |
1216 | } |
1035 | |
1217 | |
1036 | ev_watcher_init (&once->to, once_cb_to); |
1218 | ev_watcher_init (&once->to, once_cb_to); |
1037 | |
|
|
1038 | if (timeout >= 0.) |
1219 | if (timeout >= 0.) |
1039 | { |
1220 | { |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1221 | ev_timer_set (&once->to, timeout, 0.); |
1041 | ev_timer_start (&once->to); |
1222 | ev_timer_start (&once->to); |
1042 | } |
1223 | } |