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