… | |
… | |
39 | static int have_monotonic; /* runtime */ |
39 | static int have_monotonic; /* runtime */ |
40 | |
40 | |
41 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
41 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
42 | static void (*method_modify)(int fd, int oev, int nev); |
42 | static void (*method_modify)(int fd, int oev, int nev); |
43 | static void (*method_poll)(ev_tstamp timeout); |
43 | static void (*method_poll)(ev_tstamp timeout); |
|
|
44 | |
|
|
45 | /*****************************************************************************/ |
44 | |
46 | |
45 | ev_tstamp |
47 | ev_tstamp |
46 | ev_time (void) |
48 | ev_time (void) |
47 | { |
49 | { |
48 | #if HAVE_REALTIME |
50 | #if HAVE_REALTIME |
… | |
… | |
79 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
81 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
80 | init (base + cur, newcnt - cur); \ |
82 | init (base + cur, newcnt - cur); \ |
81 | cur = newcnt; \ |
83 | cur = newcnt; \ |
82 | } |
84 | } |
83 | |
85 | |
|
|
86 | /*****************************************************************************/ |
|
|
87 | |
84 | typedef struct |
88 | typedef struct |
85 | { |
89 | { |
86 | struct ev_io *head; |
90 | struct ev_io *head; |
87 | unsigned char wev, rev; /* want, received event set */ |
91 | unsigned char wev, rev; /* want, received event set */ |
88 | } ANFD; |
92 | } ANFD; |
… | |
… | |
135 | if (ev) |
139 | if (ev) |
136 | event ((struct ev_watcher *)w, ev); |
140 | event ((struct ev_watcher *)w, ev); |
137 | } |
141 | } |
138 | } |
142 | } |
139 | |
143 | |
|
|
144 | /*****************************************************************************/ |
|
|
145 | |
140 | static struct ev_timer **atimers; |
146 | static struct ev_timer **atimers; |
141 | static int atimermax, atimercnt; |
147 | static int atimermax, atimercnt; |
142 | |
148 | |
143 | static struct ev_timer **rtimers; |
149 | static struct ev_timer **rtimers; |
144 | static int rtimermax, rtimercnt; |
150 | static int rtimermax, rtimercnt; |
… | |
… | |
181 | } |
187 | } |
182 | |
188 | |
183 | timers [k] = w; |
189 | timers [k] = w; |
184 | timers [k]->active = k + 1; |
190 | timers [k]->active = k + 1; |
185 | } |
191 | } |
|
|
192 | |
|
|
193 | /*****************************************************************************/ |
186 | |
194 | |
187 | typedef struct |
195 | typedef struct |
188 | { |
196 | { |
189 | struct ev_signal *head; |
197 | struct ev_signal *head; |
190 | sig_atomic_t gotsig; |
198 | sig_atomic_t gotsig; |
… | |
… | |
250 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
258 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
251 | |
259 | |
252 | evio_set (&sigev, sigpipe [0], EV_READ); |
260 | evio_set (&sigev, sigpipe [0], EV_READ); |
253 | evio_start (&sigev); |
261 | evio_start (&sigev); |
254 | } |
262 | } |
|
|
263 | |
|
|
264 | /*****************************************************************************/ |
255 | |
265 | |
256 | #if HAVE_EPOLL |
266 | #if HAVE_EPOLL |
257 | # include "ev_epoll.c" |
267 | # include "ev_epoll.c" |
258 | #endif |
268 | #endif |
259 | #if HAVE_SELECT |
269 | #if HAVE_SELECT |
… | |
… | |
292 | } |
302 | } |
293 | |
303 | |
294 | return ev_method; |
304 | return ev_method; |
295 | } |
305 | } |
296 | |
306 | |
|
|
307 | /*****************************************************************************/ |
|
|
308 | |
297 | void ev_prefork (void) |
309 | void ev_prefork (void) |
298 | { |
310 | { |
299 | } |
311 | } |
300 | |
312 | |
301 | void ev_postfork_parent (void) |
313 | void ev_postfork_parent (void) |
… | |
… | |
314 | close (sigpipe [1]); |
326 | close (sigpipe [1]); |
315 | pipe (sigpipe); |
327 | pipe (sigpipe); |
316 | siginit (); |
328 | siginit (); |
317 | } |
329 | } |
318 | |
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 | |
319 | static void |
354 | static void |
320 | fd_reify (void) |
355 | fd_reify (void) |
321 | { |
356 | { |
322 | int i; |
357 | int i; |
323 | |
358 | |
… | |
… | |
437 | double block; |
472 | double block; |
438 | ev_loop_done = flags & EVLOOP_ONESHOT; |
473 | ev_loop_done = flags & EVLOOP_ONESHOT; |
439 | |
474 | |
440 | do |
475 | do |
441 | { |
476 | { |
|
|
477 | hook_call (EVHOOK_PREPOLL); |
|
|
478 | |
442 | /* update fd-related kernel structures */ |
479 | /* update fd-related kernel structures */ |
443 | fd_reify (); |
480 | fd_reify (); |
444 | |
481 | |
445 | /* calculate blocking time */ |
482 | /* calculate blocking time */ |
446 | if (flags & EVLOOP_NONBLOCK) |
483 | if (flags & EVLOOP_NONBLOCK) |
… | |
… | |
467 | method_poll (block); |
504 | method_poll (block); |
468 | |
505 | |
469 | /* update ev_now, do magic */ |
506 | /* update ev_now, do magic */ |
470 | time_update (); |
507 | time_update (); |
471 | |
508 | |
|
|
509 | hook_call (EVHOOK_POSTPOLL); |
|
|
510 | |
472 | /* put pending timers into pendign queue and reschedule them */ |
511 | /* put pending timers into pendign queue and reschedule them */ |
473 | /* absolute timers first */ |
512 | /* absolute timers first */ |
474 | timers_reify (atimers, atimercnt, ev_now); |
513 | timers_reify (atimers, atimercnt, ev_now); |
475 | /* relative timers second */ |
514 | /* relative timers second */ |
476 | timers_reify (rtimers, rtimercnt, now); |
515 | timers_reify (rtimers, rtimercnt, now); |
… | |
… | |
478 | call_pending (); |
517 | call_pending (); |
479 | } |
518 | } |
480 | while (!ev_loop_done); |
519 | while (!ev_loop_done); |
481 | } |
520 | } |
482 | |
521 | |
|
|
522 | /*****************************************************************************/ |
|
|
523 | |
483 | static void |
524 | static void |
484 | 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) |
485 | { |
526 | { |
486 | elem->next = *head; |
527 | elem->next = *head; |
487 | *head = elem; |
528 | *head = elem; |
… | |
… | |
516 | pendings [w->pending - 1].w = 0; |
557 | pendings [w->pending - 1].w = 0; |
517 | |
558 | |
518 | w->active = 0; |
559 | w->active = 0; |
519 | /* nop */ |
560 | /* nop */ |
520 | } |
561 | } |
|
|
562 | |
|
|
563 | /*****************************************************************************/ |
521 | |
564 | |
522 | void |
565 | void |
523 | evio_start (struct ev_io *w) |
566 | evio_start (struct ev_io *w) |
524 | { |
567 | { |
525 | if (ev_is_active (w)) |
568 | if (ev_is_active (w)) |
… | |
… | |
671 | evio_set (&sin, 0, EV_READ); |
714 | evio_set (&sin, 0, EV_READ); |
672 | evio_start (&sin); |
715 | evio_start (&sin); |
673 | |
716 | |
674 | struct ev_timer t[10000]; |
717 | struct ev_timer t[10000]; |
675 | |
718 | |
676 | #if 0 |
719 | #if 1 |
677 | int i; |
720 | int i; |
678 | for (i = 0; i < 10000; ++i) |
721 | for (i = 0; i < 10000; ++i) |
679 | { |
722 | { |
680 | struct ev_timer *w = t + i; |
723 | struct ev_timer *w = t + i; |
681 | evw_init (w, ocb, i); |
724 | evw_init (w, ocb, i); |