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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines