… | |
… | |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
26 | * (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. |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
28 | */ |
|
|
29 | #if EV_USE_CONFIG_H |
|
|
30 | # include "config.h" |
|
|
31 | #endif |
29 | |
32 | |
30 | #include <math.h> |
33 | #include <math.h> |
31 | #include <stdlib.h> |
34 | #include <stdlib.h> |
32 | #include <unistd.h> |
35 | #include <unistd.h> |
33 | #include <fcntl.h> |
36 | #include <fcntl.h> |
… | |
… | |
41 | #include <sys/types.h> |
44 | #include <sys/types.h> |
42 | #include <sys/wait.h> |
45 | #include <sys/wait.h> |
43 | #include <sys/time.h> |
46 | #include <sys/time.h> |
44 | #include <time.h> |
47 | #include <time.h> |
45 | |
48 | |
46 | #ifndef HAVE_MONOTONIC |
49 | #ifndef EV_USE_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
50 | # ifdef CLOCK_MONOTONIC |
48 | # define HAVE_MONOTONIC 1 |
51 | # define EV_USE_MONOTONIC 1 |
49 | # endif |
52 | # endif |
50 | #endif |
53 | #endif |
51 | |
54 | |
52 | #ifndef HAVE_SELECT |
55 | #ifndef EV_USE_SELECT |
53 | # define HAVE_SELECT 1 |
56 | # define EV_USE_SELECT 1 |
54 | #endif |
57 | #endif |
55 | |
58 | |
56 | #ifndef HAVE_EPOLL |
59 | #ifndef EV_USE_EPOLL |
57 | # define HAVE_EPOLL 0 |
60 | # define EV_USE_EPOLL 0 |
58 | #endif |
61 | #endif |
59 | |
62 | |
|
|
63 | #ifndef CLOCK_REALTIME |
|
|
64 | # define EV_USE_REALTIME 0 |
|
|
65 | #endif |
60 | #ifndef HAVE_REALTIME |
66 | #ifndef EV_USE_REALTIME |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
67 | # define EV_USE_REALTIME 1 /* posix requirement, but might be slower */ |
62 | #endif |
68 | #endif |
63 | |
69 | |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
70 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
65 | #define MAX_BLOCKTIME 60. |
71 | #define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */ |
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
72 | #define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ |
|
|
73 | #define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */ |
67 | |
74 | |
68 | #include "ev.h" |
75 | #include "ev.h" |
69 | |
76 | |
70 | typedef struct ev_watcher *W; |
77 | typedef struct ev_watcher *W; |
71 | typedef struct ev_watcher_list *WL; |
78 | typedef struct ev_watcher_list *WL; |
… | |
… | |
84 | /*****************************************************************************/ |
91 | /*****************************************************************************/ |
85 | |
92 | |
86 | ev_tstamp |
93 | ev_tstamp |
87 | ev_time (void) |
94 | ev_time (void) |
88 | { |
95 | { |
89 | #if HAVE_REALTIME |
96 | #if EV_USE_REALTIME |
90 | struct timespec ts; |
97 | struct timespec ts; |
91 | clock_gettime (CLOCK_REALTIME, &ts); |
98 | clock_gettime (CLOCK_REALTIME, &ts); |
92 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
99 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
93 | #else |
100 | #else |
94 | struct timeval tv; |
101 | struct timeval tv; |
… | |
… | |
98 | } |
105 | } |
99 | |
106 | |
100 | static ev_tstamp |
107 | static ev_tstamp |
101 | get_clock (void) |
108 | get_clock (void) |
102 | { |
109 | { |
103 | #if HAVE_MONOTONIC |
110 | #if EV_USE_MONOTONIC |
104 | if (have_monotonic) |
111 | if (have_monotonic) |
105 | { |
112 | { |
106 | struct timespec ts; |
113 | struct timespec ts; |
107 | clock_gettime (CLOCK_MONOTONIC, &ts); |
114 | clock_gettime (CLOCK_MONOTONIC, &ts); |
108 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
115 | return ts.tv_sec + ts.tv_nsec * 1e-9; |
109 | } |
116 | } |
110 | #endif |
117 | #endif |
111 | |
118 | |
112 | return ev_time (); |
119 | return ev_time (); |
113 | } |
120 | } |
|
|
121 | |
|
|
122 | #define array_roundsize(base,n) ((n) | 4 & ~3) |
114 | |
123 | |
115 | #define array_needsize(base,cur,cnt,init) \ |
124 | #define array_needsize(base,cur,cnt,init) \ |
116 | if ((cnt) > cur) \ |
125 | if ((cnt) > cur) \ |
117 | { \ |
126 | { \ |
118 | int newcnt = cur; \ |
127 | int newcnt = cur; \ |
119 | do \ |
128 | do \ |
120 | { \ |
129 | { \ |
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
130 | newcnt = array_roundsize (base, newcnt << 1); \ |
122 | } \ |
131 | } \ |
123 | while ((cnt) > newcnt); \ |
132 | while ((cnt) > newcnt); \ |
124 | \ |
133 | \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
134 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
126 | init (base + cur, newcnt - cur); \ |
135 | init (base + cur, newcnt - cur); \ |
… | |
… | |
130 | /*****************************************************************************/ |
139 | /*****************************************************************************/ |
131 | |
140 | |
132 | typedef struct |
141 | typedef struct |
133 | { |
142 | { |
134 | struct ev_io *head; |
143 | struct ev_io *head; |
135 | unsigned char wev, rev; /* want, received event set */ |
144 | unsigned char events; |
|
|
145 | unsigned char reify; |
136 | } ANFD; |
146 | } ANFD; |
137 | |
147 | |
138 | static ANFD *anfds; |
148 | static ANFD *anfds; |
139 | static int anfdmax; |
149 | static int anfdmax; |
140 | |
150 | |
141 | static int *fdchanges; |
|
|
142 | static int fdchangemax, fdchangecnt; |
|
|
143 | |
|
|
144 | static void |
151 | static void |
145 | anfds_init (ANFD *base, int count) |
152 | anfds_init (ANFD *base, int count) |
146 | { |
153 | { |
147 | while (count--) |
154 | while (count--) |
148 | { |
155 | { |
149 | base->head = 0; |
156 | base->head = 0; |
150 | base->wev = base->rev = EV_NONE; |
157 | base->events = EV_NONE; |
|
|
158 | base->reify = 0; |
|
|
159 | |
151 | ++base; |
160 | ++base; |
152 | } |
161 | } |
153 | } |
162 | } |
154 | |
163 | |
155 | typedef struct |
164 | typedef struct |
… | |
… | |
162 | static int pendingmax, pendingcnt; |
171 | static int pendingmax, pendingcnt; |
163 | |
172 | |
164 | static void |
173 | static void |
165 | event (W w, int events) |
174 | event (W w, int events) |
166 | { |
175 | { |
167 | if (w->active) |
176 | if (w->pending) |
|
|
177 | { |
|
|
178 | pendings [w->pending - 1].events |= events; |
|
|
179 | return; |
168 | { |
180 | } |
|
|
181 | |
169 | w->pending = ++pendingcnt; |
182 | w->pending = ++pendingcnt; |
170 | array_needsize (pendings, pendingmax, pendingcnt, ); |
183 | array_needsize (pendings, pendingmax, pendingcnt, ); |
171 | pendings [pendingcnt - 1].w = w; |
184 | pendings [pendingcnt - 1].w = w; |
172 | pendings [pendingcnt - 1].events = events; |
185 | pendings [pendingcnt - 1].events = events; |
173 | } |
186 | } |
|
|
187 | |
|
|
188 | static void |
|
|
189 | queue_events (W *events, int eventcnt, int type) |
|
|
190 | { |
|
|
191 | int i; |
|
|
192 | |
|
|
193 | for (i = 0; i < eventcnt; ++i) |
|
|
194 | event (events [i], type); |
174 | } |
195 | } |
175 | |
196 | |
176 | static void |
197 | static void |
177 | fd_event (int fd, int events) |
198 | fd_event (int fd, int events) |
178 | { |
199 | { |
… | |
… | |
186 | if (ev) |
207 | if (ev) |
187 | event ((W)w, ev); |
208 | event ((W)w, ev); |
188 | } |
209 | } |
189 | } |
210 | } |
190 | |
211 | |
|
|
212 | /*****************************************************************************/ |
|
|
213 | |
|
|
214 | static int *fdchanges; |
|
|
215 | static int fdchangemax, fdchangecnt; |
|
|
216 | |
191 | static void |
217 | static void |
192 | queue_events (W *events, int eventcnt, int type) |
218 | fd_reify (void) |
193 | { |
219 | { |
194 | int i; |
220 | int i; |
195 | |
221 | |
196 | for (i = 0; i < eventcnt; ++i) |
222 | for (i = 0; i < fdchangecnt; ++i) |
197 | event (events [i], type); |
223 | { |
|
|
224 | int fd = fdchanges [i]; |
|
|
225 | ANFD *anfd = anfds + fd; |
|
|
226 | struct ev_io *w; |
|
|
227 | |
|
|
228 | int events = 0; |
|
|
229 | |
|
|
230 | for (w = anfd->head; w; w = w->next) |
|
|
231 | events |= w->events; |
|
|
232 | |
|
|
233 | anfd->reify = 0; |
|
|
234 | |
|
|
235 | if (anfd->events != events) |
|
|
236 | { |
|
|
237 | method_modify (fd, anfd->events, events); |
|
|
238 | anfd->events = events; |
|
|
239 | } |
|
|
240 | } |
|
|
241 | |
|
|
242 | fdchangecnt = 0; |
|
|
243 | } |
|
|
244 | |
|
|
245 | static void |
|
|
246 | fd_change (int fd) |
|
|
247 | { |
|
|
248 | if (anfds [fd].reify || fdchangecnt < 0) |
|
|
249 | return; |
|
|
250 | |
|
|
251 | anfds [fd].reify = 1; |
|
|
252 | |
|
|
253 | ++fdchangecnt; |
|
|
254 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
255 | fdchanges [fdchangecnt - 1] = fd; |
198 | } |
256 | } |
199 | |
257 | |
200 | /* called on EBADF to verify fds */ |
258 | /* called on EBADF to verify fds */ |
201 | static void |
259 | static void |
202 | fd_recheck (void) |
260 | fd_recheck (void) |
203 | { |
261 | { |
204 | int fd; |
262 | int fd; |
205 | |
263 | |
206 | for (fd = 0; fd < anfdmax; ++fd) |
264 | for (fd = 0; fd < anfdmax; ++fd) |
207 | if (anfds [fd].wev) |
265 | if (anfds [fd].events) |
208 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
266 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
209 | while (anfds [fd].head) |
267 | while (anfds [fd].head) |
210 | { |
268 | { |
211 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
212 | evio_stop (anfds [fd].head); |
269 | ev_io_stop (anfds [fd].head); |
|
|
270 | event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE); |
213 | } |
271 | } |
214 | } |
272 | } |
215 | |
273 | |
216 | /*****************************************************************************/ |
274 | /*****************************************************************************/ |
217 | |
275 | |
… | |
… | |
265 | /*****************************************************************************/ |
323 | /*****************************************************************************/ |
266 | |
324 | |
267 | typedef struct |
325 | typedef struct |
268 | { |
326 | { |
269 | struct ev_signal *head; |
327 | struct ev_signal *head; |
270 | sig_atomic_t gotsig; |
328 | sig_atomic_t volatile gotsig; |
271 | } ANSIG; |
329 | } ANSIG; |
272 | |
330 | |
273 | static ANSIG *signals; |
331 | static ANSIG *signals; |
274 | static int signalmax; |
332 | static int signalmax; |
275 | |
333 | |
276 | static int sigpipe [2]; |
334 | static int sigpipe [2]; |
277 | static sig_atomic_t gotsig; |
335 | static sig_atomic_t volatile gotsig; |
278 | static struct ev_io sigev; |
336 | static struct ev_io sigev; |
279 | |
337 | |
280 | static void |
338 | static void |
281 | signals_init (ANSIG *base, int count) |
339 | signals_init (ANSIG *base, int count) |
282 | { |
340 | { |
283 | while (count--) |
341 | while (count--) |
284 | { |
342 | { |
285 | base->head = 0; |
343 | base->head = 0; |
286 | base->gotsig = 0; |
344 | base->gotsig = 0; |
|
|
345 | |
287 | ++base; |
346 | ++base; |
288 | } |
347 | } |
289 | } |
348 | } |
290 | |
349 | |
291 | static void |
350 | static void |
… | |
… | |
294 | signals [signum - 1].gotsig = 1; |
353 | signals [signum - 1].gotsig = 1; |
295 | |
354 | |
296 | if (!gotsig) |
355 | if (!gotsig) |
297 | { |
356 | { |
298 | gotsig = 1; |
357 | gotsig = 1; |
299 | write (sigpipe [1], &gotsig, 1); |
358 | write (sigpipe [1], &signum, 1); |
300 | } |
359 | } |
301 | } |
360 | } |
302 | |
361 | |
303 | static void |
362 | static void |
304 | sigcb (struct ev_io *iow, int revents) |
363 | sigcb (struct ev_io *iow, int revents) |
305 | { |
364 | { |
306 | struct ev_signal *w; |
365 | struct ev_signal *w; |
307 | int sig; |
366 | int sig; |
308 | |
367 | |
|
|
368 | read (sigpipe [0], &revents, 1); |
309 | gotsig = 0; |
369 | gotsig = 0; |
310 | read (sigpipe [0], &revents, 1); |
|
|
311 | |
370 | |
312 | for (sig = signalmax; sig--; ) |
371 | for (sig = signalmax; sig--; ) |
313 | if (signals [sig].gotsig) |
372 | if (signals [sig].gotsig) |
314 | { |
373 | { |
315 | signals [sig].gotsig = 0; |
374 | signals [sig].gotsig = 0; |
… | |
… | |
327 | |
386 | |
328 | /* rather than sort out wether we really need nb, set it */ |
387 | /* rather than sort out wether we really need nb, set it */ |
329 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
388 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
330 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
389 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
331 | |
390 | |
332 | evio_set (&sigev, sigpipe [0], EV_READ); |
391 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
333 | evio_start (&sigev); |
392 | ev_io_start (&sigev); |
334 | } |
393 | } |
335 | |
394 | |
336 | /*****************************************************************************/ |
395 | /*****************************************************************************/ |
337 | |
396 | |
338 | static struct ev_idle **idles; |
397 | static struct ev_idle **idles; |
… | |
… | |
368 | } |
427 | } |
369 | } |
428 | } |
370 | |
429 | |
371 | /*****************************************************************************/ |
430 | /*****************************************************************************/ |
372 | |
431 | |
373 | #if HAVE_EPOLL |
432 | #if EV_USE_EPOLL |
374 | # include "ev_epoll.c" |
433 | # include "ev_epoll.c" |
375 | #endif |
434 | #endif |
376 | #if HAVE_SELECT |
435 | #if EV_USE_SELECT |
377 | # include "ev_select.c" |
436 | # include "ev_select.c" |
378 | #endif |
437 | #endif |
379 | |
438 | |
380 | int |
439 | int |
381 | ev_version_major (void) |
440 | ev_version_major (void) |
… | |
… | |
391 | |
450 | |
392 | int ev_init (int flags) |
451 | int ev_init (int flags) |
393 | { |
452 | { |
394 | if (!ev_method) |
453 | if (!ev_method) |
395 | { |
454 | { |
396 | #if HAVE_MONOTONIC |
455 | #if EV_USE_MONOTONIC |
397 | { |
456 | { |
398 | struct timespec ts; |
457 | struct timespec ts; |
399 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
458 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
400 | have_monotonic = 1; |
459 | have_monotonic = 1; |
401 | } |
460 | } |
… | |
… | |
407 | |
466 | |
408 | if (pipe (sigpipe)) |
467 | if (pipe (sigpipe)) |
409 | return 0; |
468 | return 0; |
410 | |
469 | |
411 | ev_method = EVMETHOD_NONE; |
470 | ev_method = EVMETHOD_NONE; |
412 | #if HAVE_EPOLL |
471 | #if EV_USE_EPOLL |
413 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
472 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
414 | #endif |
473 | #endif |
415 | #if HAVE_SELECT |
474 | #if EV_USE_SELECT |
416 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
475 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
417 | #endif |
476 | #endif |
418 | |
477 | |
419 | if (ev_method) |
478 | if (ev_method) |
420 | { |
479 | { |
421 | evw_init (&sigev, sigcb); |
480 | ev_watcher_init (&sigev, sigcb); |
422 | siginit (); |
481 | siginit (); |
423 | |
482 | |
424 | evsignal_init (&childev, childcb, SIGCHLD); |
483 | ev_signal_init (&childev, childcb, SIGCHLD); |
425 | evsignal_start (&childev); |
484 | ev_signal_start (&childev); |
426 | } |
485 | } |
427 | } |
486 | } |
428 | |
487 | |
429 | return ev_method; |
488 | return ev_method; |
430 | } |
489 | } |
… | |
… | |
444 | } |
503 | } |
445 | |
504 | |
446 | void |
505 | void |
447 | ev_postfork_child (void) |
506 | ev_postfork_child (void) |
448 | { |
507 | { |
449 | #if HAVE_EPOLL |
508 | #if EV_USE_EPOLL |
450 | if (ev_method == EVMETHOD_EPOLL) |
509 | if (ev_method == EVMETHOD_EPOLL) |
451 | epoll_postfork_child (); |
510 | epoll_postfork_child (); |
452 | #endif |
511 | #endif |
453 | |
512 | |
454 | evio_stop (&sigev); |
513 | ev_io_stop (&sigev); |
455 | close (sigpipe [0]); |
514 | close (sigpipe [0]); |
456 | close (sigpipe [1]); |
515 | close (sigpipe [1]); |
457 | pipe (sigpipe); |
516 | pipe (sigpipe); |
458 | siginit (); |
517 | siginit (); |
459 | } |
518 | } |
460 | |
519 | |
461 | /*****************************************************************************/ |
520 | /*****************************************************************************/ |
462 | |
521 | |
463 | static void |
522 | static void |
464 | fd_reify (void) |
|
|
465 | { |
|
|
466 | int i; |
|
|
467 | |
|
|
468 | for (i = 0; i < fdchangecnt; ++i) |
|
|
469 | { |
|
|
470 | int fd = fdchanges [i]; |
|
|
471 | ANFD *anfd = anfds + fd; |
|
|
472 | struct ev_io *w; |
|
|
473 | |
|
|
474 | int wev = 0; |
|
|
475 | |
|
|
476 | for (w = anfd->head; w; w = w->next) |
|
|
477 | wev |= w->events; |
|
|
478 | |
|
|
479 | if (anfd->wev != wev) |
|
|
480 | { |
|
|
481 | method_modify (fd, anfd->wev, wev); |
|
|
482 | anfd->wev = wev; |
|
|
483 | } |
|
|
484 | } |
|
|
485 | |
|
|
486 | fdchangecnt = 0; |
|
|
487 | } |
|
|
488 | |
|
|
489 | static void |
|
|
490 | call_pending (void) |
523 | call_pending (void) |
491 | { |
524 | { |
492 | while (pendingcnt) |
525 | while (pendingcnt) |
493 | { |
526 | { |
494 | ANPENDING *p = pendings + --pendingcnt; |
527 | ANPENDING *p = pendings + --pendingcnt; |
… | |
… | |
506 | { |
539 | { |
507 | while (timercnt && timers [0]->at <= now) |
540 | while (timercnt && timers [0]->at <= now) |
508 | { |
541 | { |
509 | struct ev_timer *w = timers [0]; |
542 | struct ev_timer *w = timers [0]; |
510 | |
543 | |
511 | event ((W)w, EV_TIMEOUT); |
|
|
512 | |
|
|
513 | /* first reschedule or stop timer */ |
544 | /* first reschedule or stop timer */ |
514 | if (w->repeat) |
545 | if (w->repeat) |
515 | { |
546 | { |
|
|
547 | assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); |
516 | w->at = now + w->repeat; |
548 | w->at = now + w->repeat; |
517 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
|
|
518 | downheap ((WT *)timers, timercnt, 0); |
549 | downheap ((WT *)timers, timercnt, 0); |
519 | } |
550 | } |
520 | else |
551 | else |
521 | evtimer_stop (w); /* nonrepeating: stop timer */ |
552 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
|
|
553 | |
|
|
554 | event ((W)w, EV_TIMEOUT); |
522 | } |
555 | } |
523 | } |
556 | } |
524 | |
557 | |
525 | static void |
558 | static void |
526 | periodics_reify (void) |
559 | periodics_reify (void) |
… | |
… | |
531 | |
564 | |
532 | /* first reschedule or stop timer */ |
565 | /* first reschedule or stop timer */ |
533 | if (w->interval) |
566 | if (w->interval) |
534 | { |
567 | { |
535 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
568 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
536 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
569 | assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); |
537 | downheap ((WT *)periodics, periodiccnt, 0); |
570 | downheap ((WT *)periodics, periodiccnt, 0); |
538 | } |
571 | } |
539 | else |
572 | else |
540 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
573 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
541 | |
574 | |
542 | event ((W)w, EV_TIMEOUT); |
575 | event ((W)w, EV_PERIODIC); |
543 | } |
576 | } |
544 | } |
577 | } |
545 | |
578 | |
546 | static void |
579 | static void |
547 | periodics_reschedule (ev_tstamp diff) |
580 | periodics_reschedule (ev_tstamp diff) |
… | |
… | |
557 | { |
590 | { |
558 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
591 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
559 | |
592 | |
560 | if (fabs (diff) >= 1e-4) |
593 | if (fabs (diff) >= 1e-4) |
561 | { |
594 | { |
562 | evperiodic_stop (w); |
595 | ev_periodic_stop (w); |
563 | evperiodic_start (w); |
596 | ev_periodic_start (w); |
564 | |
597 | |
565 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
598 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
566 | } |
599 | } |
567 | } |
600 | } |
568 | } |
601 | } |
… | |
… | |
611 | int ev_loop_done; |
644 | int ev_loop_done; |
612 | |
645 | |
613 | void ev_loop (int flags) |
646 | void ev_loop (int flags) |
614 | { |
647 | { |
615 | double block; |
648 | double block; |
616 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
649 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
617 | |
650 | |
618 | do |
651 | do |
619 | { |
652 | { |
620 | /* queue check watchers (and execute them) */ |
653 | /* queue check watchers (and execute them) */ |
621 | if (preparecnt) |
654 | if (preparecnt) |
… | |
… | |
702 | head = &(*head)->next; |
735 | head = &(*head)->next; |
703 | } |
736 | } |
704 | } |
737 | } |
705 | |
738 | |
706 | static void |
739 | static void |
707 | ev_clear (W w) |
740 | ev_clear_pending (W w) |
708 | { |
741 | { |
709 | if (w->pending) |
742 | if (w->pending) |
710 | { |
743 | { |
711 | pendings [w->pending - 1].w = 0; |
744 | pendings [w->pending - 1].w = 0; |
712 | w->pending = 0; |
745 | w->pending = 0; |
… | |
… | |
726 | } |
759 | } |
727 | |
760 | |
728 | /*****************************************************************************/ |
761 | /*****************************************************************************/ |
729 | |
762 | |
730 | void |
763 | void |
731 | evio_start (struct ev_io *w) |
764 | ev_io_start (struct ev_io *w) |
732 | { |
765 | { |
733 | if (ev_is_active (w)) |
766 | if (ev_is_active (w)) |
734 | return; |
767 | return; |
735 | |
768 | |
736 | int fd = w->fd; |
769 | int fd = w->fd; |
|
|
770 | |
|
|
771 | assert (("ev_io_start called with negative fd", fd >= 0)); |
737 | |
772 | |
738 | ev_start ((W)w, 1); |
773 | ev_start ((W)w, 1); |
739 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
774 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
740 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
775 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
741 | |
776 | |
742 | ++fdchangecnt; |
777 | fd_change (fd); |
743 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
744 | fdchanges [fdchangecnt - 1] = fd; |
|
|
745 | |
|
|
746 | if (w->fd == 9) |
|
|
747 | printf ("start %p:%x\n", w, w->events);//D |
|
|
748 | } |
778 | } |
749 | |
779 | |
750 | void |
780 | void |
751 | evio_stop (struct ev_io *w) |
781 | ev_io_stop (struct ev_io *w) |
752 | { |
782 | { |
753 | if (w->fd == 9) |
|
|
754 | printf ("stop %p:%x\n", w, w->events);//D |
|
|
755 | ev_clear ((W)w); |
783 | ev_clear_pending ((W)w); |
756 | if (!ev_is_active (w)) |
784 | if (!ev_is_active (w)) |
757 | return; |
785 | return; |
758 | |
786 | |
759 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
787 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
760 | ev_stop ((W)w); |
788 | ev_stop ((W)w); |
761 | |
789 | |
762 | ++fdchangecnt; |
790 | fd_change (w->fd); |
763 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
764 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
765 | } |
791 | } |
766 | |
792 | |
767 | void |
793 | void |
768 | evtimer_start (struct ev_timer *w) |
794 | ev_timer_start (struct ev_timer *w) |
769 | { |
795 | { |
770 | if (ev_is_active (w)) |
796 | if (ev_is_active (w)) |
771 | return; |
797 | return; |
772 | |
798 | |
773 | w->at += now; |
799 | w->at += now; |
774 | |
800 | |
775 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
801 | assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); |
776 | |
802 | |
777 | ev_start ((W)w, ++timercnt); |
803 | ev_start ((W)w, ++timercnt); |
778 | array_needsize (timers, timermax, timercnt, ); |
804 | array_needsize (timers, timermax, timercnt, ); |
779 | timers [timercnt - 1] = w; |
805 | timers [timercnt - 1] = w; |
780 | upheap ((WT *)timers, timercnt - 1); |
806 | upheap ((WT *)timers, timercnt - 1); |
781 | } |
807 | } |
782 | |
808 | |
783 | void |
809 | void |
784 | evtimer_stop (struct ev_timer *w) |
810 | ev_timer_stop (struct ev_timer *w) |
785 | { |
811 | { |
786 | ev_clear ((W)w); |
812 | ev_clear_pending ((W)w); |
787 | if (!ev_is_active (w)) |
813 | if (!ev_is_active (w)) |
788 | return; |
814 | return; |
789 | |
815 | |
790 | if (w->active < timercnt--) |
816 | if (w->active < timercnt--) |
791 | { |
817 | { |
… | |
… | |
797 | |
823 | |
798 | ev_stop ((W)w); |
824 | ev_stop ((W)w); |
799 | } |
825 | } |
800 | |
826 | |
801 | void |
827 | void |
802 | evtimer_again (struct ev_timer *w) |
828 | ev_timer_again (struct ev_timer *w) |
803 | { |
829 | { |
804 | if (ev_is_active (w)) |
830 | if (ev_is_active (w)) |
805 | { |
831 | { |
806 | if (w->repeat) |
832 | if (w->repeat) |
807 | { |
833 | { |
808 | w->at = now + w->repeat; |
834 | w->at = now + w->repeat; |
809 | downheap ((WT *)timers, timercnt, w->active - 1); |
835 | downheap ((WT *)timers, timercnt, w->active - 1); |
810 | } |
836 | } |
811 | else |
837 | else |
812 | evtimer_stop (w); |
838 | ev_timer_stop (w); |
813 | } |
839 | } |
814 | else if (w->repeat) |
840 | else if (w->repeat) |
815 | evtimer_start (w); |
841 | ev_timer_start (w); |
816 | } |
842 | } |
817 | |
843 | |
818 | void |
844 | void |
819 | evperiodic_start (struct ev_periodic *w) |
845 | ev_periodic_start (struct ev_periodic *w) |
820 | { |
846 | { |
821 | if (ev_is_active (w)) |
847 | if (ev_is_active (w)) |
822 | return; |
848 | return; |
823 | |
849 | |
824 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
850 | assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); |
825 | |
851 | |
826 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
852 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
827 | if (w->interval) |
853 | if (w->interval) |
828 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
854 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
829 | |
855 | |
… | |
… | |
832 | periodics [periodiccnt - 1] = w; |
858 | periodics [periodiccnt - 1] = w; |
833 | upheap ((WT *)periodics, periodiccnt - 1); |
859 | upheap ((WT *)periodics, periodiccnt - 1); |
834 | } |
860 | } |
835 | |
861 | |
836 | void |
862 | void |
837 | evperiodic_stop (struct ev_periodic *w) |
863 | ev_periodic_stop (struct ev_periodic *w) |
838 | { |
864 | { |
839 | ev_clear ((W)w); |
865 | ev_clear_pending ((W)w); |
840 | if (!ev_is_active (w)) |
866 | if (!ev_is_active (w)) |
841 | return; |
867 | return; |
842 | |
868 | |
843 | if (w->active < periodiccnt--) |
869 | if (w->active < periodiccnt--) |
844 | { |
870 | { |
… | |
… | |
848 | |
874 | |
849 | ev_stop ((W)w); |
875 | ev_stop ((W)w); |
850 | } |
876 | } |
851 | |
877 | |
852 | void |
878 | void |
853 | evsignal_start (struct ev_signal *w) |
879 | ev_signal_start (struct ev_signal *w) |
854 | { |
880 | { |
855 | if (ev_is_active (w)) |
881 | if (ev_is_active (w)) |
856 | return; |
882 | return; |
|
|
883 | |
|
|
884 | assert (("ev_signal_start called with illegal signal number", w->signum > 0)); |
857 | |
885 | |
858 | ev_start ((W)w, 1); |
886 | ev_start ((W)w, 1); |
859 | array_needsize (signals, signalmax, w->signum, signals_init); |
887 | array_needsize (signals, signalmax, w->signum, signals_init); |
860 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
888 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
861 | |
889 | |
… | |
… | |
868 | sigaction (w->signum, &sa, 0); |
896 | sigaction (w->signum, &sa, 0); |
869 | } |
897 | } |
870 | } |
898 | } |
871 | |
899 | |
872 | void |
900 | void |
873 | evsignal_stop (struct ev_signal *w) |
901 | ev_signal_stop (struct ev_signal *w) |
874 | { |
902 | { |
875 | ev_clear ((W)w); |
903 | ev_clear_pending ((W)w); |
876 | if (!ev_is_active (w)) |
904 | if (!ev_is_active (w)) |
877 | return; |
905 | return; |
878 | |
906 | |
879 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
907 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
880 | ev_stop ((W)w); |
908 | ev_stop ((W)w); |
881 | |
909 | |
882 | if (!signals [w->signum - 1].head) |
910 | if (!signals [w->signum - 1].head) |
883 | signal (w->signum, SIG_DFL); |
911 | signal (w->signum, SIG_DFL); |
884 | } |
912 | } |
885 | |
913 | |
|
|
914 | void |
886 | void evidle_start (struct ev_idle *w) |
915 | ev_idle_start (struct ev_idle *w) |
887 | { |
916 | { |
888 | if (ev_is_active (w)) |
917 | if (ev_is_active (w)) |
889 | return; |
918 | return; |
890 | |
919 | |
891 | ev_start ((W)w, ++idlecnt); |
920 | ev_start ((W)w, ++idlecnt); |
892 | array_needsize (idles, idlemax, idlecnt, ); |
921 | array_needsize (idles, idlemax, idlecnt, ); |
893 | idles [idlecnt - 1] = w; |
922 | idles [idlecnt - 1] = w; |
894 | } |
923 | } |
895 | |
924 | |
|
|
925 | void |
896 | void evidle_stop (struct ev_idle *w) |
926 | ev_idle_stop (struct ev_idle *w) |
897 | { |
927 | { |
898 | ev_clear ((W)w); |
928 | ev_clear_pending ((W)w); |
899 | if (ev_is_active (w)) |
929 | if (ev_is_active (w)) |
900 | return; |
930 | return; |
901 | |
931 | |
902 | idles [w->active - 1] = idles [--idlecnt]; |
932 | idles [w->active - 1] = idles [--idlecnt]; |
903 | ev_stop ((W)w); |
933 | ev_stop ((W)w); |
904 | } |
934 | } |
905 | |
935 | |
|
|
936 | void |
906 | void evprepare_start (struct ev_prepare *w) |
937 | ev_prepare_start (struct ev_prepare *w) |
907 | { |
938 | { |
908 | if (ev_is_active (w)) |
939 | if (ev_is_active (w)) |
909 | return; |
940 | return; |
910 | |
941 | |
911 | ev_start ((W)w, ++preparecnt); |
942 | ev_start ((W)w, ++preparecnt); |
912 | array_needsize (prepares, preparemax, preparecnt, ); |
943 | array_needsize (prepares, preparemax, preparecnt, ); |
913 | prepares [preparecnt - 1] = w; |
944 | prepares [preparecnt - 1] = w; |
914 | } |
945 | } |
915 | |
946 | |
|
|
947 | void |
916 | void evprepare_stop (struct ev_prepare *w) |
948 | ev_prepare_stop (struct ev_prepare *w) |
917 | { |
949 | { |
918 | ev_clear ((W)w); |
950 | ev_clear_pending ((W)w); |
919 | if (ev_is_active (w)) |
951 | if (ev_is_active (w)) |
920 | return; |
952 | return; |
921 | |
953 | |
922 | prepares [w->active - 1] = prepares [--preparecnt]; |
954 | prepares [w->active - 1] = prepares [--preparecnt]; |
923 | ev_stop ((W)w); |
955 | ev_stop ((W)w); |
924 | } |
956 | } |
925 | |
957 | |
|
|
958 | void |
926 | void evcheck_start (struct ev_check *w) |
959 | ev_check_start (struct ev_check *w) |
927 | { |
960 | { |
928 | if (ev_is_active (w)) |
961 | if (ev_is_active (w)) |
929 | return; |
962 | return; |
930 | |
963 | |
931 | ev_start ((W)w, ++checkcnt); |
964 | ev_start ((W)w, ++checkcnt); |
932 | array_needsize (checks, checkmax, checkcnt, ); |
965 | array_needsize (checks, checkmax, checkcnt, ); |
933 | checks [checkcnt - 1] = w; |
966 | checks [checkcnt - 1] = w; |
934 | } |
967 | } |
935 | |
968 | |
|
|
969 | void |
936 | void evcheck_stop (struct ev_check *w) |
970 | ev_check_stop (struct ev_check *w) |
937 | { |
971 | { |
938 | ev_clear ((W)w); |
972 | ev_clear_pending ((W)w); |
939 | if (ev_is_active (w)) |
973 | if (ev_is_active (w)) |
940 | return; |
974 | return; |
941 | |
975 | |
942 | checks [w->active - 1] = checks [--checkcnt]; |
976 | checks [w->active - 1] = checks [--checkcnt]; |
943 | ev_stop ((W)w); |
977 | ev_stop ((W)w); |
944 | } |
978 | } |
945 | |
979 | |
|
|
980 | void |
946 | void evchild_start (struct ev_child *w) |
981 | ev_child_start (struct ev_child *w) |
947 | { |
982 | { |
948 | if (ev_is_active (w)) |
983 | if (ev_is_active (w)) |
949 | return; |
984 | return; |
950 | |
985 | |
951 | ev_start ((W)w, 1); |
986 | ev_start ((W)w, 1); |
952 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
987 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
953 | } |
988 | } |
954 | |
989 | |
|
|
990 | void |
955 | void evchild_stop (struct ev_child *w) |
991 | ev_child_stop (struct ev_child *w) |
956 | { |
992 | { |
957 | ev_clear ((W)w); |
993 | ev_clear_pending ((W)w); |
958 | if (ev_is_active (w)) |
994 | if (ev_is_active (w)) |
959 | return; |
995 | return; |
960 | |
996 | |
961 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
997 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
962 | ev_stop ((W)w); |
998 | ev_stop ((W)w); |
… | |
… | |
976 | once_cb (struct ev_once *once, int revents) |
1012 | once_cb (struct ev_once *once, int revents) |
977 | { |
1013 | { |
978 | void (*cb)(int revents, void *arg) = once->cb; |
1014 | void (*cb)(int revents, void *arg) = once->cb; |
979 | void *arg = once->arg; |
1015 | void *arg = once->arg; |
980 | |
1016 | |
981 | evio_stop (&once->io); |
1017 | ev_io_stop (&once->io); |
982 | evtimer_stop (&once->to); |
1018 | ev_timer_stop (&once->to); |
983 | free (once); |
1019 | free (once); |
984 | |
1020 | |
985 | cb (revents, arg); |
1021 | cb (revents, arg); |
986 | } |
1022 | } |
987 | |
1023 | |
… | |
… | |
1001 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1037 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
1002 | { |
1038 | { |
1003 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1039 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
1004 | |
1040 | |
1005 | if (!once) |
1041 | if (!once) |
1006 | cb (EV_ERROR, arg); |
1042 | cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); |
1007 | else |
1043 | else |
1008 | { |
1044 | { |
1009 | once->cb = cb; |
1045 | once->cb = cb; |
1010 | once->arg = arg; |
1046 | once->arg = arg; |
1011 | |
1047 | |
1012 | evw_init (&once->io, once_cb_io); |
1048 | ev_watcher_init (&once->io, once_cb_io); |
1013 | |
|
|
1014 | if (fd >= 0) |
1049 | if (fd >= 0) |
1015 | { |
1050 | { |
1016 | evio_set (&once->io, fd, events); |
1051 | ev_io_set (&once->io, fd, events); |
1017 | evio_start (&once->io); |
1052 | ev_io_start (&once->io); |
1018 | } |
1053 | } |
1019 | |
1054 | |
1020 | evw_init (&once->to, once_cb_to); |
1055 | ev_watcher_init (&once->to, once_cb_to); |
1021 | |
|
|
1022 | if (timeout >= 0.) |
1056 | if (timeout >= 0.) |
1023 | { |
1057 | { |
1024 | evtimer_set (&once->to, timeout, 0.); |
1058 | ev_timer_set (&once->to, timeout, 0.); |
1025 | evtimer_start (&once->to); |
1059 | ev_timer_start (&once->to); |
1026 | } |
1060 | } |
1027 | } |
1061 | } |
1028 | } |
1062 | } |
1029 | |
1063 | |
1030 | /*****************************************************************************/ |
1064 | /*****************************************************************************/ |
… | |
… | |
1041 | |
1075 | |
1042 | static void |
1076 | static void |
1043 | ocb (struct ev_timer *w, int revents) |
1077 | ocb (struct ev_timer *w, int revents) |
1044 | { |
1078 | { |
1045 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1079 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1046 | evtimer_stop (w); |
1080 | ev_timer_stop (w); |
1047 | evtimer_start (w); |
1081 | ev_timer_start (w); |
1048 | } |
1082 | } |
1049 | |
1083 | |
1050 | static void |
1084 | static void |
1051 | scb (struct ev_signal *w, int revents) |
1085 | scb (struct ev_signal *w, int revents) |
1052 | { |
1086 | { |
1053 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1087 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1054 | evio_stop (&wio); |
1088 | ev_io_stop (&wio); |
1055 | evio_start (&wio); |
1089 | ev_io_start (&wio); |
1056 | } |
1090 | } |
1057 | |
1091 | |
1058 | static void |
1092 | static void |
1059 | gcb (struct ev_signal *w, int revents) |
1093 | gcb (struct ev_signal *w, int revents) |
1060 | { |
1094 | { |
… | |
… | |
1064 | |
1098 | |
1065 | int main (void) |
1099 | int main (void) |
1066 | { |
1100 | { |
1067 | ev_init (0); |
1101 | ev_init (0); |
1068 | |
1102 | |
1069 | evio_init (&wio, sin_cb, 0, EV_READ); |
1103 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
1070 | evio_start (&wio); |
1104 | ev_io_start (&wio); |
1071 | |
1105 | |
1072 | struct ev_timer t[10000]; |
1106 | struct ev_timer t[10000]; |
1073 | |
1107 | |
1074 | #if 0 |
1108 | #if 0 |
1075 | int i; |
1109 | int i; |
1076 | for (i = 0; i < 10000; ++i) |
1110 | for (i = 0; i < 10000; ++i) |
1077 | { |
1111 | { |
1078 | struct ev_timer *w = t + i; |
1112 | struct ev_timer *w = t + i; |
1079 | evw_init (w, ocb, i); |
1113 | ev_watcher_init (w, ocb, i); |
1080 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1114 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
1081 | evtimer_start (w); |
1115 | ev_timer_start (w); |
1082 | if (drand48 () < 0.5) |
1116 | if (drand48 () < 0.5) |
1083 | evtimer_stop (w); |
1117 | ev_timer_stop (w); |
1084 | } |
1118 | } |
1085 | #endif |
1119 | #endif |
1086 | |
1120 | |
1087 | struct ev_timer t1; |
1121 | struct ev_timer t1; |
1088 | evtimer_init (&t1, ocb, 5, 10); |
1122 | ev_timer_init (&t1, ocb, 5, 10); |
1089 | evtimer_start (&t1); |
1123 | ev_timer_start (&t1); |
1090 | |
1124 | |
1091 | struct ev_signal sig; |
1125 | struct ev_signal sig; |
1092 | evsignal_init (&sig, scb, SIGQUIT); |
1126 | ev_signal_init (&sig, scb, SIGQUIT); |
1093 | evsignal_start (&sig); |
1127 | ev_signal_start (&sig); |
1094 | |
1128 | |
1095 | struct ev_check cw; |
1129 | struct ev_check cw; |
1096 | evcheck_init (&cw, gcb); |
1130 | ev_check_init (&cw, gcb); |
1097 | evcheck_start (&cw); |
1131 | ev_check_start (&cw); |
1098 | |
1132 | |
1099 | struct ev_idle iw; |
1133 | struct ev_idle iw; |
1100 | evidle_init (&iw, gcb); |
1134 | ev_idle_init (&iw, gcb); |
1101 | evidle_start (&iw); |
1135 | ev_idle_start (&iw); |
1102 | |
1136 | |
1103 | ev_loop (0); |
1137 | ev_loop (0); |
1104 | |
1138 | |
1105 | return 0; |
1139 | return 0; |
1106 | } |
1140 | } |