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

Comparing libev/ev.c (file contents):
Revision 1.26 by root, Wed Oct 31 21:50:15 2007 UTC vs.
Revision 1.33 by root, Thu Nov 1 11:11:22 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 (void) 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)
210 { 268 {
211 event ((W)anfds [fd].head, EV_ERROR);
212 evio_stop (anfds [fd].head); 269 ev_io_stop (anfds [fd].head);
270 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
213 } 271 }
214} 272}
215 273
216/*****************************************************************************/ 274/*****************************************************************************/
217 275
282{ 340{
283 while (count--) 341 while (count--)
284 { 342 {
285 base->head = 0; 343 base->head = 0;
286 base->gotsig = 0; 344 base->gotsig = 0;
345
287 ++base; 346 ++base;
288 } 347 }
289} 348}
290 349
291static void 350static void
327 386
328 /* rather than sort out wether we really need nb, set it */ 387 /* rather than sort out wether we really need nb, set it */
329 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 388 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
330 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 389 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
331 390
332 evio_set (&sigev, sigpipe [0], EV_READ); 391 ev_io_set (&sigev, sigpipe [0], EV_READ);
333 evio_start (&sigev); 392 ev_io_start (&sigev);
334} 393}
335 394
336/*****************************************************************************/ 395/*****************************************************************************/
337 396
338static struct ev_idle **idles; 397static struct ev_idle **idles;
368 } 427 }
369} 428}
370 429
371/*****************************************************************************/ 430/*****************************************************************************/
372 431
373#if HAVE_EPOLL 432#if EV_USE_EPOLL
374# include "ev_epoll.c" 433# include "ev_epoll.c"
375#endif 434#endif
376#if HAVE_SELECT 435#if EV_USE_SELECT
377# include "ev_select.c" 436# include "ev_select.c"
378#endif 437#endif
379 438
380int 439int
381ev_version_major (void) 440ev_version_major (void)
391 450
392int ev_init (int flags) 451int ev_init (int flags)
393{ 452{
394 if (!ev_method) 453 if (!ev_method)
395 { 454 {
396#if HAVE_MONOTONIC 455#if EV_USE_MONOTONIC
397 { 456 {
398 struct timespec ts; 457 struct timespec ts;
399 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 458 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
400 have_monotonic = 1; 459 have_monotonic = 1;
401 } 460 }
407 466
408 if (pipe (sigpipe)) 467 if (pipe (sigpipe))
409 return 0; 468 return 0;
410 469
411 ev_method = EVMETHOD_NONE; 470 ev_method = EVMETHOD_NONE;
412#if HAVE_EPOLL 471#if EV_USE_EPOLL
413 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 472 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
414#endif 473#endif
415#if HAVE_SELECT 474#if EV_USE_SELECT
416 if (ev_method == EVMETHOD_NONE) select_init (flags); 475 if (ev_method == EVMETHOD_NONE) select_init (flags);
417#endif 476#endif
418 477
419 if (ev_method) 478 if (ev_method)
420 { 479 {
421 evw_init (&sigev, sigcb); 480 ev_watcher_init (&sigev, sigcb);
422 siginit (); 481 siginit ();
423 482
424 evsignal_init (&childev, childcb, SIGCHLD); 483 ev_signal_init (&childev, childcb, SIGCHLD);
425 evsignal_start (&childev); 484 ev_signal_start (&childev);
426 } 485 }
427 } 486 }
428 487
429 return ev_method; 488 return ev_method;
430} 489}
444} 503}
445 504
446void 505void
447ev_postfork_child (void) 506ev_postfork_child (void)
448{ 507{
449#if HAVE_EPOLL 508#if EV_USE_EPOLL
450 if (ev_method == EVMETHOD_EPOLL) 509 if (ev_method == EVMETHOD_EPOLL)
451 epoll_postfork_child (); 510 epoll_postfork_child ();
452#endif 511#endif
453 512
454 evio_stop (&sigev); 513 ev_io_stop (&sigev);
455 close (sigpipe [0]); 514 close (sigpipe [0]);
456 close (sigpipe [1]); 515 close (sigpipe [1]);
457 pipe (sigpipe); 516 pipe (sigpipe);
458 siginit (); 517 siginit ();
459} 518}
460 519
461/*****************************************************************************/ 520/*****************************************************************************/
462 521
463static void 522static void
464fd_reify (void)
465{
466 int i;
467
468 for (i = 0; i < fdchangecnt; ++i)
469 {
470 int fd = fdchanges [i];
471 ANFD *anfd = anfds + fd;
472 struct ev_io *w;
473
474 int wev = 0;
475
476 for (w = anfd->head; w; w = w->next)
477 wev |= w->events;
478
479 if (anfd->wev != wev)
480 {
481 method_modify (fd, anfd->wev, wev);
482 anfd->wev = wev;
483 }
484 }
485
486 fdchangecnt = 0;
487}
488
489static void
490call_pending (void) 523call_pending (void)
491{ 524{
492 while (pendingcnt) 525 while (pendingcnt)
493 { 526 {
494 ANPENDING *p = pendings + --pendingcnt; 527 ANPENDING *p = pendings + --pendingcnt;
506{ 539{
507 while (timercnt && timers [0]->at <= now) 540 while (timercnt && timers [0]->at <= now)
508 { 541 {
509 struct ev_timer *w = timers [0]; 542 struct ev_timer *w = timers [0];
510 543
511 event ((W)w, EV_TIMEOUT);
512
513 /* first reschedule or stop timer */ 544 /* first reschedule or stop timer */
514 if (w->repeat) 545 if (w->repeat)
515 { 546 {
547 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
516 w->at = now + w->repeat; 548 w->at = now + w->repeat;
517 assert (("timer timeout in the past, negative repeat?", w->at > now));
518 downheap ((WT *)timers, timercnt, 0); 549 downheap ((WT *)timers, timercnt, 0);
519 } 550 }
520 else 551 else
521 evtimer_stop (w); /* nonrepeating: stop timer */ 552 ev_timer_stop (w); /* nonrepeating: stop timer */
553
554 event ((W)w, EV_TIMEOUT);
522 } 555 }
523} 556}
524 557
525static void 558static void
526periodics_reify (void) 559periodics_reify (void)
531 564
532 /* first reschedule or stop timer */ 565 /* first reschedule or stop timer */
533 if (w->interval) 566 if (w->interval)
534 { 567 {
535 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;
536 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));
537 downheap ((WT *)periodics, periodiccnt, 0); 570 downheap ((WT *)periodics, periodiccnt, 0);
538 } 571 }
539 else 572 else
540 evperiodic_stop (w); /* nonrepeating: stop timer */ 573 ev_periodic_stop (w); /* nonrepeating: stop timer */
541 574
542 event ((W)w, EV_TIMEOUT); 575 event ((W)w, EV_PERIODIC);
543 } 576 }
544} 577}
545 578
546static void 579static void
547periodics_reschedule (ev_tstamp diff) 580periodics_reschedule (ev_tstamp diff)
557 { 590 {
558 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;
559 592
560 if (fabs (diff) >= 1e-4) 593 if (fabs (diff) >= 1e-4)
561 { 594 {
562 evperiodic_stop (w); 595 ev_periodic_stop (w);
563 evperiodic_start (w); 596 ev_periodic_start (w);
564 597
565 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 598 i = 0; /* restart loop, inefficient, but time jumps should be rare */
566 } 599 }
567 } 600 }
568 } 601 }
702 head = &(*head)->next; 735 head = &(*head)->next;
703 } 736 }
704} 737}
705 738
706static void 739static void
707ev_clear (W w) 740ev_clear_pending (W w)
708{ 741{
709 if (w->pending) 742 if (w->pending)
710 { 743 {
711 pendings [w->pending - 1].w = 0; 744 pendings [w->pending - 1].w = 0;
712 w->pending = 0; 745 w->pending = 0;
726} 759}
727 760
728/*****************************************************************************/ 761/*****************************************************************************/
729 762
730void 763void
731evio_start (struct ev_io *w) 764ev_io_start (struct ev_io *w)
732{ 765{
733 if (ev_is_active (w)) 766 if (ev_is_active (w))
734 return; 767 return;
735 768
736 int fd = w->fd; 769 int fd = w->fd;
770
771 assert (("ev_io_start called with negative fd", fd >= 0));
737 772
738 ev_start ((W)w, 1); 773 ev_start ((W)w, 1);
739 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 774 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
740 wlist_add ((WL *)&anfds[fd].head, (WL)w); 775 wlist_add ((WL *)&anfds[fd].head, (WL)w);
741 776
742 ++fdchangecnt; 777 fd_change (fd);
743 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
744 fdchanges [fdchangecnt - 1] = fd;
745
746 if (w->fd == 9)
747 printf ("start %p:%x\n", w, w->events);//D
748} 778}
749 779
750void 780void
751evio_stop (struct ev_io *w) 781ev_io_stop (struct ev_io *w)
752{ 782{
753 if (w->fd == 9)
754 printf ("stop %p:%x\n", w, w->events);//D
755 ev_clear ((W)w); 783 ev_clear_pending ((W)w);
756 if (!ev_is_active (w)) 784 if (!ev_is_active (w))
757 return; 785 return;
758 786
759 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 787 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
760 ev_stop ((W)w); 788 ev_stop ((W)w);
761 789
762 ++fdchangecnt; 790 fd_change (w->fd);
763 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
764 fdchanges [fdchangecnt - 1] = w->fd;
765} 791}
766 792
767void 793void
768evtimer_start (struct ev_timer *w) 794ev_timer_start (struct ev_timer *w)
769{ 795{
770 if (ev_is_active (w)) 796 if (ev_is_active (w))
771 return; 797 return;
772 798
773 w->at += now; 799 w->at += now;
774 800
775 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.));
776 802
777 ev_start ((W)w, ++timercnt); 803 ev_start ((W)w, ++timercnt);
778 array_needsize (timers, timermax, timercnt, ); 804 array_needsize (timers, timermax, timercnt, );
779 timers [timercnt - 1] = w; 805 timers [timercnt - 1] = w;
780 upheap ((WT *)timers, timercnt - 1); 806 upheap ((WT *)timers, timercnt - 1);
781} 807}
782 808
783void 809void
784evtimer_stop (struct ev_timer *w) 810ev_timer_stop (struct ev_timer *w)
785{ 811{
786 ev_clear ((W)w); 812 ev_clear_pending ((W)w);
787 if (!ev_is_active (w)) 813 if (!ev_is_active (w))
788 return; 814 return;
789 815
790 if (w->active < timercnt--) 816 if (w->active < timercnt--)
791 { 817 {
797 823
798 ev_stop ((W)w); 824 ev_stop ((W)w);
799} 825}
800 826
801void 827void
802evtimer_again (struct ev_timer *w) 828ev_timer_again (struct ev_timer *w)
803{ 829{
804 if (ev_is_active (w)) 830 if (ev_is_active (w))
805 { 831 {
806 if (w->repeat) 832 if (w->repeat)
807 { 833 {
808 w->at = now + w->repeat; 834 w->at = now + w->repeat;
809 downheap ((WT *)timers, timercnt, w->active - 1); 835 downheap ((WT *)timers, timercnt, w->active - 1);
810 } 836 }
811 else 837 else
812 evtimer_stop (w); 838 ev_timer_stop (w);
813 } 839 }
814 else if (w->repeat) 840 else if (w->repeat)
815 evtimer_start (w); 841 ev_timer_start (w);
816} 842}
817 843
818void 844void
819evperiodic_start (struct ev_periodic *w) 845ev_periodic_start (struct ev_periodic *w)
820{ 846{
821 if (ev_is_active (w)) 847 if (ev_is_active (w))
822 return; 848 return;
823 849
824 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.));
825 851
826 /* 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 */
827 if (w->interval) 853 if (w->interval)
828 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 854 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
829 855
832 periodics [periodiccnt - 1] = w; 858 periodics [periodiccnt - 1] = w;
833 upheap ((WT *)periodics, periodiccnt - 1); 859 upheap ((WT *)periodics, periodiccnt - 1);
834} 860}
835 861
836void 862void
837evperiodic_stop (struct ev_periodic *w) 863ev_periodic_stop (struct ev_periodic *w)
838{ 864{
839 ev_clear ((W)w); 865 ev_clear_pending ((W)w);
840 if (!ev_is_active (w)) 866 if (!ev_is_active (w))
841 return; 867 return;
842 868
843 if (w->active < periodiccnt--) 869 if (w->active < periodiccnt--)
844 { 870 {
848 874
849 ev_stop ((W)w); 875 ev_stop ((W)w);
850} 876}
851 877
852void 878void
853evsignal_start (struct ev_signal *w) 879ev_signal_start (struct ev_signal *w)
854{ 880{
855 if (ev_is_active (w)) 881 if (ev_is_active (w))
856 return; 882 return;
883
884 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
857 885
858 ev_start ((W)w, 1); 886 ev_start ((W)w, 1);
859 array_needsize (signals, signalmax, w->signum, signals_init); 887 array_needsize (signals, signalmax, w->signum, signals_init);
860 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 888 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
861 889
868 sigaction (w->signum, &sa, 0); 896 sigaction (w->signum, &sa, 0);
869 } 897 }
870} 898}
871 899
872void 900void
873evsignal_stop (struct ev_signal *w) 901ev_signal_stop (struct ev_signal *w)
874{ 902{
875 ev_clear ((W)w); 903 ev_clear_pending ((W)w);
876 if (!ev_is_active (w)) 904 if (!ev_is_active (w))
877 return; 905 return;
878 906
879 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 907 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
880 ev_stop ((W)w); 908 ev_stop ((W)w);
881 909
882 if (!signals [w->signum - 1].head) 910 if (!signals [w->signum - 1].head)
883 signal (w->signum, SIG_DFL); 911 signal (w->signum, SIG_DFL);
884} 912}
885 913
914void
886void evidle_start (struct ev_idle *w) 915ev_idle_start (struct ev_idle *w)
887{ 916{
888 if (ev_is_active (w)) 917 if (ev_is_active (w))
889 return; 918 return;
890 919
891 ev_start ((W)w, ++idlecnt); 920 ev_start ((W)w, ++idlecnt);
892 array_needsize (idles, idlemax, idlecnt, ); 921 array_needsize (idles, idlemax, idlecnt, );
893 idles [idlecnt - 1] = w; 922 idles [idlecnt - 1] = w;
894} 923}
895 924
925void
896void evidle_stop (struct ev_idle *w) 926ev_idle_stop (struct ev_idle *w)
897{ 927{
898 ev_clear ((W)w); 928 ev_clear_pending ((W)w);
899 if (ev_is_active (w)) 929 if (ev_is_active (w))
900 return; 930 return;
901 931
902 idles [w->active - 1] = idles [--idlecnt]; 932 idles [w->active - 1] = idles [--idlecnt];
903 ev_stop ((W)w); 933 ev_stop ((W)w);
904} 934}
905 935
936void
906void evprepare_start (struct ev_prepare *w) 937ev_prepare_start (struct ev_prepare *w)
907{ 938{
908 if (ev_is_active (w)) 939 if (ev_is_active (w))
909 return; 940 return;
910 941
911 ev_start ((W)w, ++preparecnt); 942 ev_start ((W)w, ++preparecnt);
912 array_needsize (prepares, preparemax, preparecnt, ); 943 array_needsize (prepares, preparemax, preparecnt, );
913 prepares [preparecnt - 1] = w; 944 prepares [preparecnt - 1] = w;
914} 945}
915 946
947void
916void evprepare_stop (struct ev_prepare *w) 948ev_prepare_stop (struct ev_prepare *w)
917{ 949{
918 ev_clear ((W)w); 950 ev_clear_pending ((W)w);
919 if (ev_is_active (w)) 951 if (ev_is_active (w))
920 return; 952 return;
921 953
922 prepares [w->active - 1] = prepares [--preparecnt]; 954 prepares [w->active - 1] = prepares [--preparecnt];
923 ev_stop ((W)w); 955 ev_stop ((W)w);
924} 956}
925 957
958void
926void evcheck_start (struct ev_check *w) 959ev_check_start (struct ev_check *w)
927{ 960{
928 if (ev_is_active (w)) 961 if (ev_is_active (w))
929 return; 962 return;
930 963
931 ev_start ((W)w, ++checkcnt); 964 ev_start ((W)w, ++checkcnt);
932 array_needsize (checks, checkmax, checkcnt, ); 965 array_needsize (checks, checkmax, checkcnt, );
933 checks [checkcnt - 1] = w; 966 checks [checkcnt - 1] = w;
934} 967}
935 968
969void
936void evcheck_stop (struct ev_check *w) 970ev_check_stop (struct ev_check *w)
937{ 971{
938 ev_clear ((W)w); 972 ev_clear_pending ((W)w);
939 if (ev_is_active (w)) 973 if (ev_is_active (w))
940 return; 974 return;
941 975
942 checks [w->active - 1] = checks [--checkcnt]; 976 checks [w->active - 1] = checks [--checkcnt];
943 ev_stop ((W)w); 977 ev_stop ((W)w);
944} 978}
945 979
980void
946void evchild_start (struct ev_child *w) 981ev_child_start (struct ev_child *w)
947{ 982{
948 if (ev_is_active (w)) 983 if (ev_is_active (w))
949 return; 984 return;
950 985
951 ev_start ((W)w, 1); 986 ev_start ((W)w, 1);
952 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 987 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
953} 988}
954 989
990void
955void evchild_stop (struct ev_child *w) 991ev_child_stop (struct ev_child *w)
956{ 992{
957 ev_clear ((W)w); 993 ev_clear_pending ((W)w);
958 if (ev_is_active (w)) 994 if (ev_is_active (w))
959 return; 995 return;
960 996
961 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 997 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
962 ev_stop ((W)w); 998 ev_stop ((W)w);
976once_cb (struct ev_once *once, int revents) 1012once_cb (struct ev_once *once, int revents)
977{ 1013{
978 void (*cb)(int revents, void *arg) = once->cb; 1014 void (*cb)(int revents, void *arg) = once->cb;
979 void *arg = once->arg; 1015 void *arg = once->arg;
980 1016
981 evio_stop (&once->io); 1017 ev_io_stop (&once->io);
982 evtimer_stop (&once->to); 1018 ev_timer_stop (&once->to);
983 free (once); 1019 free (once);
984 1020
985 cb (revents, arg); 1021 cb (revents, arg);
986} 1022}
987 1023
1001ev_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)
1002{ 1038{
1003 struct ev_once *once = malloc (sizeof (struct ev_once)); 1039 struct ev_once *once = malloc (sizeof (struct ev_once));
1004 1040
1005 if (!once) 1041 if (!once)
1006 cb (EV_ERROR, arg); 1042 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1007 else 1043 else
1008 { 1044 {
1009 once->cb = cb; 1045 once->cb = cb;
1010 once->arg = arg; 1046 once->arg = arg;
1011 1047
1012 evw_init (&once->io, once_cb_io); 1048 ev_watcher_init (&once->io, once_cb_io);
1013
1014 if (fd >= 0) 1049 if (fd >= 0)
1015 { 1050 {
1016 evio_set (&once->io, fd, events); 1051 ev_io_set (&once->io, fd, events);
1017 evio_start (&once->io); 1052 ev_io_start (&once->io);
1018 } 1053 }
1019 1054
1020 evw_init (&once->to, once_cb_to); 1055 ev_watcher_init (&once->to, once_cb_to);
1021
1022 if (timeout >= 0.) 1056 if (timeout >= 0.)
1023 { 1057 {
1024 evtimer_set (&once->to, timeout, 0.); 1058 ev_timer_set (&once->to, timeout, 0.);
1025 evtimer_start (&once->to); 1059 ev_timer_start (&once->to);
1026 } 1060 }
1027 } 1061 }
1028} 1062}
1029 1063
1030/*****************************************************************************/ 1064/*****************************************************************************/
1041 1075
1042static void 1076static void
1043ocb (struct ev_timer *w, int revents) 1077ocb (struct ev_timer *w, int revents)
1044{ 1078{
1045 //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);
1046 evtimer_stop (w); 1080 ev_timer_stop (w);
1047 evtimer_start (w); 1081 ev_timer_start (w);
1048} 1082}
1049 1083
1050static void 1084static void
1051scb (struct ev_signal *w, int revents) 1085scb (struct ev_signal *w, int revents)
1052{ 1086{
1053 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1087 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1054 evio_stop (&wio); 1088 ev_io_stop (&wio);
1055 evio_start (&wio); 1089 ev_io_start (&wio);
1056} 1090}
1057 1091
1058static void 1092static void
1059gcb (struct ev_signal *w, int revents) 1093gcb (struct ev_signal *w, int revents)
1060{ 1094{
1064 1098
1065int main (void) 1099int main (void)
1066{ 1100{
1067 ev_init (0); 1101 ev_init (0);
1068 1102
1069 evio_init (&wio, sin_cb, 0, EV_READ); 1103 ev_io_init (&wio, sin_cb, 0, EV_READ);
1070 evio_start (&wio); 1104 ev_io_start (&wio);
1071 1105
1072 struct ev_timer t[10000]; 1106 struct ev_timer t[10000];
1073 1107
1074#if 0 1108#if 0
1075 int i; 1109 int i;
1076 for (i = 0; i < 10000; ++i) 1110 for (i = 0; i < 10000; ++i)
1077 { 1111 {
1078 struct ev_timer *w = t + i; 1112 struct ev_timer *w = t + i;
1079 evw_init (w, ocb, i); 1113 ev_watcher_init (w, ocb, i);
1080 evtimer_init_abs (w, ocb, drand48 (), 0.99775533); 1114 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1081 evtimer_start (w); 1115 ev_timer_start (w);
1082 if (drand48 () < 0.5) 1116 if (drand48 () < 0.5)
1083 evtimer_stop (w); 1117 ev_timer_stop (w);
1084 } 1118 }
1085#endif 1119#endif
1086 1120
1087 struct ev_timer t1; 1121 struct ev_timer t1;
1088 evtimer_init (&t1, ocb, 5, 10); 1122 ev_timer_init (&t1, ocb, 5, 10);
1089 evtimer_start (&t1); 1123 ev_timer_start (&t1);
1090 1124
1091 struct ev_signal sig; 1125 struct ev_signal sig;
1092 evsignal_init (&sig, scb, SIGQUIT); 1126 ev_signal_init (&sig, scb, SIGQUIT);
1093 evsignal_start (&sig); 1127 ev_signal_start (&sig);
1094 1128
1095 struct ev_check cw; 1129 struct ev_check cw;
1096 evcheck_init (&cw, gcb); 1130 ev_check_init (&cw, gcb);
1097 evcheck_start (&cw); 1131 ev_check_start (&cw);
1098 1132
1099 struct ev_idle iw; 1133 struct ev_idle iw;
1100 evidle_init (&iw, gcb); 1134 ev_idle_init (&iw, gcb);
1101 evidle_start (&iw); 1135 ev_idle_start (&iw);
1102 1136
1103 ev_loop (0); 1137 ev_loop (0);
1104 1138
1105 return 0; 1139 return 0;
1106} 1140}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines