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

Comparing libev/ev.c (file contents):
Revision 1.43 by root, Fri Nov 2 20:21:33 2007 UTC vs.
Revision 1.106 by root, Mon Nov 12 01:07:50 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (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
29 * 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.
30 */ 30 */
31#if EV_USE_CONFIG_H 31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
38
39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
44# define EV_USE_REALTIME 1
45# endif
46# endif
47
48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
49# define EV_USE_SELECT 1
50# endif
51
52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
53# define EV_USE_POLL 1
54# endif
55
56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
57# define EV_USE_EPOLL 1
58# endif
59
60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
61# define EV_USE_KQUEUE 1
62# endif
63
33#endif 64#endif
34 65
35#include <math.h> 66#include <math.h>
36#include <stdlib.h> 67#include <stdlib.h>
37#include <unistd.h>
38#include <fcntl.h> 68#include <fcntl.h>
39#include <signal.h>
40#include <stddef.h> 69#include <stddef.h>
41 70
42#include <stdio.h> 71#include <stdio.h>
43 72
44#include <assert.h> 73#include <assert.h>
45#include <errno.h> 74#include <errno.h>
46#include <sys/types.h> 75#include <sys/types.h>
47#include <sys/wait.h>
48#include <sys/time.h>
49#include <time.h> 76#include <time.h>
77
78#include <signal.h>
79
80#ifndef _WIN32
81# include <unistd.h>
82# include <sys/time.h>
83# include <sys/wait.h>
84#else
85# define WIN32_LEAN_AND_MEAN
86# include <windows.h>
87# ifndef EV_SELECT_IS_WINSOCKET
88# define EV_SELECT_IS_WINSOCKET 1
89# endif
90#endif
50 91
51/**/ 92/**/
52 93
53#ifndef EV_USE_MONOTONIC 94#ifndef EV_USE_MONOTONIC
54# define EV_USE_MONOTONIC 1 95# define EV_USE_MONOTONIC 1
55#endif 96#endif
56 97
57#ifndef EV_USE_SELECT 98#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1 99# define EV_USE_SELECT 1
100# define EV_SELECT_USE_FD_SET 1
59#endif 101#endif
60 102
61#ifndef EV_USE_POLL 103#ifndef EV_USE_POLL
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 104# ifdef _WIN32
105# define EV_USE_POLL 0
106# else
107# define EV_USE_POLL 1
108# endif
63#endif 109#endif
64 110
65#ifndef EV_USE_EPOLL 111#ifndef EV_USE_EPOLL
66# define EV_USE_EPOLL 0 112# define EV_USE_EPOLL 0
67#endif 113#endif
68 114
115#ifndef EV_USE_KQUEUE
116# define EV_USE_KQUEUE 0
117#endif
118
69#ifndef EV_USE_REALTIME 119#ifndef EV_USE_REALTIME
70# define EV_USE_REALTIME 1 120# define EV_USE_REALTIME 1
71#endif 121#endif
72 122
73/**/ 123/**/
124
125/* darwin simply cnanot be helped */
126#ifdef __APPLE__
127# undef EV_USE_POLL
128# undef EV_USE_KQUEUE
129#endif
74 130
75#ifndef CLOCK_MONOTONIC 131#ifndef CLOCK_MONOTONIC
76# undef EV_USE_MONOTONIC 132# undef EV_USE_MONOTONIC
77# define EV_USE_MONOTONIC 0 133# define EV_USE_MONOTONIC 0
78#endif 134#endif
79 135
80#ifndef CLOCK_REALTIME 136#ifndef CLOCK_REALTIME
81# undef EV_USE_REALTIME 137# undef EV_USE_REALTIME
82# define EV_USE_REALTIME 0 138# define EV_USE_REALTIME 0
139#endif
140
141#if EV_SELECT_IS_WINSOCKET
142# include <winsock.h>
83#endif 143#endif
84 144
85/**/ 145/**/
86 146
87#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 147#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
88#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */ 148#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
89#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */ 149#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
90/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */ 150/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
91 151
152#ifdef EV_H
153# include EV_H
154#else
92#include "ev.h" 155# include "ev.h"
156#endif
93 157
94#if __GNUC__ >= 3 158#if __GNUC__ >= 3
95# define expect(expr,value) __builtin_expect ((expr),(value)) 159# define expect(expr,value) __builtin_expect ((expr),(value))
96# define inline inline 160# define inline inline
97#else 161#else
103#define expect_true(expr) expect ((expr) != 0, 1) 167#define expect_true(expr) expect ((expr) != 0, 1)
104 168
105#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1) 169#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
106#define ABSPRI(w) ((w)->priority - EV_MINPRI) 170#define ABSPRI(w) ((w)->priority - EV_MINPRI)
107 171
172#define EMPTY /* required for microsofts broken pseudo-c compiler */
173
108typedef struct ev_watcher *W; 174typedef struct ev_watcher *W;
109typedef struct ev_watcher_list *WL; 175typedef struct ev_watcher_list *WL;
110typedef struct ev_watcher_time *WT; 176typedef struct ev_watcher_time *WT;
111 177
112static ev_tstamp now_floor, now, diff; /* monotonic clock */ 178static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
179
180#ifdef _WIN32
181# include "ev_win32.c"
182#endif
183
184/*****************************************************************************/
185
186static void (*syserr_cb)(const char *msg);
187
188void ev_set_syserr_cb (void (*cb)(const char *msg))
189{
190 syserr_cb = cb;
191}
192
193static void
194syserr (const char *msg)
195{
196 if (!msg)
197 msg = "(libev) system error";
198
199 if (syserr_cb)
200 syserr_cb (msg);
201 else
202 {
203 perror (msg);
204 abort ();
205 }
206}
207
208static void *(*alloc)(void *ptr, long size);
209
210void ev_set_allocator (void *(*cb)(void *ptr, long size))
211{
212 alloc = cb;
213}
214
215static void *
216ev_realloc (void *ptr, long size)
217{
218 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
219
220 if (!ptr && size)
221 {
222 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
223 abort ();
224 }
225
226 return ptr;
227}
228
229#define ev_malloc(size) ev_realloc (0, (size))
230#define ev_free(ptr) ev_realloc ((ptr), 0)
231
232/*****************************************************************************/
233
234typedef struct
235{
236 WL head;
237 unsigned char events;
238 unsigned char reify;
239#if EV_SELECT_IS_WINSOCKET
240 SOCKET handle;
241#endif
242} ANFD;
243
244typedef struct
245{
246 W w;
247 int events;
248} ANPENDING;
249
250#if EV_MULTIPLICITY
251
252 struct ev_loop
253 {
254 ev_tstamp ev_rt_now;
255 #define ev_rt_now ((loop)->ev_rt_now)
256 #define VAR(name,decl) decl;
257 #include "ev_vars.h"
258 #undef VAR
259 };
260 #include "ev_wrap.h"
261
262 struct ev_loop default_loop_struct;
263 static struct ev_loop *default_loop;
264
265#else
266
113ev_tstamp ev_now; 267 ev_tstamp ev_rt_now;
114int ev_method; 268 #define VAR(name,decl) static decl;
269 #include "ev_vars.h"
270 #undef VAR
115 271
116static int have_monotonic; /* runtime */ 272 static int default_loop;
117 273
118static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 274#endif
119static void (*method_modify)(int fd, int oev, int nev);
120static void (*method_poll)(ev_tstamp timeout);
121 275
122/*****************************************************************************/ 276/*****************************************************************************/
123 277
124ev_tstamp 278ev_tstamp
125ev_time (void) 279ev_time (void)
133 gettimeofday (&tv, 0); 287 gettimeofday (&tv, 0);
134 return tv.tv_sec + tv.tv_usec * 1e-6; 288 return tv.tv_sec + tv.tv_usec * 1e-6;
135#endif 289#endif
136} 290}
137 291
138static ev_tstamp 292inline ev_tstamp
139get_clock (void) 293get_clock (void)
140{ 294{
141#if EV_USE_MONOTONIC 295#if EV_USE_MONOTONIC
142 if (expect_true (have_monotonic)) 296 if (expect_true (have_monotonic))
143 { 297 {
148#endif 302#endif
149 303
150 return ev_time (); 304 return ev_time ();
151} 305}
152 306
307#if EV_MULTIPLICITY
308ev_tstamp
309ev_now (EV_P)
310{
311 return ev_rt_now;
312}
313#endif
314
153#define array_roundsize(base,n) ((n) | 4 & ~3) 315#define array_roundsize(type,n) ((n) | 4 & ~3)
154 316
155#define array_needsize(base,cur,cnt,init) \ 317#define array_needsize(type,base,cur,cnt,init) \
156 if (expect_false ((cnt) > cur)) \ 318 if (expect_false ((cnt) > cur)) \
157 { \ 319 { \
158 int newcnt = cur; \ 320 int newcnt = cur; \
159 do \ 321 do \
160 { \ 322 { \
161 newcnt = array_roundsize (base, newcnt << 1); \ 323 newcnt = array_roundsize (type, newcnt << 1); \
162 } \ 324 } \
163 while ((cnt) > newcnt); \ 325 while ((cnt) > newcnt); \
164 \ 326 \
165 base = realloc (base, sizeof (*base) * (newcnt)); \ 327 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
166 init (base + cur, newcnt - cur); \ 328 init (base + cur, newcnt - cur); \
167 cur = newcnt; \ 329 cur = newcnt; \
168 } 330 }
331
332#define array_slim(type,stem) \
333 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
334 { \
335 stem ## max = array_roundsize (stem ## cnt >> 1); \
336 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
337 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
338 }
339
340#define array_free(stem, idx) \
341 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
169 342
170/*****************************************************************************/ 343/*****************************************************************************/
171
172typedef struct
173{
174 struct ev_io *head;
175 unsigned char events;
176 unsigned char reify;
177} ANFD;
178
179static ANFD *anfds;
180static int anfdmax;
181 344
182static void 345static void
183anfds_init (ANFD *base, int count) 346anfds_init (ANFD *base, int count)
184{ 347{
185 while (count--) 348 while (count--)
190 353
191 ++base; 354 ++base;
192 } 355 }
193} 356}
194 357
195typedef struct 358void
359ev_feed_event (EV_P_ void *w, int revents)
196{ 360{
197 W w; 361 W w_ = (W)w;
198 int events;
199} ANPENDING;
200 362
201static ANPENDING *pendings [NUMPRI];
202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
203
204static void
205event (W w, int events)
206{
207 if (w->pending) 363 if (w_->pending)
208 { 364 {
209 pendings [ABSPRI (w)][w->pending - 1].events |= events; 365 pendings [ABSPRI (w_)][w_->pending - 1].events |= revents;
210 return; 366 return;
211 } 367 }
212 368
213 w->pending = ++pendingcnt [ABSPRI (w)]; 369 w_->pending = ++pendingcnt [ABSPRI (w_)];
214 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], ); 370 array_needsize (ANPENDING, pendings [ABSPRI (w_)], pendingmax [ABSPRI (w_)], pendingcnt [ABSPRI (w_)], (void));
215 pendings [ABSPRI (w)][w->pending - 1].w = w; 371 pendings [ABSPRI (w_)][w_->pending - 1].w = w_;
216 pendings [ABSPRI (w)][w->pending - 1].events = events; 372 pendings [ABSPRI (w_)][w_->pending - 1].events = revents;
217} 373}
218 374
219static void 375static void
220queue_events (W *events, int eventcnt, int type) 376queue_events (EV_P_ W *events, int eventcnt, int type)
221{ 377{
222 int i; 378 int i;
223 379
224 for (i = 0; i < eventcnt; ++i) 380 for (i = 0; i < eventcnt; ++i)
225 event (events [i], type); 381 ev_feed_event (EV_A_ events [i], type);
226} 382}
227 383
228static void 384inline void
229fd_event (int fd, int events) 385fd_event (EV_P_ int fd, int revents)
230{ 386{
231 ANFD *anfd = anfds + fd; 387 ANFD *anfd = anfds + fd;
232 struct ev_io *w; 388 struct ev_io *w;
233 389
234 for (w = anfd->head; w; w = w->next) 390 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
235 { 391 {
236 int ev = w->events & events; 392 int ev = w->events & revents;
237 393
238 if (ev) 394 if (ev)
239 event ((W)w, ev); 395 ev_feed_event (EV_A_ (W)w, ev);
240 } 396 }
397}
398
399void
400ev_feed_fd_event (EV_P_ int fd, int revents)
401{
402 fd_event (EV_A_ fd, revents);
241} 403}
242 404
243/*****************************************************************************/ 405/*****************************************************************************/
244 406
245static int *fdchanges;
246static int fdchangemax, fdchangecnt;
247
248static void 407static void
249fd_reify (void) 408fd_reify (EV_P)
250{ 409{
251 int i; 410 int i;
252 411
253 for (i = 0; i < fdchangecnt; ++i) 412 for (i = 0; i < fdchangecnt; ++i)
254 { 413 {
256 ANFD *anfd = anfds + fd; 415 ANFD *anfd = anfds + fd;
257 struct ev_io *w; 416 struct ev_io *w;
258 417
259 int events = 0; 418 int events = 0;
260 419
261 for (w = anfd->head; w; w = w->next) 420 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
262 events |= w->events; 421 events |= w->events;
263 422
423#if EV_SELECT_IS_WINSOCKET
424 if (events)
425 {
426 unsigned long argp;
427 anfd->handle = _get_osfhandle (fd);
428 assert (("libev only supports socket fds in this configuration", ioctlsocket (anfd->handle, FIONREAD, &argp) == 0));
429 }
430#endif
431
264 anfd->reify = 0; 432 anfd->reify = 0;
265 433
266 if (anfd->events != events)
267 {
268 method_modify (fd, anfd->events, events); 434 method_modify (EV_A_ fd, anfd->events, events);
269 anfd->events = events; 435 anfd->events = events;
270 }
271 } 436 }
272 437
273 fdchangecnt = 0; 438 fdchangecnt = 0;
274} 439}
275 440
276static void 441static void
277fd_change (int fd) 442fd_change (EV_P_ int fd)
278{ 443{
279 if (anfds [fd].reify || fdchangecnt < 0) 444 if (anfds [fd].reify)
280 return; 445 return;
281 446
282 anfds [fd].reify = 1; 447 anfds [fd].reify = 1;
283 448
284 ++fdchangecnt; 449 ++fdchangecnt;
285 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 450 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
286 fdchanges [fdchangecnt - 1] = fd; 451 fdchanges [fdchangecnt - 1] = fd;
287} 452}
288 453
289static void 454static void
290fd_kill (int fd) 455fd_kill (EV_P_ int fd)
291{ 456{
292 struct ev_io *w; 457 struct ev_io *w;
293 458
294 printf ("killing fd %d\n", fd);//D
295 while ((w = anfds [fd].head)) 459 while ((w = (struct ev_io *)anfds [fd].head))
296 { 460 {
297 ev_io_stop (w); 461 ev_io_stop (EV_A_ w);
298 event ((W)w, EV_ERROR | EV_READ | EV_WRITE); 462 ev_feed_event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
299 } 463 }
464}
465
466static int
467fd_valid (int fd)
468{
469#ifdef _WIN32
470 return _get_osfhandle (fd) != -1;
471#else
472 return fcntl (fd, F_GETFD) != -1;
473#endif
300} 474}
301 475
302/* called on EBADF to verify fds */ 476/* called on EBADF to verify fds */
303static void 477static void
304fd_ebadf (void) 478fd_ebadf (EV_P)
305{ 479{
306 int fd; 480 int fd;
307 481
308 for (fd = 0; fd < anfdmax; ++fd) 482 for (fd = 0; fd < anfdmax; ++fd)
309 if (anfds [fd].events) 483 if (anfds [fd].events)
310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 484 if (!fd_valid (fd) == -1 && errno == EBADF)
311 fd_kill (fd); 485 fd_kill (EV_A_ fd);
312} 486}
313 487
314/* called on ENOMEM in select/poll to kill some fds and retry */ 488/* called on ENOMEM in select/poll to kill some fds and retry */
315static void 489static void
316fd_enomem (void) 490fd_enomem (EV_P)
317{ 491{
318 int fd = anfdmax; 492 int fd;
319 493
320 while (fd--) 494 for (fd = anfdmax; fd--; )
321 if (anfds [fd].events) 495 if (anfds [fd].events)
322 { 496 {
323 close (fd);
324 fd_kill (fd); 497 fd_kill (EV_A_ fd);
325 return; 498 return;
326 } 499 }
327} 500}
328 501
502/* usually called after fork if method needs to re-arm all fds from scratch */
503static void
504fd_rearm_all (EV_P)
505{
506 int fd;
507
508 /* this should be highly optimised to not do anything but set a flag */
509 for (fd = 0; fd < anfdmax; ++fd)
510 if (anfds [fd].events)
511 {
512 anfds [fd].events = 0;
513 fd_change (EV_A_ fd);
514 }
515}
516
329/*****************************************************************************/ 517/*****************************************************************************/
330 518
331static struct ev_timer **timers;
332static int timermax, timercnt;
333
334static struct ev_periodic **periodics;
335static int periodicmax, periodiccnt;
336
337static void 519static void
338upheap (WT *timers, int k) 520upheap (WT *heap, int k)
339{ 521{
340 WT w = timers [k]; 522 WT w = heap [k];
341 523
342 while (k && timers [k >> 1]->at > w->at) 524 while (k && heap [k >> 1]->at > w->at)
343 { 525 {
344 timers [k] = timers [k >> 1]; 526 heap [k] = heap [k >> 1];
345 timers [k]->active = k + 1; 527 ((W)heap [k])->active = k + 1;
346 k >>= 1; 528 k >>= 1;
347 } 529 }
348 530
349 timers [k] = w; 531 heap [k] = w;
350 timers [k]->active = k + 1; 532 ((W)heap [k])->active = k + 1;
351 533
352} 534}
353 535
354static void 536static void
355downheap (WT *timers, int N, int k) 537downheap (WT *heap, int N, int k)
356{ 538{
357 WT w = timers [k]; 539 WT w = heap [k];
358 540
359 while (k < (N >> 1)) 541 while (k < (N >> 1))
360 { 542 {
361 int j = k << 1; 543 int j = k << 1;
362 544
363 if (j + 1 < N && timers [j]->at > timers [j + 1]->at) 545 if (j + 1 < N && heap [j]->at > heap [j + 1]->at)
364 ++j; 546 ++j;
365 547
366 if (w->at <= timers [j]->at) 548 if (w->at <= heap [j]->at)
367 break; 549 break;
368 550
369 timers [k] = timers [j]; 551 heap [k] = heap [j];
370 timers [k]->active = k + 1; 552 ((W)heap [k])->active = k + 1;
371 k = j; 553 k = j;
372 } 554 }
373 555
374 timers [k] = w; 556 heap [k] = w;
375 timers [k]->active = k + 1; 557 ((W)heap [k])->active = k + 1;
558}
559
560inline void
561adjustheap (WT *heap, int N, int k)
562{
563 upheap (heap, k);
564 downheap (heap, N, k);
376} 565}
377 566
378/*****************************************************************************/ 567/*****************************************************************************/
379 568
380typedef struct 569typedef struct
381{ 570{
382 struct ev_signal *head; 571 WL head;
383 sig_atomic_t volatile gotsig; 572 sig_atomic_t volatile gotsig;
384} ANSIG; 573} ANSIG;
385 574
386static ANSIG *signals; 575static ANSIG *signals;
387static int signalmax; 576static int signalmax;
403} 592}
404 593
405static void 594static void
406sighandler (int signum) 595sighandler (int signum)
407{ 596{
597#if _WIN32
598 signal (signum, sighandler);
599#endif
600
408 signals [signum - 1].gotsig = 1; 601 signals [signum - 1].gotsig = 1;
409 602
410 if (!gotsig) 603 if (!gotsig)
411 { 604 {
605 int old_errno = errno;
412 gotsig = 1; 606 gotsig = 1;
413 write (sigpipe [1], &signum, 1); 607 write (sigpipe [1], &signum, 1);
608 errno = old_errno;
414 } 609 }
415} 610}
416 611
612void
613ev_feed_signal_event (EV_P_ int signum)
614{
615 WL w;
616
617#if EV_MULTIPLICITY
618 assert (("feeding signal events is only supported in the default loop", loop == default_loop));
619#endif
620
621 --signum;
622
623 if (signum < 0 || signum >= signalmax)
624 return;
625
626 signals [signum].gotsig = 0;
627
628 for (w = signals [signum].head; w; w = w->next)
629 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
630}
631
417static void 632static void
418sigcb (struct ev_io *iow, int revents) 633sigcb (EV_P_ struct ev_io *iow, int revents)
419{ 634{
420 struct ev_signal *w;
421 int signum; 635 int signum;
422 636
423 read (sigpipe [0], &revents, 1); 637 read (sigpipe [0], &revents, 1);
424 gotsig = 0; 638 gotsig = 0;
425 639
426 for (signum = signalmax; signum--; ) 640 for (signum = signalmax; signum--; )
427 if (signals [signum].gotsig) 641 if (signals [signum].gotsig)
428 { 642 ev_feed_signal_event (EV_A_ signum + 1);
429 signals [signum].gotsig = 0;
430
431 for (w = signals [signum].head; w; w = w->next)
432 event ((W)w, EV_SIGNAL);
433 }
434} 643}
435 644
436static void 645inline void
437siginit (void) 646fd_intern (int fd)
438{ 647{
648#ifdef _WIN32
649 int arg = 1;
650 ioctlsocket (_get_osfhandle (fd), FIONBIO, &arg);
651#else
439 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 652 fcntl (fd, F_SETFD, FD_CLOEXEC);
440 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
441
442 /* rather than sort out wether we really need nb, set it */
443 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 653 fcntl (fd, F_SETFL, O_NONBLOCK);
444 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 654#endif
655}
656
657static void
658siginit (EV_P)
659{
660 fd_intern (sigpipe [0]);
661 fd_intern (sigpipe [1]);
445 662
446 ev_io_set (&sigev, sigpipe [0], EV_READ); 663 ev_io_set (&sigev, sigpipe [0], EV_READ);
447 ev_io_start (&sigev); 664 ev_io_start (EV_A_ &sigev);
665 ev_unref (EV_A); /* child watcher should not keep loop alive */
448} 666}
449 667
450/*****************************************************************************/ 668/*****************************************************************************/
451 669
452static struct ev_idle **idles;
453static int idlemax, idlecnt;
454
455static struct ev_prepare **prepares;
456static int preparemax, preparecnt;
457
458static struct ev_check **checks;
459static int checkmax, checkcnt;
460
461/*****************************************************************************/
462
463static struct ev_child *childs [PID_HASHSIZE]; 670static struct ev_child *childs [PID_HASHSIZE];
671
672#ifndef _WIN32
673
464static struct ev_signal childev; 674static struct ev_signal childev;
465 675
466#ifndef WCONTINUED 676#ifndef WCONTINUED
467# define WCONTINUED 0 677# define WCONTINUED 0
468#endif 678#endif
469 679
470static void 680static void
471childcb (struct ev_signal *sw, int revents) 681child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
472{ 682{
473 struct ev_child *w; 683 struct ev_child *w;
684
685 for (w = (struct ev_child *)childs [chain & (PID_HASHSIZE - 1)]; w; w = (struct ev_child *)((WL)w)->next)
686 if (w->pid == pid || !w->pid)
687 {
688 ev_priority (w) = ev_priority (sw); /* need to do it *now* */
689 w->rpid = pid;
690 w->rstatus = status;
691 ev_feed_event (EV_A_ (W)w, EV_CHILD);
692 }
693}
694
695static void
696childcb (EV_P_ struct ev_signal *sw, int revents)
697{
474 int pid, status; 698 int pid, status;
475 699
476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 700 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 701 {
478 if (w->pid == pid || !w->pid) 702 /* make sure we are called again until all childs have been reaped */
479 { 703 ev_feed_event (EV_A_ (W)sw, EV_SIGNAL);
480 w->status = status; 704
481 event ((W)w, EV_CHILD); 705 child_reap (EV_A_ sw, pid, pid, status);
482 } 706 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
707 }
483} 708}
709
710#endif
484 711
485/*****************************************************************************/ 712/*****************************************************************************/
486 713
714#if EV_USE_KQUEUE
715# include "ev_kqueue.c"
716#endif
487#if EV_USE_EPOLL 717#if EV_USE_EPOLL
488# include "ev_epoll.c" 718# include "ev_epoll.c"
489#endif 719#endif
490#if EV_USE_POLL 720#if EV_USE_POLL
491# include "ev_poll.c" 721# include "ev_poll.c"
504ev_version_minor (void) 734ev_version_minor (void)
505{ 735{
506 return EV_VERSION_MINOR; 736 return EV_VERSION_MINOR;
507} 737}
508 738
509/* return true if we are running with elevated privileges and ignore env variables */ 739/* return true if we are running with elevated privileges and should ignore env variables */
510static int 740static int
511enable_secure () 741enable_secure (void)
512{ 742{
743#ifdef _WIN32
744 return 0;
745#else
513 return getuid () != geteuid () 746 return getuid () != geteuid ()
514 || getgid () != getegid (); 747 || getgid () != getegid ();
748#endif
515} 749}
516 750
517int ev_init (int methods) 751int
752ev_method (EV_P)
518{ 753{
754 return method;
755}
756
757static void
758loop_init (EV_P_ int methods)
759{
519 if (!ev_method) 760 if (!method)
520 { 761 {
521#if EV_USE_MONOTONIC 762#if EV_USE_MONOTONIC
522 { 763 {
523 struct timespec ts; 764 struct timespec ts;
524 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 765 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
525 have_monotonic = 1; 766 have_monotonic = 1;
526 } 767 }
527#endif 768#endif
528 769
529 ev_now = ev_time (); 770 ev_rt_now = ev_time ();
530 now = get_clock (); 771 mn_now = get_clock ();
531 now_floor = now; 772 now_floor = mn_now;
532 diff = ev_now - now; 773 rtmn_diff = ev_rt_now - mn_now;
533
534 if (pipe (sigpipe))
535 return 0;
536 774
537 if (methods == EVMETHOD_AUTO) 775 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS")) 776 if (!enable_secure () && getenv ("LIBEV_METHODS"))
539 methods = atoi (getenv ("LIBEV_METHODS")); 777 methods = atoi (getenv ("LIBEV_METHODS"));
540 else 778 else
541 methods = EVMETHOD_ANY; 779 methods = EVMETHOD_ANY;
542 780
543 ev_method = 0; 781 method = 0;
782#if EV_USE_KQUEUE
783 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
784#endif
544#if EV_USE_EPOLL 785#if EV_USE_EPOLL
545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 786 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
546#endif 787#endif
547#if EV_USE_POLL 788#if EV_USE_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 789 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
549#endif 790#endif
550#if EV_USE_SELECT 791#if EV_USE_SELECT
551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); 792 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
552#endif 793#endif
553 794
795 ev_init (&sigev, sigcb);
796 ev_set_priority (&sigev, EV_MAXPRI);
797 }
798}
799
800void
801loop_destroy (EV_P)
802{
803 int i;
804
805#if EV_USE_KQUEUE
806 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
807#endif
808#if EV_USE_EPOLL
809 if (method == EVMETHOD_EPOLL ) epoll_destroy (EV_A);
810#endif
811#if EV_USE_POLL
812 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
813#endif
814#if EV_USE_SELECT
815 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
816#endif
817
818 for (i = NUMPRI; i--; )
819 array_free (pending, [i]);
820
821 /* have to use the microsoft-never-gets-it-right macro */
822 array_free (fdchange, EMPTY);
823 array_free (timer, EMPTY);
824#if EV_PERIODICS
825 array_free (periodic, EMPTY);
826#endif
827 array_free (idle, EMPTY);
828 array_free (prepare, EMPTY);
829 array_free (check, EMPTY);
830
831 method = 0;
832}
833
834static void
835loop_fork (EV_P)
836{
837#if EV_USE_EPOLL
838 if (method == EVMETHOD_EPOLL ) epoll_fork (EV_A);
839#endif
840#if EV_USE_KQUEUE
841 if (method == EVMETHOD_KQUEUE) kqueue_fork (EV_A);
842#endif
843
844 if (ev_is_active (&sigev))
845 {
846 /* default loop */
847
848 ev_ref (EV_A);
849 ev_io_stop (EV_A_ &sigev);
850 close (sigpipe [0]);
851 close (sigpipe [1]);
852
853 while (pipe (sigpipe))
854 syserr ("(libev) error creating pipe");
855
856 siginit (EV_A);
857 }
858
859 postfork = 0;
860}
861
862#if EV_MULTIPLICITY
863struct ev_loop *
864ev_loop_new (int methods)
865{
866 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
867
868 memset (loop, 0, sizeof (struct ev_loop));
869
870 loop_init (EV_A_ methods);
871
872 if (ev_method (EV_A))
873 return loop;
874
875 return 0;
876}
877
878void
879ev_loop_destroy (EV_P)
880{
881 loop_destroy (EV_A);
882 ev_free (loop);
883}
884
885void
886ev_loop_fork (EV_P)
887{
888 postfork = 1;
889}
890
891#endif
892
893#if EV_MULTIPLICITY
894struct ev_loop *
895#else
896int
897#endif
898ev_default_loop (int methods)
899{
900 if (sigpipe [0] == sigpipe [1])
901 if (pipe (sigpipe))
902 return 0;
903
904 if (!default_loop)
905 {
906#if EV_MULTIPLICITY
907 struct ev_loop *loop = default_loop = &default_loop_struct;
908#else
909 default_loop = 1;
910#endif
911
912 loop_init (EV_A_ methods);
913
554 if (ev_method) 914 if (ev_method (EV_A))
555 { 915 {
556 ev_watcher_init (&sigev, sigcb);
557 siginit (); 916 siginit (EV_A);
558 917
918#ifndef _WIN32
559 ev_signal_init (&childev, childcb, SIGCHLD); 919 ev_signal_init (&childev, childcb, SIGCHLD);
920 ev_set_priority (&childev, EV_MAXPRI);
560 ev_signal_start (&childev); 921 ev_signal_start (EV_A_ &childev);
922 ev_unref (EV_A); /* child watcher should not keep loop alive */
923#endif
561 } 924 }
925 else
926 default_loop = 0;
562 } 927 }
563 928
564 return ev_method; 929 return default_loop;
930}
931
932void
933ev_default_destroy (void)
934{
935#if EV_MULTIPLICITY
936 struct ev_loop *loop = default_loop;
937#endif
938
939#ifndef _WIN32
940 ev_ref (EV_A); /* child watcher */
941 ev_signal_stop (EV_A_ &childev);
942#endif
943
944 ev_ref (EV_A); /* signal watcher */
945 ev_io_stop (EV_A_ &sigev);
946
947 close (sigpipe [0]); sigpipe [0] = 0;
948 close (sigpipe [1]); sigpipe [1] = 0;
949
950 loop_destroy (EV_A);
951}
952
953void
954ev_default_fork (void)
955{
956#if EV_MULTIPLICITY
957 struct ev_loop *loop = default_loop;
958#endif
959
960 if (method)
961 postfork = 1;
565} 962}
566 963
567/*****************************************************************************/ 964/*****************************************************************************/
568 965
569void
570ev_fork_prepare (void)
571{
572 /* nop */
573}
574
575void
576ev_fork_parent (void)
577{
578 /* nop */
579}
580
581void
582ev_fork_child (void)
583{
584#if EV_USE_EPOLL
585 if (ev_method == EVMETHOD_EPOLL)
586 epoll_postfork_child ();
587#endif
588
589 ev_io_stop (&sigev);
590 close (sigpipe [0]);
591 close (sigpipe [1]);
592 pipe (sigpipe);
593 siginit ();
594}
595
596/*****************************************************************************/
597
598static void 966static int
967any_pending (EV_P)
968{
969 int pri;
970
971 for (pri = NUMPRI; pri--; )
972 if (pendingcnt [pri])
973 return 1;
974
975 return 0;
976}
977
978static void
599call_pending (void) 979call_pending (EV_P)
600{ 980{
601 int pri; 981 int pri;
602 982
603 for (pri = NUMPRI; pri--; ) 983 for (pri = NUMPRI; pri--; )
604 while (pendingcnt [pri]) 984 while (pendingcnt [pri])
606 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 986 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
607 987
608 if (p->w) 988 if (p->w)
609 { 989 {
610 p->w->pending = 0; 990 p->w->pending = 0;
611 p->w->cb (p->w, p->events); 991 EV_CB_INVOKE (p->w, p->events);
612 } 992 }
613 } 993 }
614} 994}
615 995
616static void 996static void
617timers_reify (void) 997timers_reify (EV_P)
618{ 998{
619 while (timercnt && timers [0]->at <= now) 999 while (timercnt && ((WT)timers [0])->at <= mn_now)
620 { 1000 {
621 struct ev_timer *w = timers [0]; 1001 struct ev_timer *w = timers [0];
1002
1003 assert (("inactive timer on timer heap detected", ev_is_active (w)));
622 1004
623 /* first reschedule or stop timer */ 1005 /* first reschedule or stop timer */
624 if (w->repeat) 1006 if (w->repeat)
625 { 1007 {
626 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 1008 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
1009
627 w->at = now + w->repeat; 1010 ((WT)w)->at += w->repeat;
1011 if (((WT)w)->at < mn_now)
1012 ((WT)w)->at = mn_now;
1013
628 downheap ((WT *)timers, timercnt, 0); 1014 downheap ((WT *)timers, timercnt, 0);
629 } 1015 }
630 else 1016 else
631 ev_timer_stop (w); /* nonrepeating: stop timer */ 1017 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
632 1018
633 event ((W)w, EV_TIMEOUT); 1019 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
634 } 1020 }
635} 1021}
636 1022
1023#if EV_PERIODICS
637static void 1024static void
638periodics_reify (void) 1025periodics_reify (EV_P)
639{ 1026{
640 while (periodiccnt && periodics [0]->at <= ev_now) 1027 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
641 { 1028 {
642 struct ev_periodic *w = periodics [0]; 1029 struct ev_periodic *w = periodics [0];
643 1030
1031 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
1032
644 /* first reschedule or stop timer */ 1033 /* first reschedule or stop timer */
645 if (w->interval) 1034 if (w->reschedule_cb)
646 { 1035 {
1036 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, ev_rt_now + 0.0001);
1037
1038 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > ev_rt_now));
1039 downheap ((WT *)periodics, periodiccnt, 0);
1040 }
1041 else if (w->interval)
1042 {
647 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 1043 ((WT)w)->at += floor ((ev_rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
648 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); 1044 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > ev_rt_now));
649 downheap ((WT *)periodics, periodiccnt, 0); 1045 downheap ((WT *)periodics, periodiccnt, 0);
650 } 1046 }
651 else 1047 else
652 ev_periodic_stop (w); /* nonrepeating: stop timer */ 1048 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
653 1049
654 event ((W)w, EV_PERIODIC); 1050 ev_feed_event (EV_A_ (W)w, EV_PERIODIC);
655 } 1051 }
656} 1052}
657 1053
658static void 1054static void
659periodics_reschedule (ev_tstamp diff) 1055periodics_reschedule (EV_P)
660{ 1056{
661 int i; 1057 int i;
662 1058
663 /* adjust periodics after time jump */ 1059 /* adjust periodics after time jump */
664 for (i = 0; i < periodiccnt; ++i) 1060 for (i = 0; i < periodiccnt; ++i)
665 { 1061 {
666 struct ev_periodic *w = periodics [i]; 1062 struct ev_periodic *w = periodics [i];
667 1063
1064 if (w->reschedule_cb)
1065 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
668 if (w->interval) 1066 else if (w->interval)
669 {
670 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 1067 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
671
672 if (fabs (diff) >= 1e-4)
673 {
674 ev_periodic_stop (w);
675 ev_periodic_start (w);
676
677 i = 0; /* restart loop, inefficient, but time jumps should be rare */
678 }
679 }
680 } 1068 }
681}
682 1069
683static int 1070 /* now rebuild the heap */
1071 for (i = periodiccnt >> 1; i--; )
1072 downheap ((WT *)periodics, periodiccnt, i);
1073}
1074#endif
1075
1076inline int
684time_update_monotonic (void) 1077time_update_monotonic (EV_P)
685{ 1078{
686 now = get_clock (); 1079 mn_now = get_clock ();
687 1080
688 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) 1081 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
689 { 1082 {
690 ev_now = now + diff; 1083 ev_rt_now = rtmn_diff + mn_now;
691 return 0; 1084 return 0;
692 } 1085 }
693 else 1086 else
694 { 1087 {
695 now_floor = now; 1088 now_floor = mn_now;
696 ev_now = ev_time (); 1089 ev_rt_now = ev_time ();
697 return 1; 1090 return 1;
698 } 1091 }
699} 1092}
700 1093
701static void 1094static void
702time_update (void) 1095time_update (EV_P)
703{ 1096{
704 int i; 1097 int i;
705 1098
706#if EV_USE_MONOTONIC 1099#if EV_USE_MONOTONIC
707 if (expect_true (have_monotonic)) 1100 if (expect_true (have_monotonic))
708 { 1101 {
709 if (time_update_monotonic ()) 1102 if (time_update_monotonic (EV_A))
710 { 1103 {
711 ev_tstamp odiff = diff; 1104 ev_tstamp odiff = rtmn_diff;
712 1105
713 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 1106 for (i = 4; --i; ) /* loop a few times, before making important decisions */
714 { 1107 {
715 diff = ev_now - now; 1108 rtmn_diff = ev_rt_now - mn_now;
716 1109
717 if (fabs (odiff - diff) < MIN_TIMEJUMP) 1110 if (fabs (odiff - rtmn_diff) < MIN_TIMEJUMP)
718 return; /* all is well */ 1111 return; /* all is well */
719 1112
720 ev_now = ev_time (); 1113 ev_rt_now = ev_time ();
721 now = get_clock (); 1114 mn_now = get_clock ();
722 now_floor = now; 1115 now_floor = mn_now;
723 } 1116 }
724 1117
1118# if EV_PERIODICS
725 periodics_reschedule (diff - odiff); 1119 periodics_reschedule (EV_A);
1120# endif
726 /* no timer adjustment, as the monotonic clock doesn't jump */ 1121 /* no timer adjustment, as the monotonic clock doesn't jump */
1122 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
727 } 1123 }
728 } 1124 }
729 else 1125 else
730#endif 1126#endif
731 { 1127 {
732 ev_now = ev_time (); 1128 ev_rt_now = ev_time ();
733 1129
734 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1130 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
735 { 1131 {
1132#if EV_PERIODICS
736 periodics_reschedule (ev_now - now); 1133 periodics_reschedule (EV_A);
1134#endif
737 1135
738 /* adjust timers. this is easy, as the offset is the same for all */ 1136 /* adjust timers. this is easy, as the offset is the same for all */
739 for (i = 0; i < timercnt; ++i) 1137 for (i = 0; i < timercnt; ++i)
740 timers [i]->at += diff; 1138 ((WT)timers [i])->at += ev_rt_now - mn_now;
741 } 1139 }
742 1140
743 now = ev_now; 1141 mn_now = ev_rt_now;
744 } 1142 }
745} 1143}
746 1144
747int ev_loop_done; 1145void
1146ev_ref (EV_P)
1147{
1148 ++activecnt;
1149}
748 1150
1151void
1152ev_unref (EV_P)
1153{
1154 --activecnt;
1155}
1156
1157static int loop_done;
1158
1159void
749void ev_loop (int flags) 1160ev_loop (EV_P_ int flags)
750{ 1161{
751 double block; 1162 double block;
752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 1163 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
753 1164
754 do 1165 do
755 { 1166 {
756 /* queue check watchers (and execute them) */ 1167 /* queue check watchers (and execute them) */
757 if (expect_false (preparecnt)) 1168 if (expect_false (preparecnt))
758 { 1169 {
759 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 1170 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
760 call_pending (); 1171 call_pending (EV_A);
761 } 1172 }
762 1173
1174 /* we might have forked, so reify kernel state if necessary */
1175 if (expect_false (postfork))
1176 loop_fork (EV_A);
1177
763 /* update fd-related kernel structures */ 1178 /* update fd-related kernel structures */
764 fd_reify (); 1179 fd_reify (EV_A);
765 1180
766 /* calculate blocking time */ 1181 /* calculate blocking time */
767 1182
768 /* we only need this for !monotonic clockor timers, but as we basically 1183 /* we only need this for !monotonic clock or timers, but as we basically
769 always have timers, we just calculate it always */ 1184 always have timers, we just calculate it always */
770#if EV_USE_MONOTONIC 1185#if EV_USE_MONOTONIC
771 if (expect_true (have_monotonic)) 1186 if (expect_true (have_monotonic))
772 time_update_monotonic (); 1187 time_update_monotonic (EV_A);
773 else 1188 else
774#endif 1189#endif
775 { 1190 {
776 ev_now = ev_time (); 1191 ev_rt_now = ev_time ();
777 now = ev_now; 1192 mn_now = ev_rt_now;
778 } 1193 }
779 1194
780 if (flags & EVLOOP_NONBLOCK || idlecnt) 1195 if (flags & EVLOOP_NONBLOCK || idlecnt)
781 block = 0.; 1196 block = 0.;
782 else 1197 else
783 { 1198 {
784 block = MAX_BLOCKTIME; 1199 block = MAX_BLOCKTIME;
785 1200
786 if (timercnt) 1201 if (timercnt)
787 { 1202 {
788 ev_tstamp to = timers [0]->at - now + method_fudge; 1203 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
789 if (block > to) block = to; 1204 if (block > to) block = to;
790 } 1205 }
791 1206
1207#if EV_PERIODICS
792 if (periodiccnt) 1208 if (periodiccnt)
793 { 1209 {
794 ev_tstamp to = periodics [0]->at - ev_now + method_fudge; 1210 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
795 if (block > to) block = to; 1211 if (block > to) block = to;
796 } 1212 }
1213#endif
797 1214
798 if (block < 0.) block = 0.; 1215 if (block < 0.) block = 0.;
799 } 1216 }
800 1217
801 method_poll (block); 1218 method_poll (EV_A_ block);
802 1219
803 /* update ev_now, do magic */ 1220 /* update ev_rt_now, do magic */
804 time_update (); 1221 time_update (EV_A);
805 1222
806 /* queue pending timers and reschedule them */ 1223 /* queue pending timers and reschedule them */
807 timers_reify (); /* relative timers called last */ 1224 timers_reify (EV_A); /* relative timers called last */
1225#if EV_PERIODICS
808 periodics_reify (); /* absolute timers called first */ 1226 periodics_reify (EV_A); /* absolute timers called first */
1227#endif
809 1228
810 /* queue idle watchers unless io or timers are pending */ 1229 /* queue idle watchers unless io or timers are pending */
811 if (!pendingcnt) 1230 if (idlecnt && !any_pending (EV_A))
812 queue_events ((W *)idles, idlecnt, EV_IDLE); 1231 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
813 1232
814 /* queue check watchers, to be executed first */ 1233 /* queue check watchers, to be executed first */
815 if (checkcnt) 1234 if (checkcnt)
816 queue_events ((W *)checks, checkcnt, EV_CHECK); 1235 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
817 1236
818 call_pending (); 1237 call_pending (EV_A);
819 } 1238 }
820 while (!ev_loop_done); 1239 while (activecnt && !loop_done);
821 1240
822 if (ev_loop_done != 2) 1241 if (loop_done != 2)
823 ev_loop_done = 0; 1242 loop_done = 0;
1243}
1244
1245void
1246ev_unloop (EV_P_ int how)
1247{
1248 loop_done = how;
824} 1249}
825 1250
826/*****************************************************************************/ 1251/*****************************************************************************/
827 1252
828static void 1253inline void
829wlist_add (WL *head, WL elem) 1254wlist_add (WL *head, WL elem)
830{ 1255{
831 elem->next = *head; 1256 elem->next = *head;
832 *head = elem; 1257 *head = elem;
833} 1258}
834 1259
835static void 1260inline void
836wlist_del (WL *head, WL elem) 1261wlist_del (WL *head, WL elem)
837{ 1262{
838 while (*head) 1263 while (*head)
839 { 1264 {
840 if (*head == elem) 1265 if (*head == elem)
845 1270
846 head = &(*head)->next; 1271 head = &(*head)->next;
847 } 1272 }
848} 1273}
849 1274
850static void 1275inline void
851ev_clear_pending (W w) 1276ev_clear_pending (EV_P_ W w)
852{ 1277{
853 if (w->pending) 1278 if (w->pending)
854 { 1279 {
855 pendings [ABSPRI (w)][w->pending - 1].w = 0; 1280 pendings [ABSPRI (w)][w->pending - 1].w = 0;
856 w->pending = 0; 1281 w->pending = 0;
857 } 1282 }
858} 1283}
859 1284
860static void 1285inline void
861ev_start (W w, int active) 1286ev_start (EV_P_ W w, int active)
862{ 1287{
863 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI; 1288 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
864 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI; 1289 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
865 1290
866 w->active = active; 1291 w->active = active;
1292 ev_ref (EV_A);
867} 1293}
868 1294
869static void 1295inline void
870ev_stop (W w) 1296ev_stop (EV_P_ W w)
871{ 1297{
1298 ev_unref (EV_A);
872 w->active = 0; 1299 w->active = 0;
873} 1300}
874 1301
875/*****************************************************************************/ 1302/*****************************************************************************/
876 1303
877void 1304void
878ev_io_start (struct ev_io *w) 1305ev_io_start (EV_P_ struct ev_io *w)
879{ 1306{
880 int fd = w->fd; 1307 int fd = w->fd;
881 1308
882 if (ev_is_active (w)) 1309 if (ev_is_active (w))
883 return; 1310 return;
884 1311
885 assert (("ev_io_start called with negative fd", fd >= 0)); 1312 assert (("ev_io_start called with negative fd", fd >= 0));
886 1313
887 ev_start ((W)w, 1); 1314 ev_start (EV_A_ (W)w, 1);
888 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1315 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
889 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1316 wlist_add ((WL *)&anfds[fd].head, (WL)w);
890 1317
891 fd_change (fd); 1318 fd_change (EV_A_ fd);
892} 1319}
893 1320
894void 1321void
895ev_io_stop (struct ev_io *w) 1322ev_io_stop (EV_P_ struct ev_io *w)
896{ 1323{
897 ev_clear_pending ((W)w); 1324 ev_clear_pending (EV_A_ (W)w);
898 if (!ev_is_active (w)) 1325 if (!ev_is_active (w))
899 return; 1326 return;
900 1327
1328 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1329
901 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1330 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
902 ev_stop ((W)w); 1331 ev_stop (EV_A_ (W)w);
903 1332
904 fd_change (w->fd); 1333 fd_change (EV_A_ w->fd);
905} 1334}
906 1335
907void 1336void
908ev_timer_start (struct ev_timer *w) 1337ev_timer_start (EV_P_ struct ev_timer *w)
909{ 1338{
910 if (ev_is_active (w)) 1339 if (ev_is_active (w))
911 return; 1340 return;
912 1341
913 w->at += now; 1342 ((WT)w)->at += mn_now;
914 1343
915 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1344 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
916 1345
917 ev_start ((W)w, ++timercnt); 1346 ev_start (EV_A_ (W)w, ++timercnt);
918 array_needsize (timers, timermax, timercnt, ); 1347 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
919 timers [timercnt - 1] = w; 1348 timers [timercnt - 1] = w;
920 upheap ((WT *)timers, timercnt - 1); 1349 upheap ((WT *)timers, timercnt - 1);
921}
922 1350
1351 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1352}
1353
923void 1354void
924ev_timer_stop (struct ev_timer *w) 1355ev_timer_stop (EV_P_ struct ev_timer *w)
925{ 1356{
926 ev_clear_pending ((W)w); 1357 ev_clear_pending (EV_A_ (W)w);
927 if (!ev_is_active (w)) 1358 if (!ev_is_active (w))
928 return; 1359 return;
929 1360
1361 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1362
930 if (w->active < timercnt--) 1363 if (((W)w)->active < timercnt--)
931 { 1364 {
932 timers [w->active - 1] = timers [timercnt]; 1365 timers [((W)w)->active - 1] = timers [timercnt];
933 downheap ((WT *)timers, timercnt, w->active - 1); 1366 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
934 } 1367 }
935 1368
936 w->at = w->repeat; 1369 ((WT)w)->at -= mn_now;
937 1370
938 ev_stop ((W)w); 1371 ev_stop (EV_A_ (W)w);
939} 1372}
940 1373
941void 1374void
942ev_timer_again (struct ev_timer *w) 1375ev_timer_again (EV_P_ struct ev_timer *w)
943{ 1376{
944 if (ev_is_active (w)) 1377 if (ev_is_active (w))
945 { 1378 {
946 if (w->repeat) 1379 if (w->repeat)
947 { 1380 {
948 w->at = now + w->repeat; 1381 ((WT)w)->at = mn_now + w->repeat;
949 downheap ((WT *)timers, timercnt, w->active - 1); 1382 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
950 } 1383 }
951 else 1384 else
952 ev_timer_stop (w); 1385 ev_timer_stop (EV_A_ w);
953 } 1386 }
954 else if (w->repeat) 1387 else if (w->repeat)
955 ev_timer_start (w); 1388 ev_timer_start (EV_A_ w);
956} 1389}
957 1390
1391#if EV_PERIODICS
958void 1392void
959ev_periodic_start (struct ev_periodic *w) 1393ev_periodic_start (EV_P_ struct ev_periodic *w)
960{ 1394{
961 if (ev_is_active (w)) 1395 if (ev_is_active (w))
962 return; 1396 return;
963 1397
1398 if (w->reschedule_cb)
1399 ((WT)w)->at = w->reschedule_cb (w, ev_rt_now);
1400 else if (w->interval)
1401 {
964 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1402 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
965
966 /* this formula differs from the one in periodic_reify because we do not always round up */ 1403 /* this formula differs from the one in periodic_reify because we do not always round up */
967 if (w->interval)
968 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1404 ((WT)w)->at += ceil ((ev_rt_now - ((WT)w)->at) / w->interval) * w->interval;
1405 }
969 1406
970 ev_start ((W)w, ++periodiccnt); 1407 ev_start (EV_A_ (W)w, ++periodiccnt);
971 array_needsize (periodics, periodicmax, periodiccnt, ); 1408 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
972 periodics [periodiccnt - 1] = w; 1409 periodics [periodiccnt - 1] = w;
973 upheap ((WT *)periodics, periodiccnt - 1); 1410 upheap ((WT *)periodics, periodiccnt - 1);
974}
975 1411
1412 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1413}
1414
976void 1415void
977ev_periodic_stop (struct ev_periodic *w) 1416ev_periodic_stop (EV_P_ struct ev_periodic *w)
978{ 1417{
979 ev_clear_pending ((W)w); 1418 ev_clear_pending (EV_A_ (W)w);
980 if (!ev_is_active (w)) 1419 if (!ev_is_active (w))
981 return; 1420 return;
982 1421
1422 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1423
983 if (w->active < periodiccnt--) 1424 if (((W)w)->active < periodiccnt--)
984 { 1425 {
985 periodics [w->active - 1] = periodics [periodiccnt]; 1426 periodics [((W)w)->active - 1] = periodics [periodiccnt];
986 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1427 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
987 } 1428 }
988 1429
989 ev_stop ((W)w); 1430 ev_stop (EV_A_ (W)w);
990} 1431}
991 1432
992void 1433void
1434ev_periodic_again (EV_P_ struct ev_periodic *w)
1435{
1436 /* TODO: use adjustheap and recalculation */
1437 ev_periodic_stop (EV_A_ w);
1438 ev_periodic_start (EV_A_ w);
1439}
1440#endif
1441
1442void
993ev_signal_start (struct ev_signal *w) 1443ev_idle_start (EV_P_ struct ev_idle *w)
994{ 1444{
995 if (ev_is_active (w)) 1445 if (ev_is_active (w))
996 return; 1446 return;
997 1447
1448 ev_start (EV_A_ (W)w, ++idlecnt);
1449 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1450 idles [idlecnt - 1] = w;
1451}
1452
1453void
1454ev_idle_stop (EV_P_ struct ev_idle *w)
1455{
1456 ev_clear_pending (EV_A_ (W)w);
1457 if (!ev_is_active (w))
1458 return;
1459
1460 idles [((W)w)->active - 1] = idles [--idlecnt];
1461 ev_stop (EV_A_ (W)w);
1462}
1463
1464void
1465ev_prepare_start (EV_P_ struct ev_prepare *w)
1466{
1467 if (ev_is_active (w))
1468 return;
1469
1470 ev_start (EV_A_ (W)w, ++preparecnt);
1471 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1472 prepares [preparecnt - 1] = w;
1473}
1474
1475void
1476ev_prepare_stop (EV_P_ struct ev_prepare *w)
1477{
1478 ev_clear_pending (EV_A_ (W)w);
1479 if (!ev_is_active (w))
1480 return;
1481
1482 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1483 ev_stop (EV_A_ (W)w);
1484}
1485
1486void
1487ev_check_start (EV_P_ struct ev_check *w)
1488{
1489 if (ev_is_active (w))
1490 return;
1491
1492 ev_start (EV_A_ (W)w, ++checkcnt);
1493 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1494 checks [checkcnt - 1] = w;
1495}
1496
1497void
1498ev_check_stop (EV_P_ struct ev_check *w)
1499{
1500 ev_clear_pending (EV_A_ (W)w);
1501 if (!ev_is_active (w))
1502 return;
1503
1504 checks [((W)w)->active - 1] = checks [--checkcnt];
1505 ev_stop (EV_A_ (W)w);
1506}
1507
1508#ifndef SA_RESTART
1509# define SA_RESTART 0
1510#endif
1511
1512void
1513ev_signal_start (EV_P_ struct ev_signal *w)
1514{
1515#if EV_MULTIPLICITY
1516 assert (("signal watchers are only supported in the default loop", loop == default_loop));
1517#endif
1518 if (ev_is_active (w))
1519 return;
1520
998 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1521 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
999 1522
1000 ev_start ((W)w, 1); 1523 ev_start (EV_A_ (W)w, 1);
1001 array_needsize (signals, signalmax, w->signum, signals_init); 1524 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1002 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1525 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1003 1526
1004 if (!w->next) 1527 if (!((WL)w)->next)
1005 { 1528 {
1529#if _WIN32
1530 signal (w->signum, sighandler);
1531#else
1006 struct sigaction sa; 1532 struct sigaction sa;
1007 sa.sa_handler = sighandler; 1533 sa.sa_handler = sighandler;
1008 sigfillset (&sa.sa_mask); 1534 sigfillset (&sa.sa_mask);
1009 sa.sa_flags = 0; 1535 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1010 sigaction (w->signum, &sa, 0); 1536 sigaction (w->signum, &sa, 0);
1537#endif
1011 } 1538 }
1012} 1539}
1013 1540
1014void 1541void
1015ev_signal_stop (struct ev_signal *w) 1542ev_signal_stop (EV_P_ struct ev_signal *w)
1016{ 1543{
1017 ev_clear_pending ((W)w); 1544 ev_clear_pending (EV_A_ (W)w);
1018 if (!ev_is_active (w)) 1545 if (!ev_is_active (w))
1019 return; 1546 return;
1020 1547
1021 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1548 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1022 ev_stop ((W)w); 1549 ev_stop (EV_A_ (W)w);
1023 1550
1024 if (!signals [w->signum - 1].head) 1551 if (!signals [w->signum - 1].head)
1025 signal (w->signum, SIG_DFL); 1552 signal (w->signum, SIG_DFL);
1026} 1553}
1027 1554
1028void 1555void
1029ev_idle_start (struct ev_idle *w) 1556ev_child_start (EV_P_ struct ev_child *w)
1030{ 1557{
1558#if EV_MULTIPLICITY
1559 assert (("child watchers are only supported in the default loop", loop == default_loop));
1560#endif
1031 if (ev_is_active (w)) 1561 if (ev_is_active (w))
1032 return; 1562 return;
1033 1563
1034 ev_start ((W)w, ++idlecnt);
1035 array_needsize (idles, idlemax, idlecnt, );
1036 idles [idlecnt - 1] = w;
1037}
1038
1039void
1040ev_idle_stop (struct ev_idle *w)
1041{
1042 ev_clear_pending ((W)w);
1043 if (ev_is_active (w))
1044 return;
1045
1046 idles [w->active - 1] = idles [--idlecnt];
1047 ev_stop ((W)w);
1048}
1049
1050void
1051ev_prepare_start (struct ev_prepare *w)
1052{
1053 if (ev_is_active (w))
1054 return;
1055
1056 ev_start ((W)w, ++preparecnt);
1057 array_needsize (prepares, preparemax, preparecnt, );
1058 prepares [preparecnt - 1] = w;
1059}
1060
1061void
1062ev_prepare_stop (struct ev_prepare *w)
1063{
1064 ev_clear_pending ((W)w);
1065 if (ev_is_active (w))
1066 return;
1067
1068 prepares [w->active - 1] = prepares [--preparecnt];
1069 ev_stop ((W)w);
1070}
1071
1072void
1073ev_check_start (struct ev_check *w)
1074{
1075 if (ev_is_active (w))
1076 return;
1077
1078 ev_start ((W)w, ++checkcnt);
1079 array_needsize (checks, checkmax, checkcnt, );
1080 checks [checkcnt - 1] = w;
1081}
1082
1083void
1084ev_check_stop (struct ev_check *w)
1085{
1086 ev_clear_pending ((W)w);
1087 if (ev_is_active (w))
1088 return;
1089
1090 checks [w->active - 1] = checks [--checkcnt];
1091 ev_stop ((W)w);
1092}
1093
1094void
1095ev_child_start (struct ev_child *w)
1096{
1097 if (ev_is_active (w))
1098 return;
1099
1100 ev_start ((W)w, 1); 1564 ev_start (EV_A_ (W)w, 1);
1101 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1565 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1102} 1566}
1103 1567
1104void 1568void
1105ev_child_stop (struct ev_child *w) 1569ev_child_stop (EV_P_ struct ev_child *w)
1106{ 1570{
1107 ev_clear_pending ((W)w); 1571 ev_clear_pending (EV_A_ (W)w);
1108 if (ev_is_active (w)) 1572 if (!ev_is_active (w))
1109 return; 1573 return;
1110 1574
1111 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1575 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1112 ev_stop ((W)w); 1576 ev_stop (EV_A_ (W)w);
1113} 1577}
1114 1578
1115/*****************************************************************************/ 1579/*****************************************************************************/
1116 1580
1117struct ev_once 1581struct ev_once
1121 void (*cb)(int revents, void *arg); 1585 void (*cb)(int revents, void *arg);
1122 void *arg; 1586 void *arg;
1123}; 1587};
1124 1588
1125static void 1589static void
1126once_cb (struct ev_once *once, int revents) 1590once_cb (EV_P_ struct ev_once *once, int revents)
1127{ 1591{
1128 void (*cb)(int revents, void *arg) = once->cb; 1592 void (*cb)(int revents, void *arg) = once->cb;
1129 void *arg = once->arg; 1593 void *arg = once->arg;
1130 1594
1131 ev_io_stop (&once->io); 1595 ev_io_stop (EV_A_ &once->io);
1132 ev_timer_stop (&once->to); 1596 ev_timer_stop (EV_A_ &once->to);
1133 free (once); 1597 ev_free (once);
1134 1598
1135 cb (revents, arg); 1599 cb (revents, arg);
1136} 1600}
1137 1601
1138static void 1602static void
1139once_cb_io (struct ev_io *w, int revents) 1603once_cb_io (EV_P_ struct ev_io *w, int revents)
1140{ 1604{
1141 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1605 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1142} 1606}
1143 1607
1144static void 1608static void
1145once_cb_to (struct ev_timer *w, int revents) 1609once_cb_to (EV_P_ struct ev_timer *w, int revents)
1146{ 1610{
1147 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1611 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1148} 1612}
1149 1613
1150void 1614void
1151ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1615ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1152{ 1616{
1153 struct ev_once *once = malloc (sizeof (struct ev_once)); 1617 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1154 1618
1155 if (!once) 1619 if (!once)
1156 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1620 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1157 else 1621 else
1158 { 1622 {
1159 once->cb = cb; 1623 once->cb = cb;
1160 once->arg = arg; 1624 once->arg = arg;
1161 1625
1162 ev_watcher_init (&once->io, once_cb_io); 1626 ev_init (&once->io, once_cb_io);
1163 if (fd >= 0) 1627 if (fd >= 0)
1164 { 1628 {
1165 ev_io_set (&once->io, fd, events); 1629 ev_io_set (&once->io, fd, events);
1166 ev_io_start (&once->io); 1630 ev_io_start (EV_A_ &once->io);
1167 } 1631 }
1168 1632
1169 ev_watcher_init (&once->to, once_cb_to); 1633 ev_init (&once->to, once_cb_to);
1170 if (timeout >= 0.) 1634 if (timeout >= 0.)
1171 { 1635 {
1172 ev_timer_set (&once->to, timeout, 0.); 1636 ev_timer_set (&once->to, timeout, 0.);
1173 ev_timer_start (&once->to); 1637 ev_timer_start (EV_A_ &once->to);
1174 } 1638 }
1175 } 1639 }
1176} 1640}
1177 1641
1178/*****************************************************************************/ 1642#ifdef __cplusplus
1179
1180#if 0
1181
1182struct ev_io wio;
1183
1184static void
1185sin_cb (struct ev_io *w, int revents)
1186{
1187 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
1188} 1643}
1189
1190static void
1191ocb (struct ev_timer *w, int revents)
1192{
1193 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
1194 ev_timer_stop (w);
1195 ev_timer_start (w);
1196}
1197
1198static void
1199scb (struct ev_signal *w, int revents)
1200{
1201 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1202 ev_io_stop (&wio);
1203 ev_io_start (&wio);
1204}
1205
1206static void
1207gcb (struct ev_signal *w, int revents)
1208{
1209 fprintf (stderr, "generic %x\n", revents);
1210
1211}
1212
1213int main (void)
1214{
1215 ev_init (0);
1216
1217 ev_io_init (&wio, sin_cb, 0, EV_READ);
1218 ev_io_start (&wio);
1219
1220 struct ev_timer t[10000];
1221
1222#if 0
1223 int i;
1224 for (i = 0; i < 10000; ++i)
1225 {
1226 struct ev_timer *w = t + i;
1227 ev_watcher_init (w, ocb, i);
1228 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
1229 ev_timer_start (w);
1230 if (drand48 () < 0.5)
1231 ev_timer_stop (w);
1232 }
1233#endif 1644#endif
1234 1645
1235 struct ev_timer t1;
1236 ev_timer_init (&t1, ocb, 5, 10);
1237 ev_timer_start (&t1);
1238
1239 struct ev_signal sig;
1240 ev_signal_init (&sig, scb, SIGQUIT);
1241 ev_signal_start (&sig);
1242
1243 struct ev_check cw;
1244 ev_check_init (&cw, gcb);
1245 ev_check_start (&cw);
1246
1247 struct ev_idle iw;
1248 ev_idle_init (&iw, gcb);
1249 ev_idle_start (&iw);
1250
1251 ev_loop (0);
1252
1253 return 0;
1254}
1255
1256#endif
1257
1258
1259
1260

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines