|
|
1 | /* |
|
|
2 | * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> |
|
|
3 | * All rights reserved. |
|
|
4 | * |
|
|
5 | * Redistribution and use in source and binary forms, with or without |
|
|
6 | * modification, are permitted provided that the following conditions are |
|
|
7 | * met: |
|
|
8 | * |
|
|
9 | * * Redistributions of source code must retain the above copyright |
|
|
10 | * notice, this list of conditions and the following disclaimer. |
|
|
11 | * |
|
|
12 | * * Redistributions in binary form must reproduce the above |
|
|
13 | * copyright notice, this list of conditions and the following |
|
|
14 | * disclaimer in the documentation and/or other materials provided |
|
|
15 | * with the distribution. |
|
|
16 | * |
|
|
17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
|
18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
|
19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
|
20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
|
21 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
|
22 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
|
23 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
|
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
|
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
|
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. |
|
|
28 | */ |
|
|
29 | |
1 | #include <math.h> |
30 | #include <math.h> |
2 | #include <stdlib.h> |
31 | #include <stdlib.h> |
3 | #include <unistd.h> |
32 | #include <unistd.h> |
4 | #include <fcntl.h> |
33 | #include <fcntl.h> |
5 | #include <signal.h> |
34 | #include <signal.h> |
|
|
35 | #include <stddef.h> |
6 | |
36 | |
7 | #include <stdio.h> |
37 | #include <stdio.h> |
8 | |
38 | |
9 | #include <assert.h> |
39 | #include <assert.h> |
10 | #include <errno.h> |
40 | #include <errno.h> |
|
|
41 | #include <sys/types.h> |
|
|
42 | #include <sys/wait.h> |
11 | #include <sys/time.h> |
43 | #include <sys/time.h> |
12 | #include <time.h> |
44 | #include <time.h> |
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
|
|
15 | |
45 | |
16 | #ifndef HAVE_MONOTONIC |
46 | #ifndef HAVE_MONOTONIC |
17 | # ifdef CLOCK_MONOTONIC |
47 | # ifdef CLOCK_MONOTONIC |
18 | # define HAVE_MONOTONIC 1 |
48 | # define HAVE_MONOTONIC 1 |
19 | # endif |
49 | # endif |
… | |
… | |
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
61 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
32 | #endif |
62 | #endif |
33 | |
63 | |
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
64 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
35 | #define MAX_BLOCKTIME 60. |
65 | #define MAX_BLOCKTIME 60. |
|
|
66 | #define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ |
36 | |
67 | |
37 | #include "ev.h" |
68 | #include "ev.h" |
38 | |
69 | |
39 | typedef struct ev_watcher *W; |
70 | typedef struct ev_watcher *W; |
40 | typedef struct ev_watcher_list *WL; |
71 | typedef struct ev_watcher_list *WL; |
… | |
… | |
82 | } |
113 | } |
83 | |
114 | |
84 | #define array_needsize(base,cur,cnt,init) \ |
115 | #define array_needsize(base,cur,cnt,init) \ |
85 | if ((cnt) > cur) \ |
116 | if ((cnt) > cur) \ |
86 | { \ |
117 | { \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
118 | int newcnt = cur; \ |
88 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
119 | do \ |
|
|
120 | { \ |
|
|
121 | newcnt = (newcnt << 1) | 4 & ~3; \ |
|
|
122 | } \ |
|
|
123 | while ((cnt) > newcnt); \ |
|
|
124 | \ |
89 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
125 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
90 | init (base + cur, newcnt - cur); \ |
126 | init (base + cur, newcnt - cur); \ |
91 | cur = newcnt; \ |
127 | cur = newcnt; \ |
92 | } |
128 | } |
93 | |
129 | |
94 | /*****************************************************************************/ |
130 | /*****************************************************************************/ |
95 | |
131 | |
96 | typedef struct |
132 | typedef struct |
97 | { |
133 | { |
98 | struct ev_io *head; |
134 | struct ev_io *head; |
99 | unsigned char wev, rev; /* want, received event set */ |
135 | int events; |
100 | } ANFD; |
136 | } ANFD; |
101 | |
137 | |
102 | static ANFD *anfds; |
138 | static ANFD *anfds; |
103 | static int anfdmax; |
139 | static int anfdmax; |
104 | |
140 | |
105 | static int *fdchanges; |
|
|
106 | static int fdchangemax, fdchangecnt; |
|
|
107 | |
|
|
108 | static void |
141 | static void |
109 | anfds_init (ANFD *base, int count) |
142 | anfds_init (ANFD *base, int count) |
110 | { |
143 | { |
111 | while (count--) |
144 | while (count--) |
112 | { |
145 | { |
113 | base->head = 0; |
146 | base->head = 0; |
114 | base->wev = base->rev = EV_NONE; |
147 | base->events = EV_NONE; |
115 | ++base; |
148 | ++base; |
116 | } |
149 | } |
117 | } |
150 | } |
118 | |
151 | |
119 | typedef struct |
152 | typedef struct |
… | |
… | |
126 | static int pendingmax, pendingcnt; |
159 | static int pendingmax, pendingcnt; |
127 | |
160 | |
128 | static void |
161 | static void |
129 | event (W w, int events) |
162 | event (W w, int events) |
130 | { |
163 | { |
|
|
164 | if (w->active) |
|
|
165 | { |
131 | w->pending = ++pendingcnt; |
166 | w->pending = ++pendingcnt; |
132 | array_needsize (pendings, pendingmax, pendingcnt, ); |
167 | array_needsize (pendings, pendingmax, pendingcnt, ); |
133 | pendings [pendingcnt - 1].w = w; |
168 | pendings [pendingcnt - 1].w = w; |
134 | pendings [pendingcnt - 1].events = events; |
169 | pendings [pendingcnt - 1].events = events; |
|
|
170 | } |
|
|
171 | } |
|
|
172 | |
|
|
173 | static void |
|
|
174 | queue_events (W *events, int eventcnt, int type) |
|
|
175 | { |
|
|
176 | int i; |
|
|
177 | |
|
|
178 | for (i = 0; i < eventcnt; ++i) |
|
|
179 | event (events [i], type); |
135 | } |
180 | } |
136 | |
181 | |
137 | static void |
182 | static void |
138 | fd_event (int fd, int events) |
183 | fd_event (int fd, int events) |
139 | { |
184 | { |
… | |
… | |
147 | if (ev) |
192 | if (ev) |
148 | event ((W)w, ev); |
193 | event ((W)w, ev); |
149 | } |
194 | } |
150 | } |
195 | } |
151 | |
196 | |
|
|
197 | /*****************************************************************************/ |
|
|
198 | |
|
|
199 | static int *fdchanges; |
|
|
200 | static int fdchangemax, fdchangecnt; |
|
|
201 | |
152 | static void |
202 | static void |
153 | queue_events (W *events, int eventcnt, int type) |
203 | fd_reify (void) |
154 | { |
204 | { |
155 | int i; |
205 | int i; |
156 | |
206 | |
157 | for (i = 0; i < eventcnt; ++i) |
207 | for (i = 0; i < fdchangecnt; ++i) |
158 | event (events [i], type); |
208 | { |
|
|
209 | int fd = fdchanges [i]; |
|
|
210 | ANFD *anfd = anfds + fd; |
|
|
211 | struct ev_io *w; |
|
|
212 | |
|
|
213 | int events = 0; |
|
|
214 | |
|
|
215 | for (w = anfd->head; w; w = w->next) |
|
|
216 | events |= w->events; |
|
|
217 | |
|
|
218 | anfd->events &= ~EV_REIFY; |
|
|
219 | |
|
|
220 | if (anfd->events != events) |
|
|
221 | { |
|
|
222 | method_modify (fd, anfd->events, events); |
|
|
223 | anfd->events = events; |
|
|
224 | } |
|
|
225 | } |
|
|
226 | |
|
|
227 | fdchangecnt = 0; |
|
|
228 | } |
|
|
229 | |
|
|
230 | static void |
|
|
231 | fd_change (int fd) |
|
|
232 | { |
|
|
233 | if (anfds [fd].events & EV_REIFY) |
|
|
234 | return; |
|
|
235 | |
|
|
236 | anfds [fd].events |= EV_REIFY; |
|
|
237 | |
|
|
238 | ++fdchangecnt; |
|
|
239 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
240 | fdchanges [fdchangecnt - 1] = fd; |
|
|
241 | } |
|
|
242 | |
|
|
243 | /* called on EBADF to verify fds */ |
|
|
244 | static void |
|
|
245 | fd_recheck (void) |
|
|
246 | { |
|
|
247 | int fd; |
|
|
248 | |
|
|
249 | for (fd = 0; fd < anfdmax; ++fd) |
|
|
250 | if (anfds [fd].events) |
|
|
251 | if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) |
|
|
252 | while (anfds [fd].head) |
|
|
253 | { |
|
|
254 | event ((W)anfds [fd].head, EV_ERROR); |
|
|
255 | ev_io_stop (anfds [fd].head); |
|
|
256 | } |
159 | } |
257 | } |
160 | |
258 | |
161 | /*****************************************************************************/ |
259 | /*****************************************************************************/ |
162 | |
260 | |
163 | static struct ev_timer **timers; |
261 | static struct ev_timer **timers; |
… | |
… | |
272 | |
370 | |
273 | /* rather than sort out wether we really need nb, set it */ |
371 | /* rather than sort out wether we really need nb, set it */ |
274 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
372 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
275 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
373 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
276 | |
374 | |
277 | evio_set (&sigev, sigpipe [0], EV_READ); |
375 | ev_io_set (&sigev, sigpipe [0], EV_READ); |
278 | evio_start (&sigev); |
376 | ev_io_start (&sigev); |
279 | } |
377 | } |
280 | |
378 | |
281 | /*****************************************************************************/ |
379 | /*****************************************************************************/ |
282 | |
380 | |
283 | static struct ev_idle **idles; |
381 | static struct ev_idle **idles; |
284 | static int idlemax, idlecnt; |
382 | static int idlemax, idlecnt; |
285 | |
383 | |
|
|
384 | static struct ev_prepare **prepares; |
|
|
385 | static int preparemax, preparecnt; |
|
|
386 | |
286 | static struct ev_check **checks; |
387 | static struct ev_check **checks; |
287 | static int checkmax, checkcnt; |
388 | static int checkmax, checkcnt; |
288 | |
389 | |
289 | /*****************************************************************************/ |
390 | /*****************************************************************************/ |
290 | |
391 | |
|
|
392 | static struct ev_child *childs [PID_HASHSIZE]; |
|
|
393 | static struct ev_signal childev; |
|
|
394 | |
|
|
395 | #ifndef WCONTINUED |
|
|
396 | # define WCONTINUED 0 |
|
|
397 | #endif |
|
|
398 | |
|
|
399 | static void |
|
|
400 | childcb (struct ev_signal *sw, int revents) |
|
|
401 | { |
|
|
402 | struct ev_child *w; |
|
|
403 | int pid, status; |
|
|
404 | |
|
|
405 | while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) |
|
|
406 | for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) |
|
|
407 | if (w->pid == pid || w->pid == -1) |
|
|
408 | { |
|
|
409 | w->status = status; |
|
|
410 | event ((W)w, EV_CHILD); |
|
|
411 | } |
|
|
412 | } |
|
|
413 | |
|
|
414 | /*****************************************************************************/ |
|
|
415 | |
291 | #if HAVE_EPOLL |
416 | #if HAVE_EPOLL |
292 | # include "ev_epoll.c" |
417 | # include "ev_epoll.c" |
293 | #endif |
418 | #endif |
294 | #if HAVE_SELECT |
419 | #if HAVE_SELECT |
295 | # include "ev_select.c" |
420 | # include "ev_select.c" |
296 | #endif |
421 | #endif |
297 | |
422 | |
|
|
423 | int |
|
|
424 | ev_version_major (void) |
|
|
425 | { |
|
|
426 | return EV_VERSION_MAJOR; |
|
|
427 | } |
|
|
428 | |
|
|
429 | int |
|
|
430 | ev_version_minor (void) |
|
|
431 | { |
|
|
432 | return EV_VERSION_MINOR; |
|
|
433 | } |
|
|
434 | |
298 | int ev_init (int flags) |
435 | int ev_init (int flags) |
299 | { |
436 | { |
|
|
437 | if (!ev_method) |
|
|
438 | { |
300 | #if HAVE_MONOTONIC |
439 | #if HAVE_MONOTONIC |
301 | { |
440 | { |
302 | struct timespec ts; |
441 | struct timespec ts; |
303 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
442 | if (!clock_gettime (CLOCK_MONOTONIC, &ts)) |
304 | have_monotonic = 1; |
443 | have_monotonic = 1; |
305 | } |
444 | } |
306 | #endif |
445 | #endif |
307 | |
446 | |
308 | ev_now = ev_time (); |
447 | ev_now = ev_time (); |
309 | now = get_clock (); |
448 | now = get_clock (); |
310 | diff = ev_now - now; |
449 | diff = ev_now - now; |
311 | |
450 | |
312 | if (pipe (sigpipe)) |
451 | if (pipe (sigpipe)) |
313 | return 0; |
452 | return 0; |
314 | |
453 | |
315 | ev_method = EVMETHOD_NONE; |
454 | ev_method = EVMETHOD_NONE; |
316 | #if HAVE_EPOLL |
455 | #if HAVE_EPOLL |
317 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
456 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
318 | #endif |
457 | #endif |
319 | #if HAVE_SELECT |
458 | #if HAVE_SELECT |
320 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
459 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
321 | #endif |
460 | #endif |
322 | |
461 | |
323 | if (ev_method) |
462 | if (ev_method) |
324 | { |
463 | { |
325 | evw_init (&sigev, sigcb); |
464 | ev_watcher_init (&sigev, sigcb); |
326 | siginit (); |
465 | siginit (); |
|
|
466 | |
|
|
467 | ev_signal_init (&childev, childcb, SIGCHLD); |
|
|
468 | ev_signal_start (&childev); |
|
|
469 | } |
327 | } |
470 | } |
328 | |
471 | |
329 | return ev_method; |
472 | return ev_method; |
330 | } |
473 | } |
331 | |
474 | |
332 | /*****************************************************************************/ |
475 | /*****************************************************************************/ |
333 | |
476 | |
|
|
477 | void |
334 | void ev_prefork (void) |
478 | ev_prefork (void) |
335 | { |
479 | { |
336 | /* nop */ |
480 | /* nop */ |
337 | } |
481 | } |
338 | |
482 | |
|
|
483 | void |
339 | void ev_postfork_parent (void) |
484 | ev_postfork_parent (void) |
340 | { |
485 | { |
341 | /* nop */ |
486 | /* nop */ |
342 | } |
487 | } |
343 | |
488 | |
|
|
489 | void |
344 | void ev_postfork_child (void) |
490 | ev_postfork_child (void) |
345 | { |
491 | { |
346 | #if HAVE_EPOLL |
492 | #if HAVE_EPOLL |
347 | if (ev_method == EVMETHOD_EPOLL) |
493 | if (ev_method == EVMETHOD_EPOLL) |
348 | epoll_postfork_child (); |
494 | epoll_postfork_child (); |
349 | #endif |
495 | #endif |
350 | |
496 | |
351 | evio_stop (&sigev); |
497 | ev_io_stop (&sigev); |
352 | close (sigpipe [0]); |
498 | close (sigpipe [0]); |
353 | close (sigpipe [1]); |
499 | close (sigpipe [1]); |
354 | pipe (sigpipe); |
500 | pipe (sigpipe); |
355 | siginit (); |
501 | siginit (); |
356 | } |
502 | } |
357 | |
503 | |
358 | /*****************************************************************************/ |
504 | /*****************************************************************************/ |
359 | |
505 | |
360 | static void |
506 | static void |
361 | fd_reify (void) |
|
|
362 | { |
|
|
363 | int i; |
|
|
364 | |
|
|
365 | for (i = 0; i < fdchangecnt; ++i) |
|
|
366 | { |
|
|
367 | int fd = fdchanges [i]; |
|
|
368 | ANFD *anfd = anfds + fd; |
|
|
369 | struct ev_io *w; |
|
|
370 | |
|
|
371 | int wev = 0; |
|
|
372 | |
|
|
373 | for (w = anfd->head; w; w = w->next) |
|
|
374 | wev |= w->events; |
|
|
375 | |
|
|
376 | if (anfd->wev != wev) |
|
|
377 | { |
|
|
378 | method_modify (fd, anfd->wev, wev); |
|
|
379 | anfd->wev = wev; |
|
|
380 | } |
|
|
381 | } |
|
|
382 | |
|
|
383 | fdchangecnt = 0; |
|
|
384 | } |
|
|
385 | |
|
|
386 | static void |
|
|
387 | call_pending () |
507 | call_pending (void) |
388 | { |
508 | { |
389 | int i; |
509 | while (pendingcnt) |
390 | |
|
|
391 | for (i = 0; i < pendingcnt; ++i) |
|
|
392 | { |
510 | { |
393 | ANPENDING *p = pendings + i; |
511 | ANPENDING *p = pendings + --pendingcnt; |
394 | |
512 | |
395 | if (p->w) |
513 | if (p->w) |
396 | { |
514 | { |
397 | p->w->pending = 0; |
515 | p->w->pending = 0; |
398 | p->w->cb (p->w, p->events); |
516 | p->w->cb (p->w, p->events); |
399 | } |
517 | } |
400 | } |
518 | } |
401 | |
|
|
402 | pendingcnt = 0; |
|
|
403 | } |
519 | } |
404 | |
520 | |
405 | static void |
521 | static void |
406 | timers_reify () |
522 | timers_reify (void) |
407 | { |
523 | { |
408 | while (timercnt && timers [0]->at <= now) |
524 | while (timercnt && timers [0]->at <= now) |
409 | { |
525 | { |
410 | struct ev_timer *w = timers [0]; |
526 | struct ev_timer *w = timers [0]; |
|
|
527 | |
|
|
528 | event ((W)w, EV_TIMEOUT); |
411 | |
529 | |
412 | /* first reschedule or stop timer */ |
530 | /* first reschedule or stop timer */ |
413 | if (w->repeat) |
531 | if (w->repeat) |
414 | { |
532 | { |
415 | w->at = now + w->repeat; |
533 | w->at = now + w->repeat; |
416 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
534 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
417 | downheap ((WT *)timers, timercnt, 0); |
535 | downheap ((WT *)timers, timercnt, 0); |
418 | } |
536 | } |
419 | else |
537 | else |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
538 | ev_timer_stop (w); /* nonrepeating: stop timer */ |
421 | |
|
|
422 | event ((W)w, EV_TIMEOUT); |
|
|
423 | } |
539 | } |
424 | } |
540 | } |
425 | |
541 | |
426 | static void |
542 | static void |
427 | periodics_reify () |
543 | periodics_reify (void) |
428 | { |
544 | { |
429 | while (periodiccnt && periodics [0]->at <= ev_now) |
545 | while (periodiccnt && periodics [0]->at <= ev_now) |
430 | { |
546 | { |
431 | struct ev_periodic *w = periodics [0]; |
547 | struct ev_periodic *w = periodics [0]; |
432 | |
548 | |
… | |
… | |
436 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
552 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
437 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
553 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
438 | downheap ((WT *)periodics, periodiccnt, 0); |
554 | downheap ((WT *)periodics, periodiccnt, 0); |
439 | } |
555 | } |
440 | else |
556 | else |
441 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
557 | ev_periodic_stop (w); /* nonrepeating: stop timer */ |
442 | |
558 | |
443 | event ((W)w, EV_TIMEOUT); |
559 | event ((W)w, EV_TIMEOUT); |
444 | } |
560 | } |
445 | } |
561 | } |
446 | |
562 | |
… | |
… | |
458 | { |
574 | { |
459 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
575 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
460 | |
576 | |
461 | if (fabs (diff) >= 1e-4) |
577 | if (fabs (diff) >= 1e-4) |
462 | { |
578 | { |
463 | evperiodic_stop (w); |
579 | ev_periodic_stop (w); |
464 | evperiodic_start (w); |
580 | ev_periodic_start (w); |
465 | |
581 | |
466 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
582 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
467 | } |
583 | } |
468 | } |
584 | } |
469 | } |
585 | } |
470 | } |
586 | } |
471 | |
587 | |
472 | static void |
588 | static void |
473 | time_update () |
589 | time_update (void) |
474 | { |
590 | { |
475 | int i; |
591 | int i; |
476 | |
592 | |
477 | ev_now = ev_time (); |
593 | ev_now = ev_time (); |
478 | |
594 | |
… | |
… | |
512 | int ev_loop_done; |
628 | int ev_loop_done; |
513 | |
629 | |
514 | void ev_loop (int flags) |
630 | void ev_loop (int flags) |
515 | { |
631 | { |
516 | double block; |
632 | double block; |
517 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
633 | ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; |
518 | |
|
|
519 | if (checkcnt) |
|
|
520 | { |
|
|
521 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
522 | call_pending (); |
|
|
523 | } |
|
|
524 | |
634 | |
525 | do |
635 | do |
526 | { |
636 | { |
|
|
637 | /* queue check watchers (and execute them) */ |
|
|
638 | if (preparecnt) |
|
|
639 | { |
|
|
640 | queue_events ((W *)prepares, preparecnt, EV_PREPARE); |
|
|
641 | call_pending (); |
|
|
642 | } |
|
|
643 | |
527 | /* update fd-related kernel structures */ |
644 | /* update fd-related kernel structures */ |
528 | fd_reify (); |
645 | fd_reify (); |
529 | |
646 | |
530 | /* calculate blocking time */ |
647 | /* calculate blocking time */ |
531 | |
648 | |
532 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
649 | /* we only need this for !monotonic clockor timers, but as we basically |
|
|
650 | always have timers, we just calculate it always */ |
533 | ev_now = ev_time (); |
651 | ev_now = ev_time (); |
534 | |
652 | |
535 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
653 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
536 | block = 0.; |
654 | block = 0.; |
537 | else |
655 | else |
… | |
… | |
557 | |
675 | |
558 | /* update ev_now, do magic */ |
676 | /* update ev_now, do magic */ |
559 | time_update (); |
677 | time_update (); |
560 | |
678 | |
561 | /* queue pending timers and reschedule them */ |
679 | /* queue pending timers and reschedule them */ |
|
|
680 | timers_reify (); /* relative timers called last */ |
562 | periodics_reify (); /* absolute timers first */ |
681 | periodics_reify (); /* absolute timers called first */ |
563 | timers_reify (); /* relative timers second */ |
|
|
564 | |
682 | |
565 | /* queue idle watchers unless io or timers are pending */ |
683 | /* queue idle watchers unless io or timers are pending */ |
566 | if (!pendingcnt) |
684 | if (!pendingcnt) |
567 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
685 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
568 | |
686 | |
569 | /* queue check and possibly idle watchers */ |
687 | /* queue check watchers, to be executed first */ |
|
|
688 | if (checkcnt) |
570 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
689 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
571 | |
690 | |
572 | call_pending (); |
691 | call_pending (); |
573 | } |
692 | } |
574 | while (!ev_loop_done); |
693 | while (!ev_loop_done); |
575 | |
694 | |
… | |
… | |
600 | head = &(*head)->next; |
719 | head = &(*head)->next; |
601 | } |
720 | } |
602 | } |
721 | } |
603 | |
722 | |
604 | static void |
723 | static void |
|
|
724 | ev_clear (W w) |
|
|
725 | { |
|
|
726 | if (w->pending) |
|
|
727 | { |
|
|
728 | pendings [w->pending - 1].w = 0; |
|
|
729 | w->pending = 0; |
|
|
730 | } |
|
|
731 | } |
|
|
732 | |
|
|
733 | static void |
605 | ev_start (W w, int active) |
734 | ev_start (W w, int active) |
606 | { |
735 | { |
607 | w->pending = 0; |
|
|
608 | w->active = active; |
736 | w->active = active; |
609 | } |
737 | } |
610 | |
738 | |
611 | static void |
739 | static void |
612 | ev_stop (W w) |
740 | ev_stop (W w) |
613 | { |
741 | { |
614 | if (w->pending) |
|
|
615 | pendings [w->pending - 1].w = 0; |
|
|
616 | |
|
|
617 | w->active = 0; |
742 | w->active = 0; |
618 | } |
743 | } |
619 | |
744 | |
620 | /*****************************************************************************/ |
745 | /*****************************************************************************/ |
621 | |
746 | |
622 | void |
747 | void |
623 | evio_start (struct ev_io *w) |
748 | ev_io_start (struct ev_io *w) |
624 | { |
749 | { |
625 | if (ev_is_active (w)) |
750 | if (ev_is_active (w)) |
626 | return; |
751 | return; |
627 | |
752 | |
628 | int fd = w->fd; |
753 | int fd = w->fd; |
629 | |
754 | |
630 | ev_start ((W)w, 1); |
755 | ev_start ((W)w, 1); |
631 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
756 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
632 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
757 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
633 | |
758 | |
634 | ++fdchangecnt; |
759 | fd_change (fd); |
635 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
636 | fdchanges [fdchangecnt - 1] = fd; |
|
|
637 | } |
760 | } |
638 | |
761 | |
639 | void |
762 | void |
640 | evio_stop (struct ev_io *w) |
763 | ev_io_stop (struct ev_io *w) |
641 | { |
764 | { |
|
|
765 | ev_clear ((W)w); |
642 | if (!ev_is_active (w)) |
766 | if (!ev_is_active (w)) |
643 | return; |
767 | return; |
644 | |
768 | |
645 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
769 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
646 | ev_stop ((W)w); |
770 | ev_stop ((W)w); |
647 | |
771 | |
648 | ++fdchangecnt; |
772 | fd_change (w->fd); |
649 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
|
|
650 | fdchanges [fdchangecnt - 1] = w->fd; |
|
|
651 | } |
773 | } |
652 | |
774 | |
653 | |
|
|
654 | void |
775 | void |
655 | evtimer_start (struct ev_timer *w) |
776 | ev_timer_start (struct ev_timer *w) |
656 | { |
777 | { |
657 | if (ev_is_active (w)) |
778 | if (ev_is_active (w)) |
658 | return; |
779 | return; |
659 | |
780 | |
660 | w->at += now; |
781 | w->at += now; |
… | |
… | |
666 | timers [timercnt - 1] = w; |
787 | timers [timercnt - 1] = w; |
667 | upheap ((WT *)timers, timercnt - 1); |
788 | upheap ((WT *)timers, timercnt - 1); |
668 | } |
789 | } |
669 | |
790 | |
670 | void |
791 | void |
671 | evtimer_stop (struct ev_timer *w) |
792 | ev_timer_stop (struct ev_timer *w) |
672 | { |
793 | { |
|
|
794 | ev_clear ((W)w); |
673 | if (!ev_is_active (w)) |
795 | if (!ev_is_active (w)) |
674 | return; |
796 | return; |
675 | |
797 | |
676 | if (w->active < timercnt--) |
798 | if (w->active < timercnt--) |
677 | { |
799 | { |
… | |
… | |
683 | |
805 | |
684 | ev_stop ((W)w); |
806 | ev_stop ((W)w); |
685 | } |
807 | } |
686 | |
808 | |
687 | void |
809 | void |
688 | evtimer_again (struct ev_timer *w) |
810 | ev_timer_again (struct ev_timer *w) |
689 | { |
811 | { |
690 | if (ev_is_active (w)) |
812 | if (ev_is_active (w)) |
691 | { |
813 | { |
692 | if (w->repeat) |
814 | if (w->repeat) |
693 | { |
815 | { |
694 | w->at = now + w->repeat; |
816 | w->at = now + w->repeat; |
695 | downheap ((WT *)timers, timercnt, w->active - 1); |
817 | downheap ((WT *)timers, timercnt, w->active - 1); |
696 | } |
818 | } |
697 | else |
819 | else |
698 | evtimer_stop (w); |
820 | ev_timer_stop (w); |
699 | } |
821 | } |
700 | else if (w->repeat) |
822 | else if (w->repeat) |
701 | evtimer_start (w); |
823 | ev_timer_start (w); |
702 | } |
824 | } |
703 | |
825 | |
704 | void |
826 | void |
705 | evperiodic_start (struct ev_periodic *w) |
827 | ev_periodic_start (struct ev_periodic *w) |
706 | { |
828 | { |
707 | if (ev_is_active (w)) |
829 | if (ev_is_active (w)) |
708 | return; |
830 | return; |
709 | |
831 | |
710 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
832 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
… | |
… | |
718 | periodics [periodiccnt - 1] = w; |
840 | periodics [periodiccnt - 1] = w; |
719 | upheap ((WT *)periodics, periodiccnt - 1); |
841 | upheap ((WT *)periodics, periodiccnt - 1); |
720 | } |
842 | } |
721 | |
843 | |
722 | void |
844 | void |
723 | evperiodic_stop (struct ev_periodic *w) |
845 | ev_periodic_stop (struct ev_periodic *w) |
724 | { |
846 | { |
|
|
847 | ev_clear ((W)w); |
725 | if (!ev_is_active (w)) |
848 | if (!ev_is_active (w)) |
726 | return; |
849 | return; |
727 | |
850 | |
728 | if (w->active < periodiccnt--) |
851 | if (w->active < periodiccnt--) |
729 | { |
852 | { |
… | |
… | |
733 | |
856 | |
734 | ev_stop ((W)w); |
857 | ev_stop ((W)w); |
735 | } |
858 | } |
736 | |
859 | |
737 | void |
860 | void |
738 | evsignal_start (struct ev_signal *w) |
861 | ev_signal_start (struct ev_signal *w) |
739 | { |
862 | { |
740 | if (ev_is_active (w)) |
863 | if (ev_is_active (w)) |
741 | return; |
864 | return; |
742 | |
865 | |
743 | ev_start ((W)w, 1); |
866 | ev_start ((W)w, 1); |
… | |
… | |
753 | sigaction (w->signum, &sa, 0); |
876 | sigaction (w->signum, &sa, 0); |
754 | } |
877 | } |
755 | } |
878 | } |
756 | |
879 | |
757 | void |
880 | void |
758 | evsignal_stop (struct ev_signal *w) |
881 | ev_signal_stop (struct ev_signal *w) |
759 | { |
882 | { |
|
|
883 | ev_clear ((W)w); |
760 | if (!ev_is_active (w)) |
884 | if (!ev_is_active (w)) |
761 | return; |
885 | return; |
762 | |
886 | |
763 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
887 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
764 | ev_stop ((W)w); |
888 | ev_stop ((W)w); |
765 | |
889 | |
766 | if (!signals [w->signum - 1].head) |
890 | if (!signals [w->signum - 1].head) |
767 | signal (w->signum, SIG_DFL); |
891 | signal (w->signum, SIG_DFL); |
768 | } |
892 | } |
769 | |
893 | |
|
|
894 | void |
770 | void evidle_start (struct ev_idle *w) |
895 | ev_idle_start (struct ev_idle *w) |
771 | { |
896 | { |
772 | if (ev_is_active (w)) |
897 | if (ev_is_active (w)) |
773 | return; |
898 | return; |
774 | |
899 | |
775 | ev_start ((W)w, ++idlecnt); |
900 | ev_start ((W)w, ++idlecnt); |
776 | array_needsize (idles, idlemax, idlecnt, ); |
901 | array_needsize (idles, idlemax, idlecnt, ); |
777 | idles [idlecnt - 1] = w; |
902 | idles [idlecnt - 1] = w; |
778 | } |
903 | } |
779 | |
904 | |
|
|
905 | void |
780 | void evidle_stop (struct ev_idle *w) |
906 | ev_idle_stop (struct ev_idle *w) |
781 | { |
907 | { |
|
|
908 | ev_clear ((W)w); |
|
|
909 | if (ev_is_active (w)) |
|
|
910 | return; |
|
|
911 | |
782 | idles [w->active - 1] = idles [--idlecnt]; |
912 | idles [w->active - 1] = idles [--idlecnt]; |
783 | ev_stop ((W)w); |
913 | ev_stop ((W)w); |
784 | } |
914 | } |
785 | |
915 | |
|
|
916 | void |
|
|
917 | ev_prepare_start (struct ev_prepare *w) |
|
|
918 | { |
|
|
919 | if (ev_is_active (w)) |
|
|
920 | return; |
|
|
921 | |
|
|
922 | ev_start ((W)w, ++preparecnt); |
|
|
923 | array_needsize (prepares, preparemax, preparecnt, ); |
|
|
924 | prepares [preparecnt - 1] = w; |
|
|
925 | } |
|
|
926 | |
|
|
927 | void |
|
|
928 | ev_prepare_stop (struct ev_prepare *w) |
|
|
929 | { |
|
|
930 | ev_clear ((W)w); |
|
|
931 | if (ev_is_active (w)) |
|
|
932 | return; |
|
|
933 | |
|
|
934 | prepares [w->active - 1] = prepares [--preparecnt]; |
|
|
935 | ev_stop ((W)w); |
|
|
936 | } |
|
|
937 | |
|
|
938 | void |
786 | void evcheck_start (struct ev_check *w) |
939 | ev_check_start (struct ev_check *w) |
787 | { |
940 | { |
788 | if (ev_is_active (w)) |
941 | if (ev_is_active (w)) |
789 | return; |
942 | return; |
790 | |
943 | |
791 | ev_start ((W)w, ++checkcnt); |
944 | ev_start ((W)w, ++checkcnt); |
792 | array_needsize (checks, checkmax, checkcnt, ); |
945 | array_needsize (checks, checkmax, checkcnt, ); |
793 | checks [checkcnt - 1] = w; |
946 | checks [checkcnt - 1] = w; |
794 | } |
947 | } |
795 | |
948 | |
|
|
949 | void |
796 | void evcheck_stop (struct ev_check *w) |
950 | ev_check_stop (struct ev_check *w) |
797 | { |
951 | { |
|
|
952 | ev_clear ((W)w); |
|
|
953 | if (ev_is_active (w)) |
|
|
954 | return; |
|
|
955 | |
798 | checks [w->active - 1] = checks [--checkcnt]; |
956 | checks [w->active - 1] = checks [--checkcnt]; |
799 | ev_stop ((W)w); |
957 | ev_stop ((W)w); |
800 | } |
958 | } |
801 | |
959 | |
|
|
960 | void |
|
|
961 | ev_child_start (struct ev_child *w) |
|
|
962 | { |
|
|
963 | if (ev_is_active (w)) |
|
|
964 | return; |
|
|
965 | |
|
|
966 | ev_start ((W)w, 1); |
|
|
967 | wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
968 | } |
|
|
969 | |
|
|
970 | void |
|
|
971 | ev_child_stop (struct ev_child *w) |
|
|
972 | { |
|
|
973 | ev_clear ((W)w); |
|
|
974 | if (ev_is_active (w)) |
|
|
975 | return; |
|
|
976 | |
|
|
977 | wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); |
|
|
978 | ev_stop ((W)w); |
|
|
979 | } |
|
|
980 | |
|
|
981 | /*****************************************************************************/ |
|
|
982 | |
|
|
983 | struct ev_once |
|
|
984 | { |
|
|
985 | struct ev_io io; |
|
|
986 | struct ev_timer to; |
|
|
987 | void (*cb)(int revents, void *arg); |
|
|
988 | void *arg; |
|
|
989 | }; |
|
|
990 | |
|
|
991 | static void |
|
|
992 | once_cb (struct ev_once *once, int revents) |
|
|
993 | { |
|
|
994 | void (*cb)(int revents, void *arg) = once->cb; |
|
|
995 | void *arg = once->arg; |
|
|
996 | |
|
|
997 | ev_io_stop (&once->io); |
|
|
998 | ev_timer_stop (&once->to); |
|
|
999 | free (once); |
|
|
1000 | |
|
|
1001 | cb (revents, arg); |
|
|
1002 | } |
|
|
1003 | |
|
|
1004 | static void |
|
|
1005 | once_cb_io (struct ev_io *w, int revents) |
|
|
1006 | { |
|
|
1007 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); |
|
|
1008 | } |
|
|
1009 | |
|
|
1010 | static void |
|
|
1011 | once_cb_to (struct ev_timer *w, int revents) |
|
|
1012 | { |
|
|
1013 | once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); |
|
|
1014 | } |
|
|
1015 | |
|
|
1016 | void |
|
|
1017 | ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) |
|
|
1018 | { |
|
|
1019 | struct ev_once *once = malloc (sizeof (struct ev_once)); |
|
|
1020 | |
|
|
1021 | if (!once) |
|
|
1022 | cb (EV_ERROR, arg); |
|
|
1023 | else |
|
|
1024 | { |
|
|
1025 | once->cb = cb; |
|
|
1026 | once->arg = arg; |
|
|
1027 | |
|
|
1028 | ev_watcher_init (&once->io, once_cb_io); |
|
|
1029 | |
|
|
1030 | if (fd >= 0) |
|
|
1031 | { |
|
|
1032 | ev_io_set (&once->io, fd, events); |
|
|
1033 | ev_io_start (&once->io); |
|
|
1034 | } |
|
|
1035 | |
|
|
1036 | ev_watcher_init (&once->to, once_cb_to); |
|
|
1037 | |
|
|
1038 | if (timeout >= 0.) |
|
|
1039 | { |
|
|
1040 | ev_timer_set (&once->to, timeout, 0.); |
|
|
1041 | ev_timer_start (&once->to); |
|
|
1042 | } |
|
|
1043 | } |
|
|
1044 | } |
|
|
1045 | |
802 | /*****************************************************************************/ |
1046 | /*****************************************************************************/ |
803 | |
1047 | |
804 | #if 0 |
1048 | #if 0 |
805 | |
1049 | |
806 | struct ev_io wio; |
1050 | struct ev_io wio; |
… | |
… | |
813 | |
1057 | |
814 | static void |
1058 | static void |
815 | ocb (struct ev_timer *w, int revents) |
1059 | ocb (struct ev_timer *w, int revents) |
816 | { |
1060 | { |
817 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
1061 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
818 | evtimer_stop (w); |
1062 | ev_timer_stop (w); |
819 | evtimer_start (w); |
1063 | ev_timer_start (w); |
820 | } |
1064 | } |
821 | |
1065 | |
822 | static void |
1066 | static void |
823 | scb (struct ev_signal *w, int revents) |
1067 | scb (struct ev_signal *w, int revents) |
824 | { |
1068 | { |
825 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
1069 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
826 | evio_stop (&wio); |
1070 | ev_io_stop (&wio); |
827 | evio_start (&wio); |
1071 | ev_io_start (&wio); |
828 | } |
1072 | } |
829 | |
1073 | |
830 | static void |
1074 | static void |
831 | gcb (struct ev_signal *w, int revents) |
1075 | gcb (struct ev_signal *w, int revents) |
832 | { |
1076 | { |
… | |
… | |
836 | |
1080 | |
837 | int main (void) |
1081 | int main (void) |
838 | { |
1082 | { |
839 | ev_init (0); |
1083 | ev_init (0); |
840 | |
1084 | |
841 | evio_init (&wio, sin_cb, 0, EV_READ); |
1085 | ev_io_init (&wio, sin_cb, 0, EV_READ); |
842 | evio_start (&wio); |
1086 | ev_io_start (&wio); |
843 | |
1087 | |
844 | struct ev_timer t[10000]; |
1088 | struct ev_timer t[10000]; |
845 | |
1089 | |
846 | #if 0 |
1090 | #if 0 |
847 | int i; |
1091 | int i; |
848 | for (i = 0; i < 10000; ++i) |
1092 | for (i = 0; i < 10000; ++i) |
849 | { |
1093 | { |
850 | struct ev_timer *w = t + i; |
1094 | struct ev_timer *w = t + i; |
851 | evw_init (w, ocb, i); |
1095 | ev_watcher_init (w, ocb, i); |
852 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
1096 | ev_timer_init_abs (w, ocb, drand48 (), 0.99775533); |
853 | evtimer_start (w); |
1097 | ev_timer_start (w); |
854 | if (drand48 () < 0.5) |
1098 | if (drand48 () < 0.5) |
855 | evtimer_stop (w); |
1099 | ev_timer_stop (w); |
856 | } |
1100 | } |
857 | #endif |
1101 | #endif |
858 | |
1102 | |
859 | struct ev_timer t1; |
1103 | struct ev_timer t1; |
860 | evtimer_init (&t1, ocb, 5, 10); |
1104 | ev_timer_init (&t1, ocb, 5, 10); |
861 | evtimer_start (&t1); |
1105 | ev_timer_start (&t1); |
862 | |
1106 | |
863 | struct ev_signal sig; |
1107 | struct ev_signal sig; |
864 | evsignal_init (&sig, scb, SIGQUIT); |
1108 | ev_signal_init (&sig, scb, SIGQUIT); |
865 | evsignal_start (&sig); |
1109 | ev_signal_start (&sig); |
866 | |
1110 | |
867 | struct ev_check cw; |
1111 | struct ev_check cw; |
868 | evcheck_init (&cw, gcb); |
1112 | ev_check_init (&cw, gcb); |
869 | evcheck_start (&cw); |
1113 | ev_check_start (&cw); |
870 | |
1114 | |
871 | struct ev_idle iw; |
1115 | struct ev_idle iw; |
872 | evidle_init (&iw, gcb); |
1116 | ev_idle_init (&iw, gcb); |
873 | evidle_start (&iw); |
1117 | ev_idle_start (&iw); |
874 | |
1118 | |
875 | ev_loop (0); |
1119 | ev_loop (0); |
876 | |
1120 | |
877 | return 0; |
1121 | return 0; |
878 | } |
1122 | } |