1 | #include <math.h> |
1 | #include <math.h> |
2 | #include <stdlib.h> |
2 | #include <stdlib.h> |
|
|
3 | #include <unistd.h> |
|
|
4 | #include <fcntl.h> |
|
|
5 | #include <signal.h> |
3 | |
6 | |
4 | #include <stdio.h> |
7 | #include <stdio.h> |
5 | |
8 | |
|
|
9 | #include <assert.h> |
6 | #include <errno.h> |
10 | #include <errno.h> |
7 | #include <sys/time.h> |
11 | #include <sys/time.h> |
8 | #include <time.h> |
12 | #include <time.h> |
9 | |
13 | |
10 | #ifdef CLOCK_MONOTONIC |
14 | #ifdef CLOCK_MONOTONIC |
11 | # define HAVE_MONOTONIC 1 |
15 | # define HAVE_MONOTONIC 1 |
12 | #endif |
16 | #endif |
13 | |
17 | |
|
|
18 | #define HAVE_REALTIME 1 |
14 | #define HAVE_EPOLL 1 |
19 | #define HAVE_EPOLL 1 |
15 | #define HAVE_REALTIME 1 |
|
|
16 | #define HAVE_SELECT 0 |
20 | #define HAVE_SELECT 1 |
17 | |
21 | |
|
|
22 | #define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ |
18 | #define MAX_BLOCKTIME 60. |
23 | #define MAX_BLOCKTIME 60. |
19 | |
24 | |
20 | #include "ev.h" |
25 | #include "ev.h" |
21 | |
26 | |
22 | struct ev_watcher { |
27 | struct ev_watcher { |
… | |
… | |
25 | |
30 | |
26 | struct ev_watcher_list { |
31 | struct ev_watcher_list { |
27 | EV_WATCHER_LIST (ev_watcher_list); |
32 | EV_WATCHER_LIST (ev_watcher_list); |
28 | }; |
33 | }; |
29 | |
34 | |
|
|
35 | static ev_tstamp now, diff; /* monotonic clock */ |
30 | ev_tstamp ev_now; |
36 | ev_tstamp ev_now; |
31 | int ev_method; |
37 | int ev_method; |
32 | |
38 | |
33 | static int have_monotonic; /* runtime */ |
39 | static int have_monotonic; /* runtime */ |
34 | |
40 | |
35 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
41 | static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ |
36 | static void (*method_reify)(void); |
42 | static void (*method_modify)(int fd, int oev, int nev); |
37 | static void (*method_poll)(ev_tstamp timeout); |
43 | static void (*method_poll)(ev_tstamp timeout); |
|
|
44 | |
|
|
45 | /*****************************************************************************/ |
38 | |
46 | |
39 | ev_tstamp |
47 | ev_tstamp |
40 | ev_time (void) |
48 | ev_time (void) |
41 | { |
49 | { |
42 | #if HAVE_REALTIME |
50 | #if HAVE_REALTIME |
… | |
… | |
73 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
81 | base = realloc (base, sizeof (*base) * (newcnt)); \ |
74 | init (base + cur, newcnt - cur); \ |
82 | init (base + cur, newcnt - cur); \ |
75 | cur = newcnt; \ |
83 | cur = newcnt; \ |
76 | } |
84 | } |
77 | |
85 | |
|
|
86 | /*****************************************************************************/ |
|
|
87 | |
78 | typedef struct |
88 | typedef struct |
79 | { |
89 | { |
80 | struct ev_io *head; |
90 | struct ev_io *head; |
81 | unsigned char wev, rev; /* want, received event set */ |
91 | unsigned char wev, rev; /* want, received event set */ |
82 | } ANFD; |
92 | } ANFD; |
… | |
… | |
129 | if (ev) |
139 | if (ev) |
130 | event ((struct ev_watcher *)w, ev); |
140 | event ((struct ev_watcher *)w, ev); |
131 | } |
141 | } |
132 | } |
142 | } |
133 | |
143 | |
|
|
144 | /*****************************************************************************/ |
|
|
145 | |
134 | static struct ev_timer **timers; |
146 | static struct ev_timer **atimers; |
135 | static int timermax, timercnt; |
147 | static int atimermax, atimercnt; |
136 | |
148 | |
|
|
149 | static struct ev_timer **rtimers; |
|
|
150 | static int rtimermax, rtimercnt; |
|
|
151 | |
137 | static void |
152 | static void |
138 | upheap (int k) |
153 | upheap (struct ev_timer **timers, int k) |
139 | { |
154 | { |
140 | struct ev_timer *w = timers [k]; |
155 | struct ev_timer *w = timers [k]; |
141 | |
156 | |
142 | while (k && timers [k >> 1]->at > w->at) |
157 | while (k && timers [k >> 1]->at > w->at) |
143 | { |
158 | { |
… | |
… | |
150 | timers [k]->active = k + 1; |
165 | timers [k]->active = k + 1; |
151 | |
166 | |
152 | } |
167 | } |
153 | |
168 | |
154 | static void |
169 | static void |
155 | downheap (int k) |
170 | downheap (struct ev_timer **timers, int N, int k) |
156 | { |
171 | { |
157 | struct ev_timer *w = timers [k]; |
172 | struct ev_timer *w = timers [k]; |
158 | |
173 | |
159 | while (k < (timercnt >> 1)) |
174 | while (k < (N >> 1)) |
160 | { |
175 | { |
161 | int j = k << 1; |
176 | int j = k << 1; |
162 | |
177 | |
163 | if (j + 1 < timercnt && timers [j]->at > timers [j + 1]->at) |
178 | if (j + 1 < N && timers [j]->at > timers [j + 1]->at) |
164 | ++j; |
179 | ++j; |
165 | |
180 | |
166 | if (w->at <= timers [j]->at) |
181 | if (w->at <= timers [j]->at) |
167 | break; |
182 | break; |
168 | |
183 | |
… | |
… | |
173 | |
188 | |
174 | timers [k] = w; |
189 | timers [k] = w; |
175 | timers [k]->active = k + 1; |
190 | timers [k]->active = k + 1; |
176 | } |
191 | } |
177 | |
192 | |
178 | static struct ev_signal **signals; |
193 | /*****************************************************************************/ |
|
|
194 | |
|
|
195 | typedef struct |
|
|
196 | { |
|
|
197 | struct ev_signal *head; |
|
|
198 | sig_atomic_t gotsig; |
|
|
199 | } ANSIG; |
|
|
200 | |
|
|
201 | static ANSIG *signals; |
179 | static int signalmax, signalcnt; |
202 | static int signalmax; |
180 | |
203 | |
|
|
204 | static int sigpipe [2]; |
|
|
205 | static sig_atomic_t gotsig; |
|
|
206 | static struct ev_io sigev; |
|
|
207 | |
181 | static void |
208 | static void |
182 | signals_init (struct ev_signal **base, int count) |
209 | signals_init (ANSIG *base, int count) |
183 | { |
210 | { |
184 | while (count--) |
211 | while (count--) |
185 | *base++ = 0; |
212 | { |
|
|
213 | base->head = 0; |
|
|
214 | base->gotsig = 0; |
|
|
215 | ++base; |
|
|
216 | } |
186 | } |
217 | } |
|
|
218 | |
|
|
219 | static void |
|
|
220 | sighandler (int signum) |
|
|
221 | { |
|
|
222 | signals [signum - 1].gotsig = 1; |
|
|
223 | |
|
|
224 | if (!gotsig) |
|
|
225 | { |
|
|
226 | gotsig = 1; |
|
|
227 | write (sigpipe [1], &gotsig, 1); |
|
|
228 | } |
|
|
229 | } |
|
|
230 | |
|
|
231 | static void |
|
|
232 | sigcb (struct ev_io *iow, int revents) |
|
|
233 | { |
|
|
234 | struct ev_signal *w; |
|
|
235 | int sig; |
|
|
236 | |
|
|
237 | gotsig = 0; |
|
|
238 | read (sigpipe [0], &revents, 1); |
|
|
239 | |
|
|
240 | for (sig = signalmax; sig--; ) |
|
|
241 | if (signals [sig].gotsig) |
|
|
242 | { |
|
|
243 | signals [sig].gotsig = 0; |
|
|
244 | |
|
|
245 | for (w = signals [sig].head; w; w = w->next) |
|
|
246 | event ((struct ev_watcher *)w, EV_SIGNAL); |
|
|
247 | } |
|
|
248 | } |
|
|
249 | |
|
|
250 | static void |
|
|
251 | siginit (void) |
|
|
252 | { |
|
|
253 | fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); |
|
|
254 | fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); |
|
|
255 | |
|
|
256 | /* rather than sort out wether we really need nb, set it */ |
|
|
257 | fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); |
|
|
258 | fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); |
|
|
259 | |
|
|
260 | evio_set (&sigev, sigpipe [0], EV_READ); |
|
|
261 | evio_start (&sigev); |
|
|
262 | } |
|
|
263 | |
|
|
264 | /*****************************************************************************/ |
187 | |
265 | |
188 | #if HAVE_EPOLL |
266 | #if HAVE_EPOLL |
189 | # include "ev_epoll.c" |
267 | # include "ev_epoll.c" |
190 | #endif |
268 | #endif |
191 | #if HAVE_SELECT |
269 | #if HAVE_SELECT |
… | |
… | |
201 | have_monotonic = 1; |
279 | have_monotonic = 1; |
202 | } |
280 | } |
203 | #endif |
281 | #endif |
204 | |
282 | |
205 | ev_now = ev_time (); |
283 | ev_now = ev_time (); |
|
|
284 | now = get_clock (); |
|
|
285 | diff = ev_now - now; |
206 | |
286 | |
|
|
287 | if (pipe (sigpipe)) |
|
|
288 | return 0; |
|
|
289 | |
|
|
290 | ev_method = EVMETHOD_NONE; |
207 | #if HAVE_EPOLL |
291 | #if HAVE_EPOLL |
208 | if (epoll_init (flags)) |
292 | if (ev_method == EVMETHOD_NONE) epoll_init (flags); |
209 | return ev_method; |
|
|
210 | #endif |
293 | #endif |
211 | #if HAVE_SELECT |
294 | #if HAVE_SELECT |
212 | if (select_init (flags)) |
295 | if (ev_method == EVMETHOD_NONE) select_init (flags); |
213 | return ev_method; |
|
|
214 | #endif |
296 | #endif |
215 | |
297 | |
216 | ev_method = EVMETHOD_NONE; |
298 | if (ev_method) |
|
|
299 | { |
|
|
300 | evw_init (&sigev, sigcb, 0); |
|
|
301 | siginit (); |
|
|
302 | } |
|
|
303 | |
217 | return ev_method; |
304 | return ev_method; |
218 | } |
305 | } |
219 | |
306 | |
|
|
307 | /*****************************************************************************/ |
|
|
308 | |
220 | void ev_prefork (void) |
309 | void ev_prefork (void) |
221 | { |
310 | { |
222 | } |
311 | } |
223 | |
312 | |
224 | void ev_postfork_parent (void) |
313 | void ev_postfork_parent (void) |
… | |
… | |
226 | } |
315 | } |
227 | |
316 | |
228 | void ev_postfork_child (void) |
317 | void ev_postfork_child (void) |
229 | { |
318 | { |
230 | #if HAVE_EPOLL |
319 | #if HAVE_EPOLL |
|
|
320 | if (ev_method == EVMETHOD_EPOLL) |
231 | epoll_postfork_child (); |
321 | epoll_postfork_child (); |
232 | #endif |
322 | #endif |
|
|
323 | |
|
|
324 | evio_stop (&sigev); |
|
|
325 | close (sigpipe [0]); |
|
|
326 | close (sigpipe [1]); |
|
|
327 | pipe (sigpipe); |
|
|
328 | siginit (); |
|
|
329 | } |
|
|
330 | |
|
|
331 | /*****************************************************************************/ |
|
|
332 | |
|
|
333 | static ev_hook hooks [EVHOOK_NUM]; |
|
|
334 | |
|
|
335 | void |
|
|
336 | ev_hook_register (int type, ev_hook hook) |
|
|
337 | { |
|
|
338 | hooks [type] = hook; |
|
|
339 | } |
|
|
340 | |
|
|
341 | void |
|
|
342 | ev_hook_unregister (int type, ev_hook hook) |
|
|
343 | { |
|
|
344 | hooks [type] = 0; |
|
|
345 | } |
|
|
346 | |
|
|
347 | static void |
|
|
348 | hook_call (int type) |
|
|
349 | { |
|
|
350 | if (hooks [type]) |
|
|
351 | hooks [type] (); |
|
|
352 | } |
|
|
353 | |
|
|
354 | static void |
|
|
355 | fd_reify (void) |
|
|
356 | { |
|
|
357 | int i; |
|
|
358 | |
|
|
359 | for (i = 0; i < fdchangecnt; ++i) |
|
|
360 | { |
|
|
361 | int fd = fdchanges [i]; |
|
|
362 | ANFD *anfd = anfds + fd; |
|
|
363 | struct ev_io *w; |
|
|
364 | |
|
|
365 | int wev = 0; |
|
|
366 | |
|
|
367 | for (w = anfd->head; w; w = w->next) |
|
|
368 | wev |= w->events; |
|
|
369 | |
|
|
370 | if (anfd->wev != wev) |
|
|
371 | { |
|
|
372 | method_modify (fd, anfd->wev, wev); |
|
|
373 | anfd->wev = wev; |
|
|
374 | } |
|
|
375 | } |
|
|
376 | |
|
|
377 | fdchangecnt = 0; |
233 | } |
378 | } |
234 | |
379 | |
235 | static void |
380 | static void |
236 | call_pending () |
381 | call_pending () |
237 | { |
382 | { |
… | |
… | |
250 | |
395 | |
251 | pendingcnt = 0; |
396 | pendingcnt = 0; |
252 | } |
397 | } |
253 | |
398 | |
254 | static void |
399 | static void |
255 | timer_reify (void) |
400 | timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) |
256 | { |
401 | { |
257 | while (timercnt && timers [0]->at <= ev_now) |
402 | while (timercnt && timers [0]->at <= now) |
258 | { |
403 | { |
259 | struct ev_timer *w = timers [0]; |
404 | struct ev_timer *w = timers [0]; |
260 | |
405 | |
261 | /* first reschedule timer */ |
406 | /* first reschedule or stop timer */ |
262 | if (w->repeat) |
407 | if (w->repeat) |
263 | { |
408 | { |
264 | if (w->is_abs) |
409 | if (w->is_abs) |
265 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
410 | w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat; |
266 | else |
411 | else |
267 | w->at = ev_now + w->repeat; |
412 | w->at = now + w->repeat; |
268 | |
413 | |
269 | downheap (0); |
414 | assert (w->at > now); |
|
|
415 | |
|
|
416 | downheap (timers, timercnt, 0); |
270 | } |
417 | } |
271 | else |
418 | else |
|
|
419 | { |
272 | evtimer_stop (w); /* nonrepeating: stop timer */ |
420 | evtimer_stop (w); /* nonrepeating: stop timer */ |
|
|
421 | --timercnt; /* maybe pass by reference instead? */ |
|
|
422 | } |
273 | |
423 | |
274 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
424 | event ((struct ev_watcher *)w, EV_TIMEOUT); |
275 | } |
425 | } |
276 | } |
426 | } |
277 | |
427 | |
|
|
428 | static void |
|
|
429 | time_update () |
|
|
430 | { |
|
|
431 | int i; |
|
|
432 | ev_now = ev_time (); |
|
|
433 | |
|
|
434 | if (have_monotonic) |
|
|
435 | { |
|
|
436 | ev_tstamp odiff = diff; |
|
|
437 | |
|
|
438 | /* detecting time jumps is much more difficult */ |
|
|
439 | for (i = 2; --i; ) /* loop a few times, before making important decisions */ |
|
|
440 | { |
|
|
441 | now = get_clock (); |
|
|
442 | diff = ev_now - now; |
|
|
443 | |
|
|
444 | if (fabs (odiff - diff) < MIN_TIMEJUMP) |
|
|
445 | return; /* all is well */ |
|
|
446 | |
|
|
447 | ev_now = ev_time (); |
|
|
448 | } |
|
|
449 | |
|
|
450 | /* time jump detected, reschedule atimers */ |
|
|
451 | for (i = 0; i < atimercnt; ++i) |
|
|
452 | { |
|
|
453 | struct ev_timer *w = atimers [i]; |
|
|
454 | w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; |
|
|
455 | } |
|
|
456 | } |
|
|
457 | else |
|
|
458 | { |
|
|
459 | if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) |
|
|
460 | /* time jump detected, adjust rtimers */ |
|
|
461 | for (i = 0; i < rtimercnt; ++i) |
|
|
462 | rtimers [i]->at += ev_now - now; |
|
|
463 | |
|
|
464 | now = ev_now; |
|
|
465 | } |
|
|
466 | } |
|
|
467 | |
278 | int ev_loop_done; |
468 | int ev_loop_done; |
279 | |
469 | |
280 | int ev_loop (int flags) |
470 | void ev_loop (int flags) |
281 | { |
471 | { |
282 | double block; |
472 | double block; |
283 | ev_loop_done = flags & EVLOOP_ONESHOT; |
473 | ev_loop_done = flags & EVLOOP_ONESHOT; |
284 | |
474 | |
285 | do |
475 | do |
286 | { |
476 | { |
|
|
477 | hook_call (EVHOOK_PREPOLL); |
|
|
478 | |
287 | /* update fd-related kernel structures */ |
479 | /* update fd-related kernel structures */ |
288 | method_reify (); fdchangecnt = 0; |
480 | fd_reify (); |
289 | |
481 | |
290 | /* calculate blocking time */ |
482 | /* calculate blocking time */ |
291 | ev_now = ev_time (); |
|
|
292 | |
|
|
293 | if (flags & EVLOOP_NONBLOCK) |
483 | if (flags & EVLOOP_NONBLOCK) |
294 | block = 0.; |
484 | block = 0.; |
295 | else if (!timercnt) |
|
|
296 | block = MAX_BLOCKTIME; |
|
|
297 | else |
485 | else |
298 | { |
486 | { |
|
|
487 | block = MAX_BLOCKTIME; |
|
|
488 | |
|
|
489 | if (rtimercnt) |
|
|
490 | { |
299 | block = timers [0]->at - ev_now + method_fudge; |
491 | ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; |
|
|
492 | if (block > to) block = to; |
|
|
493 | } |
|
|
494 | |
|
|
495 | if (atimercnt) |
|
|
496 | { |
|
|
497 | ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; |
|
|
498 | if (block > to) block = to; |
|
|
499 | } |
|
|
500 | |
300 | if (block < 0.) block = 0.; |
501 | if (block < 0.) block = 0.; |
301 | else if (block > MAX_BLOCKTIME) block = MAX_BLOCKTIME; |
|
|
302 | } |
502 | } |
303 | |
503 | |
304 | method_poll (block); |
504 | method_poll (block); |
305 | |
505 | |
|
|
506 | /* update ev_now, do magic */ |
|
|
507 | time_update (); |
|
|
508 | |
|
|
509 | hook_call (EVHOOK_POSTPOLL); |
|
|
510 | |
306 | /* put pending timers into pendign queue and reschedule them */ |
511 | /* put pending timers into pendign queue and reschedule them */ |
307 | timer_reify (); |
512 | /* absolute timers first */ |
|
|
513 | timers_reify (atimers, atimercnt, ev_now); |
|
|
514 | /* relative timers second */ |
|
|
515 | timers_reify (rtimers, rtimercnt, now); |
308 | |
516 | |
309 | ev_now = ev_time (); |
|
|
310 | call_pending (); |
517 | call_pending (); |
311 | } |
518 | } |
312 | while (!ev_loop_done); |
519 | while (!ev_loop_done); |
313 | } |
520 | } |
|
|
521 | |
|
|
522 | /*****************************************************************************/ |
314 | |
523 | |
315 | static void |
524 | static void |
316 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
525 | wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) |
317 | { |
526 | { |
318 | elem->next = *head; |
527 | elem->next = *head; |
… | |
… | |
348 | pendings [w->pending - 1].w = 0; |
557 | pendings [w->pending - 1].w = 0; |
349 | |
558 | |
350 | w->active = 0; |
559 | w->active = 0; |
351 | /* nop */ |
560 | /* nop */ |
352 | } |
561 | } |
|
|
562 | |
|
|
563 | /*****************************************************************************/ |
353 | |
564 | |
354 | void |
565 | void |
355 | evio_start (struct ev_io *w) |
566 | evio_start (struct ev_io *w) |
356 | { |
567 | { |
357 | if (ev_is_active (w)) |
568 | if (ev_is_active (w)) |
… | |
… | |
391 | if (w->is_abs) |
602 | if (w->is_abs) |
392 | { |
603 | { |
393 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
604 | /* this formula differs from the one in timer_reify becuse we do not round up */ |
394 | if (w->repeat) |
605 | if (w->repeat) |
395 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
606 | w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; |
|
|
607 | |
|
|
608 | ev_start ((struct ev_watcher *)w, ++atimercnt); |
|
|
609 | array_needsize (atimers, atimermax, atimercnt, ); |
|
|
610 | atimers [atimercnt - 1] = w; |
|
|
611 | upheap (atimers, atimercnt - 1); |
396 | } |
612 | } |
397 | else |
613 | else |
|
|
614 | { |
398 | w->at += ev_now; |
615 | w->at += now; |
399 | |
616 | |
400 | ev_start ((struct ev_watcher *)w, ++timercnt); |
617 | ev_start ((struct ev_watcher *)w, ++rtimercnt); |
401 | array_needsize (timers, timermax, timercnt, ); |
618 | array_needsize (rtimers, rtimermax, rtimercnt, ); |
402 | timers [timercnt - 1] = w; |
619 | rtimers [rtimercnt - 1] = w; |
403 | upheap (timercnt - 1); |
620 | upheap (rtimers, rtimercnt - 1); |
|
|
621 | } |
|
|
622 | |
404 | } |
623 | } |
405 | |
624 | |
406 | void |
625 | void |
407 | evtimer_stop (struct ev_timer *w) |
626 | evtimer_stop (struct ev_timer *w) |
408 | { |
627 | { |
409 | if (!ev_is_active (w)) |
628 | if (!ev_is_active (w)) |
410 | return; |
629 | return; |
411 | |
630 | |
|
|
631 | if (w->is_abs) |
|
|
632 | { |
412 | if (w->active < timercnt--) |
633 | if (w->active < atimercnt--) |
413 | { |
634 | { |
414 | timers [w->active - 1] = timers [timercnt]; |
635 | atimers [w->active - 1] = atimers [atimercnt]; |
415 | downheap (w->active - 1); |
636 | downheap (atimers, atimercnt, w->active - 1); |
|
|
637 | } |
|
|
638 | } |
|
|
639 | else |
|
|
640 | { |
|
|
641 | if (w->active < rtimercnt--) |
|
|
642 | { |
|
|
643 | rtimers [w->active - 1] = rtimers [rtimercnt]; |
|
|
644 | downheap (rtimers, rtimercnt, w->active - 1); |
|
|
645 | } |
416 | } |
646 | } |
417 | |
647 | |
418 | ev_stop ((struct ev_watcher *)w); |
648 | ev_stop ((struct ev_watcher *)w); |
419 | } |
649 | } |
420 | |
650 | |
… | |
… | |
424 | if (ev_is_active (w)) |
654 | if (ev_is_active (w)) |
425 | return; |
655 | return; |
426 | |
656 | |
427 | ev_start ((struct ev_watcher *)w, 1); |
657 | ev_start ((struct ev_watcher *)w, 1); |
428 | array_needsize (signals, signalmax, w->signum, signals_init); |
658 | array_needsize (signals, signalmax, w->signum, signals_init); |
429 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
659 | wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
|
|
660 | |
|
|
661 | if (!w->next) |
|
|
662 | { |
|
|
663 | struct sigaction sa; |
|
|
664 | sa.sa_handler = sighandler; |
|
|
665 | sigfillset (&sa.sa_mask); |
|
|
666 | sa.sa_flags = 0; |
|
|
667 | sigaction (w->signum, &sa, 0); |
|
|
668 | } |
430 | } |
669 | } |
431 | |
670 | |
432 | void |
671 | void |
433 | evsignal_stop (struct ev_signal *w) |
672 | evsignal_stop (struct ev_signal *w) |
434 | { |
673 | { |
435 | if (!ev_is_active (w)) |
674 | if (!ev_is_active (w)) |
436 | return; |
675 | return; |
437 | |
676 | |
438 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1], (struct ev_watcher_list *)w); |
677 | wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); |
439 | ev_stop ((struct ev_watcher *)w); |
678 | ev_stop ((struct ev_watcher *)w); |
|
|
679 | |
|
|
680 | if (!signals [w->signum - 1].head) |
|
|
681 | signal (w->signum, SIG_DFL); |
440 | } |
682 | } |
441 | |
683 | |
442 | /*****************************************************************************/ |
684 | /*****************************************************************************/ |
443 | #if 1 |
685 | #if 1 |
444 | |
686 | |
… | |
… | |
449 | } |
691 | } |
450 | |
692 | |
451 | static void |
693 | static void |
452 | ocb (struct ev_timer *w, int revents) |
694 | ocb (struct ev_timer *w, int revents) |
453 | { |
695 | { |
454 | fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
696 | //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); |
|
|
697 | evtimer_stop (w); |
|
|
698 | evtimer_start (w); |
|
|
699 | } |
|
|
700 | |
|
|
701 | static void |
|
|
702 | scb (struct ev_signal *w, int revents) |
|
|
703 | { |
|
|
704 | fprintf (stderr, "signal %x,%d\n", revents, w->signum); |
455 | } |
705 | } |
456 | |
706 | |
457 | int main (void) |
707 | int main (void) |
458 | { |
708 | { |
459 | struct ev_io sin; |
709 | struct ev_io sin; |
… | |
… | |
462 | |
712 | |
463 | evw_init (&sin, sin_cb, 55); |
713 | evw_init (&sin, sin_cb, 55); |
464 | evio_set (&sin, 0, EV_READ); |
714 | evio_set (&sin, 0, EV_READ); |
465 | evio_start (&sin); |
715 | evio_start (&sin); |
466 | |
716 | |
467 | struct ev_timer t[1000]; |
717 | struct ev_timer t[10000]; |
468 | |
718 | |
|
|
719 | #if 1 |
469 | int i; |
720 | int i; |
470 | for (i = 0; i < 1000; ++i) |
721 | for (i = 0; i < 10000; ++i) |
471 | { |
722 | { |
472 | struct ev_timer *w = t + i; |
723 | struct ev_timer *w = t + i; |
473 | evw_init (w, ocb, i); |
724 | evw_init (w, ocb, i); |
474 | evtimer_set_rel (w, drand48 (), 0); |
725 | evtimer_set_abs (w, drand48 (), 0.99775533); |
475 | evtimer_start (w); |
726 | evtimer_start (w); |
476 | if (drand48 () < 0.5) |
727 | if (drand48 () < 0.5) |
477 | evtimer_stop (w); |
728 | evtimer_stop (w); |
478 | } |
729 | } |
|
|
730 | #endif |
|
|
731 | |
|
|
732 | struct ev_timer t1; |
|
|
733 | evw_init (&t1, ocb, 0); |
|
|
734 | evtimer_set_abs (&t1, 5, 10); |
|
|
735 | evtimer_start (&t1); |
|
|
736 | |
|
|
737 | struct ev_signal sig; |
|
|
738 | evw_init (&sig, scb, 65535); |
|
|
739 | evsignal_set (&sig, SIGQUIT); |
|
|
740 | evsignal_start (&sig); |
479 | |
741 | |
480 | ev_loop (0); |
742 | ev_loop (0); |
481 | |
743 | |
482 | return 0; |
744 | return 0; |
483 | } |
745 | } |