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