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> |
… | |
… | |
41 | #include <sys/types.h> |
46 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
47 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
48 | #include <sys/time.h> |
44 | #include <time.h> |
49 | #include <time.h> |
45 | |
50 | |
|
|
51 | /**/ |
|
|
52 | |
46 | #ifndef HAVE_MONOTONIC |
53 | #ifndef EV_USE_MONOTONIC |
|
|
54 | # define EV_USE_MONOTONIC 1 |
|
|
55 | #endif |
|
|
56 | |
|
|
57 | #ifndef EV_USE_SELECT |
|
|
58 | # define EV_USE_SELECT 1 |
|
|
59 | #endif |
|
|
60 | |
|
|
61 | #ifndef EV_USE_POLL |
|
|
62 | # define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ |
|
|
63 | #endif |
|
|
64 | |
|
|
65 | #ifndef EV_USE_EPOLL |
|
|
66 | # define EV_USE_EPOLL 0 |
|
|
67 | #endif |
|
|
68 | |
|
|
69 | #ifndef EV_USE_KQUEUE |
|
|
70 | # define EV_USE_KQUEUE 0 |
|
|
71 | #endif |
|
|
72 | |
|
|
73 | #ifndef EV_USE_REALTIME |
|
|
74 | # define EV_USE_REALTIME 1 |
|
|
75 | #endif |
|
|
76 | |
|
|
77 | /**/ |
|
|
78 | |
47 | # ifdef CLOCK_MONOTONIC |
79 | #ifndef CLOCK_MONOTONIC |
|
|
80 | # undef EV_USE_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
81 | # define EV_USE_MONOTONIC 0 |
49 | # endif |
82 | #endif |
50 | #endif |
|
|
51 | |
83 | |
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 |
84 | #ifndef CLOCK_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
85 | # undef EV_USE_REALTIME |
|
|
86 | # define EV_USE_REALTIME 0 |
62 | #endif |
87 | #endif |
|
|
88 | |
|
|
89 | /**/ |
63 | |
90 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
91 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
92 | #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 */ |
93 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
94 | /*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ |
67 | |
95 | |
68 | #include "ev.h" |
96 | #include "ev.h" |
|
|
97 | |
|
|
98 | #if __GNUC__ >= 3 |
|
|
99 | # define expect(expr,value) __builtin_expect ((expr),(value)) |
|
|
100 | # define inline inline |
|
|
101 | #else |
|
|
102 | # define expect(expr,value) (expr) |
|
|
103 | # define inline static |
|
|
104 | #endif |
|
|
105 | |
|
|
106 | #define expect_false(expr) expect ((expr) != 0, 0) |
|
|
107 | #define expect_true(expr) expect ((expr) != 0, 1) |
|
|
108 | |
|
|
109 | #define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) |
|
|
110 | #define ABSPRI(w) ((w)->priority - EV_MINPRI) |
69 | |
111 | |
70 | typedef struct ev_watcher *W; |
112 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
113 | typedef struct ev_watcher_list *WL; |
72 | typedef struct ev_watcher_time *WT; |
114 | typedef struct ev_watcher_time *WT; |
73 | |
115 | |
74 | static ev_tstamp now, diff; /* monotonic clock */ |
116 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
75 | ev_tstamp ev_now; |
117 | ev_tstamp ev_now; |
76 | int ev_method; |
118 | int ev_method; |
77 | |
119 | |
78 | static int have_monotonic; /* runtime */ |
120 | static int have_monotonic; /* runtime */ |
79 | |
121 | |
… | |
… | |
84 | /*****************************************************************************/ |
126 | /*****************************************************************************/ |
85 | |
127 | |
86 | ev_tstamp |
128 | ev_tstamp |
87 | ev_time (void) |
129 | ev_time (void) |
88 | { |
130 | { |
89 | #if HAVE_REALTIME |
131 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
132 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
133 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
134 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
135 | #else |
94 | struct timeval tv; |
136 | struct timeval tv; |
… | |
… | |
98 | } |
140 | } |
99 | |
141 | |
100 | static ev_tstamp |
142 | static ev_tstamp |
101 | get_clock (void) |
143 | get_clock (void) |
102 | { |
144 | { |
103 | #if HAVE_MONOTONIC |
145 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
146 | if (expect_true (have_monotonic)) |
105 | { |
147 | { |
106 | struct timespec ts; |
148 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
149 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
150 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
151 | } |
110 | #endif |
152 | #endif |
111 | |
153 | |
112 | return ev_time (); |
154 | return ev_time (); |
113 | } |
155 | } |
114 | |
156 | |
|
|
157 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
|
|
158 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
159 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
160 | if (expect_false ((cnt) > cur)) \ |
117 | { \ |
161 | { \ |
118 | int newcnt = cur; \ |
162 | int newcnt = cur; \ |
119 | do \ |
163 | do \ |
120 | { \ |
164 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
165 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
166 | } \ |
123 | while ((cnt) > newcnt); \ |
167 | while ((cnt) > newcnt); \ |
124 | \ |
168 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
169 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
170 | init (base + cur, newcnt - cur); \ |
… | |
… | |
130 | /*****************************************************************************/ |
174 | /*****************************************************************************/ |
131 | |
175 | |
132 | typedef struct |
176 | typedef struct |
133 | { |
177 | { |
134 | struct ev_io *head; |
178 | struct ev_io *head; |
135 | int events; |
179 | unsigned char events; |
|
|
180 | unsigned char reify; |
136 | } ANFD; |
181 | } ANFD; |
137 | |
182 | |
138 | static ANFD *anfds; |
183 | static ANFD *anfds; |
139 | static int anfdmax; |
184 | static int anfdmax; |
140 | |
185 | |
… | |
… | |
143 | { |
188 | { |
144 | while (count--) |
189 | while (count--) |
145 | { |
190 | { |
146 | base->head = 0; |
191 | base->head = 0; |
147 | base->events = EV_NONE; |
192 | base->events = EV_NONE; |
|
|
193 | base->reify = 0; |
|
|
194 | |
148 | ++base; |
195 | ++base; |
149 | } |
196 | } |
150 | } |
197 | } |
151 | |
198 | |
152 | typedef struct |
199 | typedef struct |
153 | { |
200 | { |
154 | W w; |
201 | W w; |
155 | int events; |
202 | int events; |
156 | } ANPENDING; |
203 | } ANPENDING; |
157 | |
204 | |
158 | static ANPENDING *pendings; |
205 | static ANPENDING *pendings [NUMPRI]; |
159 | static int pendingmax, pendingcnt; |
206 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
160 | |
207 | |
161 | static void |
208 | static void |
162 | event (W w, int events) |
209 | event (W w, int events) |
163 | { |
210 | { |
164 | if (w->active) |
211 | if (w->pending) |
165 | { |
212 | { |
166 | w->pending = ++pendingcnt; |
|
|
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
|
|
168 | pendings [pendingcnt - 1].w = w; |
|
|
169 | pendings [pendingcnt - 1].events = events; |
213 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
214 | return; |
170 | } |
215 | } |
|
|
216 | |
|
|
217 | w->pending = ++pendingcnt [ABSPRI (w)]; |
|
|
218 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
|
|
219 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
|
|
220 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
171 | } |
221 | } |
172 | |
222 | |
173 | static void |
223 | static void |
174 | queue_events (W *events, int eventcnt, int type) |
224 | queue_events (W *events, int eventcnt, int type) |
175 | { |
225 | { |
… | |
… | |
213 | int events = 0; |
263 | int events = 0; |
214 | |
264 | |
215 | for (w = anfd->head; w; w = w->next) |
265 | for (w = anfd->head; w; w = w->next) |
216 | events |= w->events; |
266 | events |= w->events; |
217 | |
267 | |
218 | anfd->events &= ~EV_REIFY; |
268 | anfd->reify = 0; |
219 | |
269 | |
220 | if (anfd->events != events) |
270 | if (anfd->events != events) |
221 | { |
271 | { |
222 | method_modify (fd, anfd->events, events); |
272 | method_modify (fd, anfd->events, events); |
223 | anfd->events = events; |
273 | anfd->events = events; |
… | |
… | |
228 | } |
278 | } |
229 | |
279 | |
230 | static void |
280 | static void |
231 | fd_change (int fd) |
281 | fd_change (int fd) |
232 | { |
282 | { |
233 | if (anfds [fd].events & EV_REIFY) |
283 | if (anfds [fd].reify || fdchangecnt < 0) |
234 | return; |
284 | return; |
235 | |
285 | |
236 | anfds [fd].events |= EV_REIFY; |
286 | anfds [fd].reify = 1; |
237 | |
287 | |
238 | ++fdchangecnt; |
288 | ++fdchangecnt; |
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
289 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
240 | fdchanges [fdchangecnt - 1] = fd; |
290 | fdchanges [fdchangecnt - 1] = fd; |
241 | } |
291 | } |
242 | |
292 | |
|
|
293 | static void |
|
|
294 | fd_kill (int fd) |
|
|
295 | { |
|
|
296 | struct ev_io *w; |
|
|
297 | |
|
|
298 | printf ("killing fd %d\n", fd);//D |
|
|
299 | while ((w = anfds [fd].head)) |
|
|
300 | { |
|
|
301 | ev_io_stop (w); |
|
|
302 | event ((W)w, EV_ERROR | EV_READ | EV_WRITE); |
|
|
303 | } |
|
|
304 | } |
|
|
305 | |
243 | /* called on EBADF to verify fds */ |
306 | /* called on EBADF to verify fds */ |
244 | static void |
307 | static void |
245 | fd_recheck (void) |
308 | fd_ebadf (void) |
246 | { |
309 | { |
247 | int fd; |
310 | int fd; |
248 | |
311 | |
249 | for (fd = 0; fd < anfdmax; ++fd) |
312 | for (fd = 0; fd < anfdmax; ++fd) |
250 | if (anfds [fd].events) |
313 | if (anfds [fd].events) |
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
314 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
252 | while (anfds [fd].head) |
315 | fd_kill (fd); |
|
|
316 | } |
|
|
317 | |
|
|
318 | /* called on ENOMEM in select/poll to kill some fds and retry */ |
|
|
319 | static void |
|
|
320 | fd_enomem (void) |
|
|
321 | { |
|
|
322 | int fd = anfdmax; |
|
|
323 | |
|
|
324 | while (fd--) |
|
|
325 | if (anfds [fd].events) |
253 | { |
326 | { |
254 | event ((W)anfds [fd].head, EV_ERROR); |
327 | close (fd); |
255 | ev_io_stop (anfds [fd].head); |
328 | fd_kill (fd); |
|
|
329 | return; |
256 | } |
330 | } |
257 | } |
331 | } |
258 | |
332 | |
259 | /*****************************************************************************/ |
333 | /*****************************************************************************/ |
260 | |
334 | |
261 | static struct ev_timer **timers; |
335 | static struct ev_timer **timers; |
… | |
… | |
308 | /*****************************************************************************/ |
382 | /*****************************************************************************/ |
309 | |
383 | |
310 | typedef struct |
384 | typedef struct |
311 | { |
385 | { |
312 | struct ev_signal *head; |
386 | struct ev_signal *head; |
313 | sig_atomic_t gotsig; |
387 | sig_atomic_t volatile gotsig; |
314 | } ANSIG; |
388 | } ANSIG; |
315 | |
389 | |
316 | static ANSIG *signals; |
390 | static ANSIG *signals; |
317 | static int signalmax; |
391 | static int signalmax; |
318 | |
392 | |
319 | static int sigpipe [2]; |
393 | static int sigpipe [2]; |
320 | static sig_atomic_t gotsig; |
394 | static sig_atomic_t volatile gotsig; |
321 | static struct ev_io sigev; |
395 | static struct ev_io sigev; |
322 | |
396 | |
323 | static void |
397 | static void |
324 | signals_init (ANSIG *base, int count) |
398 | signals_init (ANSIG *base, int count) |
325 | { |
399 | { |
326 | while (count--) |
400 | while (count--) |
327 | { |
401 | { |
328 | base->head = 0; |
402 | base->head = 0; |
329 | base->gotsig = 0; |
403 | base->gotsig = 0; |
|
|
404 | |
330 | ++base; |
405 | ++base; |
331 | } |
406 | } |
332 | } |
407 | } |
333 | |
408 | |
334 | static void |
409 | static void |
… | |
… | |
337 | signals [signum - 1].gotsig = 1; |
412 | signals [signum - 1].gotsig = 1; |
338 | |
413 | |
339 | if (!gotsig) |
414 | if (!gotsig) |
340 | { |
415 | { |
341 | gotsig = 1; |
416 | gotsig = 1; |
342 | write (sigpipe [1], &gotsig, 1); |
417 | write (sigpipe [1], &signum, 1); |
343 | } |
418 | } |
344 | } |
419 | } |
345 | |
420 | |
346 | static void |
421 | static void |
347 | sigcb (struct ev_io *iow, int revents) |
422 | sigcb (struct ev_io *iow, int revents) |
348 | { |
423 | { |
349 | struct ev_signal *w; |
424 | struct ev_signal *w; |
350 | int sig; |
425 | int signum; |
351 | |
426 | |
|
|
427 | read (sigpipe [0], &revents, 1); |
352 | gotsig = 0; |
428 | gotsig = 0; |
353 | read (sigpipe [0], &revents, 1); |
|
|
354 | |
429 | |
355 | for (sig = signalmax; sig--; ) |
430 | for (signum = signalmax; signum--; ) |
356 | if (signals [sig].gotsig) |
431 | if (signals [signum].gotsig) |
357 | { |
432 | { |
358 | signals [sig].gotsig = 0; |
433 | signals [signum].gotsig = 0; |
359 | |
434 | |
360 | for (w = signals [sig].head; w; w = w->next) |
435 | for (w = signals [signum].head; w; w = w->next) |
361 | event ((W)w, EV_SIGNAL); |
436 | event ((W)w, EV_SIGNAL); |
362 | } |
437 | } |
363 | } |
438 | } |
364 | |
439 | |
365 | static void |
440 | static void |
… | |
… | |
402 | struct ev_child *w; |
477 | struct ev_child *w; |
403 | int pid, status; |
478 | int pid, status; |
404 | |
479 | |
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
480 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
481 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
407 | if (w->pid == pid || w->pid == -1) |
482 | if (w->pid == pid || !w->pid) |
408 | { |
483 | { |
409 | w->status = status; |
484 | w->status = status; |
410 | event ((W)w, EV_CHILD); |
485 | event ((W)w, EV_CHILD); |
411 | } |
486 | } |
412 | } |
487 | } |
413 | |
488 | |
414 | /*****************************************************************************/ |
489 | /*****************************************************************************/ |
415 | |
490 | |
|
|
491 | #if EV_USE_KQUEUE |
|
|
492 | # include "ev_kqueue.c" |
|
|
493 | #endif |
416 | #if HAVE_EPOLL |
494 | #if EV_USE_EPOLL |
417 | # include "ev_epoll.c" |
495 | # include "ev_epoll.c" |
418 | #endif |
496 | #endif |
|
|
497 | #if EV_USE_POLL |
|
|
498 | # include "ev_poll.c" |
|
|
499 | #endif |
419 | #if HAVE_SELECT |
500 | #if EV_USE_SELECT |
420 | # include "ev_select.c" |
501 | # include "ev_select.c" |
421 | #endif |
502 | #endif |
422 | |
503 | |
423 | int |
504 | int |
424 | ev_version_major (void) |
505 | ev_version_major (void) |
… | |
… | |
430 | ev_version_minor (void) |
511 | ev_version_minor (void) |
431 | { |
512 | { |
432 | return EV_VERSION_MINOR; |
513 | return EV_VERSION_MINOR; |
433 | } |
514 | } |
434 | |
515 | |
|
|
516 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
517 | static int |
|
|
518 | enable_secure () |
|
|
519 | { |
|
|
520 | return getuid () != geteuid () |
|
|
521 | || getgid () != getegid (); |
|
|
522 | } |
|
|
523 | |
435 | int ev_init (int flags) |
524 | int ev_init (int methods) |
436 | { |
525 | { |
437 | if (!ev_method) |
526 | if (!ev_method) |
438 | { |
527 | { |
439 | #if HAVE_MONOTONIC |
528 | #if EV_USE_MONOTONIC |
440 | { |
529 | { |
441 | struct timespec ts; |
530 | struct timespec ts; |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
531 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
443 | have_monotonic = 1; |
532 | have_monotonic = 1; |
444 | } |
533 | } |
445 | #endif |
534 | #endif |
446 | |
535 | |
447 | ev_now = ev_time (); |
536 | ev_now = ev_time (); |
448 | now = get_clock (); |
537 | now = get_clock (); |
|
|
538 | now_floor = now; |
449 | diff = ev_now - now; |
539 | diff = ev_now - now; |
450 | |
540 | |
451 | if (pipe (sigpipe)) |
541 | if (pipe (sigpipe)) |
452 | return 0; |
542 | return 0; |
453 | |
543 | |
|
|
544 | if (methods == EVMETHOD_AUTO) |
|
|
545 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
546 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
547 | else |
454 | ev_method = EVMETHOD_NONE; |
548 | methods = EVMETHOD_ANY; |
|
|
549 | |
|
|
550 | ev_method = 0; |
|
|
551 | #if EV_USE_KQUEUE |
|
|
552 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
553 | #endif |
455 | #if HAVE_EPOLL |
554 | #if EV_USE_EPOLL |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
555 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
457 | #endif |
556 | #endif |
|
|
557 | #if EV_USE_POLL |
|
|
558 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
|
|
559 | #endif |
458 | #if HAVE_SELECT |
560 | #if EV_USE_SELECT |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
561 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
460 | #endif |
562 | #endif |
461 | |
563 | |
462 | if (ev_method) |
564 | if (ev_method) |
463 | { |
565 | { |
464 | ev_watcher_init (&sigev, sigcb); |
566 | ev_watcher_init (&sigev, sigcb); |
… | |
… | |
473 | } |
575 | } |
474 | |
576 | |
475 | /*****************************************************************************/ |
577 | /*****************************************************************************/ |
476 | |
578 | |
477 | void |
579 | void |
478 | ev_prefork (void) |
580 | ev_fork_prepare (void) |
479 | { |
581 | { |
480 | /* nop */ |
582 | /* nop */ |
481 | } |
583 | } |
482 | |
584 | |
483 | void |
585 | void |
484 | ev_postfork_parent (void) |
586 | ev_fork_parent (void) |
485 | { |
587 | { |
486 | /* nop */ |
588 | /* nop */ |
487 | } |
589 | } |
488 | |
590 | |
489 | void |
591 | void |
490 | ev_postfork_child (void) |
592 | ev_fork_child (void) |
491 | { |
593 | { |
492 | #if HAVE_EPOLL |
594 | #if EV_USE_EPOLL |
493 | if (ev_method == EVMETHOD_EPOLL) |
595 | if (ev_method == EVMETHOD_EPOLL) |
494 | epoll_postfork_child (); |
596 | epoll_postfork_child (); |
495 | #endif |
597 | #endif |
496 | |
598 | |
497 | ev_io_stop (&sigev); |
599 | ev_io_stop (&sigev); |
… | |
… | |
504 | /*****************************************************************************/ |
606 | /*****************************************************************************/ |
505 | |
607 | |
506 | static void |
608 | static void |
507 | call_pending (void) |
609 | call_pending (void) |
508 | { |
610 | { |
|
|
611 | int pri; |
|
|
612 | |
|
|
613 | for (pri = NUMPRI; pri--; ) |
509 | while (pendingcnt) |
614 | while (pendingcnt [pri]) |
510 | { |
615 | { |
511 | ANPENDING *p = pendings + --pendingcnt; |
616 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
512 | |
617 | |
513 | if (p->w) |
618 | if (p->w) |
514 | { |
619 | { |
515 | p->w->pending = 0; |
620 | p->w->pending = 0; |
516 | p->w->cb (p->w, p->events); |
621 | p->w->cb (p->w, p->events); |
517 | } |
622 | } |
518 | } |
623 | } |
519 | } |
624 | } |
520 | |
625 | |
521 | static void |
626 | static void |
522 | timers_reify (void) |
627 | timers_reify (void) |
523 | { |
628 | { |
524 | while (timercnt && timers [0]->at <= now) |
629 | while (timercnt && timers [0]->at <= now) |
525 | { |
630 | { |
526 | struct ev_timer *w = timers [0]; |
631 | struct ev_timer *w = timers [0]; |
527 | |
|
|
528 | event ((W)w, EV_TIMEOUT); |
|
|
529 | |
632 | |
530 | /* first reschedule or stop timer */ |
633 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
634 | if (w->repeat) |
532 | { |
635 | { |
|
|
636 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
637 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
638 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
639 | } |
537 | else |
640 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
641 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
642 | |
|
|
643 | event ((W)w, EV_TIMEOUT); |
539 | } |
644 | } |
540 | } |
645 | } |
541 | |
646 | |
542 | static void |
647 | static void |
543 | periodics_reify (void) |
648 | periodics_reify (void) |
… | |
… | |
548 | |
653 | |
549 | /* first reschedule or stop timer */ |
654 | /* first reschedule or stop timer */ |
550 | if (w->interval) |
655 | if (w->interval) |
551 | { |
656 | { |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
657 | 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)); |
658 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
659 | downheap ((WT *)periodics, periodiccnt, 0); |
555 | } |
660 | } |
556 | else |
661 | else |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
662 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
558 | |
663 | |
559 | event ((W)w, EV_TIMEOUT); |
664 | event ((W)w, EV_PERIODIC); |
560 | } |
665 | } |
561 | } |
666 | } |
562 | |
667 | |
563 | static void |
668 | static void |
564 | periodics_reschedule (ev_tstamp diff) |
669 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
583 | } |
688 | } |
584 | } |
689 | } |
585 | } |
690 | } |
586 | } |
691 | } |
587 | |
692 | |
|
|
693 | static int |
|
|
694 | time_update_monotonic (void) |
|
|
695 | { |
|
|
696 | now = get_clock (); |
|
|
697 | |
|
|
698 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
699 | { |
|
|
700 | ev_now = now + diff; |
|
|
701 | return 0; |
|
|
702 | } |
|
|
703 | else |
|
|
704 | { |
|
|
705 | now_floor = now; |
|
|
706 | ev_now = ev_time (); |
|
|
707 | return 1; |
|
|
708 | } |
|
|
709 | } |
|
|
710 | |
588 | static void |
711 | static void |
589 | time_update (void) |
712 | time_update (void) |
590 | { |
713 | { |
591 | int i; |
714 | int i; |
592 | |
715 | |
593 | ev_now = ev_time (); |
716 | #if EV_USE_MONOTONIC |
594 | |
|
|
595 | if (have_monotonic) |
717 | if (expect_true (have_monotonic)) |
596 | { |
718 | { |
|
|
719 | if (time_update_monotonic ()) |
|
|
720 | { |
597 | ev_tstamp odiff = diff; |
721 | ev_tstamp odiff = diff; |
598 | |
722 | |
599 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
723 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
600 | { |
724 | { |
601 | now = get_clock (); |
|
|
602 | diff = ev_now - now; |
725 | diff = ev_now - now; |
603 | |
726 | |
604 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
727 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
605 | return; /* all is well */ |
728 | return; /* all is well */ |
606 | |
729 | |
607 | ev_now = ev_time (); |
730 | ev_now = ev_time (); |
|
|
731 | now = get_clock (); |
|
|
732 | now_floor = now; |
608 | } |
733 | } |
609 | |
734 | |
610 | periodics_reschedule (diff - odiff); |
735 | periodics_reschedule (diff - odiff); |
611 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
736 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
737 | } |
612 | } |
738 | } |
613 | else |
739 | else |
|
|
740 | #endif |
614 | { |
741 | { |
|
|
742 | ev_now = ev_time (); |
|
|
743 | |
615 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
744 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
616 | { |
745 | { |
617 | periodics_reschedule (ev_now - now); |
746 | periodics_reschedule (ev_now - now); |
618 | |
747 | |
619 | /* adjust timers. this is easy, as the offset is the same for all */ |
748 | /* adjust timers. this is easy, as the offset is the same for all */ |
620 | for (i = 0; i < timercnt; ++i) |
749 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
762 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
634 | |
763 | |
635 | do |
764 | do |
636 | { |
765 | { |
637 | /* queue check watchers (and execute them) */ |
766 | /* queue check watchers (and execute them) */ |
638 | if (preparecnt) |
767 | if (expect_false (preparecnt)) |
639 | { |
768 | { |
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
769 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
641 | call_pending (); |
770 | call_pending (); |
642 | } |
771 | } |
643 | |
772 | |
… | |
… | |
646 | |
775 | |
647 | /* calculate blocking time */ |
776 | /* calculate blocking time */ |
648 | |
777 | |
649 | /* we only need this for !monotonic clockor timers, but as we basically |
778 | /* we only need this for !monotonic clockor timers, but as we basically |
650 | always have timers, we just calculate it always */ |
779 | always have timers, we just calculate it always */ |
|
|
780 | #if EV_USE_MONOTONIC |
|
|
781 | if (expect_true (have_monotonic)) |
|
|
782 | time_update_monotonic (); |
|
|
783 | else |
|
|
784 | #endif |
|
|
785 | { |
651 | ev_now = ev_time (); |
786 | ev_now = ev_time (); |
|
|
787 | now = ev_now; |
|
|
788 | } |
652 | |
789 | |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
790 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
654 | block = 0.; |
791 | block = 0.; |
655 | else |
792 | else |
656 | { |
793 | { |
657 | block = MAX_BLOCKTIME; |
794 | block = MAX_BLOCKTIME; |
658 | |
795 | |
659 | if (timercnt) |
796 | if (timercnt) |
660 | { |
797 | { |
661 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
798 | ev_tstamp to = timers [0]->at - now + method_fudge; |
662 | if (block > to) block = to; |
799 | if (block > to) block = to; |
663 | } |
800 | } |
664 | |
801 | |
665 | if (periodiccnt) |
802 | if (periodiccnt) |
666 | { |
803 | { |
… | |
… | |
719 | head = &(*head)->next; |
856 | head = &(*head)->next; |
720 | } |
857 | } |
721 | } |
858 | } |
722 | |
859 | |
723 | static void |
860 | static void |
724 | ev_clear (W w) |
861 | ev_clear_pending (W w) |
725 | { |
862 | { |
726 | if (w->pending) |
863 | if (w->pending) |
727 | { |
864 | { |
728 | pendings [w->pending - 1].w = 0; |
865 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
729 | w->pending = 0; |
866 | w->pending = 0; |
730 | } |
867 | } |
731 | } |
868 | } |
732 | |
869 | |
733 | static void |
870 | static void |
734 | ev_start (W w, int active) |
871 | ev_start (W w, int active) |
735 | { |
872 | { |
|
|
873 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
874 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
875 | |
736 | w->active = active; |
876 | w->active = active; |
737 | } |
877 | } |
738 | |
878 | |
739 | static void |
879 | static void |
740 | ev_stop (W w) |
880 | ev_stop (W w) |
… | |
… | |
745 | /*****************************************************************************/ |
885 | /*****************************************************************************/ |
746 | |
886 | |
747 | void |
887 | void |
748 | ev_io_start (struct ev_io *w) |
888 | ev_io_start (struct ev_io *w) |
749 | { |
889 | { |
|
|
890 | int fd = w->fd; |
|
|
891 | |
750 | if (ev_is_active (w)) |
892 | if (ev_is_active (w)) |
751 | return; |
893 | return; |
752 | |
894 | |
753 | int fd = w->fd; |
895 | assert (("ev_io_start called with negative fd", fd >= 0)); |
754 | |
896 | |
755 | ev_start ((W)w, 1); |
897 | ev_start ((W)w, 1); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
898 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
899 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
758 | |
900 | |
… | |
… | |
760 | } |
902 | } |
761 | |
903 | |
762 | void |
904 | void |
763 | ev_io_stop (struct ev_io *w) |
905 | ev_io_stop (struct ev_io *w) |
764 | { |
906 | { |
765 | ev_clear ((W)w); |
907 | ev_clear_pending ((W)w); |
766 | if (!ev_is_active (w)) |
908 | if (!ev_is_active (w)) |
767 | return; |
909 | return; |
768 | |
910 | |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
911 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
770 | ev_stop ((W)w); |
912 | ev_stop ((W)w); |
… | |
… | |
778 | if (ev_is_active (w)) |
920 | if (ev_is_active (w)) |
779 | return; |
921 | return; |
780 | |
922 | |
781 | w->at += now; |
923 | w->at += now; |
782 | |
924 | |
783 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
925 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
784 | |
926 | |
785 | ev_start ((W)w, ++timercnt); |
927 | ev_start ((W)w, ++timercnt); |
786 | array_needsize (timers, timermax, timercnt, ); |
928 | array_needsize (timers, timermax, timercnt, ); |
787 | timers [timercnt - 1] = w; |
929 | timers [timercnt - 1] = w; |
788 | upheap ((WT *)timers, timercnt - 1); |
930 | upheap ((WT *)timers, timercnt - 1); |
789 | } |
931 | } |
790 | |
932 | |
791 | void |
933 | void |
792 | ev_timer_stop (struct ev_timer *w) |
934 | ev_timer_stop (struct ev_timer *w) |
793 | { |
935 | { |
794 | ev_clear ((W)w); |
936 | ev_clear_pending ((W)w); |
795 | if (!ev_is_active (w)) |
937 | if (!ev_is_active (w)) |
796 | return; |
938 | return; |
797 | |
939 | |
798 | if (w->active < timercnt--) |
940 | if (w->active < timercnt--) |
799 | { |
941 | { |
… | |
… | |
827 | ev_periodic_start (struct ev_periodic *w) |
969 | ev_periodic_start (struct ev_periodic *w) |
828 | { |
970 | { |
829 | if (ev_is_active (w)) |
971 | if (ev_is_active (w)) |
830 | return; |
972 | return; |
831 | |
973 | |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
974 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
833 | |
975 | |
834 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
976 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
835 | if (w->interval) |
977 | if (w->interval) |
836 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
978 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
837 | |
979 | |
… | |
… | |
842 | } |
984 | } |
843 | |
985 | |
844 | void |
986 | void |
845 | ev_periodic_stop (struct ev_periodic *w) |
987 | ev_periodic_stop (struct ev_periodic *w) |
846 | { |
988 | { |
847 | ev_clear ((W)w); |
989 | ev_clear_pending ((W)w); |
848 | if (!ev_is_active (w)) |
990 | if (!ev_is_active (w)) |
849 | return; |
991 | return; |
850 | |
992 | |
851 | if (w->active < periodiccnt--) |
993 | if (w->active < periodiccnt--) |
852 | { |
994 | { |
… | |
… | |
860 | void |
1002 | void |
861 | ev_signal_start (struct ev_signal *w) |
1003 | ev_signal_start (struct ev_signal *w) |
862 | { |
1004 | { |
863 | if (ev_is_active (w)) |
1005 | if (ev_is_active (w)) |
864 | return; |
1006 | return; |
|
|
1007 | |
|
|
1008 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
865 | |
1009 | |
866 | ev_start ((W)w, 1); |
1010 | ev_start ((W)w, 1); |
867 | array_needsize (signals, signalmax, w->signum, signals_init); |
1011 | array_needsize (signals, signalmax, w->signum, signals_init); |
868 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1012 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
869 | |
1013 | |
… | |
… | |
878 | } |
1022 | } |
879 | |
1023 | |
880 | void |
1024 | void |
881 | ev_signal_stop (struct ev_signal *w) |
1025 | ev_signal_stop (struct ev_signal *w) |
882 | { |
1026 | { |
883 | ev_clear ((W)w); |
1027 | ev_clear_pending ((W)w); |
884 | if (!ev_is_active (w)) |
1028 | if (!ev_is_active (w)) |
885 | return; |
1029 | return; |
886 | |
1030 | |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1031 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | ev_stop ((W)w); |
1032 | ev_stop ((W)w); |
… | |
… | |
903 | } |
1047 | } |
904 | |
1048 | |
905 | void |
1049 | void |
906 | ev_idle_stop (struct ev_idle *w) |
1050 | ev_idle_stop (struct ev_idle *w) |
907 | { |
1051 | { |
908 | ev_clear ((W)w); |
1052 | ev_clear_pending ((W)w); |
909 | if (ev_is_active (w)) |
1053 | if (ev_is_active (w)) |
910 | return; |
1054 | return; |
911 | |
1055 | |
912 | idles [w->active - 1] = idles [--idlecnt]; |
1056 | idles [w->active - 1] = idles [--idlecnt]; |
913 | ev_stop ((W)w); |
1057 | ev_stop ((W)w); |
… | |
… | |
925 | } |
1069 | } |
926 | |
1070 | |
927 | void |
1071 | void |
928 | ev_prepare_stop (struct ev_prepare *w) |
1072 | ev_prepare_stop (struct ev_prepare *w) |
929 | { |
1073 | { |
930 | ev_clear ((W)w); |
1074 | ev_clear_pending ((W)w); |
931 | if (ev_is_active (w)) |
1075 | if (ev_is_active (w)) |
932 | return; |
1076 | return; |
933 | |
1077 | |
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
1078 | prepares [w->active - 1] = prepares [--preparecnt]; |
935 | ev_stop ((W)w); |
1079 | ev_stop ((W)w); |
… | |
… | |
947 | } |
1091 | } |
948 | |
1092 | |
949 | void |
1093 | void |
950 | ev_check_stop (struct ev_check *w) |
1094 | ev_check_stop (struct ev_check *w) |
951 | { |
1095 | { |
952 | ev_clear ((W)w); |
1096 | ev_clear_pending ((W)w); |
953 | if (ev_is_active (w)) |
1097 | if (ev_is_active (w)) |
954 | return; |
1098 | return; |
955 | |
1099 | |
956 | checks [w->active - 1] = checks [--checkcnt]; |
1100 | checks [w->active - 1] = checks [--checkcnt]; |
957 | ev_stop ((W)w); |
1101 | ev_stop ((W)w); |
… | |
… | |
968 | } |
1112 | } |
969 | |
1113 | |
970 | void |
1114 | void |
971 | ev_child_stop (struct ev_child *w) |
1115 | ev_child_stop (struct ev_child *w) |
972 | { |
1116 | { |
973 | ev_clear ((W)w); |
1117 | ev_clear_pending ((W)w); |
974 | if (ev_is_active (w)) |
1118 | if (ev_is_active (w)) |
975 | return; |
1119 | return; |
976 | |
1120 | |
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1121 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
978 | ev_stop ((W)w); |
1122 | ev_stop ((W)w); |
… | |
… | |
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1161 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1018 | { |
1162 | { |
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1163 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1020 | |
1164 | |
1021 | if (!once) |
1165 | if (!once) |
1022 | cb (EV_ERROR, arg); |
1166 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1023 | else |
1167 | else |
1024 | { |
1168 | { |
1025 | once->cb = cb; |
1169 | once->cb = cb; |
1026 | once->arg = arg; |
1170 | once->arg = arg; |
1027 | |
1171 | |
1028 | ev_watcher_init (&once->io, once_cb_io); |
1172 | ev_watcher_init (&once->io, once_cb_io); |
1029 | |
|
|
1030 | if (fd >= 0) |
1173 | if (fd >= 0) |
1031 | { |
1174 | { |
1032 | ev_io_set (&once->io, fd, events); |
1175 | ev_io_set (&once->io, fd, events); |
1033 | ev_io_start (&once->io); |
1176 | ev_io_start (&once->io); |
1034 | } |
1177 | } |
1035 | |
1178 | |
1036 | ev_watcher_init (&once->to, once_cb_to); |
1179 | ev_watcher_init (&once->to, once_cb_to); |
1037 | |
|
|
1038 | if (timeout >= 0.) |
1180 | if (timeout >= 0.) |
1039 | { |
1181 | { |
1040 | ev_timer_set (&once->to, timeout, 0.); |
1182 | ev_timer_set (&once->to, timeout, 0.); |
1041 | ev_timer_start (&once->to); |
1183 | ev_timer_start (&once->to); |
1042 | } |
1184 | } |