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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines