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

Comparing libev/ev.c (file contents):
Revision 1.7 by root, Wed Oct 31 00:24:16 2007 UTC vs.
Revision 1.12 by root, Wed Oct 31 09:23:17 2007 UTC

9#include <assert.h> 9#include <assert.h>
10#include <errno.h> 10#include <errno.h>
11#include <sys/time.h> 11#include <sys/time.h>
12#include <time.h> 12#include <time.h>
13 13
14#ifdef CLOCK_MONOTONIC
15# define HAVE_MONOTONIC 1
16#endif
17
18#define HAVE_REALTIME 1
19#define HAVE_EPOLL 1 14#define HAVE_EPOLL 1
15
16#ifndef HAVE_MONOTONIC
17# ifdef CLOCK_MONOTONIC
18# define HAVE_MONOTONIC 1
19# endif
20#endif
21
22#ifndef HAVE_SELECT
20#define HAVE_SELECT 1 23# define HAVE_SELECT 1
24#endif
25
26#ifndef HAVE_EPOLL
27# define HAVE_EPOLL 0
28#endif
29
30#ifndef HAVE_REALTIME
31# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
32#endif
21 33
22#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 34#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23#define MAX_BLOCKTIME 60. 35#define MAX_BLOCKTIME 60.
24 36
25#include "ev.h" 37#include "ev.h"
30 42
31struct ev_watcher_list { 43struct ev_watcher_list {
32 EV_WATCHER_LIST (ev_watcher_list); 44 EV_WATCHER_LIST (ev_watcher_list);
33}; 45};
34 46
47struct ev_watcher_time {
48 EV_WATCHER_TIME (ev_watcher_time);
49};
50
51typedef struct ev_watcher *W;
52typedef struct ev_watcher_list *WL;
53typedef struct ev_watcher_time *WT;
54
35static ev_tstamp now, diff; /* monotonic clock */ 55static ev_tstamp now, diff; /* monotonic clock */
36ev_tstamp ev_now; 56ev_tstamp ev_now;
37int ev_method; 57int ev_method;
38 58
39static int have_monotonic; /* runtime */ 59static int have_monotonic; /* runtime */
40 60
41static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 61static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42static void (*method_modify)(int fd, int oev, int nev); 62static void (*method_modify)(int fd, int oev, int nev);
43static void (*method_poll)(ev_tstamp timeout); 63static void (*method_poll)(ev_tstamp timeout);
64
65/*****************************************************************************/
44 66
45ev_tstamp 67ev_tstamp
46ev_time (void) 68ev_time (void)
47{ 69{
48#if HAVE_REALTIME 70#if HAVE_REALTIME
79 base = realloc (base, sizeof (*base) * (newcnt)); \ 101 base = realloc (base, sizeof (*base) * (newcnt)); \
80 init (base + cur, newcnt - cur); \ 102 init (base + cur, newcnt - cur); \
81 cur = newcnt; \ 103 cur = newcnt; \
82 } 104 }
83 105
106/*****************************************************************************/
107
84typedef struct 108typedef struct
85{ 109{
86 struct ev_io *head; 110 struct ev_io *head;
87 unsigned char wev, rev; /* want, received event set */ 111 unsigned char wev, rev; /* want, received event set */
88} ANFD; 112} ANFD;
104 } 128 }
105} 129}
106 130
107typedef struct 131typedef struct
108{ 132{
109 struct ev_watcher *w; 133 W w;
110 int events; 134 int events;
111} ANPENDING; 135} ANPENDING;
112 136
113static ANPENDING *pendings; 137static ANPENDING *pendings;
114static int pendingmax, pendingcnt; 138static int pendingmax, pendingcnt;
115 139
116static void 140static void
117event (struct ev_watcher *w, int events) 141event (W w, int events)
118{ 142{
119 w->pending = ++pendingcnt; 143 w->pending = ++pendingcnt;
120 array_needsize (pendings, pendingmax, pendingcnt, ); 144 array_needsize (pendings, pendingmax, pendingcnt, );
121 pendings [pendingcnt - 1].w = w; 145 pendings [pendingcnt - 1].w = w;
122 pendings [pendingcnt - 1].events = events; 146 pendings [pendingcnt - 1].events = events;
131 for (w = anfd->head; w; w = w->next) 155 for (w = anfd->head; w; w = w->next)
132 { 156 {
133 int ev = w->events & events; 157 int ev = w->events & events;
134 158
135 if (ev) 159 if (ev)
136 event ((struct ev_watcher *)w, ev); 160 event ((W)w, ev);
137 } 161 }
138} 162}
139 163
164static void
165queue_events (W *events, int eventcnt, int type)
166{
167 int i;
168
169 for (i = 0; i < eventcnt; ++i)
170 event (events [i], type);
171}
172
173/*****************************************************************************/
174
140static struct ev_timer **atimers; 175static struct ev_timer **timers;
141static int atimermax, atimercnt; 176static int timermax, timercnt;
142 177
143static struct ev_timer **rtimers; 178static struct ev_periodic **periodics;
144static int rtimermax, rtimercnt; 179static int periodicmax, periodiccnt;
145 180
146static void 181static void
147upheap (struct ev_timer **timers, int k) 182upheap (WT *timers, int k)
148{ 183{
149 struct ev_timer *w = timers [k]; 184 WT w = timers [k];
150 185
151 while (k && timers [k >> 1]->at > w->at) 186 while (k && timers [k >> 1]->at > w->at)
152 { 187 {
153 timers [k] = timers [k >> 1]; 188 timers [k] = timers [k >> 1];
154 timers [k]->active = k + 1; 189 timers [k]->active = k + 1;
159 timers [k]->active = k + 1; 194 timers [k]->active = k + 1;
160 195
161} 196}
162 197
163static void 198static void
164downheap (struct ev_timer **timers, int N, int k) 199downheap (WT *timers, int N, int k)
165{ 200{
166 struct ev_timer *w = timers [k]; 201 WT w = timers [k];
167 202
168 while (k < (N >> 1)) 203 while (k < (N >> 1))
169 { 204 {
170 int j = k << 1; 205 int j = k << 1;
171 206
181 } 216 }
182 217
183 timers [k] = w; 218 timers [k] = w;
184 timers [k]->active = k + 1; 219 timers [k]->active = k + 1;
185} 220}
221
222/*****************************************************************************/
186 223
187typedef struct 224typedef struct
188{ 225{
189 struct ev_signal *head; 226 struct ev_signal *head;
190 sig_atomic_t gotsig; 227 sig_atomic_t gotsig;
233 if (signals [sig].gotsig) 270 if (signals [sig].gotsig)
234 { 271 {
235 signals [sig].gotsig = 0; 272 signals [sig].gotsig = 0;
236 273
237 for (w = signals [sig].head; w; w = w->next) 274 for (w = signals [sig].head; w; w = w->next)
238 event ((struct ev_watcher *)w, EV_SIGNAL); 275 event ((W)w, EV_SIGNAL);
239 } 276 }
240} 277}
241 278
242static void 279static void
243siginit (void) 280siginit (void)
250 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 287 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
251 288
252 evio_set (&sigev, sigpipe [0], EV_READ); 289 evio_set (&sigev, sigpipe [0], EV_READ);
253 evio_start (&sigev); 290 evio_start (&sigev);
254} 291}
292
293/*****************************************************************************/
294
295static struct ev_idle **idles;
296static int idlemax, idlecnt;
297
298static struct ev_check **checks;
299static int checkmax, checkcnt;
300
301/*****************************************************************************/
255 302
256#if HAVE_EPOLL 303#if HAVE_EPOLL
257# include "ev_epoll.c" 304# include "ev_epoll.c"
258#endif 305#endif
259#if HAVE_SELECT 306#if HAVE_SELECT
285 if (ev_method == EVMETHOD_NONE) select_init (flags); 332 if (ev_method == EVMETHOD_NONE) select_init (flags);
286#endif 333#endif
287 334
288 if (ev_method) 335 if (ev_method)
289 { 336 {
290 evw_init (&sigev, sigcb, 0); 337 evw_init (&sigev, sigcb);
291 siginit (); 338 siginit ();
292 } 339 }
293 340
294 return ev_method; 341 return ev_method;
295} 342}
296 343
344/*****************************************************************************/
345
297void ev_prefork (void) 346void ev_prefork (void)
298{ 347{
348 /* nop */
299} 349}
300 350
301void ev_postfork_parent (void) 351void ev_postfork_parent (void)
302{ 352{
353 /* nop */
303} 354}
304 355
305void ev_postfork_child (void) 356void ev_postfork_child (void)
306{ 357{
307#if HAVE_EPOLL 358#if HAVE_EPOLL
314 close (sigpipe [1]); 365 close (sigpipe [1]);
315 pipe (sigpipe); 366 pipe (sigpipe);
316 siginit (); 367 siginit ();
317} 368}
318 369
370/*****************************************************************************/
371
319static void 372static void
320fd_reify (void) 373fd_reify (void)
321{ 374{
322 int i; 375 int i;
323 376
360 413
361 pendingcnt = 0; 414 pendingcnt = 0;
362} 415}
363 416
364static void 417static void
365timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 418timers_reify ()
366{ 419{
367 while (timercnt && timers [0]->at <= now) 420 while (timercnt && timers [0]->at <= now)
368 { 421 {
369 struct ev_timer *w = timers [0]; 422 struct ev_timer *w = timers [0];
370 423
371 /* first reschedule or stop timer */ 424 /* first reschedule or stop timer */
372 if (w->repeat) 425 if (w->repeat)
373 { 426 {
374 if (w->is_abs)
375 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
376 else
377 w->at = now + w->repeat; 427 w->at = now + w->repeat;
378 428 assert (("timer timeout in the past, negative repeat?", w->at > now));
379 assert (w->at > now);
380
381 downheap (timers, timercnt, 0); 429 downheap ((WT *)timers, timercnt, 0);
382 } 430 }
383 else 431 else
432 evtimer_stop (w); /* nonrepeating: stop timer */
433
434 event ((W)w, EV_TIMEOUT);
435 }
436}
437
438static void
439periodics_reify ()
440{
441 while (periodiccnt && periodics [0]->at <= ev_now)
442 {
443 struct ev_periodic *w = periodics [0];
444
445 /* first reschedule or stop timer */
446 if (w->interval)
384 { 447 {
385 evtimer_stop (w); /* nonrepeating: stop timer */ 448 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
386 --timercnt; /* maybe pass by reference instead? */ 449 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
450 downheap ((WT *)periodics, periodiccnt, 0);
387 } 451 }
452 else
453 evperiodic_stop (w); /* nonrepeating: stop timer */
388 454
389 event ((struct ev_watcher *)w, EV_TIMEOUT); 455 event ((W)w, EV_TIMEOUT);
456 }
457}
458
459static void
460time_jump (ev_tstamp diff)
461{
462 int i;
463
464 /* adjust periodics */
465 for (i = 0; i < periodiccnt; ++i)
390 } 466 {
467 struct ev_periodic *w = periodics [i];
468
469 if (w->interval)
470 {
471 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
472
473 if (fabs (diff) >= 1e-4)
474 {
475 evperiodic_stop (w);
476 evperiodic_start (w);
477
478 i = 0; /* restart loop, inefficient, but time jumps should be rare */
479 }
480 }
481 }
482
483 /* adjust timers. this is easy, as the offset is the same for all */
484 for (i = 0; i < timercnt; ++i)
485 timers [i]->at += diff;
391} 486}
392 487
393static void 488static void
394time_update () 489time_update ()
395{ 490{
396 int i; 491 int i;
492
397 ev_now = ev_time (); 493 ev_now = ev_time ();
398 494
399 if (have_monotonic) 495 if (have_monotonic)
400 { 496 {
401 ev_tstamp odiff = diff; 497 ev_tstamp odiff = diff;
402 498
403 /* detecting time jumps is much more difficult */
404 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 499 for (i = 4; --i; ) /* loop a few times, before making important decisions */
405 { 500 {
406 now = get_clock (); 501 now = get_clock ();
407 diff = ev_now - now; 502 diff = ev_now - now;
408 503
409 if (fabs (odiff - diff) < MIN_TIMEJUMP) 504 if (fabs (odiff - diff) < MIN_TIMEJUMP)
410 return; /* all is well */ 505 return; /* all is well */
411 506
412 ev_now = ev_time (); 507 ev_now = ev_time ();
413 } 508 }
414 509
415 /* time jump detected, reschedule atimers */ 510 time_jump (diff - odiff);
416 for (i = 0; i < atimercnt; ++i)
417 {
418 struct ev_timer *w = atimers [i];
419 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
420 }
421 } 511 }
422 else 512 else
423 { 513 {
424 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 514 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
425 /* time jump detected, adjust rtimers */ 515 time_jump (ev_now - now);
426 for (i = 0; i < rtimercnt; ++i)
427 rtimers [i]->at += ev_now - now;
428 516
429 now = ev_now; 517 now = ev_now;
430 } 518 }
431} 519}
432 520
435void ev_loop (int flags) 523void ev_loop (int flags)
436{ 524{
437 double block; 525 double block;
438 ev_loop_done = flags & EVLOOP_ONESHOT; 526 ev_loop_done = flags & EVLOOP_ONESHOT;
439 527
528 if (checkcnt)
529 {
530 queue_events ((W *)checks, checkcnt, EV_CHECK);
531 call_pending ();
532 }
533
440 do 534 do
441 { 535 {
442 /* update fd-related kernel structures */ 536 /* update fd-related kernel structures */
443 fd_reify (); 537 fd_reify ();
444 538
445 /* calculate blocking time */ 539 /* calculate blocking time */
540
541 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */
542 ev_now = ev_time ();
543
446 if (flags & EVLOOP_NONBLOCK) 544 if (flags & EVLOOP_NONBLOCK || idlecnt)
447 block = 0.; 545 block = 0.;
448 else 546 else
449 { 547 {
450 block = MAX_BLOCKTIME; 548 block = MAX_BLOCKTIME;
451 549
452 if (rtimercnt) 550 if (timercnt)
453 { 551 {
454 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 552 ev_tstamp to = timers [0]->at - get_clock () + method_fudge;
455 if (block > to) block = to; 553 if (block > to) block = to;
456 } 554 }
457 555
458 if (atimercnt) 556 if (periodiccnt)
459 { 557 {
460 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 558 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
461 if (block > to) block = to; 559 if (block > to) block = to;
462 } 560 }
463 561
464 if (block < 0.) block = 0.; 562 if (block < 0.) block = 0.;
465 } 563 }
467 method_poll (block); 565 method_poll (block);
468 566
469 /* update ev_now, do magic */ 567 /* update ev_now, do magic */
470 time_update (); 568 time_update ();
471 569
472 /* put pending timers into pendign queue and reschedule them */ 570 /* queue pending timers and reschedule them */
473 /* absolute timers first */ 571 periodics_reify (); /* absolute timers first */
474 timers_reify (atimers, atimercnt, ev_now);
475 /* relative timers second */ 572 timers_reify (); /* relative timers second */
476 timers_reify (rtimers, rtimercnt, now); 573
574 /* queue idle watchers unless io or timers are pending */
575 if (!pendingcnt)
576 queue_events ((W *)idles, idlecnt, EV_IDLE);
577
578 /* queue check and possibly idle watchers */
579 queue_events ((W *)checks, checkcnt, EV_CHECK);
477 580
478 call_pending (); 581 call_pending ();
479 } 582 }
480 while (!ev_loop_done); 583 while (!ev_loop_done);
481} 584}
482 585
586/*****************************************************************************/
587
483static void 588static void
484wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 589wlist_add (WL *head, WL elem)
485{ 590{
486 elem->next = *head; 591 elem->next = *head;
487 *head = elem; 592 *head = elem;
488} 593}
489 594
490static void 595static void
491wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 596wlist_del (WL *head, WL elem)
492{ 597{
493 while (*head) 598 while (*head)
494 { 599 {
495 if (*head == elem) 600 if (*head == elem)
496 { 601 {
501 head = &(*head)->next; 606 head = &(*head)->next;
502 } 607 }
503} 608}
504 609
505static void 610static void
506ev_start (struct ev_watcher *w, int active) 611ev_start (W w, int active)
507{ 612{
508 w->pending = 0; 613 w->pending = 0;
509 w->active = active; 614 w->active = active;
510} 615}
511 616
512static void 617static void
513ev_stop (struct ev_watcher *w) 618ev_stop (W w)
514{ 619{
515 if (w->pending) 620 if (w->pending)
516 pendings [w->pending - 1].w = 0; 621 pendings [w->pending - 1].w = 0;
517 622
518 w->active = 0; 623 w->active = 0;
519 /* nop */
520} 624}
625
626/*****************************************************************************/
521 627
522void 628void
523evio_start (struct ev_io *w) 629evio_start (struct ev_io *w)
524{ 630{
525 if (ev_is_active (w)) 631 if (ev_is_active (w))
526 return; 632 return;
527 633
528 int fd = w->fd; 634 int fd = w->fd;
529 635
530 ev_start ((struct ev_watcher *)w, 1); 636 ev_start ((W)w, 1);
531 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 637 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
532 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 638 wlist_add ((WL *)&anfds[fd].head, (WL)w);
533 639
534 ++fdchangecnt; 640 ++fdchangecnt;
535 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 641 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
536 fdchanges [fdchangecnt - 1] = fd; 642 fdchanges [fdchangecnt - 1] = fd;
537} 643}
540evio_stop (struct ev_io *w) 646evio_stop (struct ev_io *w)
541{ 647{
542 if (!ev_is_active (w)) 648 if (!ev_is_active (w))
543 return; 649 return;
544 650
545 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 651 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
546 ev_stop ((struct ev_watcher *)w); 652 ev_stop ((W)w);
547 653
548 ++fdchangecnt; 654 ++fdchangecnt;
549 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 655 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
550 fdchanges [fdchangecnt - 1] = w->fd; 656 fdchanges [fdchangecnt - 1] = w->fd;
551} 657}
552 658
659
553void 660void
554evtimer_start (struct ev_timer *w) 661evtimer_start (struct ev_timer *w)
555{ 662{
556 if (ev_is_active (w)) 663 if (ev_is_active (w))
557 return; 664 return;
558 665
559 if (w->is_abs) 666 w->at += now;
560 {
561 /* this formula differs from the one in timer_reify becuse we do not round up */
562 if (w->repeat)
563 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat;
564 667
565 ev_start ((struct ev_watcher *)w, ++atimercnt); 668 ev_start ((W)w, ++timercnt);
566 array_needsize (atimers, atimermax, atimercnt, ); 669 array_needsize (timers, timermax, timercnt, );
567 atimers [atimercnt - 1] = w; 670 timers [timercnt - 1] = w;
568 upheap (atimers, atimercnt - 1); 671 upheap ((WT *)timers, timercnt - 1);
569 }
570 else
571 {
572 w->at += now;
573
574 ev_start ((struct ev_watcher *)w, ++rtimercnt);
575 array_needsize (rtimers, rtimermax, rtimercnt, );
576 rtimers [rtimercnt - 1] = w;
577 upheap (rtimers, rtimercnt - 1);
578 }
579
580} 672}
581 673
582void 674void
583evtimer_stop (struct ev_timer *w) 675evtimer_stop (struct ev_timer *w)
584{ 676{
585 if (!ev_is_active (w)) 677 if (!ev_is_active (w))
586 return; 678 return;
587 679
588 if (w->is_abs)
589 {
590 if (w->active < atimercnt--) 680 if (w->active < timercnt--)
591 { 681 {
592 atimers [w->active - 1] = atimers [atimercnt]; 682 timers [w->active - 1] = timers [timercnt];
593 downheap (atimers, atimercnt, w->active - 1); 683 downheap ((WT *)timers, timercnt, w->active - 1);
594 }
595 }
596 else
597 { 684 }
685
686 ev_stop ((W)w);
687}
688
689void
690evperiodic_start (struct ev_periodic *w)
691{
692 if (ev_is_active (w))
693 return;
694
695 /* this formula differs from the one in periodic_reify because we do not always round up */
696 if (w->interval)
697 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
698
699 ev_start ((W)w, ++periodiccnt);
700 array_needsize (periodics, periodicmax, periodiccnt, );
701 periodics [periodiccnt - 1] = w;
702 upheap ((WT *)periodics, periodiccnt - 1);
703}
704
705void
706evperiodic_stop (struct ev_periodic *w)
707{
708 if (!ev_is_active (w))
709 return;
710
598 if (w->active < rtimercnt--) 711 if (w->active < periodiccnt--)
599 {
600 rtimers [w->active - 1] = rtimers [rtimercnt];
601 downheap (rtimers, rtimercnt, w->active - 1);
602 }
603 } 712 {
713 periodics [w->active - 1] = periodics [periodiccnt];
714 downheap ((WT *)periodics, periodiccnt, w->active - 1);
715 }
604 716
605 ev_stop ((struct ev_watcher *)w); 717 ev_stop ((W)w);
606} 718}
607 719
608void 720void
609evsignal_start (struct ev_signal *w) 721evsignal_start (struct ev_signal *w)
610{ 722{
611 if (ev_is_active (w)) 723 if (ev_is_active (w))
612 return; 724 return;
613 725
614 ev_start ((struct ev_watcher *)w, 1); 726 ev_start ((W)w, 1);
615 array_needsize (signals, signalmax, w->signum, signals_init); 727 array_needsize (signals, signalmax, w->signum, signals_init);
616 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 728 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
617 729
618 if (!w->next) 730 if (!w->next)
619 { 731 {
620 struct sigaction sa; 732 struct sigaction sa;
621 sa.sa_handler = sighandler; 733 sa.sa_handler = sighandler;
629evsignal_stop (struct ev_signal *w) 741evsignal_stop (struct ev_signal *w)
630{ 742{
631 if (!ev_is_active (w)) 743 if (!ev_is_active (w))
632 return; 744 return;
633 745
634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 746 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
635 ev_stop ((struct ev_watcher *)w); 747 ev_stop ((W)w);
636 748
637 if (!signals [w->signum - 1].head) 749 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL); 750 signal (w->signum, SIG_DFL);
639} 751}
640 752
753void evidle_start (struct ev_idle *w)
754{
755 if (ev_is_active (w))
756 return;
757
758 ev_start ((W)w, ++idlecnt);
759 array_needsize (idles, idlemax, idlecnt, );
760 idles [idlecnt - 1] = w;
761}
762
763void evidle_stop (struct ev_idle *w)
764{
765 idles [w->active - 1] = idles [--idlecnt];
766 ev_stop ((W)w);
767}
768
769void evcheck_start (struct ev_check *w)
770{
771 if (ev_is_active (w))
772 return;
773
774 ev_start ((W)w, ++checkcnt);
775 array_needsize (checks, checkmax, checkcnt, );
776 checks [checkcnt - 1] = w;
777}
778
779void evcheck_stop (struct ev_check *w)
780{
781 checks [w->active - 1] = checks [--checkcnt];
782 ev_stop ((W)w);
783}
784
641/*****************************************************************************/ 785/*****************************************************************************/
786
642#if 1 787#if 1
788
789struct ev_io wio;
643 790
644static void 791static void
645sin_cb (struct ev_io *w, int revents) 792sin_cb (struct ev_io *w, int revents)
646{ 793{
647 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 794 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
657 804
658static void 805static void
659scb (struct ev_signal *w, int revents) 806scb (struct ev_signal *w, int revents)
660{ 807{
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 808 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
809 evio_stop (&wio);
810 evio_start (&wio);
811}
812
813static void
814gcb (struct ev_signal *w, int revents)
815{
816 fprintf (stderr, "generic %x\n", revents);
817
662} 818}
663 819
664int main (void) 820int main (void)
665{ 821{
666 struct ev_io sin;
667
668 ev_init (0); 822 ev_init (0);
669 823
670 evw_init (&sin, sin_cb, 55);
671 evio_set (&sin, 0, EV_READ); 824 evio_init (&wio, sin_cb, 0, EV_READ);
672 evio_start (&sin); 825 evio_start (&wio);
673 826
674 struct ev_timer t[10000]; 827 struct ev_timer t[10000];
675 828
676#if 0 829#if 0
677 int i; 830 int i;
678 for (i = 0; i < 10000; ++i) 831 for (i = 0; i < 10000; ++i)
679 { 832 {
680 struct ev_timer *w = t + i; 833 struct ev_timer *w = t + i;
681 evw_init (w, ocb, i); 834 evw_init (w, ocb, i);
682 evtimer_set_abs (w, drand48 (), 0.99775533); 835 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
683 evtimer_start (w); 836 evtimer_start (w);
684 if (drand48 () < 0.5) 837 if (drand48 () < 0.5)
685 evtimer_stop (w); 838 evtimer_stop (w);
686 } 839 }
687#endif 840#endif
688 841
689 struct ev_timer t1; 842 struct ev_timer t1;
690 evw_init (&t1, ocb, 0); 843 evtimer_init (&t1, ocb, 5, 10);
691 evtimer_set_abs (&t1, 5, 10);
692 evtimer_start (&t1); 844 evtimer_start (&t1);
693 845
694 struct ev_signal sig; 846 struct ev_signal sig;
695 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT); 847 evsignal_init (&sig, scb, SIGQUIT);
697 evsignal_start (&sig); 848 evsignal_start (&sig);
698 849
850 struct ev_check cw;
851 evcheck_init (&cw, gcb);
852 evcheck_start (&cw);
853
854 struct ev_idle iw;
855 evidle_init (&iw, gcb);
856 evidle_start (&iw);
857
699 ev_loop (0); 858 ev_loop (0);
700 859
701 return 0; 860 return 0;
702} 861}
703 862

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines