ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/libev/ev.c
(Generate patch)

Comparing libev/ev.c (file contents):
Revision 1.1 by root, Tue Oct 30 20:59:31 2007 UTC vs.
Revision 1.11 by root, Wed Oct 31 07:40:49 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines