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 |
… | |
… | |
40 | #include <stdio.h> |
42 | #include <stdio.h> |
41 | |
43 | |
42 | #include <assert.h> |
44 | #include <assert.h> |
43 | #include <errno.h> |
45 | #include <errno.h> |
44 | #include <sys/types.h> |
46 | #include <sys/types.h> |
|
|
47 | #ifndef WIN32 |
45 | #include <sys/wait.h> |
48 | # include <sys/wait.h> |
|
|
49 | #endif |
46 | #include <sys/time.h> |
50 | #include <sys/time.h> |
47 | #include <time.h> |
51 | #include <time.h> |
48 | |
52 | |
|
|
53 | /**/ |
|
|
54 | |
49 | #ifndef EV_USE_MONOTONIC |
55 | #ifndef EV_USE_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
|
|
51 | # define EV_USE_MONOTONIC 1 |
56 | # define EV_USE_MONOTONIC 1 |
52 | # endif |
|
|
53 | #endif |
57 | #endif |
54 | |
58 | |
55 | #ifndef EV_USE_SELECT |
59 | #ifndef EV_USE_SELECT |
56 | # define EV_USE_SELECT 1 |
60 | # define EV_USE_SELECT 1 |
57 | #endif |
61 | #endif |
58 | |
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 | |
59 | #ifndef EV_USE_EPOLL |
67 | #ifndef EV_USE_EPOLL |
60 | # define EV_USE_EPOLL 0 |
68 | # define EV_USE_EPOLL 0 |
61 | #endif |
69 | #endif |
62 | |
70 | |
|
|
71 | #ifndef EV_USE_KQUEUE |
|
|
72 | # define EV_USE_KQUEUE 0 |
|
|
73 | #endif |
|
|
74 | |
63 | #ifndef EV_USE_REALTIME |
75 | #ifndef EV_USE_REALTIME |
64 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
76 | # define EV_USE_REALTIME 1 |
65 | #endif |
77 | #endif |
|
|
78 | |
|
|
79 | /**/ |
|
|
80 | |
|
|
81 | #ifndef CLOCK_MONOTONIC |
|
|
82 | # undef EV_USE_MONOTONIC |
|
|
83 | # define EV_USE_MONOTONIC 0 |
|
|
84 | #endif |
|
|
85 | |
|
|
86 | #ifndef CLOCK_REALTIME |
|
|
87 | # undef EV_USE_REALTIME |
|
|
88 | # define EV_USE_REALTIME 0 |
|
|
89 | #endif |
|
|
90 | |
|
|
91 | /**/ |
66 | |
92 | |
67 | #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) */ |
68 | #define MAX_BLOCKTIME 59.731 |
94 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ |
69 | #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 */ |
70 | |
97 | |
71 | #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) |
72 | |
113 | |
73 | typedef struct ev_watcher *W; |
114 | typedef struct ev_watcher *W; |
74 | typedef struct ev_watcher_list *WL; |
115 | typedef struct ev_watcher_list *WL; |
75 | typedef struct ev_watcher_time *WT; |
116 | typedef struct ev_watcher_time *WT; |
76 | |
117 | |
77 | static ev_tstamp now, diff; /* monotonic clock */ |
118 | static ev_tstamp now_floor, now, diff; /* monotonic clock */ |
78 | ev_tstamp ev_now; |
119 | ev_tstamp ev_now; |
79 | int ev_method; |
120 | int ev_method; |
80 | |
121 | |
81 | static int have_monotonic; /* runtime */ |
122 | static int have_monotonic; /* runtime */ |
82 | |
123 | |
… | |
… | |
102 | |
143 | |
103 | static ev_tstamp |
144 | static ev_tstamp |
104 | get_clock (void) |
145 | get_clock (void) |
105 | { |
146 | { |
106 | #if EV_USE_MONOTONIC |
147 | #if EV_USE_MONOTONIC |
107 | if (have_monotonic) |
148 | if (expect_true (have_monotonic)) |
108 | { |
149 | { |
109 | struct timespec ts; |
150 | struct timespec ts; |
110 | clock_gettime (CLOCK_MONOTONIC, &ts); |
151 | clock_gettime (CLOCK_MONOTONIC, &ts); |
111 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
152 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
112 | } |
153 | } |
… | |
… | |
116 | } |
157 | } |
117 | |
158 | |
118 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
159 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
119 | |
160 | |
120 | #define array_needsize(base,cur,cnt,init) \ |
161 | #define array_needsize(base,cur,cnt,init) \ |
121 | if ((cnt) > cur) \ |
162 | if (expect_false ((cnt) > cur)) \ |
122 | { \ |
163 | { \ |
123 | int newcnt = cur; \ |
164 | int newcnt = cur; \ |
124 | do \ |
165 | do \ |
125 | { \ |
166 | { \ |
126 | newcnt = array_roundsize (base, newcnt << 1); \ |
167 | newcnt = array_roundsize (base, newcnt << 1); \ |
… | |
… | |
135 | /*****************************************************************************/ |
176 | /*****************************************************************************/ |
136 | |
177 | |
137 | typedef struct |
178 | typedef struct |
138 | { |
179 | { |
139 | struct ev_io *head; |
180 | struct ev_io *head; |
140 | int events; |
181 | unsigned char events; |
|
|
182 | unsigned char reify; |
141 | } ANFD; |
183 | } ANFD; |
142 | |
184 | |
143 | static ANFD *anfds; |
185 | static ANFD *anfds; |
144 | static int anfdmax; |
186 | static int anfdmax; |
145 | |
187 | |
… | |
… | |
148 | { |
190 | { |
149 | while (count--) |
191 | while (count--) |
150 | { |
192 | { |
151 | base->head = 0; |
193 | base->head = 0; |
152 | base->events = EV_NONE; |
194 | base->events = EV_NONE; |
|
|
195 | base->reify = 0; |
|
|
196 | |
153 | ++base; |
197 | ++base; |
154 | } |
198 | } |
155 | } |
199 | } |
156 | |
200 | |
157 | typedef struct |
201 | typedef struct |
158 | { |
202 | { |
159 | W w; |
203 | W w; |
160 | int events; |
204 | int events; |
161 | } ANPENDING; |
205 | } ANPENDING; |
162 | |
206 | |
163 | static ANPENDING *pendings; |
207 | static ANPENDING *pendings [NUMPRI]; |
164 | static int pendingmax, pendingcnt; |
208 | static int pendingmax [NUMPRI], pendingcnt [NUMPRI]; |
165 | |
209 | |
166 | static void |
210 | static void |
167 | event (W w, int events) |
211 | event (W w, int events) |
168 | { |
212 | { |
|
|
213 | if (w->pending) |
|
|
214 | { |
|
|
215 | pendings [ABSPRI (w)][w->pending - 1].events |= events; |
|
|
216 | return; |
|
|
217 | } |
|
|
218 | |
169 | w->pending = ++pendingcnt; |
219 | w->pending = ++pendingcnt [ABSPRI (w)]; |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
220 | array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); |
171 | pendings [pendingcnt - 1].w = w; |
221 | pendings [ABSPRI (w)][w->pending - 1].w = w; |
172 | pendings [pendingcnt - 1].events = events; |
222 | pendings [ABSPRI (w)][w->pending - 1].events = events; |
173 | } |
223 | } |
174 | |
224 | |
175 | static void |
225 | static void |
176 | queue_events (W *events, int eventcnt, int type) |
226 | queue_events (W *events, int eventcnt, int type) |
177 | { |
227 | { |
… | |
… | |
215 | int events = 0; |
265 | int events = 0; |
216 | |
266 | |
217 | for (w = anfd->head; w; w = w->next) |
267 | for (w = anfd->head; w; w = w->next) |
218 | events |= w->events; |
268 | events |= w->events; |
219 | |
269 | |
220 | anfd->events &= ~EV_REIFY; |
270 | anfd->reify = 0; |
221 | |
271 | |
222 | if (anfd->events != events) |
272 | if (anfd->events != events) |
223 | { |
273 | { |
224 | method_modify (fd, anfd->events, events); |
274 | method_modify (fd, anfd->events, events); |
225 | anfd->events = events; |
275 | anfd->events = events; |
… | |
… | |
230 | } |
280 | } |
231 | |
281 | |
232 | static void |
282 | static void |
233 | fd_change (int fd) |
283 | fd_change (int fd) |
234 | { |
284 | { |
235 | if (anfds [fd].events & EV_REIFY || fdchangecnt < 0) |
285 | if (anfds [fd].reify || fdchangecnt < 0) |
236 | return; |
286 | return; |
237 | |
287 | |
238 | anfds [fd].events |= EV_REIFY; |
288 | anfds [fd].reify = 1; |
239 | |
289 | |
240 | ++fdchangecnt; |
290 | ++fdchangecnt; |
241 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
291 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
242 | fdchanges [fdchangecnt - 1] = fd; |
292 | fdchanges [fdchangecnt - 1] = fd; |
243 | } |
293 | } |
244 | |
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 | |
245 | /* called on EBADF to verify fds */ |
308 | /* called on EBADF to verify fds */ |
246 | static void |
309 | static void |
247 | fd_recheck (void) |
310 | fd_ebadf (void) |
248 | { |
311 | { |
249 | int fd; |
312 | int fd; |
250 | |
313 | |
251 | for (fd = 0; fd < anfdmax; ++fd) |
314 | for (fd = 0; fd < anfdmax; ++fd) |
252 | if (anfds [fd].events) |
315 | if (anfds [fd].events) |
253 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
316 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
254 | 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) |
255 | { |
328 | { |
256 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT); |
329 | close (fd); |
257 | ev_io_stop (anfds [fd].head); |
330 | fd_kill (fd); |
|
|
331 | return; |
258 | } |
332 | } |
259 | } |
333 | } |
260 | |
334 | |
261 | /*****************************************************************************/ |
335 | /*****************************************************************************/ |
262 | |
336 | |
263 | static struct ev_timer **timers; |
337 | static struct ev_timer **timers; |
… | |
… | |
310 | /*****************************************************************************/ |
384 | /*****************************************************************************/ |
311 | |
385 | |
312 | typedef struct |
386 | typedef struct |
313 | { |
387 | { |
314 | struct ev_signal *head; |
388 | struct ev_signal *head; |
315 | sig_atomic_t gotsig; |
389 | sig_atomic_t volatile gotsig; |
316 | } ANSIG; |
390 | } ANSIG; |
317 | |
391 | |
318 | static ANSIG *signals; |
392 | static ANSIG *signals; |
319 | static int signalmax; |
393 | static int signalmax; |
320 | |
394 | |
321 | static int sigpipe [2]; |
395 | static int sigpipe [2]; |
322 | static sig_atomic_t gotsig; |
396 | static sig_atomic_t volatile gotsig; |
323 | static struct ev_io sigev; |
397 | static struct ev_io sigev; |
324 | |
398 | |
325 | static void |
399 | static void |
326 | signals_init (ANSIG *base, int count) |
400 | signals_init (ANSIG *base, int count) |
327 | { |
401 | { |
328 | while (count--) |
402 | while (count--) |
329 | { |
403 | { |
330 | base->head = 0; |
404 | base->head = 0; |
331 | base->gotsig = 0; |
405 | base->gotsig = 0; |
|
|
406 | |
332 | ++base; |
407 | ++base; |
333 | } |
408 | } |
334 | } |
409 | } |
335 | |
410 | |
336 | static void |
411 | static void |
… | |
… | |
339 | signals [signum - 1].gotsig = 1; |
414 | signals [signum - 1].gotsig = 1; |
340 | |
415 | |
341 | if (!gotsig) |
416 | if (!gotsig) |
342 | { |
417 | { |
343 | gotsig = 1; |
418 | gotsig = 1; |
344 | write (sigpipe [1], &gotsig, 1); |
419 | write (sigpipe [1], &signum, 1); |
345 | } |
420 | } |
346 | } |
421 | } |
347 | |
422 | |
348 | static void |
423 | static void |
349 | sigcb (struct ev_io *iow, int revents) |
424 | sigcb (struct ev_io *iow, int revents) |
350 | { |
425 | { |
351 | struct ev_signal *w; |
426 | struct ev_signal *w; |
352 | int sig; |
427 | int signum; |
353 | |
428 | |
|
|
429 | read (sigpipe [0], &revents, 1); |
354 | gotsig = 0; |
430 | gotsig = 0; |
355 | read (sigpipe [0], &revents, 1); |
|
|
356 | |
431 | |
357 | for (sig = signalmax; sig--; ) |
432 | for (signum = signalmax; signum--; ) |
358 | if (signals [sig].gotsig) |
433 | if (signals [signum].gotsig) |
359 | { |
434 | { |
360 | signals [sig].gotsig = 0; |
435 | signals [signum].gotsig = 0; |
361 | |
436 | |
362 | for (w = signals [sig].head; w; w = w->next) |
437 | for (w = signals [signum].head; w; w = w->next) |
363 | event ((W)w, EV_SIGNAL); |
438 | event ((W)w, EV_SIGNAL); |
364 | } |
439 | } |
365 | } |
440 | } |
366 | |
441 | |
367 | static void |
442 | static void |
368 | siginit (void) |
443 | siginit (void) |
369 | { |
444 | { |
|
|
445 | #ifndef WIN32 |
370 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
446 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
371 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
447 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
372 | |
448 | |
373 | /* rather than sort out wether we really need nb, set it */ |
449 | /* rather than sort out wether we really need nb, set it */ |
374 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
450 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
375 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
451 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
452 | #endif |
376 | |
453 | |
377 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
454 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
378 | ev_io_start (&sigev); |
455 | ev_io_start (&sigev); |
379 | } |
456 | } |
380 | |
457 | |
… | |
… | |
392 | /*****************************************************************************/ |
469 | /*****************************************************************************/ |
393 | |
470 | |
394 | static struct ev_child *childs [PID_HASHSIZE]; |
471 | static struct ev_child *childs [PID_HASHSIZE]; |
395 | static struct ev_signal childev; |
472 | static struct ev_signal childev; |
396 | |
473 | |
|
|
474 | #ifndef WIN32 |
|
|
475 | |
397 | #ifndef WCONTINUED |
476 | #ifndef WCONTINUED |
398 | # define WCONTINUED 0 |
477 | # define WCONTINUED 0 |
399 | #endif |
478 | #endif |
400 | |
479 | |
401 | static void |
480 | static void |
… | |
… | |
404 | struct ev_child *w; |
483 | struct ev_child *w; |
405 | int pid, status; |
484 | int pid, status; |
406 | |
485 | |
407 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
486 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
408 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
487 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
409 | if (w->pid == pid || w->pid == -1) |
488 | if (w->pid == pid || !w->pid) |
410 | { |
489 | { |
411 | w->status = status; |
490 | w->status = status; |
412 | event ((W)w, EV_CHILD); |
491 | event ((W)w, EV_CHILD); |
413 | } |
492 | } |
414 | } |
493 | } |
415 | |
494 | |
416 | /*****************************************************************************/ |
495 | #endif |
417 | |
496 | |
|
|
497 | /*****************************************************************************/ |
|
|
498 | |
|
|
499 | #if EV_USE_KQUEUE |
|
|
500 | # include "ev_kqueue.c" |
|
|
501 | #endif |
418 | #if EV_USE_EPOLL |
502 | #if EV_USE_EPOLL |
419 | # include "ev_epoll.c" |
503 | # include "ev_epoll.c" |
420 | #endif |
504 | #endif |
|
|
505 | #if EV_USE_POLL |
|
|
506 | # include "ev_poll.c" |
|
|
507 | #endif |
421 | #if EV_USE_SELECT |
508 | #if EV_USE_SELECT |
422 | # include "ev_select.c" |
509 | # include "ev_select.c" |
423 | #endif |
510 | #endif |
424 | |
511 | |
425 | int |
512 | int |
… | |
… | |
432 | ev_version_minor (void) |
519 | ev_version_minor (void) |
433 | { |
520 | { |
434 | return EV_VERSION_MINOR; |
521 | return EV_VERSION_MINOR; |
435 | } |
522 | } |
436 | |
523 | |
|
|
524 | /* return true if we are running with elevated privileges and ignore env variables */ |
|
|
525 | static int |
|
|
526 | enable_secure () |
|
|
527 | { |
|
|
528 | return getuid () != geteuid () |
|
|
529 | || getgid () != getegid (); |
|
|
530 | } |
|
|
531 | |
437 | int ev_init (int flags) |
532 | int ev_init (int methods) |
438 | { |
533 | { |
439 | if (!ev_method) |
534 | if (!ev_method) |
440 | { |
535 | { |
441 | #if EV_USE_MONOTONIC |
536 | #if EV_USE_MONOTONIC |
442 | { |
537 | { |
… | |
… | |
444 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
539 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
445 | have_monotonic = 1; |
540 | have_monotonic = 1; |
446 | } |
541 | } |
447 | #endif |
542 | #endif |
448 | |
543 | |
449 | ev_now = ev_time (); |
544 | ev_now = ev_time (); |
450 | now = get_clock (); |
545 | now = get_clock (); |
|
|
546 | now_floor = now; |
451 | diff = ev_now - now; |
547 | diff = ev_now - now; |
452 | |
548 | |
453 | if (pipe (sigpipe)) |
549 | if (pipe (sigpipe)) |
454 | return 0; |
550 | return 0; |
455 | |
551 | |
|
|
552 | if (methods == EVMETHOD_AUTO) |
|
|
553 | if (!enable_secure () && getenv ("LIBEV_METHODS")) |
|
|
554 | methods = atoi (getenv ("LIBEV_METHODS")); |
|
|
555 | else |
456 | ev_method = EVMETHOD_NONE; |
556 | methods = EVMETHOD_ANY; |
|
|
557 | |
|
|
558 | ev_method = 0; |
|
|
559 | #if EV_USE_KQUEUE |
|
|
560 | if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods); |
|
|
561 | #endif |
457 | #if EV_USE_EPOLL |
562 | #if EV_USE_EPOLL |
458 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
563 | if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); |
|
|
564 | #endif |
|
|
565 | #if EV_USE_POLL |
|
|
566 | if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); |
459 | #endif |
567 | #endif |
460 | #if EV_USE_SELECT |
568 | #if EV_USE_SELECT |
461 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
569 | if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); |
462 | #endif |
570 | #endif |
463 | |
571 | |
464 | if (ev_method) |
572 | if (ev_method) |
465 | { |
573 | { |
466 | ev_watcher_init (&sigev, sigcb); |
574 | ev_watcher_init (&sigev, sigcb); |
467 | siginit (); |
575 | siginit (); |
468 | |
576 | |
|
|
577 | #ifndef WIN32 |
469 | ev_signal_init (&childev, childcb, SIGCHLD); |
578 | ev_signal_init (&childev, childcb, SIGCHLD); |
470 | ev_signal_start (&childev); |
579 | ev_signal_start (&childev); |
|
|
580 | #endif |
471 | } |
581 | } |
472 | } |
582 | } |
473 | |
583 | |
474 | return ev_method; |
584 | return ev_method; |
475 | } |
585 | } |
476 | |
586 | |
477 | /*****************************************************************************/ |
587 | /*****************************************************************************/ |
478 | |
588 | |
479 | void |
589 | void |
480 | ev_prefork (void) |
590 | ev_fork_prepare (void) |
481 | { |
591 | { |
482 | /* nop */ |
592 | /* nop */ |
483 | } |
593 | } |
484 | |
594 | |
485 | void |
595 | void |
486 | ev_postfork_parent (void) |
596 | ev_fork_parent (void) |
487 | { |
597 | { |
488 | /* nop */ |
598 | /* nop */ |
489 | } |
599 | } |
490 | |
600 | |
491 | void |
601 | void |
492 | ev_postfork_child (void) |
602 | ev_fork_child (void) |
493 | { |
603 | { |
494 | #if EV_USE_EPOLL |
604 | #if EV_USE_EPOLL |
495 | if (ev_method == EVMETHOD_EPOLL) |
605 | if (ev_method == EVMETHOD_EPOLL) |
496 | epoll_postfork_child (); |
606 | epoll_postfork_child (); |
497 | #endif |
607 | #endif |
… | |
… | |
506 | /*****************************************************************************/ |
616 | /*****************************************************************************/ |
507 | |
617 | |
508 | static void |
618 | static void |
509 | call_pending (void) |
619 | call_pending (void) |
510 | { |
620 | { |
|
|
621 | int pri; |
|
|
622 | |
|
|
623 | for (pri = NUMPRI; pri--; ) |
511 | while (pendingcnt) |
624 | while (pendingcnt [pri]) |
512 | { |
625 | { |
513 | ANPENDING *p = pendings + --pendingcnt; |
626 | ANPENDING *p = pendings [pri] + --pendingcnt [pri]; |
514 | |
627 | |
515 | if (p->w) |
628 | if (p->w) |
516 | { |
629 | { |
517 | p->w->pending = 0; |
630 | p->w->pending = 0; |
518 | p->w->cb (p->w, p->events); |
631 | p->w->cb (p->w, p->events); |
519 | } |
632 | } |
520 | } |
633 | } |
521 | } |
634 | } |
522 | |
635 | |
523 | static void |
636 | static void |
524 | timers_reify (void) |
637 | timers_reify (void) |
525 | { |
638 | { |
… | |
… | |
528 | struct ev_timer *w = timers [0]; |
641 | struct ev_timer *w = timers [0]; |
529 | |
642 | |
530 | /* first reschedule or stop timer */ |
643 | /* first reschedule or stop timer */ |
531 | if (w->repeat) |
644 | if (w->repeat) |
532 | { |
645 | { |
|
|
646 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
533 | w->at = now + w->repeat; |
647 | w->at = now + w->repeat; |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
535 | downheap ((WT *)timers, timercnt, 0); |
648 | downheap ((WT *)timers, timercnt, 0); |
536 | } |
649 | } |
537 | else |
650 | else |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
651 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
539 | |
652 | |
… | |
… | |
550 | |
663 | |
551 | /* first reschedule or stop timer */ |
664 | /* first reschedule or stop timer */ |
552 | if (w->interval) |
665 | if (w->interval) |
553 | { |
666 | { |
554 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
667 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
555 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
668 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
556 | downheap ((WT *)periodics, periodiccnt, 0); |
669 | downheap ((WT *)periodics, periodiccnt, 0); |
557 | } |
670 | } |
558 | else |
671 | else |
559 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
672 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
560 | |
673 | |
561 | event ((W)w, EV_TIMEOUT); |
674 | event ((W)w, EV_PERIODIC); |
562 | } |
675 | } |
563 | } |
676 | } |
564 | |
677 | |
565 | static void |
678 | static void |
566 | periodics_reschedule (ev_tstamp diff) |
679 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
585 | } |
698 | } |
586 | } |
699 | } |
587 | } |
700 | } |
588 | } |
701 | } |
589 | |
702 | |
|
|
703 | static int |
|
|
704 | time_update_monotonic (void) |
|
|
705 | { |
|
|
706 | now = get_clock (); |
|
|
707 | |
|
|
708 | if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) |
|
|
709 | { |
|
|
710 | ev_now = now + diff; |
|
|
711 | return 0; |
|
|
712 | } |
|
|
713 | else |
|
|
714 | { |
|
|
715 | now_floor = now; |
|
|
716 | ev_now = ev_time (); |
|
|
717 | return 1; |
|
|
718 | } |
|
|
719 | } |
|
|
720 | |
590 | static void |
721 | static void |
591 | time_update (void) |
722 | time_update (void) |
592 | { |
723 | { |
593 | int i; |
724 | int i; |
594 | |
725 | |
595 | ev_now = ev_time (); |
726 | #if EV_USE_MONOTONIC |
596 | |
|
|
597 | if (have_monotonic) |
727 | if (expect_true (have_monotonic)) |
598 | { |
728 | { |
|
|
729 | if (time_update_monotonic ()) |
|
|
730 | { |
599 | ev_tstamp odiff = diff; |
731 | ev_tstamp odiff = diff; |
600 | |
732 | |
601 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
733 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
602 | { |
734 | { |
603 | now = get_clock (); |
|
|
604 | diff = ev_now - now; |
735 | diff = ev_now - now; |
605 | |
736 | |
606 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
737 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
607 | return; /* all is well */ |
738 | return; /* all is well */ |
608 | |
739 | |
609 | ev_now = ev_time (); |
740 | ev_now = ev_time (); |
|
|
741 | now = get_clock (); |
|
|
742 | now_floor = now; |
610 | } |
743 | } |
611 | |
744 | |
612 | periodics_reschedule (diff - odiff); |
745 | periodics_reschedule (diff - odiff); |
613 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
746 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
747 | } |
614 | } |
748 | } |
615 | else |
749 | else |
|
|
750 | #endif |
616 | { |
751 | { |
|
|
752 | ev_now = ev_time (); |
|
|
753 | |
617 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
754 | if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) |
618 | { |
755 | { |
619 | periodics_reschedule (ev_now - now); |
756 | periodics_reschedule (ev_now - now); |
620 | |
757 | |
621 | /* adjust timers. this is easy, as the offset is the same for all */ |
758 | /* adjust timers. this is easy, as the offset is the same for all */ |
622 | for (i = 0; i < timercnt; ++i) |
759 | for (i = 0; i < timercnt; ++i) |
… | |
… | |
635 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
772 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
636 | |
773 | |
637 | do |
774 | do |
638 | { |
775 | { |
639 | /* queue check watchers (and execute them) */ |
776 | /* queue check watchers (and execute them) */ |
640 | if (preparecnt) |
777 | if (expect_false (preparecnt)) |
641 | { |
778 | { |
642 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
779 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
643 | call_pending (); |
780 | call_pending (); |
644 | } |
781 | } |
645 | |
782 | |
… | |
… | |
648 | |
785 | |
649 | /* calculate blocking time */ |
786 | /* calculate blocking time */ |
650 | |
787 | |
651 | /* we only need this for !monotonic clockor timers, but as we basically |
788 | /* we only need this for !monotonic clockor timers, but as we basically |
652 | always have timers, we just calculate it always */ |
789 | always have timers, we just calculate it always */ |
|
|
790 | #if EV_USE_MONOTONIC |
|
|
791 | if (expect_true (have_monotonic)) |
|
|
792 | time_update_monotonic (); |
|
|
793 | else |
|
|
794 | #endif |
|
|
795 | { |
653 | ev_now = ev_time (); |
796 | ev_now = ev_time (); |
|
|
797 | now = ev_now; |
|
|
798 | } |
654 | |
799 | |
655 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
800 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
656 | block = 0.; |
801 | block = 0.; |
657 | else |
802 | else |
658 | { |
803 | { |
659 | block = MAX_BLOCKTIME; |
804 | block = MAX_BLOCKTIME; |
660 | |
805 | |
661 | if (timercnt) |
806 | if (timercnt) |
662 | { |
807 | { |
663 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
808 | ev_tstamp to = timers [0]->at - now + method_fudge; |
664 | if (block > to) block = to; |
809 | if (block > to) block = to; |
665 | } |
810 | } |
666 | |
811 | |
667 | if (periodiccnt) |
812 | if (periodiccnt) |
668 | { |
813 | { |
… | |
… | |
721 | head = &(*head)->next; |
866 | head = &(*head)->next; |
722 | } |
867 | } |
723 | } |
868 | } |
724 | |
869 | |
725 | static void |
870 | static void |
726 | ev_clear (W w) |
871 | ev_clear_pending (W w) |
727 | { |
872 | { |
728 | if (w->pending) |
873 | if (w->pending) |
729 | { |
874 | { |
730 | pendings [w->pending - 1].w = 0; |
875 | pendings [ABSPRI (w)][w->pending - 1].w = 0; |
731 | w->pending = 0; |
876 | w->pending = 0; |
732 | } |
877 | } |
733 | } |
878 | } |
734 | |
879 | |
735 | static void |
880 | static void |
736 | ev_start (W w, int active) |
881 | ev_start (W w, int active) |
737 | { |
882 | { |
|
|
883 | if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; |
|
|
884 | if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; |
|
|
885 | |
738 | w->active = active; |
886 | w->active = active; |
739 | } |
887 | } |
740 | |
888 | |
741 | static void |
889 | static void |
742 | ev_stop (W w) |
890 | ev_stop (W w) |
… | |
… | |
747 | /*****************************************************************************/ |
895 | /*****************************************************************************/ |
748 | |
896 | |
749 | void |
897 | void |
750 | ev_io_start (struct ev_io *w) |
898 | ev_io_start (struct ev_io *w) |
751 | { |
899 | { |
|
|
900 | int fd = w->fd; |
|
|
901 | |
752 | if (ev_is_active (w)) |
902 | if (ev_is_active (w)) |
753 | return; |
903 | return; |
754 | |
904 | |
755 | int fd = w->fd; |
905 | assert (("ev_io_start called with negative fd", fd >= 0)); |
756 | |
906 | |
757 | ev_start ((W)w, 1); |
907 | ev_start ((W)w, 1); |
758 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
908 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
759 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
909 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
760 | |
910 | |
… | |
… | |
762 | } |
912 | } |
763 | |
913 | |
764 | void |
914 | void |
765 | ev_io_stop (struct ev_io *w) |
915 | ev_io_stop (struct ev_io *w) |
766 | { |
916 | { |
767 | ev_clear ((W)w); |
917 | ev_clear_pending ((W)w); |
768 | if (!ev_is_active (w)) |
918 | if (!ev_is_active (w)) |
769 | return; |
919 | return; |
770 | |
920 | |
771 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
921 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
772 | ev_stop ((W)w); |
922 | ev_stop ((W)w); |
… | |
… | |
780 | if (ev_is_active (w)) |
930 | if (ev_is_active (w)) |
781 | return; |
931 | return; |
782 | |
932 | |
783 | w->at += now; |
933 | w->at += now; |
784 | |
934 | |
785 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
935 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
786 | |
936 | |
787 | ev_start ((W)w, ++timercnt); |
937 | ev_start ((W)w, ++timercnt); |
788 | array_needsize (timers, timermax, timercnt, ); |
938 | array_needsize (timers, timermax, timercnt, ); |
789 | timers [timercnt - 1] = w; |
939 | timers [timercnt - 1] = w; |
790 | upheap ((WT *)timers, timercnt - 1); |
940 | upheap ((WT *)timers, timercnt - 1); |
791 | } |
941 | } |
792 | |
942 | |
793 | void |
943 | void |
794 | ev_timer_stop (struct ev_timer *w) |
944 | ev_timer_stop (struct ev_timer *w) |
795 | { |
945 | { |
796 | ev_clear ((W)w); |
946 | ev_clear_pending ((W)w); |
797 | if (!ev_is_active (w)) |
947 | if (!ev_is_active (w)) |
798 | return; |
948 | return; |
799 | |
949 | |
800 | if (w->active < timercnt--) |
950 | if (w->active < timercnt--) |
801 | { |
951 | { |
… | |
… | |
829 | ev_periodic_start (struct ev_periodic *w) |
979 | ev_periodic_start (struct ev_periodic *w) |
830 | { |
980 | { |
831 | if (ev_is_active (w)) |
981 | if (ev_is_active (w)) |
832 | return; |
982 | return; |
833 | |
983 | |
834 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
984 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
835 | |
985 | |
836 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
986 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
837 | if (w->interval) |
987 | if (w->interval) |
838 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
988 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
839 | |
989 | |
… | |
… | |
844 | } |
994 | } |
845 | |
995 | |
846 | void |
996 | void |
847 | ev_periodic_stop (struct ev_periodic *w) |
997 | ev_periodic_stop (struct ev_periodic *w) |
848 | { |
998 | { |
849 | ev_clear ((W)w); |
999 | ev_clear_pending ((W)w); |
850 | if (!ev_is_active (w)) |
1000 | if (!ev_is_active (w)) |
851 | return; |
1001 | return; |
852 | |
1002 | |
853 | if (w->active < periodiccnt--) |
1003 | if (w->active < periodiccnt--) |
854 | { |
1004 | { |
… | |
… | |
862 | void |
1012 | void |
863 | ev_signal_start (struct ev_signal *w) |
1013 | ev_signal_start (struct ev_signal *w) |
864 | { |
1014 | { |
865 | if (ev_is_active (w)) |
1015 | if (ev_is_active (w)) |
866 | return; |
1016 | return; |
|
|
1017 | |
|
|
1018 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
867 | |
1019 | |
868 | ev_start ((W)w, 1); |
1020 | ev_start ((W)w, 1); |
869 | array_needsize (signals, signalmax, w->signum, signals_init); |
1021 | array_needsize (signals, signalmax, w->signum, signals_init); |
870 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
1022 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
871 | |
1023 | |
… | |
… | |
880 | } |
1032 | } |
881 | |
1033 | |
882 | void |
1034 | void |
883 | ev_signal_stop (struct ev_signal *w) |
1035 | ev_signal_stop (struct ev_signal *w) |
884 | { |
1036 | { |
885 | ev_clear ((W)w); |
1037 | ev_clear_pending ((W)w); |
886 | if (!ev_is_active (w)) |
1038 | if (!ev_is_active (w)) |
887 | return; |
1039 | return; |
888 | |
1040 | |
889 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
1041 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
890 | ev_stop ((W)w); |
1042 | ev_stop ((W)w); |
… | |
… | |
905 | } |
1057 | } |
906 | |
1058 | |
907 | void |
1059 | void |
908 | ev_idle_stop (struct ev_idle *w) |
1060 | ev_idle_stop (struct ev_idle *w) |
909 | { |
1061 | { |
910 | ev_clear ((W)w); |
1062 | ev_clear_pending ((W)w); |
911 | if (ev_is_active (w)) |
1063 | if (ev_is_active (w)) |
912 | return; |
1064 | return; |
913 | |
1065 | |
914 | idles [w->active - 1] = idles [--idlecnt]; |
1066 | idles [w->active - 1] = idles [--idlecnt]; |
915 | ev_stop ((W)w); |
1067 | ev_stop ((W)w); |
… | |
… | |
927 | } |
1079 | } |
928 | |
1080 | |
929 | void |
1081 | void |
930 | ev_prepare_stop (struct ev_prepare *w) |
1082 | ev_prepare_stop (struct ev_prepare *w) |
931 | { |
1083 | { |
932 | ev_clear ((W)w); |
1084 | ev_clear_pending ((W)w); |
933 | if (ev_is_active (w)) |
1085 | if (ev_is_active (w)) |
934 | return; |
1086 | return; |
935 | |
1087 | |
936 | prepares [w->active - 1] = prepares [--preparecnt]; |
1088 | prepares [w->active - 1] = prepares [--preparecnt]; |
937 | ev_stop ((W)w); |
1089 | ev_stop ((W)w); |
… | |
… | |
949 | } |
1101 | } |
950 | |
1102 | |
951 | void |
1103 | void |
952 | ev_check_stop (struct ev_check *w) |
1104 | ev_check_stop (struct ev_check *w) |
953 | { |
1105 | { |
954 | ev_clear ((W)w); |
1106 | ev_clear_pending ((W)w); |
955 | if (ev_is_active (w)) |
1107 | if (ev_is_active (w)) |
956 | return; |
1108 | return; |
957 | |
1109 | |
958 | checks [w->active - 1] = checks [--checkcnt]; |
1110 | checks [w->active - 1] = checks [--checkcnt]; |
959 | ev_stop ((W)w); |
1111 | ev_stop ((W)w); |
… | |
… | |
970 | } |
1122 | } |
971 | |
1123 | |
972 | void |
1124 | void |
973 | ev_child_stop (struct ev_child *w) |
1125 | ev_child_stop (struct ev_child *w) |
974 | { |
1126 | { |
975 | ev_clear ((W)w); |
1127 | ev_clear_pending ((W)w); |
976 | if (ev_is_active (w)) |
1128 | if (ev_is_active (w)) |
977 | return; |
1129 | return; |
978 | |
1130 | |
979 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
1131 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
980 | ev_stop ((W)w); |
1132 | ev_stop ((W)w); |