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 | |
|
|
9 | #include <assert.h> |
6 | #include <errno.h> |
10 | #include <errno.h> |
7 | #include <sys/time.h> |
11 | #include <sys/time.h> |
8 | #include <time.h> |
12 | #include <time.h> |
9 | |
13 | |
10 | #ifdef CLOCK_MONOTONIC |
|
|
11 | # define HAVE_MONOTONIC 1 |
|
|
12 | #endif |
|
|
13 | |
|
|
14 | #define HAVE_EPOLL 1 |
14 | #define HAVE_EPOLL 1 |
15 | #define HAVE_REALTIME 1 |
15 | |
|
|
16 | #ifndef HAVE_MONOTONIC |
|
|
17 | # ifdef CLOCK_MONOTONIC |
|
|
18 | # define HAVE_MONOTONIC 1 |
|
|
19 | # endif |
|
|
20 | #endif |
|
|
21 | |
|
|
22 | #ifndef HAVE_SELECT |
16 | #define HAVE_SELECT 0 |
23 | # define HAVE_SELECT 1 |
|
|
24 | #endif |
17 | |
25 | |
|
|
26 | #ifndef HAVE_EPOLL |
|
|
27 | # define HAVE_EPOLL 0 |
|
|
28 | #endif |
|
|
29 | |
|
|
30 | #ifndef HAVE_REALTIME |
|
|
31 | # define HAVE_REALTIME 1 /* posix requirement, but might be slower */ |
|
|
32 | #endif |
|
|
33 | |
|
|
34 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
18 | #define MAX_BLOCKTIME 60. |
35 | #define MAX_BLOCKTIME 60. |
19 | |
36 | |
20 | #include "ev.h" |
37 | #include "ev.h" |
21 | |
38 | |
22 | struct ev_watcher { |
39 | typedef struct ev_watcher *W; |
23 | EV_WATCHER (ev_watcher); |
|
|
24 | }; |
|
|
25 | |
|
|
26 | struct ev_watcher_list { |
40 | typedef struct ev_watcher_list *WL; |
27 | EV_WATCHER_LIST (ev_watcher_list); |
41 | typedef struct ev_watcher_time *WT; |
28 | }; |
|
|
29 | |
42 | |
|
|
43 | static ev_tstamp now, diff; /* monotonic clock */ |
30 | ev_tstamp ev_now; |
44 | ev_tstamp ev_now; |
31 | int ev_method; |
45 | int ev_method; |
32 | |
46 | |
33 | static int have_monotonic; /* runtime */ |
47 | static int have_monotonic; /* runtime */ |
34 | |
48 | |
35 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
49 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
36 | static void (*method_reify)(void); |
50 | static void (*method_modify)(int fd, int oev, int nev); |
37 | static void (*method_poll)(ev_tstamp timeout); |
51 | static void (*method_poll)(ev_tstamp timeout); |
|
|
52 | |
|
|
53 | /*****************************************************************************/ |
38 | |
54 | |
39 | ev_tstamp |
55 | ev_tstamp |
40 | ev_time (void) |
56 | ev_time (void) |
41 | { |
57 | { |
42 | #if HAVE_REALTIME |
58 | #if HAVE_REALTIME |
… | |
… | |
67 | |
83 | |
68 | #define array_needsize(base,cur,cnt,init) \ |
84 | #define array_needsize(base,cur,cnt,init) \ |
69 | if ((cnt) > cur) \ |
85 | if ((cnt) > cur) \ |
70 | { \ |
86 | { \ |
71 | int newcnt = cur ? cur << 1 : 16; \ |
87 | int newcnt = cur ? cur << 1 : 16; \ |
72 | fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ |
|
|
73 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
88 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
74 | init (base + cur, newcnt - cur); \ |
89 | init (base + cur, newcnt - cur); \ |
75 | cur = newcnt; \ |
90 | cur = newcnt; \ |
76 | } |
91 | } |
77 | |
92 | |
|
|
93 | /*****************************************************************************/ |
|
|
94 | |
78 | typedef struct |
95 | typedef struct |
79 | { |
96 | { |
80 | struct ev_io *head; |
97 | struct ev_io *head; |
81 | unsigned char wev, rev; /* want, received event set */ |
98 | unsigned char wev, rev; /* want, received event set */ |
82 | } ANFD; |
99 | } ANFD; |
… | |
… | |
98 | } |
115 | } |
99 | } |
116 | } |
100 | |
117 | |
101 | typedef struct |
118 | typedef struct |
102 | { |
119 | { |
103 | struct ev_watcher *w; |
120 | W w; |
104 | int events; |
121 | int events; |
105 | } ANPENDING; |
122 | } ANPENDING; |
106 | |
123 | |
107 | static ANPENDING *pendings; |
124 | static ANPENDING *pendings; |
108 | static int pendingmax, pendingcnt; |
125 | static int pendingmax, pendingcnt; |
109 | |
126 | |
110 | static void |
127 | static void |
111 | event (struct ev_watcher *w, int events) |
128 | event (W w, int events) |
112 | { |
129 | { |
113 | w->pending = ++pendingcnt; |
130 | w->pending = ++pendingcnt; |
114 | array_needsize (pendings, pendingmax, pendingcnt, ); |
131 | array_needsize (pendings, pendingmax, pendingcnt, ); |
115 | pendings [pendingcnt - 1].w = w; |
132 | pendings [pendingcnt - 1].w = w; |
116 | pendings [pendingcnt - 1].events = events; |
133 | pendings [pendingcnt - 1].events = events; |
… | |
… | |
125 | for (w = anfd->head; w; w = w->next) |
142 | for (w = anfd->head; w; w = w->next) |
126 | { |
143 | { |
127 | int ev = w->events & events; |
144 | int ev = w->events & events; |
128 | |
145 | |
129 | if (ev) |
146 | if (ev) |
130 | event ((struct ev_watcher *)w, ev); |
147 | event ((W)w, ev); |
131 | } |
148 | } |
132 | } |
149 | } |
|
|
150 | |
|
|
151 | static void |
|
|
152 | queue_events (W *events, int eventcnt, int type) |
|
|
153 | { |
|
|
154 | int i; |
|
|
155 | |
|
|
156 | for (i = 0; i < eventcnt; ++i) |
|
|
157 | event (events [i], type); |
|
|
158 | } |
|
|
159 | |
|
|
160 | /*****************************************************************************/ |
133 | |
161 | |
134 | static struct ev_timer **timers; |
162 | static struct ev_timer **timers; |
135 | static int timermax, timercnt; |
163 | static int timermax, timercnt; |
136 | |
164 | |
|
|
165 | static struct ev_periodic **periodics; |
|
|
166 | static int periodicmax, periodiccnt; |
|
|
167 | |
137 | static void |
168 | static void |
138 | upheap (int k) |
169 | upheap (WT *timers, int k) |
139 | { |
170 | { |
140 | struct ev_timer *w = timers [k]; |
171 | WT w = timers [k]; |
141 | |
172 | |
142 | while (k && timers [k >> 1]->at > w->at) |
173 | while (k && timers [k >> 1]->at > w->at) |
143 | { |
174 | { |
144 | timers [k] = timers [k >> 1]; |
175 | timers [k] = timers [k >> 1]; |
145 | timers [k]->active = k + 1; |
176 | timers [k]->active = k + 1; |
… | |
… | |
150 | timers [k]->active = k + 1; |
181 | timers [k]->active = k + 1; |
151 | |
182 | |
152 | } |
183 | } |
153 | |
184 | |
154 | static void |
185 | static void |
155 | downheap (int k) |
186 | downheap (WT *timers, int N, int k) |
156 | { |
187 | { |
157 | struct ev_timer *w = timers [k]; |
188 | WT w = timers [k]; |
158 | |
189 | |
159 | while (k < (timercnt >> 1)) |
190 | while (k < (N >> 1)) |
160 | { |
191 | { |
161 | int j = k << 1; |
192 | int j = k << 1; |
162 | |
193 | |
163 | if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) |
194 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
164 | ++j; |
195 | ++j; |
165 | |
196 | |
166 | if (w->at <= timers [j]->at) |
197 | if (w->at <= timers [j]->at) |
167 | break; |
198 | break; |
168 | |
199 | |
… | |
… | |
173 | |
204 | |
174 | timers [k] = w; |
205 | timers [k] = w; |
175 | timers [k]->active = k + 1; |
206 | timers [k]->active = k + 1; |
176 | } |
207 | } |
177 | |
208 | |
178 | static struct ev_signal **signals; |
209 | /*****************************************************************************/ |
|
|
210 | |
|
|
211 | typedef struct |
|
|
212 | { |
|
|
213 | struct ev_signal *head; |
|
|
214 | sig_atomic_t gotsig; |
|
|
215 | } ANSIG; |
|
|
216 | |
|
|
217 | static ANSIG *signals; |
179 | static int signalmax, signalcnt; |
218 | static int signalmax; |
180 | |
219 | |
|
|
220 | static int sigpipe [2]; |
|
|
221 | static sig_atomic_t gotsig; |
|
|
222 | static struct ev_io sigev; |
|
|
223 | |
181 | static void |
224 | static void |
182 | signals_init (struct ev_signal **base, int count) |
225 | signals_init (ANSIG *base, int count) |
183 | { |
226 | { |
184 | while (count--) |
227 | while (count--) |
185 | *base++ = 0; |
228 | { |
|
|
229 | base->head = 0; |
|
|
230 | base->gotsig = 0; |
|
|
231 | ++base; |
|
|
232 | } |
186 | } |
233 | } |
|
|
234 | |
|
|
235 | static void |
|
|
236 | sighandler (int signum) |
|
|
237 | { |
|
|
238 | signals [signum - 1].gotsig = 1; |
|
|
239 | |
|
|
240 | if (!gotsig) |
|
|
241 | { |
|
|
242 | gotsig = 1; |
|
|
243 | write (sigpipe [1], &gotsig, 1); |
|
|
244 | } |
|
|
245 | } |
|
|
246 | |
|
|
247 | static void |
|
|
248 | sigcb (struct ev_io *iow, int revents) |
|
|
249 | { |
|
|
250 | struct ev_signal *w; |
|
|
251 | int sig; |
|
|
252 | |
|
|
253 | gotsig = 0; |
|
|
254 | read (sigpipe [0], &revents, 1); |
|
|
255 | |
|
|
256 | for (sig = signalmax; sig--; ) |
|
|
257 | if (signals [sig].gotsig) |
|
|
258 | { |
|
|
259 | signals [sig].gotsig = 0; |
|
|
260 | |
|
|
261 | for (w = signals [sig].head; w; w = w->next) |
|
|
262 | event ((W)w, EV_SIGNAL); |
|
|
263 | } |
|
|
264 | } |
|
|
265 | |
|
|
266 | static void |
|
|
267 | siginit (void) |
|
|
268 | { |
|
|
269 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
270 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
271 | |
|
|
272 | /* rather than sort out wether we really need nb, set it */ |
|
|
273 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
274 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
275 | |
|
|
276 | evio_set (&sigev, sigpipe [0], EV_READ); |
|
|
277 | evio_start (&sigev); |
|
|
278 | } |
|
|
279 | |
|
|
280 | /*****************************************************************************/ |
|
|
281 | |
|
|
282 | static struct ev_idle **idles; |
|
|
283 | static int idlemax, idlecnt; |
|
|
284 | |
|
|
285 | static struct ev_check **checks; |
|
|
286 | static int checkmax, checkcnt; |
|
|
287 | |
|
|
288 | /*****************************************************************************/ |
187 | |
289 | |
188 | #if HAVE_EPOLL |
290 | #if HAVE_EPOLL |
189 | # include "ev_epoll.c" |
291 | # include "ev_epoll.c" |
190 | #endif |
292 | #endif |
191 | #if HAVE_SELECT |
293 | #if HAVE_SELECT |
… | |
… | |
201 | have_monotonic = 1; |
303 | have_monotonic = 1; |
202 | } |
304 | } |
203 | #endif |
305 | #endif |
204 | |
306 | |
205 | ev_now = ev_time (); |
307 | ev_now = ev_time (); |
|
|
308 | now = get_clock (); |
|
|
309 | diff = ev_now - now; |
206 | |
310 | |
|
|
311 | if (pipe (sigpipe)) |
|
|
312 | return 0; |
|
|
313 | |
|
|
314 | ev_method = EVMETHOD_NONE; |
207 | #if HAVE_EPOLL |
315 | #if HAVE_EPOLL |
208 | if (epoll_init (flags)) |
316 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
209 | return ev_method; |
|
|
210 | #endif |
317 | #endif |
211 | #if HAVE_SELECT |
318 | #if HAVE_SELECT |
212 | if (select_init (flags)) |
319 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
213 | return ev_method; |
|
|
214 | #endif |
320 | #endif |
215 | |
321 | |
216 | ev_method = EVMETHOD_NONE; |
322 | if (ev_method) |
|
|
323 | { |
|
|
324 | evw_init (&sigev, sigcb); |
|
|
325 | siginit (); |
|
|
326 | } |
|
|
327 | |
217 | return ev_method; |
328 | return ev_method; |
218 | } |
329 | } |
219 | |
330 | |
|
|
331 | /*****************************************************************************/ |
|
|
332 | |
220 | void ev_prefork (void) |
333 | void ev_prefork (void) |
221 | { |
334 | { |
|
|
335 | /* nop */ |
222 | } |
336 | } |
223 | |
337 | |
224 | void ev_postfork_parent (void) |
338 | void ev_postfork_parent (void) |
225 | { |
339 | { |
|
|
340 | /* nop */ |
226 | } |
341 | } |
227 | |
342 | |
228 | void ev_postfork_child (void) |
343 | void ev_postfork_child (void) |
229 | { |
344 | { |
230 | #if HAVE_EPOLL |
345 | #if HAVE_EPOLL |
|
|
346 | if (ev_method == EVMETHOD_EPOLL) |
231 | epoll_postfork_child (); |
347 | epoll_postfork_child (); |
232 | #endif |
348 | #endif |
|
|
349 | |
|
|
350 | evio_stop (&sigev); |
|
|
351 | close (sigpipe [0]); |
|
|
352 | close (sigpipe [1]); |
|
|
353 | pipe (sigpipe); |
|
|
354 | siginit (); |
|
|
355 | } |
|
|
356 | |
|
|
357 | /*****************************************************************************/ |
|
|
358 | |
|
|
359 | static void |
|
|
360 | fd_reify (void) |
|
|
361 | { |
|
|
362 | int i; |
|
|
363 | |
|
|
364 | for (i = 0; i < fdchangecnt; ++i) |
|
|
365 | { |
|
|
366 | int fd = fdchanges [i]; |
|
|
367 | ANFD *anfd = anfds + fd; |
|
|
368 | struct ev_io *w; |
|
|
369 | |
|
|
370 | int wev = 0; |
|
|
371 | |
|
|
372 | for (w = anfd->head; w; w = w->next) |
|
|
373 | wev |= w->events; |
|
|
374 | |
|
|
375 | if (anfd->wev != wev) |
|
|
376 | { |
|
|
377 | method_modify (fd, anfd->wev, wev); |
|
|
378 | anfd->wev = wev; |
|
|
379 | } |
|
|
380 | } |
|
|
381 | |
|
|
382 | fdchangecnt = 0; |
233 | } |
383 | } |
234 | |
384 | |
235 | static void |
385 | static void |
236 | call_pending () |
386 | call_pending () |
237 | { |
387 | { |
… | |
… | |
250 | |
400 | |
251 | pendingcnt = 0; |
401 | pendingcnt = 0; |
252 | } |
402 | } |
253 | |
403 | |
254 | static void |
404 | static void |
255 | timer_reify (void) |
405 | timers_reify () |
256 | { |
406 | { |
257 | while (timercnt && timers [0]->at <= ev_now) |
407 | while (timercnt && timers [0]->at <= now) |
258 | { |
408 | { |
259 | struct ev_timer *w = timers [0]; |
409 | struct ev_timer *w = timers [0]; |
260 | |
410 | |
261 | /* first reschedule timer */ |
411 | /* first reschedule or stop timer */ |
262 | if (w->repeat) |
412 | if (w->repeat) |
263 | { |
413 | { |
264 | if (w->is_abs) |
|
|
265 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
266 | else |
|
|
267 | w->at = ev_now + w->repeat; |
414 | w->at = now + w->repeat; |
268 | |
415 | assert (("timer timeout in the past, negative repeat?", w->at > now)); |
269 | downheap (0); |
416 | downheap ((WT *)timers, timercnt, 0); |
270 | } |
417 | } |
271 | else |
418 | else |
272 | evtimer_stop (w); /* nonrepeating: stop timer */ |
419 | evtimer_stop (w); /* nonrepeating: stop timer */ |
273 | |
420 | |
274 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
421 | event ((W)w, EV_TIMEOUT); |
|
|
422 | } |
|
|
423 | } |
|
|
424 | |
|
|
425 | static void |
|
|
426 | periodics_reify () |
|
|
427 | { |
|
|
428 | while (periodiccnt && periodics [0]->at <= ev_now) |
|
|
429 | { |
|
|
430 | struct ev_periodic *w = periodics [0]; |
|
|
431 | |
|
|
432 | /* first reschedule or stop timer */ |
|
|
433 | if (w->interval) |
|
|
434 | { |
|
|
435 | w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; |
|
|
436 | assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); |
|
|
437 | downheap ((WT *)periodics, periodiccnt, 0); |
|
|
438 | } |
|
|
439 | else |
|
|
440 | evperiodic_stop (w); /* nonrepeating: stop timer */ |
|
|
441 | |
|
|
442 | event ((W)w, EV_TIMEOUT); |
|
|
443 | } |
|
|
444 | } |
|
|
445 | |
|
|
446 | static void |
|
|
447 | periodics_reschedule (ev_tstamp diff) |
|
|
448 | { |
|
|
449 | int i; |
|
|
450 | |
|
|
451 | /* adjust periodics after time jump */ |
|
|
452 | for (i = 0; i < periodiccnt; ++i) |
|
|
453 | { |
|
|
454 | struct ev_periodic *w = periodics [i]; |
|
|
455 | |
|
|
456 | if (w->interval) |
|
|
457 | { |
|
|
458 | ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
459 | |
|
|
460 | if (fabs (diff) >= 1e-4) |
|
|
461 | { |
|
|
462 | evperiodic_stop (w); |
|
|
463 | evperiodic_start (w); |
|
|
464 | |
|
|
465 | i = 0; /* restart loop, inefficient, but time jumps should be rare */ |
|
|
466 | } |
|
|
467 | } |
|
|
468 | } |
|
|
469 | } |
|
|
470 | |
|
|
471 | static void |
|
|
472 | time_update () |
|
|
473 | { |
|
|
474 | int i; |
|
|
475 | |
|
|
476 | ev_now = ev_time (); |
|
|
477 | |
|
|
478 | if (have_monotonic) |
|
|
479 | { |
|
|
480 | ev_tstamp odiff = diff; |
|
|
481 | |
|
|
482 | for (i = 4; --i; ) /* loop a few times, before making important decisions */ |
|
|
483 | { |
|
|
484 | now = get_clock (); |
|
|
485 | diff = ev_now - now; |
|
|
486 | |
|
|
487 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
|
|
488 | return; /* all is well */ |
|
|
489 | |
|
|
490 | ev_now = ev_time (); |
|
|
491 | } |
|
|
492 | |
|
|
493 | periodics_reschedule (diff - odiff); |
|
|
494 | /* no timer adjustment, as the monotonic clock doesn't jump */ |
|
|
495 | } |
|
|
496 | else |
|
|
497 | { |
|
|
498 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
|
|
499 | { |
|
|
500 | periodics_reschedule (ev_now - now); |
|
|
501 | |
|
|
502 | /* adjust timers. this is easy, as the offset is the same for all */ |
|
|
503 | for (i = 0; i < timercnt; ++i) |
|
|
504 | timers [i]->at += diff; |
|
|
505 | } |
|
|
506 | |
|
|
507 | now = ev_now; |
275 | } |
508 | } |
276 | } |
509 | } |
277 | |
510 | |
278 | int ev_loop_done; |
511 | int ev_loop_done; |
279 | |
512 | |
280 | int ev_loop (int flags) |
513 | void ev_loop (int flags) |
281 | { |
514 | { |
282 | double block; |
515 | double block; |
283 | ev_loop_done = flags & EVLOOP_ONESHOT; |
516 | ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; |
|
|
517 | |
|
|
518 | if (checkcnt) |
|
|
519 | { |
|
|
520 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
521 | call_pending (); |
|
|
522 | } |
284 | |
523 | |
285 | do |
524 | do |
286 | { |
525 | { |
287 | /* update fd-related kernel structures */ |
526 | /* update fd-related kernel structures */ |
288 | method_reify (); fdchangecnt = 0; |
527 | fd_reify (); |
289 | |
528 | |
290 | /* calculate blocking time */ |
529 | /* calculate blocking time */ |
|
|
530 | |
|
|
531 | /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ |
291 | ev_now = ev_time (); |
532 | ev_now = ev_time (); |
292 | |
533 | |
293 | if (flags & EVLOOP_NONBLOCK) |
534 | if (flags & EVLOOP_NONBLOCK || idlecnt) |
294 | block = 0.; |
535 | block = 0.; |
295 | else if (!timercnt) |
|
|
296 | block = MAX_BLOCKTIME; |
|
|
297 | else |
536 | else |
298 | { |
537 | { |
|
|
538 | block = MAX_BLOCKTIME; |
|
|
539 | |
|
|
540 | if (timercnt) |
|
|
541 | { |
|
|
542 | ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge; |
|
|
543 | if (block > to) block = to; |
|
|
544 | } |
|
|
545 | |
|
|
546 | if (periodiccnt) |
|
|
547 | { |
299 | block = timers [0]->at - ev_now + method_fudge; |
548 | ev_tstamp to = periodics [0]->at - ev_now + method_fudge; |
|
|
549 | if (block > to) block = to; |
|
|
550 | } |
|
|
551 | |
300 | if (block < 0.) block = 0.; |
552 | if (block < 0.) block = 0.; |
301 | else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME; |
|
|
302 | } |
553 | } |
303 | |
554 | |
304 | method_poll (block); |
555 | method_poll (block); |
305 | |
556 | |
|
|
557 | /* update ev_now, do magic */ |
|
|
558 | time_update (); |
|
|
559 | |
306 | /* put pending timers into pendign queue and reschedule them */ |
560 | /* queue pending timers and reschedule them */ |
307 | timer_reify (); |
561 | periodics_reify (); /* absolute timers first */ |
|
|
562 | timers_reify (); /* relative timers second */ |
308 | |
563 | |
309 | ev_now = ev_time (); |
564 | /* queue idle watchers unless io or timers are pending */ |
|
|
565 | if (!pendingcnt) |
|
|
566 | queue_events ((W *)idles, idlecnt, EV_IDLE); |
|
|
567 | |
|
|
568 | /* queue check and possibly idle watchers */ |
|
|
569 | queue_events ((W *)checks, checkcnt, EV_CHECK); |
|
|
570 | |
310 | call_pending (); |
571 | call_pending (); |
311 | } |
572 | } |
312 | while (!ev_loop_done); |
573 | while (!ev_loop_done); |
313 | } |
|
|
314 | |
574 | |
|
|
575 | if (ev_loop_done != 2) |
|
|
576 | ev_loop_done = 0; |
|
|
577 | } |
|
|
578 | |
|
|
579 | /*****************************************************************************/ |
|
|
580 | |
315 | static void |
581 | static void |
316 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
582 | wlist_add (WL *head, WL elem) |
317 | { |
583 | { |
318 | elem->next = *head; |
584 | elem->next = *head; |
319 | *head = elem; |
585 | *head = elem; |
320 | } |
586 | } |
321 | |
587 | |
322 | static void |
588 | static void |
323 | wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
589 | wlist_del (WL *head, WL elem) |
324 | { |
590 | { |
325 | while (*head) |
591 | while (*head) |
326 | { |
592 | { |
327 | if (*head == elem) |
593 | if (*head == elem) |
328 | { |
594 | { |
… | |
… | |
333 | head = &(*head)->next; |
599 | head = &(*head)->next; |
334 | } |
600 | } |
335 | } |
601 | } |
336 | |
602 | |
337 | static void |
603 | static void |
338 | ev_start (struct ev_watcher *w, int active) |
604 | ev_start (W w, int active) |
339 | { |
605 | { |
340 | w->pending = 0; |
606 | w->pending = 0; |
341 | w->active = active; |
607 | w->active = active; |
342 | } |
608 | } |
343 | |
609 | |
344 | static void |
610 | static void |
345 | ev_stop (struct ev_watcher *w) |
611 | ev_stop (W w) |
346 | { |
612 | { |
347 | if (w->pending) |
613 | if (w->pending) |
348 | pendings [w->pending - 1].w = 0; |
614 | pendings [w->pending - 1].w = 0; |
349 | |
615 | |
350 | w->active = 0; |
616 | w->active = 0; |
351 | /* nop */ |
|
|
352 | } |
617 | } |
|
|
618 | |
|
|
619 | /*****************************************************************************/ |
353 | |
620 | |
354 | void |
621 | void |
355 | evio_start (struct ev_io *w) |
622 | evio_start (struct ev_io *w) |
356 | { |
623 | { |
357 | if (ev_is_active (w)) |
624 | if (ev_is_active (w)) |
358 | return; |
625 | return; |
359 | |
626 | |
360 | int fd = w->fd; |
627 | int fd = w->fd; |
361 | |
628 | |
362 | ev_start ((struct ev_watcher *)w, 1); |
629 | ev_start ((W)w, 1); |
363 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
630 | array_needsize (anfds, anfdmax, fd + 1, anfds_init); |
364 | wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); |
631 | wlist_add ((WL *)&anfds[fd].head, (WL)w); |
365 | |
632 | |
366 | ++fdchangecnt; |
633 | ++fdchangecnt; |
367 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
634 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
368 | fdchanges [fdchangecnt - 1] = fd; |
635 | fdchanges [fdchangecnt - 1] = fd; |
369 | } |
636 | } |
… | |
… | |
372 | evio_stop (struct ev_io *w) |
639 | evio_stop (struct ev_io *w) |
373 | { |
640 | { |
374 | if (!ev_is_active (w)) |
641 | if (!ev_is_active (w)) |
375 | return; |
642 | return; |
376 | |
643 | |
377 | wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); |
644 | wlist_del ((WL *)&anfds[w->fd].head, (WL)w); |
378 | ev_stop ((struct ev_watcher *)w); |
645 | ev_stop ((W)w); |
379 | |
646 | |
380 | ++fdchangecnt; |
647 | ++fdchangecnt; |
381 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
648 | array_needsize (fdchanges, fdchangemax, fdchangecnt, ); |
382 | fdchanges [fdchangecnt - 1] = w->fd; |
649 | fdchanges [fdchangecnt - 1] = w->fd; |
383 | } |
650 | } |
384 | |
651 | |
|
|
652 | |
385 | void |
653 | void |
386 | evtimer_start (struct ev_timer *w) |
654 | evtimer_start (struct ev_timer *w) |
387 | { |
655 | { |
388 | if (ev_is_active (w)) |
656 | if (ev_is_active (w)) |
389 | return; |
657 | return; |
390 | |
658 | |
391 | if (w->is_abs) |
|
|
392 | { |
|
|
393 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
|
|
394 | if (w->repeat) |
|
|
395 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
396 | } |
|
|
397 | else |
|
|
398 | w->at += ev_now; |
659 | w->at += now; |
399 | |
660 | |
400 | ev_start ((struct ev_watcher *)w, ++timercnt); |
661 | assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); |
|
|
662 | |
|
|
663 | ev_start ((W)w, ++timercnt); |
401 | array_needsize (timers, timermax, timercnt, ); |
664 | array_needsize (timers, timermax, timercnt, ); |
402 | timers [timercnt - 1] = w; |
665 | timers [timercnt - 1] = w; |
403 | upheap (timercnt - 1); |
666 | upheap ((WT *)timers, timercnt - 1); |
404 | } |
667 | } |
405 | |
668 | |
406 | void |
669 | void |
407 | evtimer_stop (struct ev_timer *w) |
670 | evtimer_stop (struct ev_timer *w) |
408 | { |
671 | { |
… | |
… | |
410 | return; |
673 | return; |
411 | |
674 | |
412 | if (w->active < timercnt--) |
675 | if (w->active < timercnt--) |
413 | { |
676 | { |
414 | timers [w->active - 1] = timers [timercnt]; |
677 | timers [w->active - 1] = timers [timercnt]; |
415 | downheap (w->active - 1); |
678 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
679 | } |
|
|
680 | |
|
|
681 | w->at = w->repeat; |
|
|
682 | |
|
|
683 | ev_stop ((W)w); |
|
|
684 | } |
|
|
685 | |
|
|
686 | void |
|
|
687 | evtimer_again (struct ev_timer *w) |
|
|
688 | { |
|
|
689 | if (ev_is_active (w)) |
416 | } |
690 | { |
|
|
691 | if (w->repeat) |
|
|
692 | { |
|
|
693 | w->at = now + w->repeat; |
|
|
694 | downheap ((WT *)timers, timercnt, w->active - 1); |
|
|
695 | } |
|
|
696 | else |
|
|
697 | evtimer_stop (w); |
|
|
698 | } |
|
|
699 | else if (w->repeat) |
|
|
700 | evtimer_start (w); |
|
|
701 | } |
417 | |
702 | |
418 | ev_stop ((struct ev_watcher *)w); |
703 | void |
|
|
704 | evperiodic_start (struct ev_periodic *w) |
|
|
705 | { |
|
|
706 | if (ev_is_active (w)) |
|
|
707 | return; |
|
|
708 | |
|
|
709 | assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); |
|
|
710 | |
|
|
711 | /* this formula differs from the one in periodic_reify because we do not always round up */ |
|
|
712 | if (w->interval) |
|
|
713 | w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; |
|
|
714 | |
|
|
715 | ev_start ((W)w, ++periodiccnt); |
|
|
716 | array_needsize (periodics, periodicmax, periodiccnt, ); |
|
|
717 | periodics [periodiccnt - 1] = w; |
|
|
718 | upheap ((WT *)periodics, periodiccnt - 1); |
|
|
719 | } |
|
|
720 | |
|
|
721 | void |
|
|
722 | evperiodic_stop (struct ev_periodic *w) |
|
|
723 | { |
|
|
724 | if (!ev_is_active (w)) |
|
|
725 | return; |
|
|
726 | |
|
|
727 | if (w->active < periodiccnt--) |
|
|
728 | { |
|
|
729 | periodics [w->active - 1] = periodics [periodiccnt]; |
|
|
730 | downheap ((WT *)periodics, periodiccnt, w->active - 1); |
|
|
731 | } |
|
|
732 | |
|
|
733 | ev_stop ((W)w); |
419 | } |
734 | } |
420 | |
735 | |
421 | void |
736 | void |
422 | evsignal_start (struct ev_signal *w) |
737 | evsignal_start (struct ev_signal *w) |
423 | { |
738 | { |
424 | if (ev_is_active (w)) |
739 | if (ev_is_active (w)) |
425 | return; |
740 | return; |
426 | |
741 | |
427 | ev_start ((struct ev_watcher *)w, 1); |
742 | ev_start ((W)w, 1); |
428 | array_needsize (signals, signalmax, w->signum, signals_init); |
743 | array_needsize (signals, signalmax, w->signum, signals_init); |
429 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
744 | wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); |
|
|
745 | |
|
|
746 | if (!w->next) |
|
|
747 | { |
|
|
748 | struct sigaction sa; |
|
|
749 | sa.sa_handler = sighandler; |
|
|
750 | sigfillset (&sa.sa_mask); |
|
|
751 | sa.sa_flags = 0; |
|
|
752 | sigaction (w->signum, &sa, 0); |
|
|
753 | } |
430 | } |
754 | } |
431 | |
755 | |
432 | void |
756 | void |
433 | evsignal_stop (struct ev_signal *w) |
757 | evsignal_stop (struct ev_signal *w) |
434 | { |
758 | { |
435 | if (!ev_is_active (w)) |
759 | if (!ev_is_active (w)) |
436 | return; |
760 | return; |
437 | |
761 | |
438 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
762 | wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); |
439 | ev_stop ((struct ev_watcher *)w); |
763 | ev_stop ((W)w); |
440 | } |
|
|
441 | |
764 | |
|
|
765 | if (!signals [w->signum - 1].head) |
|
|
766 | signal (w->signum, SIG_DFL); |
|
|
767 | } |
|
|
768 | |
|
|
769 | void evidle_start (struct ev_idle *w) |
|
|
770 | { |
|
|
771 | if (ev_is_active (w)) |
|
|
772 | return; |
|
|
773 | |
|
|
774 | ev_start ((W)w, ++idlecnt); |
|
|
775 | array_needsize (idles, idlemax, idlecnt, ); |
|
|
776 | idles [idlecnt - 1] = w; |
|
|
777 | } |
|
|
778 | |
|
|
779 | void evidle_stop (struct ev_idle *w) |
|
|
780 | { |
|
|
781 | idles [w->active - 1] = idles [--idlecnt]; |
|
|
782 | ev_stop ((W)w); |
|
|
783 | } |
|
|
784 | |
|
|
785 | void evcheck_start (struct ev_check *w) |
|
|
786 | { |
|
|
787 | if (ev_is_active (w)) |
|
|
788 | return; |
|
|
789 | |
|
|
790 | ev_start ((W)w, ++checkcnt); |
|
|
791 | array_needsize (checks, checkmax, checkcnt, ); |
|
|
792 | checks [checkcnt - 1] = w; |
|
|
793 | } |
|
|
794 | |
|
|
795 | void evcheck_stop (struct ev_check *w) |
|
|
796 | { |
|
|
797 | checks [w->active - 1] = checks [--checkcnt]; |
|
|
798 | ev_stop ((W)w); |
|
|
799 | } |
|
|
800 | |
442 | /*****************************************************************************/ |
801 | /*****************************************************************************/ |
|
|
802 | |
443 | #if 1 |
803 | #if 0 |
|
|
804 | |
|
|
805 | struct ev_io wio; |
444 | |
806 | |
445 | static void |
807 | static void |
446 | sin_cb (struct ev_io *w, int revents) |
808 | sin_cb (struct ev_io *w, int revents) |
447 | { |
809 | { |
448 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
810 | fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); |
449 | } |
811 | } |
450 | |
812 | |
451 | static void |
813 | static void |
452 | ocb (struct ev_timer *w, int revents) |
814 | ocb (struct ev_timer *w, int revents) |
453 | { |
815 | { |
454 | fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
816 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
817 | evtimer_stop (w); |
|
|
818 | evtimer_start (w); |
|
|
819 | } |
|
|
820 | |
|
|
821 | static void |
|
|
822 | scb (struct ev_signal *w, int revents) |
|
|
823 | { |
|
|
824 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
|
|
825 | evio_stop (&wio); |
|
|
826 | evio_start (&wio); |
|
|
827 | } |
|
|
828 | |
|
|
829 | static void |
|
|
830 | gcb (struct ev_signal *w, int revents) |
|
|
831 | { |
|
|
832 | fprintf (stderr, "generic %x\n", revents); |
|
|
833 | |
455 | } |
834 | } |
456 | |
835 | |
457 | int main (void) |
836 | int main (void) |
458 | { |
837 | { |
459 | struct ev_io sin; |
|
|
460 | |
|
|
461 | ev_init (0); |
838 | ev_init (0); |
462 | |
839 | |
463 | evw_init (&sin, sin_cb, 55); |
|
|
464 | evio_set (&sin, 0, EV_READ); |
840 | evio_init (&wio, sin_cb, 0, EV_READ); |
465 | evio_start (&sin); |
841 | evio_start (&wio); |
466 | |
842 | |
467 | struct ev_timer t[1000]; |
843 | struct ev_timer t[10000]; |
468 | |
844 | |
|
|
845 | #if 0 |
469 | int i; |
846 | int i; |
470 | for (i = 0; i < 1000; ++i) |
847 | for (i = 0; i < 10000; ++i) |
471 | { |
848 | { |
472 | struct ev_timer *w = t + i; |
849 | struct ev_timer *w = t + i; |
473 | evw_init (w, ocb, i); |
850 | evw_init (w, ocb, i); |
474 | evtimer_set_rel (w, drand48 (), 0); |
851 | evtimer_init_abs (w, ocb, drand48 (), 0.99775533); |
475 | evtimer_start (w); |
852 | evtimer_start (w); |
476 | if (drand48 () < 0.5) |
853 | if (drand48 () < 0.5) |
477 | evtimer_stop (w); |
854 | evtimer_stop (w); |
478 | } |
855 | } |
|
|
856 | #endif |
|
|
857 | |
|
|
858 | struct ev_timer t1; |
|
|
859 | evtimer_init (&t1, ocb, 5, 10); |
|
|
860 | evtimer_start (&t1); |
|
|
861 | |
|
|
862 | struct ev_signal sig; |
|
|
863 | evsignal_init (&sig, scb, SIGQUIT); |
|
|
864 | evsignal_start (&sig); |
|
|
865 | |
|
|
866 | struct ev_check cw; |
|
|
867 | evcheck_init (&cw, gcb); |
|
|
868 | evcheck_start (&cw); |
|
|
869 | |
|
|
870 | struct ev_idle iw; |
|
|
871 | evidle_init (&iw, gcb); |
|
|
872 | evidle_start (&iw); |
479 | |
873 | |
480 | ev_loop (0); |
874 | ev_loop (0); |
481 | |
875 | |
482 | return 0; |
876 | return 0; |
483 | } |
877 | } |