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