1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
|
|
3 | #include <unistd.h> |
|
|
4 | #include <fcntl.h> |
|
|
5 | #include <signal.h> |
3 | |
6 | |
4 | #include <stdio.h> |
7 | #include <stdio.h> |
5 | |
8 | |
6 | #include <assert.h> |
9 | #include <assert.h> |
7 | #include <errno.h> |
10 | #include <errno.h> |
… | |
… | |
10 | |
13 | |
11 | #ifdef CLOCK_MONOTONIC |
14 | #ifdef CLOCK_MONOTONIC |
12 | # define HAVE_MONOTONIC 1 |
15 | # define HAVE_MONOTONIC 1 |
13 | #endif |
16 | #endif |
14 | |
17 | |
|
|
18 | #define HAVE_REALTIME 1 |
15 | #define HAVE_EPOLL 1 |
19 | #define HAVE_EPOLL 1 |
16 | #define HAVE_REALTIME 1 |
|
|
17 | #define HAVE_SELECT 0 |
20 | #define HAVE_SELECT 1 |
18 | |
21 | |
19 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
22 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
20 | #define MAX_BLOCKTIME 60. |
23 | #define MAX_BLOCKTIME 60. |
21 | |
24 | |
22 | #include "ev.h" |
25 | #include "ev.h" |
… | |
… | |
34 | int ev_method; |
37 | int ev_method; |
35 | |
38 | |
36 | static int have_monotonic; /* runtime */ |
39 | static int have_monotonic; /* runtime */ |
37 | |
40 | |
38 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
41 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
39 | static void (*method_reify)(void); |
42 | static void (*method_modify)(int fd, int oev, int nev); |
40 | static void (*method_poll)(ev_tstamp timeout); |
43 | static void (*method_poll)(ev_tstamp timeout); |
|
|
44 | |
|
|
45 | /*****************************************************************************/ |
41 | |
46 | |
42 | ev_tstamp |
47 | ev_tstamp |
43 | ev_time (void) |
48 | ev_time (void) |
44 | { |
49 | { |
45 | #if HAVE_REALTIME |
50 | #if HAVE_REALTIME |
… | |
… | |
76 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
81 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
77 | init (base + cur, newcnt - cur); \ |
82 | init (base + cur, newcnt - cur); \ |
78 | cur = newcnt; \ |
83 | cur = newcnt; \ |
79 | } |
84 | } |
80 | |
85 | |
|
|
86 | /*****************************************************************************/ |
|
|
87 | |
81 | typedef struct |
88 | typedef struct |
82 | { |
89 | { |
83 | struct ev_io *head; |
90 | struct ev_io *head; |
84 | unsigned char wev, rev; /* want, received event set */ |
91 | unsigned char wev, rev; /* want, received event set */ |
85 | } ANFD; |
92 | } ANFD; |
… | |
… | |
132 | if (ev) |
139 | if (ev) |
133 | event ((struct ev_watcher *)w, ev); |
140 | event ((struct ev_watcher *)w, ev); |
134 | } |
141 | } |
135 | } |
142 | } |
136 | |
143 | |
|
|
144 | /*****************************************************************************/ |
|
|
145 | |
137 | static struct ev_timer **atimers; |
146 | static struct ev_timer **atimers; |
138 | static int atimermax, atimercnt; |
147 | static int atimermax, atimercnt; |
139 | |
148 | |
140 | static struct ev_timer **rtimers; |
149 | static struct ev_timer **rtimers; |
141 | static int rtimermax, rtimercnt; |
150 | static int rtimermax, rtimercnt; |
… | |
… | |
179 | |
188 | |
180 | timers [k] = w; |
189 | timers [k] = w; |
181 | timers [k]->active = k + 1; |
190 | timers [k]->active = k + 1; |
182 | } |
191 | } |
183 | |
192 | |
184 | static struct ev_signal **signals; |
193 | /*****************************************************************************/ |
|
|
194 | |
|
|
195 | typedef struct |
|
|
196 | { |
|
|
197 | struct ev_signal *head; |
|
|
198 | sig_atomic_t gotsig; |
|
|
199 | } ANSIG; |
|
|
200 | |
|
|
201 | static ANSIG *signals; |
185 | static int signalmax; |
202 | static int signalmax; |
186 | |
203 | |
|
|
204 | static int sigpipe [2]; |
|
|
205 | static sig_atomic_t gotsig; |
|
|
206 | static struct ev_io sigev; |
|
|
207 | |
187 | static void |
208 | static void |
188 | signals_init (struct ev_signal **base, int count) |
209 | signals_init (ANSIG *base, int count) |
189 | { |
210 | { |
190 | while (count--) |
211 | while (count--) |
191 | *base++ = 0; |
212 | { |
|
|
213 | base->head = 0; |
|
|
214 | base->gotsig = 0; |
|
|
215 | ++base; |
|
|
216 | } |
192 | } |
217 | } |
|
|
218 | |
|
|
219 | static void |
|
|
220 | sighandler (int signum) |
|
|
221 | { |
|
|
222 | signals [signum - 1].gotsig = 1; |
|
|
223 | |
|
|
224 | if (!gotsig) |
|
|
225 | { |
|
|
226 | gotsig = 1; |
|
|
227 | write (sigpipe [1], &gotsig, 1); |
|
|
228 | } |
|
|
229 | } |
|
|
230 | |
|
|
231 | static void |
|
|
232 | sigcb (struct ev_io *iow, int revents) |
|
|
233 | { |
|
|
234 | struct ev_signal *w; |
|
|
235 | int sig; |
|
|
236 | |
|
|
237 | gotsig = 0; |
|
|
238 | read (sigpipe [0], &revents, 1); |
|
|
239 | |
|
|
240 | for (sig = signalmax; sig--; ) |
|
|
241 | if (signals [sig].gotsig) |
|
|
242 | { |
|
|
243 | signals [sig].gotsig = 0; |
|
|
244 | |
|
|
245 | for (w = signals [sig].head; w; w = w->next) |
|
|
246 | event ((struct ev_watcher *)w, EV_SIGNAL); |
|
|
247 | } |
|
|
248 | } |
|
|
249 | |
|
|
250 | static void |
|
|
251 | siginit (void) |
|
|
252 | { |
|
|
253 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
254 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
255 | |
|
|
256 | /* rather than sort out wether we really need nb, set it */ |
|
|
257 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
258 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
259 | |
|
|
260 | evio_set (&sigev, sigpipe [0], EV_READ); |
|
|
261 | evio_start (&sigev); |
|
|
262 | } |
|
|
263 | |
|
|
264 | /*****************************************************************************/ |
193 | |
265 | |
194 | #if HAVE_EPOLL |
266 | #if HAVE_EPOLL |
195 | # include "ev_epoll.c" |
267 | # include "ev_epoll.c" |
196 | #endif |
268 | #endif |
197 | #if HAVE_SELECT |
269 | #if HAVE_SELECT |
… | |
… | |
210 | |
282 | |
211 | ev_now = ev_time (); |
283 | ev_now = ev_time (); |
212 | now = get_clock (); |
284 | now = get_clock (); |
213 | diff = ev_now - now; |
285 | diff = ev_now - now; |
214 | |
286 | |
|
|
287 | if (pipe (sigpipe)) |
|
|
288 | return 0; |
|
|
289 | |
|
|
290 | ev_method = EVMETHOD_NONE; |
215 | #if HAVE_EPOLL |
291 | #if HAVE_EPOLL |
216 | if (epoll_init (flags)) |
292 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
217 | return ev_method; |
|
|
218 | #endif |
293 | #endif |
219 | #if HAVE_SELECT |
294 | #if HAVE_SELECT |
220 | if (select_init (flags)) |
295 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
221 | return ev_method; |
|
|
222 | #endif |
296 | #endif |
223 | |
297 | |
224 | ev_method = EVMETHOD_NONE; |
298 | if (ev_method) |
|
|
299 | { |
|
|
300 | evw_init (&sigev, sigcb, 0); |
|
|
301 | siginit (); |
|
|
302 | } |
|
|
303 | |
225 | return ev_method; |
304 | return ev_method; |
226 | } |
305 | } |
227 | |
306 | |
|
|
307 | /*****************************************************************************/ |
|
|
308 | |
228 | void ev_prefork (void) |
309 | void ev_prefork (void) |
229 | { |
310 | { |
230 | } |
311 | } |
231 | |
312 | |
232 | void ev_postfork_parent (void) |
313 | void ev_postfork_parent (void) |
… | |
… | |
234 | } |
315 | } |
235 | |
316 | |
236 | void ev_postfork_child (void) |
317 | void ev_postfork_child (void) |
237 | { |
318 | { |
238 | #if HAVE_EPOLL |
319 | #if HAVE_EPOLL |
|
|
320 | if (ev_method == EVMETHOD_EPOLL) |
239 | epoll_postfork_child (); |
321 | epoll_postfork_child (); |
240 | #endif |
322 | #endif |
|
|
323 | |
|
|
324 | evio_stop (&sigev); |
|
|
325 | close (sigpipe [0]); |
|
|
326 | close (sigpipe [1]); |
|
|
327 | pipe (sigpipe); |
|
|
328 | siginit (); |
|
|
329 | } |
|
|
330 | |
|
|
331 | /*****************************************************************************/ |
|
|
332 | |
|
|
333 | static ev_hook hooks [EVHOOK_NUM]; |
|
|
334 | |
|
|
335 | void |
|
|
336 | ev_hook_register (int type, ev_hook hook) |
|
|
337 | { |
|
|
338 | hooks [type] = hook; |
|
|
339 | } |
|
|
340 | |
|
|
341 | void |
|
|
342 | ev_hook_unregister (int type, ev_hook hook) |
|
|
343 | { |
|
|
344 | hooks [type] = 0; |
|
|
345 | } |
|
|
346 | |
|
|
347 | static void |
|
|
348 | hook_call (int type) |
|
|
349 | { |
|
|
350 | if (hooks [type]) |
|
|
351 | hooks [type] (); |
|
|
352 | } |
|
|
353 | |
|
|
354 | static void |
|
|
355 | fd_reify (void) |
|
|
356 | { |
|
|
357 | int i; |
|
|
358 | |
|
|
359 | for (i = 0; i < fdchangecnt; ++i) |
|
|
360 | { |
|
|
361 | int fd = fdchanges [i]; |
|
|
362 | ANFD *anfd = anfds + fd; |
|
|
363 | struct ev_io *w; |
|
|
364 | |
|
|
365 | int wev = 0; |
|
|
366 | |
|
|
367 | for (w = anfd->head; w; w = w->next) |
|
|
368 | wev |= w->events; |
|
|
369 | |
|
|
370 | if (anfd->wev != wev) |
|
|
371 | { |
|
|
372 | method_modify (fd, anfd->wev, wev); |
|
|
373 | anfd->wev = wev; |
|
|
374 | } |
|
|
375 | } |
|
|
376 | |
|
|
377 | fdchangecnt = 0; |
241 | } |
378 | } |
242 | |
379 | |
243 | static void |
380 | static void |
244 | call_pending () |
381 | call_pending () |
245 | { |
382 | { |
… | |
… | |
335 | double block; |
472 | double block; |
336 | ev_loop_done = flags & EVLOOP_ONESHOT; |
473 | ev_loop_done = flags & EVLOOP_ONESHOT; |
337 | |
474 | |
338 | do |
475 | do |
339 | { |
476 | { |
|
|
477 | hook_call (EVHOOK_PREPOLL); |
|
|
478 | |
340 | /* update fd-related kernel structures */ |
479 | /* update fd-related kernel structures */ |
341 | method_reify (); fdchangecnt = 0; |
480 | fd_reify (); |
342 | |
481 | |
343 | /* calculate blocking time */ |
482 | /* calculate blocking time */ |
344 | if (flags & EVLOOP_NONBLOCK) |
483 | if (flags & EVLOOP_NONBLOCK) |
345 | block = 0.; |
484 | block = 0.; |
346 | else |
485 | else |
… | |
… | |
365 | method_poll (block); |
504 | method_poll (block); |
366 | |
505 | |
367 | /* update ev_now, do magic */ |
506 | /* update ev_now, do magic */ |
368 | time_update (); |
507 | time_update (); |
369 | |
508 | |
|
|
509 | hook_call (EVHOOK_POSTPOLL); |
|
|
510 | |
370 | /* put pending timers into pendign queue and reschedule them */ |
511 | /* put pending timers into pendign queue and reschedule them */ |
371 | /* absolute timers first */ |
512 | /* absolute timers first */ |
372 | timers_reify (atimers, atimercnt, ev_now); |
513 | timers_reify (atimers, atimercnt, ev_now); |
373 | /* relative timers second */ |
514 | /* relative timers second */ |
374 | timers_reify (rtimers, rtimercnt, now); |
515 | timers_reify (rtimers, rtimercnt, now); |
… | |
… | |
376 | call_pending (); |
517 | call_pending (); |
377 | } |
518 | } |
378 | while (!ev_loop_done); |
519 | while (!ev_loop_done); |
379 | } |
520 | } |
380 | |
521 | |
|
|
522 | /*****************************************************************************/ |
|
|
523 | |
381 | static void |
524 | static void |
382 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
525 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
383 | { |
526 | { |
384 | elem->next = *head; |
527 | elem->next = *head; |
385 | *head = elem; |
528 | *head = elem; |
… | |
… | |
414 | pendings [w->pending - 1].w = 0; |
557 | pendings [w->pending - 1].w = 0; |
415 | |
558 | |
416 | w->active = 0; |
559 | w->active = 0; |
417 | /* nop */ |
560 | /* nop */ |
418 | } |
561 | } |
|
|
562 | |
|
|
563 | /*****************************************************************************/ |
419 | |
564 | |
420 | void |
565 | void |
421 | evio_start (struct ev_io *w) |
566 | evio_start (struct ev_io *w) |
422 | { |
567 | { |
423 | if (ev_is_active (w)) |
568 | if (ev_is_active (w)) |
… | |
… | |
509 | if (ev_is_active (w)) |
654 | if (ev_is_active (w)) |
510 | return; |
655 | return; |
511 | |
656 | |
512 | ev_start ((struct ev_watcher *)w, 1); |
657 | ev_start ((struct ev_watcher *)w, 1); |
513 | array_needsize (signals, signalmax, w->signum, signals_init); |
658 | array_needsize (signals, signalmax, w->signum, signals_init); |
514 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
659 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
|
|
660 | |
|
|
661 | if (!w->next) |
|
|
662 | { |
|
|
663 | struct sigaction sa; |
|
|
664 | sa.sa_handler = sighandler; |
|
|
665 | sigfillset (&sa.sa_mask); |
|
|
666 | sa.sa_flags = 0; |
|
|
667 | sigaction (w->signum, &sa, 0); |
|
|
668 | } |
515 | } |
669 | } |
516 | |
670 | |
517 | void |
671 | void |
518 | evsignal_stop (struct ev_signal *w) |
672 | evsignal_stop (struct ev_signal *w) |
519 | { |
673 | { |
520 | if (!ev_is_active (w)) |
674 | if (!ev_is_active (w)) |
521 | return; |
675 | return; |
522 | |
676 | |
523 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
677 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
524 | ev_stop ((struct ev_watcher *)w); |
678 | ev_stop ((struct ev_watcher *)w); |
|
|
679 | |
|
|
680 | if (!signals [w->signum - 1].head) |
|
|
681 | signal (w->signum, SIG_DFL); |
525 | } |
682 | } |
526 | |
683 | |
527 | /*****************************************************************************/ |
684 | /*****************************************************************************/ |
528 | #if 1 |
685 | #if 1 |
529 | |
686 | |
… | |
… | |
537 | ocb (struct ev_timer *w, int revents) |
694 | ocb (struct ev_timer *w, int revents) |
538 | { |
695 | { |
539 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
696 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
540 | evtimer_stop (w); |
697 | evtimer_stop (w); |
541 | evtimer_start (w); |
698 | evtimer_start (w); |
|
|
699 | } |
|
|
700 | |
|
|
701 | static void |
|
|
702 | scb (struct ev_signal *w, int revents) |
|
|
703 | { |
|
|
704 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
542 | } |
705 | } |
543 | |
706 | |
544 | int main (void) |
707 | int main (void) |
545 | { |
708 | { |
546 | struct ev_io sin; |
709 | struct ev_io sin; |
… | |
… | |
569 | struct ev_timer t1; |
732 | struct ev_timer t1; |
570 | evw_init (&t1, ocb, 0); |
733 | evw_init (&t1, ocb, 0); |
571 | evtimer_set_abs (&t1, 5, 10); |
734 | evtimer_set_abs (&t1, 5, 10); |
572 | evtimer_start (&t1); |
735 | evtimer_start (&t1); |
573 | |
736 | |
|
|
737 | struct ev_signal sig; |
|
|
738 | evw_init (&sig, scb, 65535); |
|
|
739 | evsignal_set (&sig, SIGQUIT); |
|
|
740 | evsignal_start (&sig); |
|
|
741 | |
574 | ev_loop (0); |
742 | ev_loop (0); |
575 | |
743 | |
576 | return 0; |
744 | return 0; |
577 | } |
745 | } |
578 | |
746 | |