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

Comparing libev/ev.c (file contents):
Revision 1.15 by root, Wed Oct 31 11:56:34 2007 UTC vs.
Revision 1.28 by root, Thu Nov 1 06:48:49 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
14#define HAVE_EPOLL 1
15 45
16#ifndef HAVE_MONOTONIC 46#ifndef HAVE_MONOTONIC
17# ifdef CLOCK_MONOTONIC 47# ifdef CLOCK_MONOTONIC
18# define HAVE_MONOTONIC 1 48# define HAVE_MONOTONIC 1
19# endif 49# endif
31# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */
32#endif 62#endif
33 63
34#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) */
35#define MAX_BLOCKTIME 60. 65#define MAX_BLOCKTIME 60.
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */
36 67
37#include "ev.h" 68#include "ev.h"
38 69
39typedef struct ev_watcher *W; 70typedef struct ev_watcher *W;
40typedef struct ev_watcher_list *WL; 71typedef struct ev_watcher_list *WL;
82} 113}
83 114
84#define array_needsize(base,cur,cnt,init) \ 115#define array_needsize(base,cur,cnt,init) \
85 if ((cnt) > cur) \ 116 if ((cnt) > cur) \
86 { \ 117 { \
87 int newcnt = cur ? cur << 1 : 16; \ 118 int newcnt = cur; \
119 do \
120 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \
122 } \
123 while ((cnt) > newcnt); \
124 \
88 base = realloc (base, sizeof (*base) * (newcnt)); \ 125 base = realloc (base, sizeof (*base) * (newcnt)); \
89 init (base + cur, newcnt - cur); \ 126 init (base + cur, newcnt - cur); \
90 cur = newcnt; \ 127 cur = newcnt; \
91 } 128 }
92 129
93/*****************************************************************************/ 130/*****************************************************************************/
94 131
95typedef struct 132typedef struct
96{ 133{
97 struct ev_io *head; 134 struct ev_io *head;
98 unsigned char wev, rev; /* want, received event set */ 135 int events;
99} ANFD; 136} ANFD;
100 137
101static ANFD *anfds; 138static ANFD *anfds;
102static int anfdmax; 139static int anfdmax;
103 140
104static int *fdchanges;
105static int fdchangemax, fdchangecnt;
106
107static void 141static void
108anfds_init (ANFD *base, int count) 142anfds_init (ANFD *base, int count)
109{ 143{
110 while (count--) 144 while (count--)
111 { 145 {
112 base->head = 0; 146 base->head = 0;
113 base->wev = base->rev = EV_NONE; 147 base->events = EV_NONE;
114 ++base; 148 ++base;
115 } 149 }
116} 150}
117 151
118typedef struct 152typedef struct
125static int pendingmax, pendingcnt; 159static int pendingmax, pendingcnt;
126 160
127static void 161static void
128event (W w, int events) 162event (W w, int events)
129{ 163{
164 if (w->active)
165 {
130 w->pending = ++pendingcnt; 166 w->pending = ++pendingcnt;
131 array_needsize (pendings, pendingmax, pendingcnt, ); 167 array_needsize (pendings, pendingmax, pendingcnt, );
132 pendings [pendingcnt - 1].w = w; 168 pendings [pendingcnt - 1].w = w;
133 pendings [pendingcnt - 1].events = events; 169 pendings [pendingcnt - 1].events = events;
170 }
171}
172
173static void
174queue_events (W *events, int eventcnt, int type)
175{
176 int i;
177
178 for (i = 0; i < eventcnt; ++i)
179 event (events [i], type);
134} 180}
135 181
136static void 182static void
137fd_event (int fd, int events) 183fd_event (int fd, int events)
138{ 184{
146 if (ev) 192 if (ev)
147 event ((W)w, ev); 193 event ((W)w, ev);
148 } 194 }
149} 195}
150 196
197/*****************************************************************************/
198
199static int *fdchanges;
200static int fdchangemax, fdchangecnt;
201
151static void 202static void
152queue_events (W *events, int eventcnt, int type) 203fd_reify (void)
153{ 204{
154 int i; 205 int i;
155 206
156 for (i = 0; i < eventcnt; ++i) 207 for (i = 0; i < fdchangecnt; ++i)
157 event (events [i], type); 208 {
209 int fd = fdchanges [i];
210 ANFD *anfd = anfds + fd;
211 struct ev_io *w;
212
213 int events = 0;
214
215 for (w = anfd->head; w; w = w->next)
216 events |= w->events;
217
218 anfd->events &= ~EV_REIFY;
219
220 if (anfd->events != events)
221 {
222 method_modify (fd, anfd->events, events);
223 anfd->events = events;
224 }
225 }
226
227 fdchangecnt = 0;
228}
229
230static void
231fd_change (int fd)
232{
233 if (anfds [fd].events & EV_REIFY)
234 return;
235
236 anfds [fd].events |= EV_REIFY;
237
238 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd;
241}
242
243/* called on EBADF to verify fds */
244static void
245fd_recheck (void)
246{
247 int fd;
248
249 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head)
253 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 ev_io_stop (anfds [fd].head);
256 }
158} 257}
159 258
160/*****************************************************************************/ 259/*****************************************************************************/
161 260
162static struct ev_timer **timers; 261static struct ev_timer **timers;
271 370
272 /* rather than sort out wether we really need nb, set it */ 371 /* rather than sort out wether we really need nb, set it */
273 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 372 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
274 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 373 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
275 374
276 evio_set (&sigev, sigpipe [0], EV_READ); 375 ev_io_set (&sigev, sigpipe [0], EV_READ);
277 evio_start (&sigev); 376 ev_io_start (&sigev);
278} 377}
279 378
280/*****************************************************************************/ 379/*****************************************************************************/
281 380
282static struct ev_idle **idles; 381static struct ev_idle **idles;
283static int idlemax, idlecnt; 382static int idlemax, idlecnt;
284 383
384static struct ev_prepare **prepares;
385static int preparemax, preparecnt;
386
285static struct ev_check **checks; 387static struct ev_check **checks;
286static int checkmax, checkcnt; 388static int checkmax, checkcnt;
287 389
288/*****************************************************************************/ 390/*****************************************************************************/
289 391
392static struct ev_child *childs [PID_HASHSIZE];
393static struct ev_signal childev;
394
395#ifndef WCONTINUED
396# define WCONTINUED 0
397#endif
398
399static void
400childcb (struct ev_signal *sw, int revents)
401{
402 struct ev_child *w;
403 int pid, status;
404
405 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1)
406 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next)
407 if (w->pid == pid || w->pid == -1)
408 {
409 w->status = status;
410 event ((W)w, EV_CHILD);
411 }
412}
413
414/*****************************************************************************/
415
290#if HAVE_EPOLL 416#if HAVE_EPOLL
291# include "ev_epoll.c" 417# include "ev_epoll.c"
292#endif 418#endif
293#if HAVE_SELECT 419#if HAVE_SELECT
294# include "ev_select.c" 420# include "ev_select.c"
295#endif 421#endif
296 422
423int
424ev_version_major (void)
425{
426 return EV_VERSION_MAJOR;
427}
428
429int
430ev_version_minor (void)
431{
432 return EV_VERSION_MINOR;
433}
434
297int ev_init (int flags) 435int ev_init (int flags)
298{ 436{
437 if (!ev_method)
438 {
299#if HAVE_MONOTONIC 439#if HAVE_MONOTONIC
300 { 440 {
301 struct timespec ts; 441 struct timespec ts;
302 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 442 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
303 have_monotonic = 1; 443 have_monotonic = 1;
304 } 444 }
305#endif 445#endif
306 446
307 ev_now = ev_time (); 447 ev_now = ev_time ();
308 now = get_clock (); 448 now = get_clock ();
309 diff = ev_now - now; 449 diff = ev_now - now;
310 450
311 if (pipe (sigpipe)) 451 if (pipe (sigpipe))
312 return 0; 452 return 0;
313 453
314 ev_method = EVMETHOD_NONE; 454 ev_method = EVMETHOD_NONE;
315#if HAVE_EPOLL 455#if HAVE_EPOLL
316 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 456 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
317#endif 457#endif
318#if HAVE_SELECT 458#if HAVE_SELECT
319 if (ev_method == EVMETHOD_NONE) select_init (flags); 459 if (ev_method == EVMETHOD_NONE) select_init (flags);
320#endif 460#endif
321 461
322 if (ev_method) 462 if (ev_method)
323 { 463 {
324 evw_init (&sigev, sigcb); 464 ev_watcher_init (&sigev, sigcb);
325 siginit (); 465 siginit ();
466
467 ev_signal_init (&childev, childcb, SIGCHLD);
468 ev_signal_start (&childev);
469 }
326 } 470 }
327 471
328 return ev_method; 472 return ev_method;
329} 473}
330 474
331/*****************************************************************************/ 475/*****************************************************************************/
332 476
477void
333void ev_prefork (void) 478ev_prefork (void)
334{ 479{
335 /* nop */ 480 /* nop */
336} 481}
337 482
483void
338void ev_postfork_parent (void) 484ev_postfork_parent (void)
339{ 485{
340 /* nop */ 486 /* nop */
341} 487}
342 488
489void
343void ev_postfork_child (void) 490ev_postfork_child (void)
344{ 491{
345#if HAVE_EPOLL 492#if HAVE_EPOLL
346 if (ev_method == EVMETHOD_EPOLL) 493 if (ev_method == EVMETHOD_EPOLL)
347 epoll_postfork_child (); 494 epoll_postfork_child ();
348#endif 495#endif
349 496
350 evio_stop (&sigev); 497 ev_io_stop (&sigev);
351 close (sigpipe [0]); 498 close (sigpipe [0]);
352 close (sigpipe [1]); 499 close (sigpipe [1]);
353 pipe (sigpipe); 500 pipe (sigpipe);
354 siginit (); 501 siginit ();
355} 502}
356 503
357/*****************************************************************************/ 504/*****************************************************************************/
358 505
359static void 506static void
360fd_reify (void)
361{
362 int i;
363
364 for (i = 0; i < fdchangecnt; ++i)
365 {
366 int fd = fdchanges [i];
367 ANFD *anfd = anfds + fd;
368 struct ev_io *w;
369
370 int wev = 0;
371
372 for (w = anfd->head; w; w = w->next)
373 wev |= w->events;
374
375 if (anfd->wev != wev)
376 {
377 method_modify (fd, anfd->wev, wev);
378 anfd->wev = wev;
379 }
380 }
381
382 fdchangecnt = 0;
383}
384
385static void
386call_pending () 507call_pending (void)
387{ 508{
388 int i; 509 while (pendingcnt)
389
390 for (i = 0; i < pendingcnt; ++i)
391 { 510 {
392 ANPENDING *p = pendings + i; 511 ANPENDING *p = pendings + --pendingcnt;
393 512
394 if (p->w) 513 if (p->w)
395 { 514 {
396 p->w->pending = 0; 515 p->w->pending = 0;
397 p->w->cb (p->w, p->events); 516 p->w->cb (p->w, p->events);
398 } 517 }
399 } 518 }
400
401 pendingcnt = 0;
402} 519}
403 520
404static void 521static void
405timers_reify () 522timers_reify (void)
406{ 523{
407 while (timercnt && timers [0]->at <= now) 524 while (timercnt && timers [0]->at <= now)
408 { 525 {
409 struct ev_timer *w = timers [0]; 526 struct ev_timer *w = timers [0];
527
528 event ((W)w, EV_TIMEOUT);
410 529
411 /* first reschedule or stop timer */ 530 /* first reschedule or stop timer */
412 if (w->repeat) 531 if (w->repeat)
413 { 532 {
414 w->at = now + w->repeat; 533 w->at = now + w->repeat;
415 assert (("timer timeout in the past, negative repeat?", w->at > now)); 534 assert (("timer timeout in the past, negative repeat?", w->at > now));
416 downheap ((WT *)timers, timercnt, 0); 535 downheap ((WT *)timers, timercnt, 0);
417 } 536 }
418 else 537 else
419 evtimer_stop (w); /* nonrepeating: stop timer */ 538 ev_timer_stop (w); /* nonrepeating: stop timer */
420
421 event ((W)w, EV_TIMEOUT);
422 } 539 }
423} 540}
424 541
425static void 542static void
426periodics_reify () 543periodics_reify (void)
427{ 544{
428 while (periodiccnt && periodics [0]->at <= ev_now) 545 while (periodiccnt && periodics [0]->at <= ev_now)
429 { 546 {
430 struct ev_periodic *w = periodics [0]; 547 struct ev_periodic *w = periodics [0];
431 548
435 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 552 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)); 553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now));
437 downheap ((WT *)periodics, periodiccnt, 0); 554 downheap ((WT *)periodics, periodiccnt, 0);
438 } 555 }
439 else 556 else
440 evperiodic_stop (w); /* nonrepeating: stop timer */ 557 ev_periodic_stop (w); /* nonrepeating: stop timer */
441 558
442 event ((W)w, EV_TIMEOUT); 559 event ((W)w, EV_TIMEOUT);
443 } 560 }
444} 561}
445 562
457 { 574 {
458 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 575 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
459 576
460 if (fabs (diff) >= 1e-4) 577 if (fabs (diff) >= 1e-4)
461 { 578 {
462 evperiodic_stop (w); 579 ev_periodic_stop (w);
463 evperiodic_start (w); 580 ev_periodic_start (w);
464 581
465 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 582 i = 0; /* restart loop, inefficient, but time jumps should be rare */
466 } 583 }
467 } 584 }
468 } 585 }
469} 586}
470 587
471static void 588static void
472time_update () 589time_update (void)
473{ 590{
474 int i; 591 int i;
475 592
476 ev_now = ev_time (); 593 ev_now = ev_time ();
477 594
511int ev_loop_done; 628int ev_loop_done;
512 629
513void ev_loop (int flags) 630void ev_loop (int flags)
514{ 631{
515 double block; 632 double block;
516 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 633 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
517
518 if (checkcnt)
519 {
520 queue_events ((W *)checks, checkcnt, EV_CHECK);
521 call_pending ();
522 }
523 634
524 do 635 do
525 { 636 {
637 /* queue check watchers (and execute them) */
638 if (preparecnt)
639 {
640 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
641 call_pending ();
642 }
643
526 /* update fd-related kernel structures */ 644 /* update fd-related kernel structures */
527 fd_reify (); 645 fd_reify ();
528 646
529 /* calculate blocking time */ 647 /* calculate blocking time */
530 648
531 /* we only need this for !monotonic clock, but as we always have timers, we just calculate it every time */ 649 /* we only need this for !monotonic clockor timers, but as we basically
650 always have timers, we just calculate it always */
532 ev_now = ev_time (); 651 ev_now = ev_time ();
533 652
534 if (flags & EVLOOP_NONBLOCK || idlecnt) 653 if (flags & EVLOOP_NONBLOCK || idlecnt)
535 block = 0.; 654 block = 0.;
536 else 655 else
556 675
557 /* update ev_now, do magic */ 676 /* update ev_now, do magic */
558 time_update (); 677 time_update ();
559 678
560 /* queue pending timers and reschedule them */ 679 /* queue pending timers and reschedule them */
680 timers_reify (); /* relative timers called last */
561 periodics_reify (); /* absolute timers first */ 681 periodics_reify (); /* absolute timers called first */
562 timers_reify (); /* relative timers second */
563 682
564 /* queue idle watchers unless io or timers are pending */ 683 /* queue idle watchers unless io or timers are pending */
565 if (!pendingcnt) 684 if (!pendingcnt)
566 queue_events ((W *)idles, idlecnt, EV_IDLE); 685 queue_events ((W *)idles, idlecnt, EV_IDLE);
567 686
568 /* queue check and possibly idle watchers */ 687 /* queue check watchers, to be executed first */
688 if (checkcnt)
569 queue_events ((W *)checks, checkcnt, EV_CHECK); 689 queue_events ((W *)checks, checkcnt, EV_CHECK);
570 690
571 call_pending (); 691 call_pending ();
572 } 692 }
573 while (!ev_loop_done); 693 while (!ev_loop_done);
574 694
599 head = &(*head)->next; 719 head = &(*head)->next;
600 } 720 }
601} 721}
602 722
603static void 723static void
724ev_clear (W w)
725{
726 if (w->pending)
727 {
728 pendings [w->pending - 1].w = 0;
729 w->pending = 0;
730 }
731}
732
733static void
604ev_start (W w, int active) 734ev_start (W w, int active)
605{ 735{
606 w->pending = 0;
607 w->active = active; 736 w->active = active;
608} 737}
609 738
610static void 739static void
611ev_stop (W w) 740ev_stop (W w)
612{ 741{
613 if (w->pending)
614 pendings [w->pending - 1].w = 0;
615
616 w->active = 0; 742 w->active = 0;
617} 743}
618 744
619/*****************************************************************************/ 745/*****************************************************************************/
620 746
621void 747void
622evio_start (struct ev_io *w) 748ev_io_start (struct ev_io *w)
623{ 749{
624 if (ev_is_active (w)) 750 if (ev_is_active (w))
625 return; 751 return;
626 752
627 int fd = w->fd; 753 int fd = w->fd;
628 754
629 ev_start ((W)w, 1); 755 ev_start ((W)w, 1);
630 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 756 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
631 wlist_add ((WL *)&anfds[fd].head, (WL)w); 757 wlist_add ((WL *)&anfds[fd].head, (WL)w);
632 758
633 ++fdchangecnt; 759 fd_change (fd);
634 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
635 fdchanges [fdchangecnt - 1] = fd;
636} 760}
637 761
638void 762void
639evio_stop (struct ev_io *w) 763ev_io_stop (struct ev_io *w)
640{ 764{
765 ev_clear ((W)w);
641 if (!ev_is_active (w)) 766 if (!ev_is_active (w))
642 return; 767 return;
643 768
644 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
645 ev_stop ((W)w); 770 ev_stop ((W)w);
646 771
647 ++fdchangecnt; 772 fd_change (w->fd);
648 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
649 fdchanges [fdchangecnt - 1] = w->fd;
650} 773}
651 774
652
653void 775void
654evtimer_start (struct ev_timer *w) 776ev_timer_start (struct ev_timer *w)
655{ 777{
656 if (ev_is_active (w)) 778 if (ev_is_active (w))
657 return; 779 return;
658 780
659 w->at += now; 781 w->at += now;
665 timers [timercnt - 1] = w; 787 timers [timercnt - 1] = w;
666 upheap ((WT *)timers, timercnt - 1); 788 upheap ((WT *)timers, timercnt - 1);
667} 789}
668 790
669void 791void
670evtimer_stop (struct ev_timer *w) 792ev_timer_stop (struct ev_timer *w)
671{ 793{
794 ev_clear ((W)w);
672 if (!ev_is_active (w)) 795 if (!ev_is_active (w))
673 return; 796 return;
674 797
675 if (w->active < timercnt--) 798 if (w->active < timercnt--)
676 { 799 {
682 805
683 ev_stop ((W)w); 806 ev_stop ((W)w);
684} 807}
685 808
686void 809void
687evtimer_again (struct ev_timer *w) 810ev_timer_again (struct ev_timer *w)
688{ 811{
689 if (ev_is_active (w)) 812 if (ev_is_active (w))
690 { 813 {
691 if (w->repeat) 814 if (w->repeat)
692 { 815 {
693 w->at = now + w->repeat; 816 w->at = now + w->repeat;
694 downheap ((WT *)timers, timercnt, w->active - 1); 817 downheap ((WT *)timers, timercnt, w->active - 1);
695 } 818 }
696 else 819 else
697 evtimer_stop (w); 820 ev_timer_stop (w);
698 } 821 }
699 else if (w->repeat) 822 else if (w->repeat)
700 evtimer_start (w); 823 ev_timer_start (w);
701} 824}
702 825
703void 826void
704evperiodic_start (struct ev_periodic *w) 827ev_periodic_start (struct ev_periodic *w)
705{ 828{
706 if (ev_is_active (w)) 829 if (ev_is_active (w))
707 return; 830 return;
708 831
709 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.));
717 periodics [periodiccnt - 1] = w; 840 periodics [periodiccnt - 1] = w;
718 upheap ((WT *)periodics, periodiccnt - 1); 841 upheap ((WT *)periodics, periodiccnt - 1);
719} 842}
720 843
721void 844void
722evperiodic_stop (struct ev_periodic *w) 845ev_periodic_stop (struct ev_periodic *w)
723{ 846{
847 ev_clear ((W)w);
724 if (!ev_is_active (w)) 848 if (!ev_is_active (w))
725 return; 849 return;
726 850
727 if (w->active < periodiccnt--) 851 if (w->active < periodiccnt--)
728 { 852 {
732 856
733 ev_stop ((W)w); 857 ev_stop ((W)w);
734} 858}
735 859
736void 860void
737evsignal_start (struct ev_signal *w) 861ev_signal_start (struct ev_signal *w)
738{ 862{
739 if (ev_is_active (w)) 863 if (ev_is_active (w))
740 return; 864 return;
741 865
742 ev_start ((W)w, 1); 866 ev_start ((W)w, 1);
752 sigaction (w->signum, &sa, 0); 876 sigaction (w->signum, &sa, 0);
753 } 877 }
754} 878}
755 879
756void 880void
757evsignal_stop (struct ev_signal *w) 881ev_signal_stop (struct ev_signal *w)
758{ 882{
883 ev_clear ((W)w);
759 if (!ev_is_active (w)) 884 if (!ev_is_active (w))
760 return; 885 return;
761 886
762 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
763 ev_stop ((W)w); 888 ev_stop ((W)w);
764 889
765 if (!signals [w->signum - 1].head) 890 if (!signals [w->signum - 1].head)
766 signal (w->signum, SIG_DFL); 891 signal (w->signum, SIG_DFL);
767} 892}
768 893
894void
769void evidle_start (struct ev_idle *w) 895ev_idle_start (struct ev_idle *w)
770{ 896{
771 if (ev_is_active (w)) 897 if (ev_is_active (w))
772 return; 898 return;
773 899
774 ev_start ((W)w, ++idlecnt); 900 ev_start ((W)w, ++idlecnt);
775 array_needsize (idles, idlemax, idlecnt, ); 901 array_needsize (idles, idlemax, idlecnt, );
776 idles [idlecnt - 1] = w; 902 idles [idlecnt - 1] = w;
777} 903}
778 904
905void
779void evidle_stop (struct ev_idle *w) 906ev_idle_stop (struct ev_idle *w)
780{ 907{
908 ev_clear ((W)w);
909 if (ev_is_active (w))
910 return;
911
781 idles [w->active - 1] = idles [--idlecnt]; 912 idles [w->active - 1] = idles [--idlecnt];
782 ev_stop ((W)w); 913 ev_stop ((W)w);
783} 914}
784 915
916void
917ev_prepare_start (struct ev_prepare *w)
918{
919 if (ev_is_active (w))
920 return;
921
922 ev_start ((W)w, ++preparecnt);
923 array_needsize (prepares, preparemax, preparecnt, );
924 prepares [preparecnt - 1] = w;
925}
926
927void
928ev_prepare_stop (struct ev_prepare *w)
929{
930 ev_clear ((W)w);
931 if (ev_is_active (w))
932 return;
933
934 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w);
936}
937
938void
785void evcheck_start (struct ev_check *w) 939ev_check_start (struct ev_check *w)
786{ 940{
787 if (ev_is_active (w)) 941 if (ev_is_active (w))
788 return; 942 return;
789 943
790 ev_start ((W)w, ++checkcnt); 944 ev_start ((W)w, ++checkcnt);
791 array_needsize (checks, checkmax, checkcnt, ); 945 array_needsize (checks, checkmax, checkcnt, );
792 checks [checkcnt - 1] = w; 946 checks [checkcnt - 1] = w;
793} 947}
794 948
949void
795void evcheck_stop (struct ev_check *w) 950ev_check_stop (struct ev_check *w)
796{ 951{
952 ev_clear ((W)w);
953 if (ev_is_active (w))
954 return;
955
797 checks [w->active - 1] = checks [--checkcnt]; 956 checks [w->active - 1] = checks [--checkcnt];
798 ev_stop ((W)w); 957 ev_stop ((W)w);
799} 958}
800 959
960void
961ev_child_start (struct ev_child *w)
962{
963 if (ev_is_active (w))
964 return;
965
966 ev_start ((W)w, 1);
967 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
968}
969
970void
971ev_child_stop (struct ev_child *w)
972{
973 ev_clear ((W)w);
974 if (ev_is_active (w))
975 return;
976
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w);
979}
980
981/*****************************************************************************/
982
983struct ev_once
984{
985 struct ev_io io;
986 struct ev_timer to;
987 void (*cb)(int revents, void *arg);
988 void *arg;
989};
990
991static void
992once_cb (struct ev_once *once, int revents)
993{
994 void (*cb)(int revents, void *arg) = once->cb;
995 void *arg = once->arg;
996
997 ev_io_stop (&once->io);
998 ev_timer_stop (&once->to);
999 free (once);
1000
1001 cb (revents, arg);
1002}
1003
1004static void
1005once_cb_io (struct ev_io *w, int revents)
1006{
1007 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1008}
1009
1010static void
1011once_cb_to (struct ev_timer *w, int revents)
1012{
1013 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1014}
1015
1016void
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{
1019 struct ev_once *once = malloc (sizeof (struct ev_once));
1020
1021 if (!once)
1022 cb (EV_ERROR, arg);
1023 else
1024 {
1025 once->cb = cb;
1026 once->arg = arg;
1027
1028 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0)
1031 {
1032 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io);
1034 }
1035
1036 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.)
1039 {
1040 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to);
1042 }
1043 }
1044}
1045
801/*****************************************************************************/ 1046/*****************************************************************************/
802 1047
803#if 0 1048#if 0
804 1049
805struct ev_io wio; 1050struct ev_io wio;
812 1057
813static void 1058static void
814ocb (struct ev_timer *w, int revents) 1059ocb (struct ev_timer *w, int revents)
815{ 1060{
816 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1061 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
817 evtimer_stop (w); 1062 ev_timer_stop (w);
818 evtimer_start (w); 1063 ev_timer_start (w);
819} 1064}
820 1065
821static void 1066static void
822scb (struct ev_signal *w, int revents) 1067scb (struct ev_signal *w, int revents)
823{ 1068{
824 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1069 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
825 evio_stop (&wio); 1070 ev_io_stop (&wio);
826 evio_start (&wio); 1071 ev_io_start (&wio);
827} 1072}
828 1073
829static void 1074static void
830gcb (struct ev_signal *w, int revents) 1075gcb (struct ev_signal *w, int revents)
831{ 1076{
835 1080
836int main (void) 1081int main (void)
837{ 1082{
838 ev_init (0); 1083 ev_init (0);
839 1084
840 evio_init (&wio, sin_cb, 0, EV_READ); 1085 ev_io_init (&wio, sin_cb, 0, EV_READ);
841 evio_start (&wio); 1086 ev_io_start (&wio);
842 1087
843 struct ev_timer t[10000]; 1088 struct ev_timer t[10000];
844 1089
845#if 0 1090#if 0
846 int i; 1091 int i;
847 for (i = 0; i < 10000; ++i) 1092 for (i = 0; i < 10000; ++i)
848 { 1093 {
849 struct ev_timer *w = t + i; 1094 struct ev_timer *w = t + i;
850 evw_init (w, ocb, i); 1095 ev_watcher_init (w, ocb, i);
851 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1096 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
852 evtimer_start (w); 1097 ev_timer_start (w);
853 if (drand48 () < 0.5) 1098 if (drand48 () < 0.5)
854 evtimer_stop (w); 1099 ev_timer_stop (w);
855 } 1100 }
856#endif 1101#endif
857 1102
858 struct ev_timer t1; 1103 struct ev_timer t1;
859 evtimer_init (&t1, ocb, 5, 10); 1104 ev_timer_init (&t1, ocb, 5, 10);
860 evtimer_start (&t1); 1105 ev_timer_start (&t1);
861 1106
862 struct ev_signal sig; 1107 struct ev_signal sig;
863 evsignal_init (&sig, scb, SIGQUIT); 1108 ev_signal_init (&sig, scb, SIGQUIT);
864 evsignal_start (&sig); 1109 ev_signal_start (&sig);
865 1110
866 struct ev_check cw; 1111 struct ev_check cw;
867 evcheck_init (&cw, gcb); 1112 ev_check_init (&cw, gcb);
868 evcheck_start (&cw); 1113 ev_check_start (&cw);
869 1114
870 struct ev_idle iw; 1115 struct ev_idle iw;
871 evidle_init (&iw, gcb); 1116 ev_idle_init (&iw, gcb);
872 evidle_start (&iw); 1117 ev_idle_start (&iw);
873 1118
874 ev_loop (0); 1119 ev_loop (0);
875 1120
876 return 0; 1121 return 0;
877} 1122}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines