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