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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines