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

Comparing libev/ev.c (file contents):
Revision 1.11 by root, Wed Oct 31 07:40:49 2007 UTC vs.
Revision 1.25 by root, Wed Oct 31 21:34:45 2007 UTC

1/*
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met:
8 *
9 * * Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials provided
15 * with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
1#include <math.h> 30#include <math.h>
2#include <stdlib.h> 31#include <stdlib.h>
3#include <unistd.h> 32#include <unistd.h>
4#include <fcntl.h> 33#include <fcntl.h>
5#include <signal.h> 34#include <signal.h>
35#include <stddef.h>
6 36
7#include <stdio.h> 37#include <stdio.h>
8 38
9#include <assert.h> 39#include <assert.h>
10#include <errno.h> 40#include <errno.h>
41#include <sys/types.h>
42#include <sys/wait.h>
11#include <sys/time.h> 43#include <sys/time.h>
12#include <time.h> 44#include <time.h>
13 45
14#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
15# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
29# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
30#endif 62#endif
31 63
32#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
33#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
34 67
35#include "ev.h" 68#include "ev.h"
36
37struct ev_watcher {
38 EV_WATCHER (ev_watcher);
39};
40
41struct ev_watcher_list {
42 EV_WATCHER_LIST (ev_watcher_list);
43};
44 69
45typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
46typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
72typedef struct ev_watcher_time *WT;
47 73
48static ev_tstamp now, diff; /* monotonic clock */ 74static ev_tstamp now, diff; /* monotonic clock */
49ev_tstamp ev_now; 75ev_tstamp ev_now;
50int ev_method; 76int ev_method;
51 77
87} 113}
88 114
89#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
90 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
91 { \ 117 { \
92 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
93 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ 119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
94 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
95 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
96 cur = newcnt; \ 127 cur = newcnt; \
97 } 128 }
98 129
131static int pendingmax, pendingcnt; 162static int pendingmax, pendingcnt;
132 163
133static void 164static void
134event (W w, int events) 165event (W w, int events)
135{ 166{
167 if (w->active)
168 {
136 w->pending = ++pendingcnt; 169 w->pending = ++pendingcnt;
137 array_needsize (pendings, pendingmax, pendingcnt, ); 170 array_needsize (pendings, pendingmax, pendingcnt, );
138 pendings [pendingcnt - 1].w = w; 171 pendings [pendingcnt - 1].w = w;
139 pendings [pendingcnt - 1].events = events; 172 pendings [pendingcnt - 1].events = events;
173 }
140} 174}
141 175
142static void 176static void
143fd_event (int fd, int events) 177fd_event (int fd, int events)
144{ 178{
161 195
162 for (i = 0; i < eventcnt; ++i) 196 for (i = 0; i < eventcnt; ++i)
163 event (events [i], type); 197 event (events [i], type);
164} 198}
165 199
166/*****************************************************************************/ 200/* called on EBADF to verify fds */
201static void
202fd_recheck (void)
203{
204 int fd;
167 205
206 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head)
210 {
211 event ((W)anfds [fd].head, EV_ERROR);
212 evio_stop (anfds [fd].head);
213 }
214}
215
216/*****************************************************************************/
217
168static struct ev_timer **atimers; 218static struct ev_timer **timers;
169static int atimermax, atimercnt; 219static int timermax, timercnt;
170 220
171static struct ev_timer **rtimers; 221static struct ev_periodic **periodics;
172static int rtimermax, rtimercnt; 222static int periodicmax, periodiccnt;
173 223
174static void 224static void
175upheap (struct ev_timer **timers, int k) 225upheap (WT *timers, int k)
176{ 226{
177 struct ev_timer *w = timers [k]; 227 WT w = timers [k];
178 228
179 while (k && timers [k >> 1]->at > w->at) 229 while (k && timers [k >> 1]->at > w->at)
180 { 230 {
181 timers [k] = timers [k >> 1]; 231 timers [k] = timers [k >> 1];
182 timers [k]->active = k + 1; 232 timers [k]->active = k + 1;
187 timers [k]->active = k + 1; 237 timers [k]->active = k + 1;
188 238
189} 239}
190 240
191static void 241static void
192downheap (struct ev_timer **timers, int N, int k) 242downheap (WT *timers, int N, int k)
193{ 243{
194 struct ev_timer *w = timers [k]; 244 WT w = timers [k];
195 245
196 while (k < (N >> 1)) 246 while (k < (N >> 1))
197 { 247 {
198 int j = k << 1; 248 int j = k << 1;
199 249
286/*****************************************************************************/ 336/*****************************************************************************/
287 337
288static struct ev_idle **idles; 338static struct ev_idle **idles;
289static int idlemax, idlecnt; 339static int idlemax, idlecnt;
290 340
341static struct ev_prepare **prepares;
342static int preparemax, preparecnt;
343
291static struct ev_check **checks; 344static struct ev_check **checks;
292static int checkmax, checkcnt; 345static int checkmax, checkcnt;
293 346
294/*****************************************************************************/ 347/*****************************************************************************/
295 348
349static struct ev_child *childs [PID_HASHSIZE];
350static struct ev_signal childev;
351
352#ifndef WCONTINUED
353# define WCONTINUED 0
354#endif
355
356static void
357childcb (struct ev_signal *sw, int revents)
358{
359 struct ev_child *w;
360 int pid, status;
361
362 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
363 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
364 if (w->pid == pid || w->pid == -1)
365 {
366 w->status = status;
367 event ((W)w, EV_CHILD);
368 }
369}
370
371/*****************************************************************************/
372
296#if HAVE_EPOLL 373#if HAVE_EPOLL
297# include "ev_epoll.c" 374# include "ev_epoll.c"
298#endif 375#endif
299#if HAVE_SELECT 376#if HAVE_SELECT
300# include "ev_select.c" 377# include "ev_select.c"
301#endif 378#endif
302 379
380int
381ev_version_major (void)
382{
383 return EV_VERSION_MAJOR;
384}
385
386int
387ev_version_minor (void)
388{
389 return EV_VERSION_MINOR;
390}
391
303int ev_init (int flags) 392int ev_init (int flags)
304{ 393{
394 if (!ev_method)
395 {
305#if HAVE_MONOTONIC 396#if HAVE_MONOTONIC
306 { 397 {
307 struct timespec ts; 398 struct timespec ts;
308 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 399 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
309 have_monotonic = 1; 400 have_monotonic = 1;
310 } 401 }
311#endif 402#endif
312 403
313 ev_now = ev_time (); 404 ev_now = ev_time ();
314 now = get_clock (); 405 now = get_clock ();
315 diff = ev_now - now; 406 diff = ev_now - now;
316 407
317 if (pipe (sigpipe)) 408 if (pipe (sigpipe))
318 return 0; 409 return 0;
319 410
320 ev_method = EVMETHOD_NONE; 411 ev_method = EVMETHOD_NONE;
321#if HAVE_EPOLL 412#if HAVE_EPOLL
322 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 413 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
323#endif 414#endif
324#if HAVE_SELECT 415#if HAVE_SELECT
325 if (ev_method == EVMETHOD_NONE) select_init (flags); 416 if (ev_method == EVMETHOD_NONE) select_init (flags);
326#endif 417#endif
327 418
328 if (ev_method) 419 if (ev_method)
329 { 420 {
330 evw_init (&sigev, sigcb, 0); 421 evw_init (&sigev, sigcb);
331 siginit (); 422 siginit ();
423
424 evsignal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev);
426 }
332 } 427 }
333 428
334 return ev_method; 429 return ev_method;
335} 430}
336 431
337/*****************************************************************************/ 432/*****************************************************************************/
338 433
434void
339void ev_prefork (void) 435ev_prefork (void)
340{ 436{
341 /* nop */ 437 /* nop */
342} 438}
343 439
440void
344void ev_postfork_parent (void) 441ev_postfork_parent (void)
345{ 442{
346 /* nop */ 443 /* nop */
347} 444}
348 445
446void
349void ev_postfork_child (void) 447ev_postfork_child (void)
350{ 448{
351#if HAVE_EPOLL 449#if HAVE_EPOLL
352 if (ev_method == EVMETHOD_EPOLL) 450 if (ev_method == EVMETHOD_EPOLL)
353 epoll_postfork_child (); 451 epoll_postfork_child ();
354#endif 452#endif
387 485
388 fdchangecnt = 0; 486 fdchangecnt = 0;
389} 487}
390 488
391static void 489static void
392call_pending () 490call_pending (void)
393{ 491{
394 int i; 492 while (pendingcnt)
395
396 for (i = 0; i < pendingcnt; ++i)
397 { 493 {
398 ANPENDING *p = pendings + i; 494 ANPENDING *p = pendings + --pendingcnt;
399 495
400 if (p->w) 496 if (p->w)
401 { 497 {
402 p->w->pending = 0; 498 p->w->pending = 0;
403 p->w->cb (p->w, p->events); 499 p->w->cb (p->w, p->events);
404 } 500 }
405 } 501 }
406
407 pendingcnt = 0;
408} 502}
409 503
410static void 504static void
411timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 505timers_reify (void)
412{ 506{
413 while (timercnt && timers [0]->at <= now) 507 while (timercnt && timers [0]->at <= now)
414 { 508 {
415 struct ev_timer *w = timers [0]; 509 struct ev_timer *w = timers [0];
510
511 event ((W)w, EV_TIMEOUT);
416 512
417 /* first reschedule or stop timer */ 513 /* first reschedule or stop timer */
418 if (w->repeat) 514 if (w->repeat)
419 { 515 {
420 if (w->is_abs)
421 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
422 else
423 w->at = now + w->repeat; 516 w->at = now + w->repeat;
424 517 assert (("timer timeout in the past, negative repeat?", w->at > now));
425 assert (w->at > now);
426
427 downheap (timers, timercnt, 0); 518 downheap ((WT *)timers, timercnt, 0);
428 } 519 }
429 else 520 else
430 {
431 evtimer_stop (w); /* nonrepeating: stop timer */ 521 evtimer_stop (w); /* nonrepeating: stop timer */
432 --timercnt; /* maybe pass by reference instead? */ 522 }
523}
524
525static void
526periodics_reify (void)
527{
528 while (periodiccnt && periodics [0]->at <= ev_now)
529 {
530 struct ev_periodic *w = periodics [0];
531
532 /* first reschedule or stop timer */
533 if (w->interval)
433 } 534 {
535 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
536 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
537 downheap ((WT *)periodics, periodiccnt, 0);
538 }
539 else
540 evperiodic_stop (w); /* nonrepeating: stop timer */
434 541
435 event ((W)w, EV_TIMEOUT); 542 event ((W)w, EV_TIMEOUT);
436 } 543 }
437} 544}
438 545
439static void 546static void
440time_update () 547periodics_reschedule (ev_tstamp diff)
441{ 548{
442 int i; 549 int i;
550
551 /* adjust periodics after time jump */
552 for (i = 0; i < periodiccnt; ++i)
553 {
554 struct ev_periodic *w = periodics [i];
555
556 if (w->interval)
557 {
558 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
559
560 if (fabs (diff) >= 1e-4)
561 {
562 evperiodic_stop (w);
563 evperiodic_start (w);
564
565 i = 0; /* restart loop, inefficient, but time jumps should be rare */
566 }
567 }
568 }
569}
570
571static void
572time_update (void)
573{
574 int i;
575
443 ev_now = ev_time (); 576 ev_now = ev_time ();
444 577
445 if (have_monotonic) 578 if (have_monotonic)
446 { 579 {
447 ev_tstamp odiff = diff; 580 ev_tstamp odiff = diff;
448 581
449 /* detecting time jumps is much more difficult */
450 for (i = 2; --i; ) /* loop a few times, before making important decisions */ 582 for (i = 4; --i; ) /* loop a few times, before making important decisions */
451 { 583 {
452 now = get_clock (); 584 now = get_clock ();
453 diff = ev_now - now; 585 diff = ev_now - now;
454 586
455 if (fabs (odiff - diff) < MIN_TIMEJUMP) 587 if (fabs (odiff - diff) < MIN_TIMEJUMP)
456 return; /* all is well */ 588 return; /* all is well */
457 589
458 ev_now = ev_time (); 590 ev_now = ev_time ();
459 } 591 }
460 592
461 /* time jump detected, reschedule atimers */ 593 periodics_reschedule (diff - odiff);
462 for (i = 0; i < atimercnt; ++i) 594 /* no timer adjustment, as the monotonic clock doesn't jump */
463 {
464 struct ev_timer *w = atimers [i];
465 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat;
466 }
467 } 595 }
468 else 596 else
469 { 597 {
470 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 598 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)
471 /* time jump detected, adjust rtimers */ 599 {
600 periodics_reschedule (ev_now - now);
601
602 /* adjust timers. this is easy, as the offset is the same for all */
472 for (i = 0; i < rtimercnt; ++i) 603 for (i = 0; i < timercnt; ++i)
473 rtimers [i]->at += ev_now - now; 604 timers [i]->at += diff;
605 }
474 606
475 now = ev_now; 607 now = ev_now;
476 } 608 }
477} 609}
478 610
479int ev_loop_done; 611int ev_loop_done;
480 612
481void ev_loop (int flags) 613void ev_loop (int flags)
482{ 614{
483 double block; 615 double block;
484 ev_loop_done = flags & EVLOOP_ONESHOT; 616 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0;
485
486 if (checkcnt)
487 {
488 queue_events ((W *)checks, checkcnt, EV_CHECK);
489 call_pending ();
490 }
491 617
492 do 618 do
493 { 619 {
620 /* queue check watchers (and execute them) */
621 if (preparecnt)
622 {
623 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
624 call_pending ();
625 }
626
494 /* update fd-related kernel structures */ 627 /* update fd-related kernel structures */
495 fd_reify (); 628 fd_reify ();
496 629
497 /* calculate blocking time */ 630 /* calculate blocking time */
631
632 /* we only need this for !monotonic clockor timers, but as we basically
633 always have timers, we just calculate it always */
634 ev_now = ev_time ();
635
498 if (flags & EVLOOP_NONBLOCK || idlecnt) 636 if (flags & EVLOOP_NONBLOCK || idlecnt)
499 block = 0.; 637 block = 0.;
500 else 638 else
501 { 639 {
502 block = MAX_BLOCKTIME; 640 block = MAX_BLOCKTIME;
503 641
504 if (rtimercnt) 642 if (timercnt)
505 { 643 {
506 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 644 ev_tstamp to = timers [0]->at - (have_monotonic ? get_clock () : ev_now) + method_fudge;
507 if (block > to) block = to; 645 if (block > to) block = to;
508 } 646 }
509 647
510 if (atimercnt) 648 if (periodiccnt)
511 { 649 {
512 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 650 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
513 if (block > to) block = to; 651 if (block > to) block = to;
514 } 652 }
515 653
516 if (block < 0.) block = 0.; 654 if (block < 0.) block = 0.;
517 } 655 }
520 658
521 /* update ev_now, do magic */ 659 /* update ev_now, do magic */
522 time_update (); 660 time_update ();
523 661
524 /* queue pending timers and reschedule them */ 662 /* queue pending timers and reschedule them */
525 /* absolute timers first */ 663 timers_reify (); /* relative timers called last */
526 timers_reify (atimers, atimercnt, ev_now); 664 periodics_reify (); /* absolute timers called first */
527 /* relative timers second */
528 timers_reify (rtimers, rtimercnt, now);
529 665
530 /* queue idle watchers unless io or timers are pending */ 666 /* queue idle watchers unless io or timers are pending */
531 if (!pendingcnt) 667 if (!pendingcnt)
532 queue_events ((W *)idles, idlecnt, EV_IDLE); 668 queue_events ((W *)idles, idlecnt, EV_IDLE);
533 669
534 /* queue check and possibly idle watchers */ 670 /* queue check watchers, to be executed first */
671 if (checkcnt)
535 queue_events ((W *)checks, checkcnt, EV_CHECK); 672 queue_events ((W *)checks, checkcnt, EV_CHECK);
536 673
537 call_pending (); 674 call_pending ();
538 } 675 }
539 while (!ev_loop_done); 676 while (!ev_loop_done);
677
678 if (ev_loop_done != 2)
679 ev_loop_done = 0;
540} 680}
541 681
542/*****************************************************************************/ 682/*****************************************************************************/
543 683
544static void 684static void
562 head = &(*head)->next; 702 head = &(*head)->next;
563 } 703 }
564} 704}
565 705
566static void 706static void
707ev_clear (W w)
708{
709 if (w->pending)
710 {
711 pendings [w->pending - 1].w = 0;
712 w->pending = 0;
713 }
714}
715
716static void
567ev_start (W w, int active) 717ev_start (W w, int active)
568{ 718{
569 w->pending = 0;
570 w->active = active; 719 w->active = active;
571} 720}
572 721
573static void 722static void
574ev_stop (W w) 723ev_stop (W w)
575{ 724{
576 if (w->pending)
577 pendings [w->pending - 1].w = 0;
578
579 w->active = 0; 725 w->active = 0;
580} 726}
581 727
582/*****************************************************************************/ 728/*****************************************************************************/
583 729
594 wlist_add ((WL *)&anfds[fd].head, (WL)w); 740 wlist_add ((WL *)&anfds[fd].head, (WL)w);
595 741
596 ++fdchangecnt; 742 ++fdchangecnt;
597 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
598 fdchanges [fdchangecnt - 1] = fd; 744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
599} 748}
600 749
601void 750void
602evio_stop (struct ev_io *w) 751evio_stop (struct ev_io *w)
603{ 752{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w);
604 if (!ev_is_active (w)) 756 if (!ev_is_active (w))
605 return; 757 return;
606 758
607 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
608 ev_stop ((W)w); 760 ev_stop ((W)w);
616evtimer_start (struct ev_timer *w) 768evtimer_start (struct ev_timer *w)
617{ 769{
618 if (ev_is_active (w)) 770 if (ev_is_active (w))
619 return; 771 return;
620 772
621 if (w->is_abs) 773 w->at += now;
774
775 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.));
776
777 ev_start ((W)w, ++timercnt);
778 array_needsize (timers, timermax, timercnt, );
779 timers [timercnt - 1] = w;
780 upheap ((WT *)timers, timercnt - 1);
781}
782
783void
784evtimer_stop (struct ev_timer *w)
785{
786 ev_clear ((W)w);
787 if (!ev_is_active (w))
788 return;
789
790 if (w->active < timercnt--)
791 {
792 timers [w->active - 1] = timers [timercnt];
793 downheap ((WT *)timers, timercnt, w->active - 1);
622 { 794 }
623 /* this formula differs from the one in timer_reify becuse we do not round up */ 795
796 w->at = w->repeat;
797
798 ev_stop ((W)w);
799}
800
801void
802evtimer_again (struct ev_timer *w)
803{
804 if (ev_is_active (w))
805 {
624 if (w->repeat) 806 if (w->repeat)
807 {
808 w->at = now + w->repeat;
809 downheap ((WT *)timers, timercnt, w->active - 1);
810 }
811 else
812 evtimer_stop (w);
813 }
814 else if (w->repeat)
815 evtimer_start (w);
816}
817
818void
819evperiodic_start (struct ev_periodic *w)
820{
821 if (ev_is_active (w))
822 return;
823
824 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
825
826 /* this formula differs from the one in periodic_reify because we do not always round up */
827 if (w->interval)
625 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 828 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
626 829
627 ev_start ((W)w, ++atimercnt); 830 ev_start ((W)w, ++periodiccnt);
628 array_needsize (atimers, atimermax, atimercnt, ); 831 array_needsize (periodics, periodicmax, periodiccnt, );
629 atimers [atimercnt - 1] = w; 832 periodics [periodiccnt - 1] = w;
630 upheap (atimers, atimercnt - 1); 833 upheap ((WT *)periodics, periodiccnt - 1);
631 }
632 else
633 {
634 w->at += now;
635
636 ev_start ((W)w, ++rtimercnt);
637 array_needsize (rtimers, rtimermax, rtimercnt, );
638 rtimers [rtimercnt - 1] = w;
639 upheap (rtimers, rtimercnt - 1);
640 }
641
642} 834}
643 835
644void 836void
645evtimer_stop (struct ev_timer *w) 837evperiodic_stop (struct ev_periodic *w)
646{ 838{
839 ev_clear ((W)w);
647 if (!ev_is_active (w)) 840 if (!ev_is_active (w))
648 return; 841 return;
649 842
650 if (w->is_abs)
651 {
652 if (w->active < atimercnt--) 843 if (w->active < periodiccnt--)
653 {
654 atimers [w->active - 1] = atimers [atimercnt];
655 downheap (atimers, atimercnt, w->active - 1);
656 }
657 } 844 {
658 else 845 periodics [w->active - 1] = periodics [periodiccnt];
659 { 846 downheap ((WT *)periodics, periodiccnt, w->active - 1);
660 if (w->active < rtimercnt--)
661 {
662 rtimers [w->active - 1] = rtimers [rtimercnt];
663 downheap (rtimers, rtimercnt, w->active - 1);
664 }
665 } 847 }
666 848
667 ev_stop ((W)w); 849 ev_stop ((W)w);
668} 850}
669 851
688} 870}
689 871
690void 872void
691evsignal_stop (struct ev_signal *w) 873evsignal_stop (struct ev_signal *w)
692{ 874{
875 ev_clear ((W)w);
693 if (!ev_is_active (w)) 876 if (!ev_is_active (w))
694 return; 877 return;
695 878
696 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 879 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
697 ev_stop ((W)w); 880 ev_stop ((W)w);
710 idles [idlecnt - 1] = w; 893 idles [idlecnt - 1] = w;
711} 894}
712 895
713void evidle_stop (struct ev_idle *w) 896void evidle_stop (struct ev_idle *w)
714{ 897{
898 ev_clear ((W)w);
899 if (ev_is_active (w))
900 return;
901
715 idles [w->active - 1] = idles [--idlecnt]; 902 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w);
904}
905
906void evprepare_start (struct ev_prepare *w)
907{
908 if (ev_is_active (w))
909 return;
910
911 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w;
914}
915
916void evprepare_stop (struct ev_prepare *w)
917{
918 ev_clear ((W)w);
919 if (ev_is_active (w))
920 return;
921
922 prepares [w->active - 1] = prepares [--preparecnt];
716 ev_stop ((W)w); 923 ev_stop ((W)w);
717} 924}
718 925
719void evcheck_start (struct ev_check *w) 926void evcheck_start (struct ev_check *w)
720{ 927{
726 checks [checkcnt - 1] = w; 933 checks [checkcnt - 1] = w;
727} 934}
728 935
729void evcheck_stop (struct ev_check *w) 936void evcheck_stop (struct ev_check *w)
730{ 937{
938 ev_clear ((W)w);
939 if (ev_is_active (w))
940 return;
941
731 checks [w->active - 1] = checks [--checkcnt]; 942 checks [w->active - 1] = checks [--checkcnt];
732 ev_stop ((W)w); 943 ev_stop ((W)w);
733} 944}
734 945
946void evchild_start (struct ev_child *w)
947{
948 if (ev_is_active (w))
949 return;
950
951 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953}
954
955void evchild_stop (struct ev_child *w)
956{
957 ev_clear ((W)w);
958 if (ev_is_active (w))
959 return;
960
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
962 ev_stop ((W)w);
963}
964
965/*****************************************************************************/
966
967struct ev_once
968{
969 struct ev_io io;
970 struct ev_timer to;
971 void (*cb)(int revents, void *arg);
972 void *arg;
973};
974
975static void
976once_cb (struct ev_once *once, int revents)
977{
978 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg;
980
981 evio_stop (&once->io);
982 evtimer_stop (&once->to);
983 free (once);
984
985 cb (revents, arg);
986}
987
988static void
989once_cb_io (struct ev_io *w, int revents)
990{
991 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
992}
993
994static void
995once_cb_to (struct ev_timer *w, int revents)
996{
997 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
998}
999
1000void
1001ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1002{
1003 struct ev_once *once = malloc (sizeof (struct ev_once));
1004
1005 if (!once)
1006 cb (EV_ERROR, arg);
1007 else
1008 {
1009 once->cb = cb;
1010 once->arg = arg;
1011
1012 evw_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0)
1015 {
1016 evio_set (&once->io, fd, events);
1017 evio_start (&once->io);
1018 }
1019
1020 evw_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.)
1023 {
1024 evtimer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to);
1026 }
1027 }
1028}
1029
735/*****************************************************************************/ 1030/*****************************************************************************/
736 1031
737#if 0 1032#if 0
1033
1034struct ev_io wio;
738 1035
739static void 1036static void
740sin_cb (struct ev_io *w, int revents) 1037sin_cb (struct ev_io *w, int revents)
741{ 1038{
742 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1039 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
752 1049
753static void 1050static void
754scb (struct ev_signal *w, int revents) 1051scb (struct ev_signal *w, int revents)
755{ 1052{
756 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1053 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1054 evio_stop (&wio);
1055 evio_start (&wio);
757} 1056}
758 1057
759static void 1058static void
760gcb (struct ev_signal *w, int revents) 1059gcb (struct ev_signal *w, int revents)
761{ 1060{
762 fprintf (stderr, "generic %x\n", revents); 1061 fprintf (stderr, "generic %x\n", revents);
1062
763} 1063}
764 1064
765int main (void) 1065int main (void)
766{ 1066{
767 struct ev_io sin;
768
769 ev_init (0); 1067 ev_init (0);
770 1068
771 evw_init (&sin, sin_cb, 55);
772 evio_set (&sin, 0, EV_READ); 1069 evio_init (&wio, sin_cb, 0, EV_READ);
773 evio_start (&sin); 1070 evio_start (&wio);
774 1071
775 struct ev_timer t[10000]; 1072 struct ev_timer t[10000];
776 1073
777#if 0 1074#if 0
778 int i; 1075 int i;
779 for (i = 0; i < 10000; ++i) 1076 for (i = 0; i < 10000; ++i)
780 { 1077 {
781 struct ev_timer *w = t + i; 1078 struct ev_timer *w = t + i;
782 evw_init (w, ocb, i); 1079 evw_init (w, ocb, i);
783 evtimer_set_abs (w, drand48 (), 0.99775533); 1080 evtimer_init_abs (w, ocb, drand48 (), 0.99775533);
784 evtimer_start (w); 1081 evtimer_start (w);
785 if (drand48 () < 0.5) 1082 if (drand48 () < 0.5)
786 evtimer_stop (w); 1083 evtimer_stop (w);
787 } 1084 }
788#endif 1085#endif
789 1086
790 struct ev_timer t1; 1087 struct ev_timer t1;
791 evw_init (&t1, ocb, 0); 1088 evtimer_init (&t1, ocb, 5, 10);
792 evtimer_set_abs (&t1, 5, 10);
793 evtimer_start (&t1); 1089 evtimer_start (&t1);
794 1090
795 struct ev_signal sig; 1091 struct ev_signal sig;
796 evw_init (&sig, scb, 65535);
797 evsignal_set (&sig, SIGQUIT); 1092 evsignal_init (&sig, scb, SIGQUIT);
798 evsignal_start (&sig); 1093 evsignal_start (&sig);
799 1094
800 struct ev_check cw; 1095 struct ev_check cw;
801 evw_init (&cw, gcb, 0); 1096 evcheck_init (&cw, gcb);
802 evcheck_start (&cw); 1097 evcheck_start (&cw);
803 1098
804 struct ev_idle iw; 1099 struct ev_idle iw;
805 evw_init (&iw, gcb, 0); 1100 evidle_init (&iw, gcb);
806 evidle_start (&iw); 1101 evidle_start (&iw);
807 1102
808 ev_loop (0); 1103 ev_loop (0);
809 1104
810 return 0; 1105 return 0;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines