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

Comparing libev/ev.c (file contents):
Revision 1.23 by root, Wed Oct 31 20:10:17 2007 UTC vs.
Revision 1.35 by root, Thu Nov 1 11:55:54 2007 UTC

24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 28 */
29#if EV_USE_CONFIG_H
30# include "config.h"
31#endif
29 32
30#include <math.h> 33#include <math.h>
31#include <stdlib.h> 34#include <stdlib.h>
32#include <unistd.h> 35#include <unistd.h>
33#include <fcntl.h> 36#include <fcntl.h>
41#include <sys/types.h> 44#include <sys/types.h>
42#include <sys/wait.h> 45#include <sys/wait.h>
43#include <sys/time.h> 46#include <sys/time.h>
44#include <time.h> 47#include <time.h>
45 48
46#ifndef HAVE_MONOTONIC 49#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 50# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 51# define EV_USE_MONOTONIC 1
49# endif 52# endif
50#endif 53#endif
51 54
52#ifndef HAVE_SELECT 55#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 56# define EV_USE_SELECT 1
54#endif 57#endif
55 58
56#ifndef HAVE_EPOLL 59#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 60# define EV_USE_EPOLL 0
58#endif 61#endif
59 62
63#ifndef CLOCK_REALTIME
64# define EV_USE_REALTIME 0
65#endif
60#ifndef HAVE_REALTIME 66#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 67# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 68#endif
63 69
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 70#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 71#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detetc time jumps) */
66#define PID_HASHSIZE 16 /* size of pid hahs table, must be power of two */ 72#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
73#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
67 74
68#include "ev.h" 75#include "ev.h"
69 76
70typedef struct ev_watcher *W; 77typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 78typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 91/*****************************************************************************/
85 92
86ev_tstamp 93ev_tstamp
87ev_time (void) 94ev_time (void)
88{ 95{
89#if HAVE_REALTIME 96#if EV_USE_REALTIME
90 struct timespec ts; 97 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 98 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 99 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 100#else
94 struct timeval tv; 101 struct timeval tv;
98} 105}
99 106
100static ev_tstamp 107static ev_tstamp
101get_clock (void) 108get_clock (void)
102{ 109{
103#if HAVE_MONOTONIC 110#if EV_USE_MONOTONIC
104 if (have_monotonic) 111 if (have_monotonic)
105 { 112 {
106 struct timespec ts; 113 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 114 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 115 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 116 }
110#endif 117#endif
111 118
112 return ev_time (); 119 return ev_time ();
113} 120}
121
122#define array_roundsize(base,n) ((n) | 4 & ~3)
114 123
115#define array_needsize(base,cur,cnt,init) \ 124#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 125 if ((cnt) > cur) \
117 { \ 126 { \
118 int newcnt = cur; \ 127 int newcnt = cur; \
119 do \ 128 do \
120 { \ 129 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 130 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 131 } \
123 while ((cnt) > newcnt); \ 132 while ((cnt) > newcnt); \
124 \ 133 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 134 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 135 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 139/*****************************************************************************/
131 140
132typedef struct 141typedef struct
133{ 142{
134 struct ev_io *head; 143 struct ev_io *head;
135 unsigned char wev, rev; /* want, received event set */ 144 unsigned char events;
145 unsigned char reify;
136} ANFD; 146} ANFD;
137 147
138static ANFD *anfds; 148static ANFD *anfds;
139static int anfdmax; 149static int anfdmax;
140 150
141static int *fdchanges;
142static int fdchangemax, fdchangecnt;
143
144static void 151static void
145anfds_init (ANFD *base, int count) 152anfds_init (ANFD *base, int count)
146{ 153{
147 while (count--) 154 while (count--)
148 { 155 {
149 base->head = 0; 156 base->head = 0;
150 base->wev = base->rev = EV_NONE; 157 base->events = EV_NONE;
158 base->reify = 0;
159
151 ++base; 160 ++base;
152 } 161 }
153} 162}
154 163
155typedef struct 164typedef struct
162static int pendingmax, pendingcnt; 171static int pendingmax, pendingcnt;
163 172
164static void 173static void
165event (W w, int events) 174event (W w, int events)
166{ 175{
167 if (w->active) 176 if (w->pending)
177 {
178 pendings [w->pending - 1].events |= events;
179 return;
168 { 180 }
181
169 w->pending = ++pendingcnt; 182 w->pending = ++pendingcnt;
170 array_needsize (pendings, pendingmax, pendingcnt, ); 183 array_needsize (pendings, pendingmax, pendingcnt, );
171 pendings [pendingcnt - 1].w = w; 184 pendings [pendingcnt - 1].w = w;
172 pendings [pendingcnt - 1].events = events; 185 pendings [pendingcnt - 1].events = events;
173 } 186}
187
188static void
189queue_events (W *events, int eventcnt, int type)
190{
191 int i;
192
193 for (i = 0; i < eventcnt; ++i)
194 event (events [i], type);
174} 195}
175 196
176static void 197static void
177fd_event (int fd, int events) 198fd_event (int fd, int events)
178{ 199{
186 if (ev) 207 if (ev)
187 event ((W)w, ev); 208 event ((W)w, ev);
188 } 209 }
189} 210}
190 211
212/*****************************************************************************/
213
214static int *fdchanges;
215static int fdchangemax, fdchangecnt;
216
191static void 217static void
192queue_events (W *events, int eventcnt, int type) 218fd_reify (void)
193{ 219{
194 int i; 220 int i;
195 221
196 for (i = 0; i < eventcnt; ++i) 222 for (i = 0; i < fdchangecnt; ++i)
197 event (events [i], type); 223 {
224 int fd = fdchanges [i];
225 ANFD *anfd = anfds + fd;
226 struct ev_io *w;
227
228 int events = 0;
229
230 for (w = anfd->head; w; w = w->next)
231 events |= w->events;
232
233 anfd->reify = 0;
234
235 if (anfd->events != events)
236 {
237 method_modify (fd, anfd->events, events);
238 anfd->events = events;
239 }
240 }
241
242 fdchangecnt = 0;
243}
244
245static void
246fd_change (int fd)
247{
248 if (anfds [fd].reify || fdchangecnt < 0)
249 return;
250
251 anfds [fd].reify = 1;
252
253 ++fdchangecnt;
254 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
255 fdchanges [fdchangecnt - 1] = fd;
198} 256}
199 257
200/* called on EBADF to verify fds */ 258/* called on EBADF to verify fds */
201static void 259static void
202fd_recheck () 260fd_recheck (void)
203{ 261{
204 int fd; 262 int fd;
205 263
206 for (fd = 0; fd < anfdmax; ++fd) 264 for (fd = 0; fd < anfdmax; ++fd)
207 if (anfds [fd].wev) 265 if (anfds [fd].events)
208 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 266 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
209 while (anfds [fd].head) 267 while (anfds [fd].head)
268 {
210 evio_stop (anfds [fd].head); 269 ev_io_stop (anfds [fd].head);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
271 }
211} 272}
212 273
213/*****************************************************************************/ 274/*****************************************************************************/
214 275
215static struct ev_timer **timers; 276static struct ev_timer **timers;
262/*****************************************************************************/ 323/*****************************************************************************/
263 324
264typedef struct 325typedef struct
265{ 326{
266 struct ev_signal *head; 327 struct ev_signal *head;
267 sig_atomic_t gotsig; 328 sig_atomic_t volatile gotsig;
268} ANSIG; 329} ANSIG;
269 330
270static ANSIG *signals; 331static ANSIG *signals;
271static int signalmax; 332static int signalmax;
272 333
273static int sigpipe [2]; 334static int sigpipe [2];
274static sig_atomic_t gotsig; 335static sig_atomic_t volatile gotsig;
275static struct ev_io sigev; 336static struct ev_io sigev;
276 337
277static void 338static void
278signals_init (ANSIG *base, int count) 339signals_init (ANSIG *base, int count)
279{ 340{
280 while (count--) 341 while (count--)
281 { 342 {
282 base->head = 0; 343 base->head = 0;
283 base->gotsig = 0; 344 base->gotsig = 0;
345
284 ++base; 346 ++base;
285 } 347 }
286} 348}
287 349
288static void 350static void
291 signals [signum - 1].gotsig = 1; 353 signals [signum - 1].gotsig = 1;
292 354
293 if (!gotsig) 355 if (!gotsig)
294 { 356 {
295 gotsig = 1; 357 gotsig = 1;
296 write (sigpipe [1], &gotsig, 1); 358 write (sigpipe [1], &signum, 1);
297 } 359 }
298} 360}
299 361
300static void 362static void
301sigcb (struct ev_io *iow, int revents) 363sigcb (struct ev_io *iow, int revents)
302{ 364{
303 struct ev_signal *w; 365 struct ev_signal *w;
304 int sig; 366 int sig;
305 367
368 read (sigpipe [0], &revents, 1);
306 gotsig = 0; 369 gotsig = 0;
307 read (sigpipe [0], &revents, 1);
308 370
309 for (sig = signalmax; sig--; ) 371 for (sig = signalmax; sig--; )
310 if (signals [sig].gotsig) 372 if (signals [sig].gotsig)
311 { 373 {
312 signals [sig].gotsig = 0; 374 signals [sig].gotsig = 0;
324 386
325 /* rather than sort out wether we really need nb, set it */ 387 /* rather than sort out wether we really need nb, set it */
326 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
327 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
328 390
329 evio_set (&sigev, sigpipe [0], EV_READ); 391 ev_io_set (&sigev, sigpipe [0], EV_READ);
330 evio_start (&sigev); 392 ev_io_start (&sigev);
331} 393}
332 394
333/*****************************************************************************/ 395/*****************************************************************************/
334 396
335static struct ev_idle **idles; 397static struct ev_idle **idles;
365 } 427 }
366} 428}
367 429
368/*****************************************************************************/ 430/*****************************************************************************/
369 431
370#if HAVE_EPOLL 432#if EV_USE_EPOLL
371# include "ev_epoll.c" 433# include "ev_epoll.c"
372#endif 434#endif
373#if HAVE_SELECT 435#if EV_USE_SELECT
374# include "ev_select.c" 436# include "ev_select.c"
375#endif 437#endif
376 438
439int
440ev_version_major (void)
441{
442 return EV_VERSION_MAJOR;
443}
444
445int
446ev_version_minor (void)
447{
448 return EV_VERSION_MINOR;
449}
450
377int ev_init (int flags) 451int ev_init (int flags)
378{ 452{
379 if (!ev_method) 453 if (!ev_method)
380 { 454 {
381#if HAVE_MONOTONIC 455#if EV_USE_MONOTONIC
382 { 456 {
383 struct timespec ts; 457 struct timespec ts;
384 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 458 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
385 have_monotonic = 1; 459 have_monotonic = 1;
386 } 460 }
392 466
393 if (pipe (sigpipe)) 467 if (pipe (sigpipe))
394 return 0; 468 return 0;
395 469
396 ev_method = EVMETHOD_NONE; 470 ev_method = EVMETHOD_NONE;
397#if HAVE_EPOLL 471#if EV_USE_EPOLL
398 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 472 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
399#endif 473#endif
400#if HAVE_SELECT 474#if EV_USE_SELECT
401 if (ev_method == EVMETHOD_NONE) select_init (flags); 475 if (ev_method == EVMETHOD_NONE) select_init (flags);
402#endif 476#endif
403 477
404 if (ev_method) 478 if (ev_method)
405 { 479 {
406 evw_init (&sigev, sigcb); 480 ev_watcher_init (&sigev, sigcb);
407 siginit (); 481 siginit ();
408 482
409 evsignal_init (&childev, childcb, SIGCHLD); 483 ev_signal_init (&childev, childcb, SIGCHLD);
410 evsignal_start (&childev); 484 ev_signal_start (&childev);
411 } 485 }
412 } 486 }
413 487
414 return ev_method; 488 return ev_method;
415} 489}
416 490
417/*****************************************************************************/ 491/*****************************************************************************/
418 492
419void ev_prefork (void) 493void
494ev_fork_prepare (void)
420{ 495{
421 /* nop */ 496 /* nop */
422} 497}
423 498
499void
424void ev_postfork_parent (void) 500ev_fork_parent (void)
425{ 501{
426 /* nop */ 502 /* nop */
427} 503}
428 504
505void
429void ev_postfork_child (void) 506ev_fork_child (void)
430{ 507{
431#if HAVE_EPOLL 508#if EV_USE_EPOLL
432 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
433 epoll_postfork_child (); 510 epoll_postfork_child ();
434#endif 511#endif
435 512
436 evio_stop (&sigev); 513 ev_io_stop (&sigev);
437 close (sigpipe [0]); 514 close (sigpipe [0]);
438 close (sigpipe [1]); 515 close (sigpipe [1]);
439 pipe (sigpipe); 516 pipe (sigpipe);
440 siginit (); 517 siginit ();
441} 518}
442 519
443/*****************************************************************************/ 520/*****************************************************************************/
444 521
445static void 522static void
446fd_reify (void)
447{
448 int i;
449
450 for (i = 0; i < fdchangecnt; ++i)
451 {
452 int fd = fdchanges [i];
453 ANFD *anfd = anfds + fd;
454 struct ev_io *w;
455
456 int wev = 0;
457
458 for (w = anfd->head; w; w = w->next)
459 wev |= w->events;
460
461 if (anfd->wev != wev)
462 {
463 method_modify (fd, anfd->wev, wev);
464 anfd->wev = wev;
465 }
466 }
467
468 fdchangecnt = 0;
469}
470
471static void
472call_pending () 523call_pending (void)
473{ 524{
474 while (pendingcnt) 525 while (pendingcnt)
475 { 526 {
476 ANPENDING *p = pendings + --pendingcnt; 527 ANPENDING *p = pendings + --pendingcnt;
477 528
482 } 533 }
483 } 534 }
484} 535}
485 536
486static void 537static void
487timers_reify () 538timers_reify (void)
488{ 539{
489 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
490 { 541 {
491 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
492
493 event ((W)w, EV_TIMEOUT);
494 543
495 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
496 if (w->repeat) 545 if (w->repeat)
497 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
498 w->at = now + w->repeat; 548 w->at = now + w->repeat;
499 assert (("timer timeout in the past, negative repeat?", w->at > now));
500 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
501 } 550 }
502 else 551 else
503 evtimer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
504 }
505}
506 553
554 event ((W)w, EV_TIMEOUT);
555 }
556}
557
507static void 558static void
508periodics_reify () 559periodics_reify (void)
509{ 560{
510 while (periodiccnt && periodics [0]->at <= ev_now) 561 while (periodiccnt && periodics [0]->at <= ev_now)
511 { 562 {
512 struct ev_periodic *w = periodics [0]; 563 struct ev_periodic *w = periodics [0];
513 564
514 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
515 if (w->interval) 566 if (w->interval)
516 { 567 {
517 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 568 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
518 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 569 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
519 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
520 } 571 }
521 else 572 else
522 evperiodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
523 574
524 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
525 } 576 }
526} 577}
527 578
528static void 579static void
529periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
539 { 590 {
540 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 591 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
541 592
542 if (fabs (diff) >= 1e-4) 593 if (fabs (diff) >= 1e-4)
543 { 594 {
544 evperiodic_stop (w); 595 ev_periodic_stop (w);
545 evperiodic_start (w); 596 ev_periodic_start (w);
546 597
547 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 598 i = 0; /* restart loop, inefficient, but time jumps should be rare */
548 } 599 }
549 } 600 }
550 } 601 }
551} 602}
552 603
553static void 604static void
554time_update () 605time_update (void)
555{ 606{
556 int i; 607 int i;
557 608
558 ev_now = ev_time (); 609 ev_now = ev_time ();
559 610
593int ev_loop_done; 644int ev_loop_done;
594 645
595void ev_loop (int flags) 646void ev_loop (int flags)
596{ 647{
597 double block; 648 double block;
598 ev_loop_done = flags & EVLOOP_ONESHOT ? 1 : 0; 649 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
599 650
600 do 651 do
601 { 652 {
602 /* queue check watchers (and execute them) */ 653 /* queue check watchers (and execute them) */
603 if (preparecnt) 654 if (preparecnt)
684 head = &(*head)->next; 735 head = &(*head)->next;
685 } 736 }
686} 737}
687 738
688static void 739static void
689ev_clear (W w) 740ev_clear_pending (W w)
690{ 741{
691 if (w->pending) 742 if (w->pending)
692 { 743 {
693 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
694 w->pending = 0; 745 w->pending = 0;
708} 759}
709 760
710/*****************************************************************************/ 761/*****************************************************************************/
711 762
712void 763void
713evio_start (struct ev_io *w) 764ev_io_start (struct ev_io *w)
714{ 765{
715 if (ev_is_active (w)) 766 if (ev_is_active (w))
716 return; 767 return;
717 768
718 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
719 772
720 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
721 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
722 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
723 776
724 ++fdchangecnt; 777 fd_change (fd);
725 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
726 fdchanges [fdchangecnt - 1] = fd;
727} 778}
728 779
729void 780void
730evio_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
731{ 782{
732 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
733 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
734 return; 785 return;
735 786
736 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
737 ev_stop ((W)w); 788 ev_stop ((W)w);
738 789
739 ++fdchangecnt; 790 fd_change (w->fd);
740 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
741 fdchanges [fdchangecnt - 1] = w->fd;
742} 791}
743 792
744void 793void
745evtimer_start (struct ev_timer *w) 794ev_timer_start (struct ev_timer *w)
746{ 795{
747 if (ev_is_active (w)) 796 if (ev_is_active (w))
748 return; 797 return;
749 798
750 w->at += now; 799 w->at += now;
751 800
752 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 801 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
753 802
754 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
755 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
756 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
757 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
758} 807}
759 808
760void 809void
761evtimer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
762{ 811{
763 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
764 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
765 return; 814 return;
766 815
767 if (w->active < timercnt--) 816 if (w->active < timercnt--)
768 { 817 {
774 823
775 ev_stop ((W)w); 824 ev_stop ((W)w);
776} 825}
777 826
778void 827void
779evtimer_again (struct ev_timer *w) 828ev_timer_again (struct ev_timer *w)
780{ 829{
781 if (ev_is_active (w)) 830 if (ev_is_active (w))
782 { 831 {
783 if (w->repeat) 832 if (w->repeat)
784 { 833 {
785 w->at = now + w->repeat; 834 w->at = now + w->repeat;
786 downheap ((WT *)timers, timercnt, w->active - 1); 835 downheap ((WT *)timers, timercnt, w->active - 1);
787 } 836 }
788 else 837 else
789 evtimer_stop (w); 838 ev_timer_stop (w);
790 } 839 }
791 else if (w->repeat) 840 else if (w->repeat)
792 evtimer_start (w); 841 ev_timer_start (w);
793} 842}
794 843
795void 844void
796evperiodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
797{ 846{
798 if (ev_is_active (w)) 847 if (ev_is_active (w))
799 return; 848 return;
800 849
801 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 850 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
802 851
803 /* this formula differs from the one in periodic_reify because we do not always round up */ 852 /* this formula differs from the one in periodic_reify because we do not always round up */
804 if (w->interval) 853 if (w->interval)
805 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
806 855
809 periodics [periodiccnt - 1] = w; 858 periodics [periodiccnt - 1] = w;
810 upheap ((WT *)periodics, periodiccnt - 1); 859 upheap ((WT *)periodics, periodiccnt - 1);
811} 860}
812 861
813void 862void
814evperiodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
815{ 864{
816 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
817 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
818 return; 867 return;
819 868
820 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
821 { 870 {
825 874
826 ev_stop ((W)w); 875 ev_stop ((W)w);
827} 876}
828 877
829void 878void
830evsignal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
831{ 880{
832 if (ev_is_active (w)) 881 if (ev_is_active (w))
833 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
834 885
835 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
836 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
837 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
838 889
845 sigaction (w->signum, &sa, 0); 896 sigaction (w->signum, &sa, 0);
846 } 897 }
847} 898}
848 899
849void 900void
850evsignal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
851{ 902{
852 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
853 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
854 return; 905 return;
855 906
856 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
857 ev_stop ((W)w); 908 ev_stop ((W)w);
858 909
859 if (!signals [w->signum - 1].head) 910 if (!signals [w->signum - 1].head)
860 signal (w->signum, SIG_DFL); 911 signal (w->signum, SIG_DFL);
861} 912}
862 913
914void
863void evidle_start (struct ev_idle *w) 915ev_idle_start (struct ev_idle *w)
864{ 916{
865 if (ev_is_active (w)) 917 if (ev_is_active (w))
866 return; 918 return;
867 919
868 ev_start ((W)w, ++idlecnt); 920 ev_start ((W)w, ++idlecnt);
869 array_needsize (idles, idlemax, idlecnt, ); 921 array_needsize (idles, idlemax, idlecnt, );
870 idles [idlecnt - 1] = w; 922 idles [idlecnt - 1] = w;
871} 923}
872 924
925void
873void evidle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
874{ 927{
875 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
876 if (ev_is_active (w)) 929 if (ev_is_active (w))
877 return; 930 return;
878 931
879 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
880 ev_stop ((W)w); 933 ev_stop ((W)w);
881} 934}
882 935
936void
883void evprepare_start (struct ev_prepare *w) 937ev_prepare_start (struct ev_prepare *w)
884{ 938{
885 if (ev_is_active (w)) 939 if (ev_is_active (w))
886 return; 940 return;
887 941
888 ev_start ((W)w, ++preparecnt); 942 ev_start ((W)w, ++preparecnt);
889 array_needsize (prepares, preparemax, preparecnt, ); 943 array_needsize (prepares, preparemax, preparecnt, );
890 prepares [preparecnt - 1] = w; 944 prepares [preparecnt - 1] = w;
891} 945}
892 946
947void
893void evprepare_stop (struct ev_prepare *w) 948ev_prepare_stop (struct ev_prepare *w)
894{ 949{
895 ev_clear ((W)w); 950 ev_clear_pending ((W)w);
896 if (ev_is_active (w)) 951 if (ev_is_active (w))
897 return; 952 return;
898 953
899 prepares [w->active - 1] = prepares [--preparecnt]; 954 prepares [w->active - 1] = prepares [--preparecnt];
900 ev_stop ((W)w); 955 ev_stop ((W)w);
901} 956}
902 957
958void
903void evcheck_start (struct ev_check *w) 959ev_check_start (struct ev_check *w)
904{ 960{
905 if (ev_is_active (w)) 961 if (ev_is_active (w))
906 return; 962 return;
907 963
908 ev_start ((W)w, ++checkcnt); 964 ev_start ((W)w, ++checkcnt);
909 array_needsize (checks, checkmax, checkcnt, ); 965 array_needsize (checks, checkmax, checkcnt, );
910 checks [checkcnt - 1] = w; 966 checks [checkcnt - 1] = w;
911} 967}
912 968
969void
913void evcheck_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
914{ 971{
915 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
916 if (ev_is_active (w)) 973 if (ev_is_active (w))
917 return; 974 return;
918 975
919 checks [w->active - 1] = checks [--checkcnt]; 976 checks [w->active - 1] = checks [--checkcnt];
920 ev_stop ((W)w); 977 ev_stop ((W)w);
921} 978}
922 979
980void
923void evchild_start (struct ev_child *w) 981ev_child_start (struct ev_child *w)
924{ 982{
925 if (ev_is_active (w)) 983 if (ev_is_active (w))
926 return; 984 return;
927 985
928 ev_start ((W)w, 1); 986 ev_start ((W)w, 1);
929 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 987 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
930} 988}
931 989
990void
932void evchild_stop (struct ev_child *w) 991ev_child_stop (struct ev_child *w)
933{ 992{
934 ev_clear ((W)w); 993 ev_clear_pending ((W)w);
935 if (ev_is_active (w)) 994 if (ev_is_active (w))
936 return; 995 return;
937 996
938 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 997 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
939 ev_stop ((W)w); 998 ev_stop ((W)w);
953once_cb (struct ev_once *once, int revents) 1012once_cb (struct ev_once *once, int revents)
954{ 1013{
955 void (*cb)(int revents, void *arg) = once->cb; 1014 void (*cb)(int revents, void *arg) = once->cb;
956 void *arg = once->arg; 1015 void *arg = once->arg;
957 1016
958 evio_stop (&once->io); 1017 ev_io_stop (&once->io);
959 evtimer_stop (&once->to); 1018 ev_timer_stop (&once->to);
960 free (once); 1019 free (once);
961 1020
962 cb (revents, arg); 1021 cb (revents, arg);
963} 1022}
964 1023
978ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1037ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
979{ 1038{
980 struct ev_once *once = malloc (sizeof (struct ev_once)); 1039 struct ev_once *once = malloc (sizeof (struct ev_once));
981 1040
982 if (!once) 1041 if (!once)
983 cb (EV_ERROR, arg); 1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
984 else 1043 else
985 { 1044 {
986 once->cb = cb; 1045 once->cb = cb;
987 once->arg = arg; 1046 once->arg = arg;
988 1047
989 evw_init (&once->io, once_cb_io); 1048 ev_watcher_init (&once->io, once_cb_io);
990
991 if (fd >= 0) 1049 if (fd >= 0)
992 { 1050 {
993 evio_set (&once->io, fd, events); 1051 ev_io_set (&once->io, fd, events);
994 evio_start (&once->io); 1052 ev_io_start (&once->io);
995 } 1053 }
996 1054
997 evw_init (&once->to, once_cb_to); 1055 ev_watcher_init (&once->to, once_cb_to);
998
999 if (timeout >= 0.) 1056 if (timeout >= 0.)
1000 { 1057 {
1001 evtimer_set (&once->to, timeout, 0.); 1058 ev_timer_set (&once->to, timeout, 0.);
1002 evtimer_start (&once->to); 1059 ev_timer_start (&once->to);
1003 } 1060 }
1004 } 1061 }
1005} 1062}
1006 1063
1007/*****************************************************************************/ 1064/*****************************************************************************/
1018 1075
1019static void 1076static void
1020ocb (struct ev_timer *w, int revents) 1077ocb (struct ev_timer *w, int revents)
1021{ 1078{
1022 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1079 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1023 evtimer_stop (w); 1080 ev_timer_stop (w);
1024 evtimer_start (w); 1081 ev_timer_start (w);
1025} 1082}
1026 1083
1027static void 1084static void
1028scb (struct ev_signal *w, int revents) 1085scb (struct ev_signal *w, int revents)
1029{ 1086{
1030 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1031 evio_stop (&wio); 1088 ev_io_stop (&wio);
1032 evio_start (&wio); 1089 ev_io_start (&wio);
1033} 1090}
1034 1091
1035static void 1092static void
1036gcb (struct ev_signal *w, int revents) 1093gcb (struct ev_signal *w, int revents)
1037{ 1094{
1041 1098
1042int main (void) 1099int main (void)
1043{ 1100{
1044 ev_init (0); 1101 ev_init (0);
1045 1102
1046 evio_init (&wio, sin_cb, 0, EV_READ); 1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
1047 evio_start (&wio); 1104 ev_io_start (&wio);
1048 1105
1049 struct ev_timer t[10000]; 1106 struct ev_timer t[10000];
1050 1107
1051#if 0 1108#if 0
1052 int i; 1109 int i;
1053 for (i = 0; i < 10000; ++i) 1110 for (i = 0; i < 10000; ++i)
1054 { 1111 {
1055 struct ev_timer *w = t + i; 1112 struct ev_timer *w = t + i;
1056 evw_init (w, ocb, i); 1113 ev_watcher_init (w, ocb, i);
1057 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1058 evtimer_start (w); 1115 ev_timer_start (w);
1059 if (drand48 () < 0.5) 1116 if (drand48 () < 0.5)
1060 evtimer_stop (w); 1117 ev_timer_stop (w);
1061 } 1118 }
1062#endif 1119#endif
1063 1120
1064 struct ev_timer t1; 1121 struct ev_timer t1;
1065 evtimer_init (&t1, ocb, 5, 10); 1122 ev_timer_init (&t1, ocb, 5, 10);
1066 evtimer_start (&t1); 1123 ev_timer_start (&t1);
1067 1124
1068 struct ev_signal sig; 1125 struct ev_signal sig;
1069 evsignal_init (&sig, scb, SIGQUIT); 1126 ev_signal_init (&sig, scb, SIGQUIT);
1070 evsignal_start (&sig); 1127 ev_signal_start (&sig);
1071 1128
1072 struct ev_check cw; 1129 struct ev_check cw;
1073 evcheck_init (&cw, gcb); 1130 ev_check_init (&cw, gcb);
1074 evcheck_start (&cw); 1131 ev_check_start (&cw);
1075 1132
1076 struct ev_idle iw; 1133 struct ev_idle iw;
1077 evidle_init (&iw, gcb); 1134 ev_idle_init (&iw, gcb);
1078 evidle_start (&iw); 1135 ev_idle_start (&iw);
1079 1136
1080 ev_loop (0); 1137 ev_loop (0);
1081 1138
1082 return 0; 1139 return 0;
1083} 1140}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines