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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines