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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines