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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines