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

Comparing libev/ev.c (file contents):
Revision 1.7 by root, Wed Oct 31 00:24:16 2007 UTC vs.
Revision 1.48 by root, Sat Nov 3 12:19:31 2007 UTC

1/*
2 * libev event processing core, watcher management
3 *
4 * Copyright (c) 2007 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer in the documentation and/or other materials provided
17 * with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
30 */
31#if EV_USE_CONFIG_H
32# include "config.h"
33#endif
34
1#include <math.h> 35#include <math.h>
2#include <stdlib.h> 36#include <stdlib.h>
3#include <unistd.h> 37#include <unistd.h>
4#include <fcntl.h> 38#include <fcntl.h>
5#include <signal.h> 39#include <signal.h>
40#include <stddef.h>
6 41
7#include <stdio.h> 42#include <stdio.h>
8 43
9#include <assert.h> 44#include <assert.h>
10#include <errno.h> 45#include <errno.h>
46#include <sys/types.h>
47#ifndef WIN32
48# include <sys/wait.h>
49#endif
11#include <sys/time.h> 50#include <sys/time.h>
12#include <time.h> 51#include <time.h>
13 52
53/**/
54
55#ifndef EV_USE_MONOTONIC
56# define EV_USE_MONOTONIC 1
57#endif
58
59#ifndef EV_USE_SELECT
60# define EV_USE_SELECT 1
61#endif
62
63#ifndef EV_USE_POLL
64# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */
65#endif
66
67#ifndef EV_USE_EPOLL
68# define EV_USE_EPOLL 0
69#endif
70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
73#endif
74
75#ifndef EV_USE_REALTIME
76# define EV_USE_REALTIME 1
77#endif
78
79/**/
80
14#ifdef CLOCK_MONOTONIC 81#ifndef CLOCK_MONOTONIC
82# undef EV_USE_MONOTONIC
15# define HAVE_MONOTONIC 1 83# define EV_USE_MONOTONIC 0
16#endif 84#endif
17 85
86#ifndef CLOCK_REALTIME
87# undef EV_USE_REALTIME
18#define HAVE_REALTIME 1 88# define EV_USE_REALTIME 0
19#define HAVE_EPOLL 1 89#endif
20#define HAVE_SELECT 1 90
91/**/
21 92
22#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */ 93#define MIN_TIMEJUMP 1. /* minimum timejump that gets detected (if monotonic clock available) */
23#define MAX_BLOCKTIME 60. 94#define MAX_BLOCKTIME 59.731 /* never wait longer than this time (to detect time jumps) */
95#define PID_HASHSIZE 16 /* size of pid hash table, must be power of two */
96/*#define CLEANUP_INTERVAL 300. /* how often to try to free memory and re-check fds */
24 97
25#include "ev.h" 98#include "ev.h"
26 99
100#if __GNUC__ >= 3
101# define expect(expr,value) __builtin_expect ((expr),(value))
102# define inline inline
103#else
104# define expect(expr,value) (expr)
105# define inline static
106#endif
107
108#define expect_false(expr) expect ((expr) != 0, 0)
109#define expect_true(expr) expect ((expr) != 0, 1)
110
111#define NUMPRI (EV_MAXPRI - EV_MINPRI + 1)
112#define ABSPRI(w) ((w)->priority - EV_MINPRI)
113
27struct ev_watcher { 114typedef struct ev_watcher *W;
28 EV_WATCHER (ev_watcher);
29};
30
31struct ev_watcher_list { 115typedef struct ev_watcher_list *WL;
32 EV_WATCHER_LIST (ev_watcher_list); 116typedef struct ev_watcher_time *WT;
33};
34 117
35static ev_tstamp now, diff; /* monotonic clock */ 118static ev_tstamp now_floor, now, diff; /* monotonic clock */
36ev_tstamp ev_now; 119ev_tstamp ev_now;
37int ev_method; 120int ev_method;
38 121
39static int have_monotonic; /* runtime */ 122static int have_monotonic; /* runtime */
40 123
41static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */ 124static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
42static void (*method_modify)(int fd, int oev, int nev); 125static void (*method_modify)(int fd, int oev, int nev);
43static void (*method_poll)(ev_tstamp timeout); 126static void (*method_poll)(ev_tstamp timeout);
44 127
128/*****************************************************************************/
129
45ev_tstamp 130ev_tstamp
46ev_time (void) 131ev_time (void)
47{ 132{
48#if HAVE_REALTIME 133#if EV_USE_REALTIME
49 struct timespec ts; 134 struct timespec ts;
50 clock_gettime (CLOCK_REALTIME, &ts); 135 clock_gettime (CLOCK_REALTIME, &ts);
51 return ts.tv_sec + ts.tv_nsec * 1e-9; 136 return ts.tv_sec + ts.tv_nsec * 1e-9;
52#else 137#else
53 struct timeval tv; 138 struct timeval tv;
57} 142}
58 143
59static ev_tstamp 144static ev_tstamp
60get_clock (void) 145get_clock (void)
61{ 146{
62#if HAVE_MONOTONIC 147#if EV_USE_MONOTONIC
63 if (have_monotonic) 148 if (expect_true (have_monotonic))
64 { 149 {
65 struct timespec ts; 150 struct timespec ts;
66 clock_gettime (CLOCK_MONOTONIC, &ts); 151 clock_gettime (CLOCK_MONOTONIC, &ts);
67 return ts.tv_sec + ts.tv_nsec * 1e-9; 152 return ts.tv_sec + ts.tv_nsec * 1e-9;
68 } 153 }
69#endif 154#endif
70 155
71 return ev_time (); 156 return ev_time ();
72} 157}
73 158
159#define array_roundsize(base,n) ((n) | 4 & ~3)
160
74#define array_needsize(base,cur,cnt,init) \ 161#define array_needsize(base,cur,cnt,init) \
75 if ((cnt) > cur) \ 162 if (expect_false ((cnt) > cur)) \
76 { \ 163 { \
77 int newcnt = cur ? cur << 1 : 16; \ 164 int newcnt = cur; \
78 fprintf (stderr, "resize(" # base ") from %d to %d\n", cur, newcnt);\ 165 do \
166 { \
167 newcnt = array_roundsize (base, newcnt << 1); \
168 } \
169 while ((cnt) > newcnt); \
170 \
79 base = realloc (base, sizeof (*base) * (newcnt)); \ 171 base = realloc (base, sizeof (*base) * (newcnt)); \
80 init (base + cur, newcnt - cur); \ 172 init (base + cur, newcnt - cur); \
81 cur = newcnt; \ 173 cur = newcnt; \
82 } 174 }
83 175
176/*****************************************************************************/
177
84typedef struct 178typedef struct
85{ 179{
86 struct ev_io *head; 180 struct ev_io *head;
87 unsigned char wev, rev; /* want, received event set */ 181 unsigned char events;
182 unsigned char reify;
88} ANFD; 183} ANFD;
89 184
90static ANFD *anfds; 185static ANFD *anfds;
91static int anfdmax; 186static int anfdmax;
92 187
188static void
189anfds_init (ANFD *base, int count)
190{
191 while (count--)
192 {
193 base->head = 0;
194 base->events = EV_NONE;
195 base->reify = 0;
196
197 ++base;
198 }
199}
200
201typedef struct
202{
203 W w;
204 int events;
205} ANPENDING;
206
207static ANPENDING *pendings [NUMPRI];
208static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
209
210static void
211event (W w, int events)
212{
213 if (w->pending)
214 {
215 pendings [ABSPRI (w)][w->pending - 1].events |= events;
216 return;
217 }
218
219 w->pending = ++pendingcnt [ABSPRI (w)];
220 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], );
221 pendings [ABSPRI (w)][w->pending - 1].w = w;
222 pendings [ABSPRI (w)][w->pending - 1].events = events;
223}
224
225static void
226queue_events (W *events, int eventcnt, int type)
227{
228 int i;
229
230 for (i = 0; i < eventcnt; ++i)
231 event (events [i], type);
232}
233
234static void
235fd_event (int fd, int events)
236{
237 ANFD *anfd = anfds + fd;
238 struct ev_io *w;
239
240 for (w = anfd->head; w; w = w->next)
241 {
242 int ev = w->events & events;
243
244 if (ev)
245 event ((W)w, ev);
246 }
247}
248
249/*****************************************************************************/
250
93static int *fdchanges; 251static int *fdchanges;
94static int fdchangemax, fdchangecnt; 252static int fdchangemax, fdchangecnt;
95 253
96static void 254static void
97anfds_init (ANFD *base, int count) 255fd_reify (void)
98{ 256{
99 while (count--) 257 int i;
100 { 258
101 base->head = 0; 259 for (i = 0; i < fdchangecnt; ++i)
102 base->wev = base->rev = EV_NONE;
103 ++base;
104 } 260 {
105} 261 int fd = fdchanges [i];
106
107typedef struct
108{
109 struct ev_watcher *w;
110 int events;
111} ANPENDING;
112
113static ANPENDING *pendings;
114static int pendingmax, pendingcnt;
115
116static void
117event (struct ev_watcher *w, int events)
118{
119 w->pending = ++pendingcnt;
120 array_needsize (pendings, pendingmax, pendingcnt, );
121 pendings [pendingcnt - 1].w = w;
122 pendings [pendingcnt - 1].events = events;
123}
124
125static void
126fd_event (int fd, int events)
127{
128 ANFD *anfd = anfds + fd; 262 ANFD *anfd = anfds + fd;
263 struct ev_io *w;
264
265 int events = 0;
266
267 for (w = anfd->head; w; w = w->next)
268 events |= w->events;
269
270 anfd->reify = 0;
271
272 if (anfd->events != events)
273 {
274 method_modify (fd, anfd->events, events);
275 anfd->events = events;
276 }
277 }
278
279 fdchangecnt = 0;
280}
281
282static void
283fd_change (int fd)
284{
285 if (anfds [fd].reify || fdchangecnt < 0)
286 return;
287
288 anfds [fd].reify = 1;
289
290 ++fdchangecnt;
291 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
292 fdchanges [fdchangecnt - 1] = fd;
293}
294
295static void
296fd_kill (int fd)
297{
129 struct ev_io *w; 298 struct ev_io *w;
130 299
131 for (w = anfd->head; w; w = w->next) 300 printf ("killing fd %d\n", fd);//D
301 while ((w = anfds [fd].head))
302 {
303 ev_io_stop (w);
304 event ((W)w, EV_ERROR | EV_READ | EV_WRITE);
305 }
306}
307
308/* called on EBADF to verify fds */
309static void
310fd_ebadf (void)
311{
312 int fd;
313
314 for (fd = 0; fd < anfdmax; ++fd)
315 if (anfds [fd].events)
316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
317 fd_kill (fd);
318}
319
320/* called on ENOMEM in select/poll to kill some fds and retry */
321static void
322fd_enomem (void)
323{
324 int fd = anfdmax;
325
326 while (fd--)
327 if (anfds [fd].events)
132 { 328 {
133 int ev = w->events & events; 329 close (fd);
134 330 fd_kill (fd);
135 if (ev) 331 return;
136 event ((struct ev_watcher *)w, ev);
137 } 332 }
138} 333}
139 334
335/*****************************************************************************/
336
140static struct ev_timer **atimers; 337static struct ev_timer **timers;
141static int atimermax, atimercnt; 338static int timermax, timercnt;
142 339
143static struct ev_timer **rtimers; 340static struct ev_periodic **periodics;
144static int rtimermax, rtimercnt; 341static int periodicmax, periodiccnt;
145 342
146static void 343static void
147upheap (struct ev_timer **timers, int k) 344upheap (WT *timers, int k)
148{ 345{
149 struct ev_timer *w = timers [k]; 346 WT w = timers [k];
150 347
151 while (k && timers [k >> 1]->at > w->at) 348 while (k && timers [k >> 1]->at > w->at)
152 { 349 {
153 timers [k] = timers [k >> 1]; 350 timers [k] = timers [k >> 1];
154 timers [k]->active = k + 1; 351 timers [k]->active = k + 1;
159 timers [k]->active = k + 1; 356 timers [k]->active = k + 1;
160 357
161} 358}
162 359
163static void 360static void
164downheap (struct ev_timer **timers, int N, int k) 361downheap (WT *timers, int N, int k)
165{ 362{
166 struct ev_timer *w = timers [k]; 363 WT w = timers [k];
167 364
168 while (k < (N >> 1)) 365 while (k < (N >> 1))
169 { 366 {
170 int j = k << 1; 367 int j = k << 1;
171 368
182 379
183 timers [k] = w; 380 timers [k] = w;
184 timers [k]->active = k + 1; 381 timers [k]->active = k + 1;
185} 382}
186 383
384/*****************************************************************************/
385
187typedef struct 386typedef struct
188{ 387{
189 struct ev_signal *head; 388 struct ev_signal *head;
190 sig_atomic_t gotsig; 389 sig_atomic_t volatile gotsig;
191} ANSIG; 390} ANSIG;
192 391
193static ANSIG *signals; 392static ANSIG *signals;
194static int signalmax; 393static int signalmax;
195 394
196static int sigpipe [2]; 395static int sigpipe [2];
197static sig_atomic_t gotsig; 396static sig_atomic_t volatile gotsig;
198static struct ev_io sigev; 397static struct ev_io sigev;
199 398
200static void 399static void
201signals_init (ANSIG *base, int count) 400signals_init (ANSIG *base, int count)
202{ 401{
203 while (count--) 402 while (count--)
204 { 403 {
205 base->head = 0; 404 base->head = 0;
206 base->gotsig = 0; 405 base->gotsig = 0;
406
207 ++base; 407 ++base;
208 } 408 }
209} 409}
210 410
211static void 411static void
213{ 413{
214 signals [signum - 1].gotsig = 1; 414 signals [signum - 1].gotsig = 1;
215 415
216 if (!gotsig) 416 if (!gotsig)
217 { 417 {
418 int old_errno = errno;
218 gotsig = 1; 419 gotsig = 1;
219 write (sigpipe [1], &gotsig, 1); 420 write (sigpipe [1], &signum, 1);
421 errno = old_errno;
220 } 422 }
221} 423}
222 424
223static void 425static void
224sigcb (struct ev_io *iow, int revents) 426sigcb (struct ev_io *iow, int revents)
225{ 427{
226 struct ev_signal *w; 428 struct ev_signal *w;
227 int sig; 429 int signum;
228 430
431 read (sigpipe [0], &revents, 1);
229 gotsig = 0; 432 gotsig = 0;
230 read (sigpipe [0], &revents, 1);
231 433
232 for (sig = signalmax; sig--; ) 434 for (signum = signalmax; signum--; )
233 if (signals [sig].gotsig) 435 if (signals [signum].gotsig)
234 { 436 {
235 signals [sig].gotsig = 0; 437 signals [signum].gotsig = 0;
236 438
237 for (w = signals [sig].head; w; w = w->next) 439 for (w = signals [signum].head; w; w = w->next)
238 event ((struct ev_watcher *)w, EV_SIGNAL); 440 event ((W)w, EV_SIGNAL);
239 } 441 }
240} 442}
241 443
242static void 444static void
243siginit (void) 445siginit (void)
244{ 446{
447#ifndef WIN32
245 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 448 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
246 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 449 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
247 450
248 /* rather than sort out wether we really need nb, set it */ 451 /* rather than sort out wether we really need nb, set it */
249 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 452 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
250 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 453 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
454#endif
251 455
252 evio_set (&sigev, sigpipe [0], EV_READ); 456 ev_io_set (&sigev, sigpipe [0], EV_READ);
253 evio_start (&sigev); 457 ev_io_start (&sigev);
254} 458}
255 459
460/*****************************************************************************/
461
462static struct ev_idle **idles;
463static int idlemax, idlecnt;
464
465static struct ev_prepare **prepares;
466static int preparemax, preparecnt;
467
468static struct ev_check **checks;
469static int checkmax, checkcnt;
470
471/*****************************************************************************/
472
473static struct ev_child *childs [PID_HASHSIZE];
474static struct ev_signal childev;
475
476#ifndef WIN32
477
478#ifndef WCONTINUED
479# define WCONTINUED 0
480#endif
481
482static void
483child_reap (struct ev_signal *sw, int chain, int pid, int status)
484{
485 struct ev_child *w;
486
487 for (w = childs [chain & (PID_HASHSIZE - 1)]; w; w = w->next)
488 if (w->pid == pid || !w->pid)
489 {
490 w->priority = sw->priority; /* need to do it *now* */
491 w->rpid = pid;
492 w->rstatus = status;
493 printf ("rpid %p %d %d\n", w, pid, w->pid);//D
494 event ((W)w, EV_CHILD);
495 }
496}
497
498static void
499childcb (struct ev_signal *sw, int revents)
500{
501 int pid, status;
502
503 printf ("chld %x\n", revents);//D
504 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
505 {
506 /* make sure we are called again until all childs have been reaped */
507 event ((W)sw, EV_SIGNAL);
508
509 child_reap (sw, pid, pid, status);
510 child_reap (sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
511 }
512}
513
514#endif
515
516/*****************************************************************************/
517
518#if EV_USE_KQUEUE
519# include "ev_kqueue.c"
520#endif
256#if HAVE_EPOLL 521#if EV_USE_EPOLL
257# include "ev_epoll.c" 522# include "ev_epoll.c"
258#endif 523#endif
524#if EV_USE_POLL
525# include "ev_poll.c"
526#endif
259#if HAVE_SELECT 527#if EV_USE_SELECT
260# include "ev_select.c" 528# include "ev_select.c"
261#endif 529#endif
262 530
531int
532ev_version_major (void)
533{
534 return EV_VERSION_MAJOR;
535}
536
537int
538ev_version_minor (void)
539{
540 return EV_VERSION_MINOR;
541}
542
543/* return true if we are running with elevated privileges and ignore env variables */
544static int
545enable_secure ()
546{
547 return getuid () != geteuid ()
548 || getgid () != getegid ();
549}
550
263int ev_init (int flags) 551int ev_init (int methods)
264{ 552{
265#if HAVE_MONOTONIC
266 {
267 struct timespec ts;
268 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
269 have_monotonic = 1;
270 }
271#endif
272
273 ev_now = ev_time ();
274 now = get_clock ();
275 diff = ev_now - now;
276
277 if (pipe (sigpipe))
278 return 0;
279
280 ev_method = EVMETHOD_NONE;
281#if HAVE_EPOLL
282 if (ev_method == EVMETHOD_NONE) epoll_init (flags);
283#endif
284#if HAVE_SELECT
285 if (ev_method == EVMETHOD_NONE) select_init (flags);
286#endif
287
288 if (ev_method) 553 if (!ev_method)
554 {
555#if EV_USE_MONOTONIC
289 { 556 {
557 struct timespec ts;
558 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
559 have_monotonic = 1;
560 }
561#endif
562
563 ev_now = ev_time ();
564 now = get_clock ();
565 now_floor = now;
566 diff = ev_now - now;
567
568 if (pipe (sigpipe))
569 return 0;
570
571 if (methods == EVMETHOD_AUTO)
572 if (!enable_secure () && getenv ("LIBEV_METHODS"))
573 methods = atoi (getenv ("LIBEV_METHODS"));
574 else
575 methods = EVMETHOD_ANY;
576
577 ev_method = 0;
578#if EV_USE_KQUEUE
579 if (!ev_method && (methods & EVMETHOD_KQUEUE)) kqueue_init (methods);
580#endif
581#if EV_USE_EPOLL
582 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods);
583#endif
584#if EV_USE_POLL
585 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods);
586#endif
587#if EV_USE_SELECT
588 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods);
589#endif
590
591 if (ev_method)
592 {
290 evw_init (&sigev, sigcb, 0); 593 ev_watcher_init (&sigev, sigcb);
594 ev_set_priority (&sigev, EV_MAXPRI);
291 siginit (); 595 siginit ();
596
597#ifndef WIN32
598 ev_signal_init (&childev, childcb, SIGCHLD);
599 ev_set_priority (&childev, EV_MAXPRI);
600 ev_signal_start (&childev);
601#endif
602 }
292 } 603 }
293 604
294 return ev_method; 605 return ev_method;
295} 606}
296 607
297void ev_prefork (void) 608/*****************************************************************************/
298{
299}
300 609
610void
611ev_fork_prepare (void)
612{
613 /* nop */
614}
615
616void
301void ev_postfork_parent (void) 617ev_fork_parent (void)
302{ 618{
619 /* nop */
303} 620}
304 621
622void
305void ev_postfork_child (void) 623ev_fork_child (void)
306{ 624{
307#if HAVE_EPOLL 625#if EV_USE_EPOLL
308 if (ev_method == EVMETHOD_EPOLL) 626 if (ev_method == EVMETHOD_EPOLL)
309 epoll_postfork_child (); 627 epoll_postfork_child ();
310#endif 628#endif
311 629
312 evio_stop (&sigev); 630 ev_io_stop (&sigev);
313 close (sigpipe [0]); 631 close (sigpipe [0]);
314 close (sigpipe [1]); 632 close (sigpipe [1]);
315 pipe (sigpipe); 633 pipe (sigpipe);
316 siginit (); 634 siginit ();
317} 635}
318 636
637/*****************************************************************************/
638
319static void 639static void
320fd_reify (void) 640call_pending (void)
321{ 641{
322 int i; 642 int pri;
323 643
324 for (i = 0; i < fdchangecnt; ++i) 644 for (pri = NUMPRI; pri--; )
325 { 645 while (pendingcnt [pri])
326 int fd = fdchanges [i];
327 ANFD *anfd = anfds + fd;
328 struct ev_io *w;
329
330 int wev = 0;
331
332 for (w = anfd->head; w; w = w->next)
333 wev |= w->events;
334
335 if (anfd->wev != wev)
336 { 646 {
337 method_modify (fd, anfd->wev, wev); 647 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
338 anfd->wev = wev;
339 }
340 }
341 648
342 fdchangecnt = 0;
343}
344
345static void
346call_pending ()
347{
348 int i;
349
350 for (i = 0; i < pendingcnt; ++i)
351 {
352 ANPENDING *p = pendings + i;
353
354 if (p->w) 649 if (p->w)
355 { 650 {
356 p->w->pending = 0; 651 p->w->pending = 0;
357 p->w->cb (p->w, p->events); 652 p->w->cb (p->w, p->events);
358 } 653 }
359 } 654 }
360
361 pendingcnt = 0;
362} 655}
363 656
364static void 657static void
365timers_reify (struct ev_timer **timers, int timercnt, ev_tstamp now) 658timers_reify (void)
366{ 659{
367 while (timercnt && timers [0]->at <= now) 660 while (timercnt && timers [0]->at <= now)
368 { 661 {
369 struct ev_timer *w = timers [0]; 662 struct ev_timer *w = timers [0];
370 663
371 /* first reschedule or stop timer */ 664 /* first reschedule or stop timer */
372 if (w->repeat) 665 if (w->repeat)
373 { 666 {
374 if (w->is_abs) 667 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
375 w->at += floor ((now - w->at) / w->repeat + 1.) * w->repeat;
376 else
377 w->at = now + w->repeat; 668 w->at = now + w->repeat;
378
379 assert (w->at > now);
380
381 downheap (timers, timercnt, 0); 669 downheap ((WT *)timers, timercnt, 0);
382 } 670 }
383 else 671 else
672 ev_timer_stop (w); /* nonrepeating: stop timer */
673
674 event ((W)w, EV_TIMEOUT);
675 }
676}
677
678static void
679periodics_reify (void)
680{
681 while (periodiccnt && periodics [0]->at <= ev_now)
682 {
683 struct ev_periodic *w = periodics [0];
684
685 /* first reschedule or stop timer */
686 if (w->interval)
384 { 687 {
385 evtimer_stop (w); /* nonrepeating: stop timer */ 688 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval;
386 --timercnt; /* maybe pass by reference instead? */ 689 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now));
690 downheap ((WT *)periodics, periodiccnt, 0);
387 } 691 }
692 else
693 ev_periodic_stop (w); /* nonrepeating: stop timer */
388 694
389 event ((struct ev_watcher *)w, EV_TIMEOUT); 695 event ((W)w, EV_PERIODIC);
390 } 696 }
391} 697}
392 698
393static void 699static void
394time_update () 700periodics_reschedule (ev_tstamp diff)
395{ 701{
396 int i; 702 int i;
397 ev_now = ev_time ();
398 703
399 if (have_monotonic) 704 /* adjust periodics after time jump */
705 for (i = 0; i < periodiccnt; ++i)
400 { 706 {
401 ev_tstamp odiff = diff; 707 struct ev_periodic *w = periodics [i];
402 708
403 /* detecting time jumps is much more difficult */ 709 if (w->interval)
404 for (i = 2; --i; ) /* loop a few times, before making important decisions */
405 { 710 {
406 now = get_clock (); 711 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval;
407 diff = ev_now - now;
408 712
409 if (fabs (odiff - diff) < MIN_TIMEJUMP) 713 if (fabs (diff) >= 1e-4)
410 return; /* all is well */ 714 {
715 ev_periodic_stop (w);
716 ev_periodic_start (w);
411 717
412 ev_now = ev_time (); 718 i = 0; /* restart loop, inefficient, but time jumps should be rare */
719 }
413 } 720 }
721 }
722}
414 723
415 /* time jump detected, reschedule atimers */ 724static int
416 for (i = 0; i < atimercnt; ++i) 725time_update_monotonic (void)
726{
727 now = get_clock ();
728
729 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5))
730 {
731 ev_now = now + diff;
732 return 0;
733 }
734 else
735 {
736 now_floor = now;
737 ev_now = ev_time ();
738 return 1;
739 }
740}
741
742static void
743time_update (void)
744{
745 int i;
746
747#if EV_USE_MONOTONIC
748 if (expect_true (have_monotonic))
749 {
750 if (time_update_monotonic ())
417 { 751 {
418 struct ev_timer *w = atimers [i]; 752 ev_tstamp odiff = diff;
419 w->at += ceil ((ev_now - w->at) / w->repeat + 1.) * w->repeat; 753
754 for (i = 4; --i; ) /* loop a few times, before making important decisions */
755 {
756 diff = ev_now - now;
757
758 if (fabs (odiff - diff) < MIN_TIMEJUMP)
759 return; /* all is well */
760
761 ev_now = ev_time ();
762 now = get_clock ();
763 now_floor = now;
764 }
765
766 periodics_reschedule (diff - odiff);
767 /* no timer adjustment, as the monotonic clock doesn't jump */
420 } 768 }
421 } 769 }
422 else 770 else
771#endif
423 { 772 {
773 ev_now = ev_time ();
774
424 if (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP) 775 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
425 /* time jump detected, adjust rtimers */ 776 {
777 periodics_reschedule (ev_now - now);
778
779 /* adjust timers. this is easy, as the offset is the same for all */
426 for (i = 0; i < rtimercnt; ++i) 780 for (i = 0; i < timercnt; ++i)
427 rtimers [i]->at += ev_now - now; 781 timers [i]->at += diff;
782 }
428 783
429 now = ev_now; 784 now = ev_now;
430 } 785 }
431} 786}
432 787
433int ev_loop_done; 788int ev_loop_done;
434 789
435void ev_loop (int flags) 790void ev_loop (int flags)
436{ 791{
437 double block; 792 double block;
438 ev_loop_done = flags & EVLOOP_ONESHOT; 793 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
439 794
440 do 795 do
441 { 796 {
797 /* queue check watchers (and execute them) */
798 if (expect_false (preparecnt))
799 {
800 queue_events ((W *)prepares, preparecnt, EV_PREPARE);
801 call_pending ();
802 }
803
442 /* update fd-related kernel structures */ 804 /* update fd-related kernel structures */
443 fd_reify (); 805 fd_reify ();
444 806
445 /* calculate blocking time */ 807 /* calculate blocking time */
808
809 /* we only need this for !monotonic clockor timers, but as we basically
810 always have timers, we just calculate it always */
811#if EV_USE_MONOTONIC
812 if (expect_true (have_monotonic))
813 time_update_monotonic ();
814 else
815#endif
816 {
817 ev_now = ev_time ();
818 now = ev_now;
819 }
820
446 if (flags & EVLOOP_NONBLOCK) 821 if (flags & EVLOOP_NONBLOCK || idlecnt)
447 block = 0.; 822 block = 0.;
448 else 823 else
449 { 824 {
450 block = MAX_BLOCKTIME; 825 block = MAX_BLOCKTIME;
451 826
452 if (rtimercnt) 827 if (timercnt)
453 { 828 {
454 ev_tstamp to = rtimers [0]->at - get_clock () + method_fudge; 829 ev_tstamp to = timers [0]->at - now + method_fudge;
455 if (block > to) block = to; 830 if (block > to) block = to;
456 } 831 }
457 832
458 if (atimercnt) 833 if (periodiccnt)
459 { 834 {
460 ev_tstamp to = atimers [0]->at - ev_time () + method_fudge; 835 ev_tstamp to = periodics [0]->at - ev_now + method_fudge;
461 if (block > to) block = to; 836 if (block > to) block = to;
462 } 837 }
463 838
464 if (block < 0.) block = 0.; 839 if (block < 0.) block = 0.;
465 } 840 }
467 method_poll (block); 842 method_poll (block);
468 843
469 /* update ev_now, do magic */ 844 /* update ev_now, do magic */
470 time_update (); 845 time_update ();
471 846
472 /* put pending timers into pendign queue and reschedule them */ 847 /* queue pending timers and reschedule them */
473 /* absolute timers first */ 848 timers_reify (); /* relative timers called last */
474 timers_reify (atimers, atimercnt, ev_now); 849 periodics_reify (); /* absolute timers called first */
475 /* relative timers second */ 850
476 timers_reify (rtimers, rtimercnt, now); 851 /* queue idle watchers unless io or timers are pending */
852 if (!pendingcnt)
853 queue_events ((W *)idles, idlecnt, EV_IDLE);
854
855 /* queue check watchers, to be executed first */
856 if (checkcnt)
857 queue_events ((W *)checks, checkcnt, EV_CHECK);
477 858
478 call_pending (); 859 call_pending ();
479 } 860 }
480 while (!ev_loop_done); 861 while (!ev_loop_done);
481}
482 862
863 if (ev_loop_done != 2)
864 ev_loop_done = 0;
865}
866
867/*****************************************************************************/
868
483static void 869static void
484wlist_add (struct ev_watcher_list **head, struct ev_watcher_list *elem) 870wlist_add (WL *head, WL elem)
485{ 871{
486 elem->next = *head; 872 elem->next = *head;
487 *head = elem; 873 *head = elem;
488} 874}
489 875
490static void 876static void
491wlist_del (struct ev_watcher_list **head, struct ev_watcher_list *elem) 877wlist_del (WL *head, WL elem)
492{ 878{
493 while (*head) 879 while (*head)
494 { 880 {
495 if (*head == elem) 881 if (*head == elem)
496 { 882 {
501 head = &(*head)->next; 887 head = &(*head)->next;
502 } 888 }
503} 889}
504 890
505static void 891static void
506ev_start (struct ev_watcher *w, int active) 892ev_clear_pending (W w)
507{ 893{
894 if (w->pending)
895 {
896 pendings [ABSPRI (w)][w->pending - 1].w = 0;
508 w->pending = 0; 897 w->pending = 0;
898 }
899}
900
901static void
902ev_start (W w, int active)
903{
904 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
905 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
906
509 w->active = active; 907 w->active = active;
510} 908}
511 909
512static void 910static void
513ev_stop (struct ev_watcher *w) 911ev_stop (W w)
514{ 912{
515 if (w->pending)
516 pendings [w->pending - 1].w = 0;
517
518 w->active = 0; 913 w->active = 0;
519 /* nop */
520} 914}
521 915
916/*****************************************************************************/
917
522void 918void
523evio_start (struct ev_io *w) 919ev_io_start (struct ev_io *w)
524{ 920{
921 int fd = w->fd;
922
525 if (ev_is_active (w)) 923 if (ev_is_active (w))
526 return; 924 return;
527 925
528 int fd = w->fd; 926 assert (("ev_io_start called with negative fd", fd >= 0));
529 927
530 ev_start ((struct ev_watcher *)w, 1); 928 ev_start ((W)w, 1);
531 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 929 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
532 wlist_add ((struct ev_watcher_list **)&anfds[fd].head, (struct ev_watcher_list *)w); 930 wlist_add ((WL *)&anfds[fd].head, (WL)w);
533 931
534 ++fdchangecnt; 932 fd_change (fd);
535 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
536 fdchanges [fdchangecnt - 1] = fd;
537} 933}
538 934
539void 935void
540evio_stop (struct ev_io *w) 936ev_io_stop (struct ev_io *w)
541{ 937{
938 ev_clear_pending ((W)w);
542 if (!ev_is_active (w)) 939 if (!ev_is_active (w))
543 return; 940 return;
544 941
545 wlist_del ((struct ev_watcher_list **)&anfds[w->fd].head, (struct ev_watcher_list *)w); 942 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
546 ev_stop ((struct ev_watcher *)w); 943 ev_stop ((W)w);
547 944
548 ++fdchangecnt; 945 fd_change (w->fd);
549 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
550 fdchanges [fdchangecnt - 1] = w->fd;
551} 946}
552 947
553void 948void
554evtimer_start (struct ev_timer *w) 949ev_timer_start (struct ev_timer *w)
555{ 950{
556 if (ev_is_active (w)) 951 if (ev_is_active (w))
557 return; 952 return;
558 953
559 if (w->is_abs) 954 w->at += now;
955
956 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
957
958 ev_start ((W)w, ++timercnt);
959 array_needsize (timers, timermax, timercnt, );
960 timers [timercnt - 1] = w;
961 upheap ((WT *)timers, timercnt - 1);
962}
963
964void
965ev_timer_stop (struct ev_timer *w)
966{
967 ev_clear_pending ((W)w);
968 if (!ev_is_active (w))
969 return;
970
971 if (w->active < timercnt--)
972 {
973 timers [w->active - 1] = timers [timercnt];
974 downheap ((WT *)timers, timercnt, w->active - 1);
560 { 975 }
561 /* this formula differs from the one in timer_reify becuse we do not round up */ 976
977 w->at = w->repeat;
978
979 ev_stop ((W)w);
980}
981
982void
983ev_timer_again (struct ev_timer *w)
984{
985 if (ev_is_active (w))
986 {
562 if (w->repeat) 987 if (w->repeat)
988 {
989 w->at = now + w->repeat;
990 downheap ((WT *)timers, timercnt, w->active - 1);
991 }
992 else
993 ev_timer_stop (w);
994 }
995 else if (w->repeat)
996 ev_timer_start (w);
997}
998
999void
1000ev_periodic_start (struct ev_periodic *w)
1001{
1002 if (ev_is_active (w))
1003 return;
1004
1005 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1006
1007 /* this formula differs from the one in periodic_reify because we do not always round up */
1008 if (w->interval)
563 w->at += ceil ((ev_now - w->at) / w->repeat) * w->repeat; 1009 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval;
564 1010
565 ev_start ((struct ev_watcher *)w, ++atimercnt); 1011 ev_start ((W)w, ++periodiccnt);
566 array_needsize (atimers, atimermax, atimercnt, ); 1012 array_needsize (periodics, periodicmax, periodiccnt, );
567 atimers [atimercnt - 1] = w; 1013 periodics [periodiccnt - 1] = w;
568 upheap (atimers, atimercnt - 1); 1014 upheap ((WT *)periodics, periodiccnt - 1);
569 }
570 else
571 {
572 w->at += now;
573
574 ev_start ((struct ev_watcher *)w, ++rtimercnt);
575 array_needsize (rtimers, rtimermax, rtimercnt, );
576 rtimers [rtimercnt - 1] = w;
577 upheap (rtimers, rtimercnt - 1);
578 }
579
580} 1015}
581 1016
582void 1017void
583evtimer_stop (struct ev_timer *w) 1018ev_periodic_stop (struct ev_periodic *w)
584{ 1019{
1020 ev_clear_pending ((W)w);
585 if (!ev_is_active (w)) 1021 if (!ev_is_active (w))
586 return; 1022 return;
587 1023
588 if (w->is_abs)
589 {
590 if (w->active < atimercnt--) 1024 if (w->active < periodiccnt--)
591 {
592 atimers [w->active - 1] = atimers [atimercnt];
593 downheap (atimers, atimercnt, w->active - 1);
594 }
595 } 1025 {
596 else 1026 periodics [w->active - 1] = periodics [periodiccnt];
1027 downheap ((WT *)periodics, periodiccnt, w->active - 1);
597 { 1028 }
598 if (w->active < rtimercnt--)
599 {
600 rtimers [w->active - 1] = rtimers [rtimercnt];
601 downheap (rtimers, rtimercnt, w->active - 1);
602 }
603 }
604 1029
605 ev_stop ((struct ev_watcher *)w); 1030 ev_stop ((W)w);
606} 1031}
607 1032
1033#ifndef SA_RESTART
1034# define SA_RESTART 0
1035#endif
1036
608void 1037void
609evsignal_start (struct ev_signal *w) 1038ev_signal_start (struct ev_signal *w)
610{ 1039{
611 if (ev_is_active (w)) 1040 if (ev_is_active (w))
612 return; 1041 return;
613 1042
614 ev_start ((struct ev_watcher *)w, 1); 1043 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1044
1045 ev_start ((W)w, 1);
615 array_needsize (signals, signalmax, w->signum, signals_init); 1046 array_needsize (signals, signalmax, w->signum, signals_init);
616 wlist_add ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 1047 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
617 1048
618 if (!w->next) 1049 if (!w->next)
619 { 1050 {
620 struct sigaction sa; 1051 struct sigaction sa;
621 sa.sa_handler = sighandler; 1052 sa.sa_handler = sighandler;
622 sigfillset (&sa.sa_mask); 1053 sigfillset (&sa.sa_mask);
623 sa.sa_flags = 0; 1054 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
624 sigaction (w->signum, &sa, 0); 1055 sigaction (w->signum, &sa, 0);
625 } 1056 }
626} 1057}
627 1058
628void 1059void
629evsignal_stop (struct ev_signal *w) 1060ev_signal_stop (struct ev_signal *w)
630{ 1061{
1062 ev_clear_pending ((W)w);
631 if (!ev_is_active (w)) 1063 if (!ev_is_active (w))
632 return; 1064 return;
633 1065
634 wlist_del ((struct ev_watcher_list **)&signals [w->signum - 1].head, (struct ev_watcher_list *)w); 1066 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
635 ev_stop ((struct ev_watcher *)w); 1067 ev_stop ((W)w);
636 1068
637 if (!signals [w->signum - 1].head) 1069 if (!signals [w->signum - 1].head)
638 signal (w->signum, SIG_DFL); 1070 signal (w->signum, SIG_DFL);
639} 1071}
640 1072
1073void
1074ev_idle_start (struct ev_idle *w)
1075{
1076 if (ev_is_active (w))
1077 return;
1078
1079 ev_start ((W)w, ++idlecnt);
1080 array_needsize (idles, idlemax, idlecnt, );
1081 idles [idlecnt - 1] = w;
1082}
1083
1084void
1085ev_idle_stop (struct ev_idle *w)
1086{
1087 ev_clear_pending ((W)w);
1088 if (ev_is_active (w))
1089 return;
1090
1091 idles [w->active - 1] = idles [--idlecnt];
1092 ev_stop ((W)w);
1093}
1094
1095void
1096ev_prepare_start (struct ev_prepare *w)
1097{
1098 if (ev_is_active (w))
1099 return;
1100
1101 ev_start ((W)w, ++preparecnt);
1102 array_needsize (prepares, preparemax, preparecnt, );
1103 prepares [preparecnt - 1] = w;
1104}
1105
1106void
1107ev_prepare_stop (struct ev_prepare *w)
1108{
1109 ev_clear_pending ((W)w);
1110 if (ev_is_active (w))
1111 return;
1112
1113 prepares [w->active - 1] = prepares [--preparecnt];
1114 ev_stop ((W)w);
1115}
1116
1117void
1118ev_check_start (struct ev_check *w)
1119{
1120 if (ev_is_active (w))
1121 return;
1122
1123 ev_start ((W)w, ++checkcnt);
1124 array_needsize (checks, checkmax, checkcnt, );
1125 checks [checkcnt - 1] = w;
1126}
1127
1128void
1129ev_check_stop (struct ev_check *w)
1130{
1131 ev_clear_pending ((W)w);
1132 if (ev_is_active (w))
1133 return;
1134
1135 checks [w->active - 1] = checks [--checkcnt];
1136 ev_stop ((W)w);
1137}
1138
1139void
1140ev_child_start (struct ev_child *w)
1141{
1142 if (ev_is_active (w))
1143 return;
1144
1145 ev_start ((W)w, 1);
1146 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1147}
1148
1149void
1150ev_child_stop (struct ev_child *w)
1151{
1152 ev_clear_pending ((W)w);
1153 if (ev_is_active (w))
1154 return;
1155
1156 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1157 ev_stop ((W)w);
1158}
1159
641/*****************************************************************************/ 1160/*****************************************************************************/
1161
1162struct ev_once
1163{
1164 struct ev_io io;
1165 struct ev_timer to;
1166 void (*cb)(int revents, void *arg);
1167 void *arg;
1168};
1169
1170static void
1171once_cb (struct ev_once *once, int revents)
1172{
1173 void (*cb)(int revents, void *arg) = once->cb;
1174 void *arg = once->arg;
1175
1176 ev_io_stop (&once->io);
1177 ev_timer_stop (&once->to);
1178 free (once);
1179
1180 cb (revents, arg);
1181}
1182
1183static void
1184once_cb_io (struct ev_io *w, int revents)
1185{
1186 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1187}
1188
1189static void
1190once_cb_to (struct ev_timer *w, int revents)
1191{
1192 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1193}
1194
1195void
1196ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1197{
1198 struct ev_once *once = malloc (sizeof (struct ev_once));
1199
1200 if (!once)
1201 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1202 else
1203 {
1204 once->cb = cb;
1205 once->arg = arg;
1206
1207 ev_watcher_init (&once->io, once_cb_io);
1208 if (fd >= 0)
1209 {
1210 ev_io_set (&once->io, fd, events);
1211 ev_io_start (&once->io);
1212 }
1213
1214 ev_watcher_init (&once->to, once_cb_to);
1215 if (timeout >= 0.)
1216 {
1217 ev_timer_set (&once->to, timeout, 0.);
1218 ev_timer_start (&once->to);
1219 }
1220 }
1221}
1222
1223/*****************************************************************************/
1224
642#if 1 1225#if 0
1226
1227struct ev_io wio;
643 1228
644static void 1229static void
645sin_cb (struct ev_io *w, int revents) 1230sin_cb (struct ev_io *w, int revents)
646{ 1231{
647 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents); 1232 fprintf (stderr, "sin %d, revents %d\n", w->fd, revents);
649 1234
650static void 1235static void
651ocb (struct ev_timer *w, int revents) 1236ocb (struct ev_timer *w, int revents)
652{ 1237{
653 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data); 1238 //fprintf (stderr, "timer %f,%f (%x) (%f) d%p\n", w->at, w->repeat, revents, w->at - ev_time (), w->data);
654 evtimer_stop (w); 1239 ev_timer_stop (w);
655 evtimer_start (w); 1240 ev_timer_start (w);
656} 1241}
657 1242
658static void 1243static void
659scb (struct ev_signal *w, int revents) 1244scb (struct ev_signal *w, int revents)
660{ 1245{
661 fprintf (stderr, "signal %x,%d\n", revents, w->signum); 1246 fprintf (stderr, "signal %x,%d\n", revents, w->signum);
1247 ev_io_stop (&wio);
1248 ev_io_start (&wio);
1249}
1250
1251static void
1252gcb (struct ev_signal *w, int revents)
1253{
1254 fprintf (stderr, "generic %x\n", revents);
1255
662} 1256}
663 1257
664int main (void) 1258int main (void)
665{ 1259{
666 struct ev_io sin;
667
668 ev_init (0); 1260 ev_init (0);
669 1261
670 evw_init (&sin, sin_cb, 55);
671 evio_set (&sin, 0, EV_READ); 1262 ev_io_init (&wio, sin_cb, 0, EV_READ);
672 evio_start (&sin); 1263 ev_io_start (&wio);
673 1264
674 struct ev_timer t[10000]; 1265 struct ev_timer t[10000];
675 1266
676#if 0 1267#if 0
677 int i; 1268 int i;
678 for (i = 0; i < 10000; ++i) 1269 for (i = 0; i < 10000; ++i)
679 { 1270 {
680 struct ev_timer *w = t + i; 1271 struct ev_timer *w = t + i;
681 evw_init (w, ocb, i); 1272 ev_watcher_init (w, ocb, i);
682 evtimer_set_abs (w, drand48 (), 0.99775533); 1273 ev_timer_init_abs (w, ocb, drand48 (), 0.99775533);
683 evtimer_start (w); 1274 ev_timer_start (w);
684 if (drand48 () < 0.5) 1275 if (drand48 () < 0.5)
685 evtimer_stop (w); 1276 ev_timer_stop (w);
686 } 1277 }
687#endif 1278#endif
688 1279
689 struct ev_timer t1; 1280 struct ev_timer t1;
690 evw_init (&t1, ocb, 0); 1281 ev_timer_init (&t1, ocb, 5, 10);
691 evtimer_set_abs (&t1, 5, 10);
692 evtimer_start (&t1); 1282 ev_timer_start (&t1);
693 1283
694 struct ev_signal sig; 1284 struct ev_signal sig;
695 evw_init (&sig, scb, 65535);
696 evsignal_set (&sig, SIGQUIT); 1285 ev_signal_init (&sig, scb, SIGQUIT);
697 evsignal_start (&sig); 1286 ev_signal_start (&sig);
1287
1288 struct ev_check cw;
1289 ev_check_init (&cw, gcb);
1290 ev_check_start (&cw);
1291
1292 struct ev_idle iw;
1293 ev_idle_init (&iw, gcb);
1294 ev_idle_start (&iw);
698 1295
699 ev_loop (0); 1296 ev_loop (0);
700 1297
701 return 0; 1298 return 0;
702} 1299}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines