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.16 by root, Wed Oct 31 13:57:34 2007 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines