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

Comparing libev/ev.c (file contents):
Revision 1.28 by root, Thu Nov 1 06:48:49 2007 UTC vs.
Revision 1.36 by root, Thu Nov 1 13:11:11 2007 UTC

1/* 1/*
2 * libev event processing core, watcher management
3 *
2 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
3 * All rights reserved. 5 * All rights reserved.
4 * 6 *
5 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are 8 * modification, are permitted provided that the following conditions are
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (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. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */ 30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
29 34
30#include <math.h> 35#include <math.h>
31#include <stdlib.h> 36#include <stdlib.h>
32#include <unistd.h> 37#include <unistd.h>
33#include <fcntl.h> 38#include <fcntl.h>
41#include <sys/types.h> 46#include <sys/types.h>
42#include <sys/wait.h> 47#include <sys/wait.h>
43#include <sys/time.h> 48#include <sys/time.h>
44#include <time.h> 49#include <time.h>
45 50
46#ifndef HAVE_MONOTONIC 51#ifndef EV_USE_MONOTONIC
47# ifdef CLOCK_MONOTONIC 52# ifdef CLOCK_MONOTONIC
48# define HAVE_MONOTONIC 1 53# define EV_USE_MONOTONIC 1
49# endif 54# endif
50#endif 55#endif
51 56
52#ifndef HAVE_SELECT 57#ifndef EV_USE_SELECT
53# define HAVE_SELECT 1 58# define EV_USE_SELECT 1
54#endif 59#endif
55 60
56#ifndef HAVE_EPOLL 61#ifndef EV_USE_EPOLL
57# define HAVE_EPOLL 0 62# define EV_USE_EPOLL 0
58#endif 63#endif
59 64
65#ifndef CLOCK_REALTIME
66# define EV_USE_REALTIME 0
67#endif
60#ifndef HAVE_REALTIME 68#ifndef EV_USE_REALTIME
61# define HAVE_REALTIME 1 /* posix requirement, but might be slower */ 69# define EV_USE_REALTIME 1 /* posix requirement, but might be slower */
62#endif 70#endif
63 71
64#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 72#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
65#define MAX_BLOCKTIME 60. 73#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 */ 74#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
75#define CLEANUP_INTERVAL (MAX_BLOCKTIME * 5.) /* how often to try to free memory and re-check fds */
67 76
68#include "ev.h" 77#include "ev.h"
69 78
70typedef struct ev_watcher *W; 79typedef struct ev_watcher *W;
71typedef struct ev_watcher_list *WL; 80typedef struct ev_watcher_list *WL;
84/*****************************************************************************/ 93/*****************************************************************************/
85 94
86ev_tstamp 95ev_tstamp
87ev_time (void) 96ev_time (void)
88{ 97{
89#if HAVE_REALTIME 98#if EV_USE_REALTIME
90 struct timespec ts; 99 struct timespec ts;
91 clock_gettime (CLOCK_REALTIME, &ts); 100 clock_gettime (CLOCK_REALTIME, &ts);
92 return ts.tv_sec + ts.tv_nsec * 1e-9; 101 return ts.tv_sec + ts.tv_nsec * 1e-9;
93#else 102#else
94 struct timeval tv; 103 struct timeval tv;
98} 107}
99 108
100static ev_tstamp 109static ev_tstamp
101get_clock (void) 110get_clock (void)
102{ 111{
103#if HAVE_MONOTONIC 112#if EV_USE_MONOTONIC
104 if (have_monotonic) 113 if (have_monotonic)
105 { 114 {
106 struct timespec ts; 115 struct timespec ts;
107 clock_gettime (CLOCK_MONOTONIC, &ts); 116 clock_gettime (CLOCK_MONOTONIC, &ts);
108 return ts.tv_sec + ts.tv_nsec * 1e-9; 117 return ts.tv_sec + ts.tv_nsec * 1e-9;
109 } 118 }
110#endif 119#endif
111 120
112 return ev_time (); 121 return ev_time ();
113} 122}
123
124#define array_roundsize(base,n) ((n) | 4 & ~3)
114 125
115#define array_needsize(base,cur,cnt,init) \ 126#define array_needsize(base,cur,cnt,init) \
116 if ((cnt) > cur) \ 127 if ((cnt) > cur) \
117 { \ 128 { \
118 int newcnt = cur; \ 129 int newcnt = cur; \
119 do \ 130 do \
120 { \ 131 { \
121 newcnt = (newcnt << 1) | 4 & ~3; \ 132 newcnt = array_roundsize (base, newcnt << 1); \
122 } \ 133 } \
123 while ((cnt) > newcnt); \ 134 while ((cnt) > newcnt); \
124 \ 135 \
125 base = realloc (base, sizeof (*base) * (newcnt)); \ 136 base = realloc (base, sizeof (*base) * (newcnt)); \
126 init (base + cur, newcnt - cur); \ 137 init (base + cur, newcnt - cur); \
130/*****************************************************************************/ 141/*****************************************************************************/
131 142
132typedef struct 143typedef struct
133{ 144{
134 struct ev_io *head; 145 struct ev_io *head;
135 int events; 146 unsigned char events;
147 unsigned char reify;
136} ANFD; 148} ANFD;
137 149
138static ANFD *anfds; 150static ANFD *anfds;
139static int anfdmax; 151static int anfdmax;
140 152
143{ 155{
144 while (count--) 156 while (count--)
145 { 157 {
146 base->head = 0; 158 base->head = 0;
147 base->events = EV_NONE; 159 base->events = EV_NONE;
160 base->reify = 0;
161
148 ++base; 162 ++base;
149 } 163 }
150} 164}
151 165
152typedef struct 166typedef struct
159static int pendingmax, pendingcnt; 173static int pendingmax, pendingcnt;
160 174
161static void 175static void
162event (W w, int events) 176event (W w, int events)
163{ 177{
164 if (w->active) 178 if (w->pending)
179 {
180 pendings [w->pending - 1].events |= events;
181 return;
165 { 182 }
183
166 w->pending = ++pendingcnt; 184 w->pending = ++pendingcnt;
167 array_needsize (pendings, pendingmax, pendingcnt, ); 185 array_needsize (pendings, pendingmax, pendingcnt, );
168 pendings [pendingcnt - 1].w = w; 186 pendings [pendingcnt - 1].w = w;
169 pendings [pendingcnt - 1].events = events; 187 pendings [pendingcnt - 1].events = events;
170 }
171} 188}
172 189
173static void 190static void
174queue_events (W *events, int eventcnt, int type) 191queue_events (W *events, int eventcnt, int type)
175{ 192{
213 int events = 0; 230 int events = 0;
214 231
215 for (w = anfd->head; w; w = w->next) 232 for (w = anfd->head; w; w = w->next)
216 events |= w->events; 233 events |= w->events;
217 234
218 anfd->events &= ~EV_REIFY; 235 anfd->reify = 0;
219 236
220 if (anfd->events != events) 237 if (anfd->events != events)
221 { 238 {
222 method_modify (fd, anfd->events, events); 239 method_modify (fd, anfd->events, events);
223 anfd->events = events; 240 anfd->events = events;
228} 245}
229 246
230static void 247static void
231fd_change (int fd) 248fd_change (int fd)
232{ 249{
233 if (anfds [fd].events & EV_REIFY) 250 if (anfds [fd].reify || fdchangecnt < 0)
234 return; 251 return;
235 252
236 anfds [fd].events |= EV_REIFY; 253 anfds [fd].reify = 1;
237 254
238 ++fdchangecnt; 255 ++fdchangecnt;
239 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 256 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
240 fdchanges [fdchangecnt - 1] = fd; 257 fdchanges [fdchangecnt - 1] = fd;
241} 258}
249 for (fd = 0; fd < anfdmax; ++fd) 266 for (fd = 0; fd < anfdmax; ++fd)
250 if (anfds [fd].events) 267 if (anfds [fd].events)
251 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 268 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
252 while (anfds [fd].head) 269 while (anfds [fd].head)
253 { 270 {
254 event ((W)anfds [fd].head, EV_ERROR);
255 ev_io_stop (anfds [fd].head); 271 ev_io_stop (anfds [fd].head);
272 event ((W)anfds [fd].head, EV_ERROR | EV_READ | EV_WRITE);
256 } 273 }
257} 274}
258 275
259/*****************************************************************************/ 276/*****************************************************************************/
260 277
308/*****************************************************************************/ 325/*****************************************************************************/
309 326
310typedef struct 327typedef struct
311{ 328{
312 struct ev_signal *head; 329 struct ev_signal *head;
313 sig_atomic_t gotsig; 330 sig_atomic_t volatile gotsig;
314} ANSIG; 331} ANSIG;
315 332
316static ANSIG *signals; 333static ANSIG *signals;
317static int signalmax; 334static int signalmax;
318 335
319static int sigpipe [2]; 336static int sigpipe [2];
320static sig_atomic_t gotsig; 337static sig_atomic_t volatile gotsig;
321static struct ev_io sigev; 338static struct ev_io sigev;
322 339
323static void 340static void
324signals_init (ANSIG *base, int count) 341signals_init (ANSIG *base, int count)
325{ 342{
326 while (count--) 343 while (count--)
327 { 344 {
328 base->head = 0; 345 base->head = 0;
329 base->gotsig = 0; 346 base->gotsig = 0;
347
330 ++base; 348 ++base;
331 } 349 }
332} 350}
333 351
334static void 352static void
337 signals [signum - 1].gotsig = 1; 355 signals [signum - 1].gotsig = 1;
338 356
339 if (!gotsig) 357 if (!gotsig)
340 { 358 {
341 gotsig = 1; 359 gotsig = 1;
342 write (sigpipe [1], &gotsig, 1); 360 write (sigpipe [1], &signum, 1);
343 } 361 }
344} 362}
345 363
346static void 364static void
347sigcb (struct ev_io *iow, int revents) 365sigcb (struct ev_io *iow, int revents)
348{ 366{
349 struct ev_signal *w; 367 struct ev_signal *w;
350 int sig; 368 int sig;
351 369
370 read (sigpipe [0], &revents, 1);
352 gotsig = 0; 371 gotsig = 0;
353 read (sigpipe [0], &revents, 1);
354 372
355 for (sig = signalmax; sig--; ) 373 for (sig = signalmax; sig--; )
356 if (signals [sig].gotsig) 374 if (signals [sig].gotsig)
357 { 375 {
358 signals [sig].gotsig = 0; 376 signals [sig].gotsig = 0;
411 } 429 }
412} 430}
413 431
414/*****************************************************************************/ 432/*****************************************************************************/
415 433
416#if HAVE_EPOLL 434#if EV_USE_EPOLL
417# include "ev_epoll.c" 435# include "ev_epoll.c"
418#endif 436#endif
419#if HAVE_SELECT 437#if EV_USE_SELECT
420# include "ev_select.c" 438# include "ev_select.c"
421#endif 439#endif
422 440
423int 441int
424ev_version_major (void) 442ev_version_major (void)
434 452
435int ev_init (int flags) 453int ev_init (int flags)
436{ 454{
437 if (!ev_method) 455 if (!ev_method)
438 { 456 {
439#if HAVE_MONOTONIC 457#if EV_USE_MONOTONIC
440 { 458 {
441 struct timespec ts; 459 struct timespec ts;
442 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 460 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
443 have_monotonic = 1; 461 have_monotonic = 1;
444 } 462 }
450 468
451 if (pipe (sigpipe)) 469 if (pipe (sigpipe))
452 return 0; 470 return 0;
453 471
454 ev_method = EVMETHOD_NONE; 472 ev_method = EVMETHOD_NONE;
455#if HAVE_EPOLL 473#if EV_USE_EPOLL
456 if (ev_method == EVMETHOD_NONE) epoll_init (flags); 474 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
457#endif 475#endif
458#if HAVE_SELECT 476#if EV_USE_SELECT
459 if (ev_method == EVMETHOD_NONE) select_init (flags); 477 if (ev_method == EVMETHOD_NONE) select_init (flags);
460#endif 478#endif
461 479
462 if (ev_method) 480 if (ev_method)
463 { 481 {
473} 491}
474 492
475/*****************************************************************************/ 493/*****************************************************************************/
476 494
477void 495void
478ev_prefork (void) 496ev_fork_prepare (void)
479{ 497{
480 /* nop */ 498 /* nop */
481} 499}
482 500
483void 501void
484ev_postfork_parent (void) 502ev_fork_parent (void)
485{ 503{
486 /* nop */ 504 /* nop */
487} 505}
488 506
489void 507void
490ev_postfork_child (void) 508ev_fork_child (void)
491{ 509{
492#if HAVE_EPOLL 510#if EV_USE_EPOLL
493 if (ev_method == EVMETHOD_EPOLL) 511 if (ev_method == EVMETHOD_EPOLL)
494 epoll_postfork_child (); 512 epoll_postfork_child ();
495#endif 513#endif
496 514
497 ev_io_stop (&sigev); 515 ev_io_stop (&sigev);
523{ 541{
524 while (timercnt && timers [0]->at <= now) 542 while (timercnt && timers [0]->at <= now)
525 { 543 {
526 struct ev_timer *w = timers [0]; 544 struct ev_timer *w = timers [0];
527 545
528 event ((W)w, EV_TIMEOUT);
529
530 /* first reschedule or stop timer */ 546 /* first reschedule or stop timer */
531 if (w->repeat) 547 if (w->repeat)
532 { 548 {
549 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
533 w->at = now + w->repeat; 550 w->at = now + w->repeat;
534 assert (("timer timeout in the past, negative repeat?", w->at > now));
535 downheap ((WT *)timers, timercnt, 0); 551 downheap ((WT *)timers, timercnt, 0);
536 } 552 }
537 else 553 else
538 ev_timer_stop (w); /* nonrepeating: stop timer */ 554 ev_timer_stop (w); /* nonrepeating: stop timer */
555
556 event ((W)w, EV_TIMEOUT);
539 } 557 }
540} 558}
541 559
542static void 560static void
543periodics_reify (void) 561periodics_reify (void)
548 566
549 /* first reschedule or stop timer */ 567 /* first reschedule or stop timer */
550 if (w->interval) 568 if (w->interval)
551 { 569 {
552 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 570 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
553 assert (("periodic timeout in the past, negative interval?", w->at > ev_now)); 571 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
554 downheap ((WT *)periodics, periodiccnt, 0); 572 downheap ((WT *)periodics, periodiccnt, 0);
555 } 573 }
556 else 574 else
557 ev_periodic_stop (w); /* nonrepeating: stop timer */ 575 ev_periodic_stop (w); /* nonrepeating: stop timer */
558 576
559 event ((W)w, EV_TIMEOUT); 577 event ((W)w, EV_PERIODIC);
560 } 578 }
561} 579}
562 580
563static void 581static void
564periodics_reschedule (ev_tstamp diff) 582periodics_reschedule (ev_tstamp diff)
719 head = &(*head)->next; 737 head = &(*head)->next;
720 } 738 }
721} 739}
722 740
723static void 741static void
724ev_clear (W w) 742ev_clear_pending (W w)
725{ 743{
726 if (w->pending) 744 if (w->pending)
727 { 745 {
728 pendings [w->pending - 1].w = 0; 746 pendings [w->pending - 1].w = 0;
729 w->pending = 0; 747 w->pending = 0;
749{ 767{
750 if (ev_is_active (w)) 768 if (ev_is_active (w))
751 return; 769 return;
752 770
753 int fd = w->fd; 771 int fd = w->fd;
772
773 assert (("ev_io_start called with negative fd", fd >= 0));
754 774
755 ev_start ((W)w, 1); 775 ev_start ((W)w, 1);
756 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 776 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
757 wlist_add ((WL *)&anfds[fd].head, (WL)w); 777 wlist_add ((WL *)&anfds[fd].head, (WL)w);
758 778
760} 780}
761 781
762void 782void
763ev_io_stop (struct ev_io *w) 783ev_io_stop (struct ev_io *w)
764{ 784{
765 ev_clear ((W)w); 785 ev_clear_pending ((W)w);
766 if (!ev_is_active (w)) 786 if (!ev_is_active (w))
767 return; 787 return;
768 788
769 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 789 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
770 ev_stop ((W)w); 790 ev_stop ((W)w);
778 if (ev_is_active (w)) 798 if (ev_is_active (w))
779 return; 799 return;
780 800
781 w->at += now; 801 w->at += now;
782 802
783 assert (("timer repeat value less than zero not allowed", w->repeat >= 0.)); 803 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
784 804
785 ev_start ((W)w, ++timercnt); 805 ev_start ((W)w, ++timercnt);
786 array_needsize (timers, timermax, timercnt, ); 806 array_needsize (timers, timermax, timercnt, );
787 timers [timercnt - 1] = w; 807 timers [timercnt - 1] = w;
788 upheap ((WT *)timers, timercnt - 1); 808 upheap ((WT *)timers, timercnt - 1);
789} 809}
790 810
791void 811void
792ev_timer_stop (struct ev_timer *w) 812ev_timer_stop (struct ev_timer *w)
793{ 813{
794 ev_clear ((W)w); 814 ev_clear_pending ((W)w);
795 if (!ev_is_active (w)) 815 if (!ev_is_active (w))
796 return; 816 return;
797 817
798 if (w->active < timercnt--) 818 if (w->active < timercnt--)
799 { 819 {
827ev_periodic_start (struct ev_periodic *w) 847ev_periodic_start (struct ev_periodic *w)
828{ 848{
829 if (ev_is_active (w)) 849 if (ev_is_active (w))
830 return; 850 return;
831 851
832 assert (("periodic interval value less than zero not allowed", w->interval >= 0.)); 852 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
833 853
834 /* this formula differs from the one in periodic_reify because we do not always round up */ 854 /* this formula differs from the one in periodic_reify because we do not always round up */
835 if (w->interval) 855 if (w->interval)
836 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 856 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
837 857
842} 862}
843 863
844void 864void
845ev_periodic_stop (struct ev_periodic *w) 865ev_periodic_stop (struct ev_periodic *w)
846{ 866{
847 ev_clear ((W)w); 867 ev_clear_pending ((W)w);
848 if (!ev_is_active (w)) 868 if (!ev_is_active (w))
849 return; 869 return;
850 870
851 if (w->active < periodiccnt--) 871 if (w->active < periodiccnt--)
852 { 872 {
860void 880void
861ev_signal_start (struct ev_signal *w) 881ev_signal_start (struct ev_signal *w)
862{ 882{
863 if (ev_is_active (w)) 883 if (ev_is_active (w))
864 return; 884 return;
885
886 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
865 887
866 ev_start ((W)w, 1); 888 ev_start ((W)w, 1);
867 array_needsize (signals, signalmax, w->signum, signals_init); 889 array_needsize (signals, signalmax, w->signum, signals_init);
868 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 890 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
869 891
878} 900}
879 901
880void 902void
881ev_signal_stop (struct ev_signal *w) 903ev_signal_stop (struct ev_signal *w)
882{ 904{
883 ev_clear ((W)w); 905 ev_clear_pending ((W)w);
884 if (!ev_is_active (w)) 906 if (!ev_is_active (w))
885 return; 907 return;
886 908
887 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 909 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
888 ev_stop ((W)w); 910 ev_stop ((W)w);
903} 925}
904 926
905void 927void
906ev_idle_stop (struct ev_idle *w) 928ev_idle_stop (struct ev_idle *w)
907{ 929{
908 ev_clear ((W)w); 930 ev_clear_pending ((W)w);
909 if (ev_is_active (w)) 931 if (ev_is_active (w))
910 return; 932 return;
911 933
912 idles [w->active - 1] = idles [--idlecnt]; 934 idles [w->active - 1] = idles [--idlecnt];
913 ev_stop ((W)w); 935 ev_stop ((W)w);
925} 947}
926 948
927void 949void
928ev_prepare_stop (struct ev_prepare *w) 950ev_prepare_stop (struct ev_prepare *w)
929{ 951{
930 ev_clear ((W)w); 952 ev_clear_pending ((W)w);
931 if (ev_is_active (w)) 953 if (ev_is_active (w))
932 return; 954 return;
933 955
934 prepares [w->active - 1] = prepares [--preparecnt]; 956 prepares [w->active - 1] = prepares [--preparecnt];
935 ev_stop ((W)w); 957 ev_stop ((W)w);
947} 969}
948 970
949void 971void
950ev_check_stop (struct ev_check *w) 972ev_check_stop (struct ev_check *w)
951{ 973{
952 ev_clear ((W)w); 974 ev_clear_pending ((W)w);
953 if (ev_is_active (w)) 975 if (ev_is_active (w))
954 return; 976 return;
955 977
956 checks [w->active - 1] = checks [--checkcnt]; 978 checks [w->active - 1] = checks [--checkcnt];
957 ev_stop ((W)w); 979 ev_stop ((W)w);
968} 990}
969 991
970void 992void
971ev_child_stop (struct ev_child *w) 993ev_child_stop (struct ev_child *w)
972{ 994{
973 ev_clear ((W)w); 995 ev_clear_pending ((W)w);
974 if (ev_is_active (w)) 996 if (ev_is_active (w))
975 return; 997 return;
976 998
977 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 999 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
978 ev_stop ((W)w); 1000 ev_stop ((W)w);
1017ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1039ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1018{ 1040{
1019 struct ev_once *once = malloc (sizeof (struct ev_once)); 1041 struct ev_once *once = malloc (sizeof (struct ev_once));
1020 1042
1021 if (!once) 1043 if (!once)
1022 cb (EV_ERROR, arg); 1044 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1023 else 1045 else
1024 { 1046 {
1025 once->cb = cb; 1047 once->cb = cb;
1026 once->arg = arg; 1048 once->arg = arg;
1027 1049
1028 ev_watcher_init (&once->io, once_cb_io); 1050 ev_watcher_init (&once->io, once_cb_io);
1029
1030 if (fd >= 0) 1051 if (fd >= 0)
1031 { 1052 {
1032 ev_io_set (&once->io, fd, events); 1053 ev_io_set (&once->io, fd, events);
1033 ev_io_start (&once->io); 1054 ev_io_start (&once->io);
1034 } 1055 }
1035 1056
1036 ev_watcher_init (&once->to, once_cb_to); 1057 ev_watcher_init (&once->to, once_cb_to);
1037
1038 if (timeout >= 0.) 1058 if (timeout >= 0.)
1039 { 1059 {
1040 ev_timer_set (&once->to, timeout, 0.); 1060 ev_timer_set (&once->to, timeout, 0.);
1041 ev_timer_start (&once->to); 1061 ev_timer_start (&once->to);
1042 } 1062 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines