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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines