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

Comparing libev/ev.c (file contents):
Revision 1.42 by root, Fri Nov 2 20:05:05 2007 UTC vs.
Revision 1.53 by root, Sat Nov 3 22:31:11 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#endif 33#endif
34 34
35#include <math.h> 35#include <math.h>
36#include <stdlib.h> 36#include <stdlib.h>
42#include <stdio.h> 42#include <stdio.h>
43 43
44#include <assert.h> 44#include <assert.h>
45#include <errno.h> 45#include <errno.h>
46#include <sys/types.h> 46#include <sys/types.h>
47#ifndef WIN32
47#include <sys/wait.h> 48# include <sys/wait.h>
49#endif
48#include <sys/time.h> 50#include <sys/time.h>
49#include <time.h> 51#include <time.h>
50 52
51/**/ 53/**/
52 54
56 58
57#ifndef EV_USE_SELECT 59#ifndef EV_USE_SELECT
58# define EV_USE_SELECT 1 60# define EV_USE_SELECT 1
59#endif 61#endif
60 62
61#ifndef EV_USE_POLL 63#ifndef EV_USEV_POLL
62# define EV_USE_POLL 0 /* poll is usually slower than select, and not as well tested */ 64# define EV_USEV_POLL 0 /* poll is usually slower than select, and not as well tested */
63#endif 65#endif
64 66
65#ifndef EV_USE_EPOLL 67#ifndef EV_USE_EPOLL
66# define EV_USE_EPOLL 0 68# define EV_USE_EPOLL 0
69#endif
70
71#ifndef EV_USE_KQUEUE
72# define EV_USE_KQUEUE 0
67#endif 73#endif
68 74
69#ifndef EV_USE_REALTIME 75#ifndef EV_USE_REALTIME
70# define EV_USE_REALTIME 1 76# define EV_USE_REALTIME 1
71#endif 77#endif
107 113
108typedef struct ev_watcher *W; 114typedef struct ev_watcher *W;
109typedef struct ev_watcher_list *WL; 115typedef struct ev_watcher_list *WL;
110typedef struct ev_watcher_time *WT; 116typedef struct ev_watcher_time *WT;
111 117
112static ev_tstamp now_floor, now, diff; /* monotonic clock */
113ev_tstamp ev_now;
114int ev_method;
115
116static int have_monotonic; /* runtime */
117
118static ev_tstamp method_fudge; /* stupid epoll-returns-early bug */
119static void (*method_modify)(int fd, int oev, int nev);
120static void (*method_poll)(ev_tstamp timeout);
121
122/*****************************************************************************/ 118/*****************************************************************************/
123 119
124ev_tstamp 120typedef struct
121{
122 struct ev_watcher_list *head;
123 unsigned char events;
124 unsigned char reify;
125} ANFD;
126
127typedef struct
128{
129 W w;
130 int events;
131} ANPENDING;
132
133#ifdef EV_MULTIPLICITY
134struct ev_loop
135{
136# define VAR(name,decl) decl
137# include "ev_vars.h"
138};
139#else
140# define VAR(name,decl) static decl
141# include "ev_vars.h"
142#endif
143#undef VAR
144
145/*****************************************************************************/
146
147inline ev_tstamp
125ev_time (void) 148ev_time (void)
126{ 149{
127#if EV_USE_REALTIME 150#if EV_USE_REALTIME
128 struct timespec ts; 151 struct timespec ts;
129 clock_gettime (CLOCK_REALTIME, &ts); 152 clock_gettime (CLOCK_REALTIME, &ts);
133 gettimeofday (&tv, 0); 156 gettimeofday (&tv, 0);
134 return tv.tv_sec + tv.tv_usec * 1e-6; 157 return tv.tv_sec + tv.tv_usec * 1e-6;
135#endif 158#endif
136} 159}
137 160
138static ev_tstamp 161inline ev_tstamp
139get_clock (void) 162get_clock (void)
140{ 163{
141#if EV_USE_MONOTONIC 164#if EV_USE_MONOTONIC
142 if (expect_true (have_monotonic)) 165 if (expect_true (have_monotonic))
143 { 166 {
146 return ts.tv_sec + ts.tv_nsec * 1e-9; 169 return ts.tv_sec + ts.tv_nsec * 1e-9;
147 } 170 }
148#endif 171#endif
149 172
150 return ev_time (); 173 return ev_time ();
174}
175
176ev_tstamp
177ev_now (EV_P)
178{
179 return rt_now;
151} 180}
152 181
153#define array_roundsize(base,n) ((n) | 4 & ~3) 182#define array_roundsize(base,n) ((n) | 4 & ~3)
154 183
155#define array_needsize(base,cur,cnt,init) \ 184#define array_needsize(base,cur,cnt,init) \
167 cur = newcnt; \ 196 cur = newcnt; \
168 } 197 }
169 198
170/*****************************************************************************/ 199/*****************************************************************************/
171 200
172typedef struct
173{
174 struct ev_io *head;
175 unsigned char events;
176 unsigned char reify;
177} ANFD;
178
179static ANFD *anfds;
180static int anfdmax;
181
182static void 201static void
183anfds_init (ANFD *base, int count) 202anfds_init (ANFD *base, int count)
184{ 203{
185 while (count--) 204 while (count--)
186 { 205 {
190 209
191 ++base; 210 ++base;
192 } 211 }
193} 212}
194 213
195typedef struct
196{
197 W w;
198 int events;
199} ANPENDING;
200
201static ANPENDING *pendings [NUMPRI];
202static int pendingmax [NUMPRI], pendingcnt [NUMPRI];
203
204static void 214static void
205event (W w, int events) 215event (EV_P_ W w, int events)
206{ 216{
207 if (w->pending) 217 if (w->pending)
208 { 218 {
209 pendings [ABSPRI (w)][w->pending - 1].events |= events; 219 pendings [ABSPRI (w)][w->pending - 1].events |= events;
210 return; 220 return;
215 pendings [ABSPRI (w)][w->pending - 1].w = w; 225 pendings [ABSPRI (w)][w->pending - 1].w = w;
216 pendings [ABSPRI (w)][w->pending - 1].events = events; 226 pendings [ABSPRI (w)][w->pending - 1].events = events;
217} 227}
218 228
219static void 229static void
220queue_events (W *events, int eventcnt, int type) 230queue_events (EV_P_ W *events, int eventcnt, int type)
221{ 231{
222 int i; 232 int i;
223 233
224 for (i = 0; i < eventcnt; ++i) 234 for (i = 0; i < eventcnt; ++i)
225 event (events [i], type); 235 event (EV_A_ events [i], type);
226} 236}
227 237
228static void 238static void
229fd_event (int fd, int events) 239fd_event (EV_P_ int fd, int events)
230{ 240{
231 ANFD *anfd = anfds + fd; 241 ANFD *anfd = anfds + fd;
232 struct ev_io *w; 242 struct ev_io *w;
233 243
234 for (w = anfd->head; w; w = w->next) 244 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
235 { 245 {
236 int ev = w->events & events; 246 int ev = w->events & events;
237 247
238 if (ev) 248 if (ev)
239 event ((W)w, ev); 249 event (EV_A_ (W)w, ev);
240 } 250 }
241} 251}
242 252
243/*****************************************************************************/ 253/*****************************************************************************/
244 254
245static int *fdchanges;
246static int fdchangemax, fdchangecnt;
247
248static void 255static void
249fd_reify (void) 256fd_reify (EV_P)
250{ 257{
251 int i; 258 int i;
252 259
253 for (i = 0; i < fdchangecnt; ++i) 260 for (i = 0; i < fdchangecnt; ++i)
254 { 261 {
256 ANFD *anfd = anfds + fd; 263 ANFD *anfd = anfds + fd;
257 struct ev_io *w; 264 struct ev_io *w;
258 265
259 int events = 0; 266 int events = 0;
260 267
261 for (w = anfd->head; w; w = w->next) 268 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
262 events |= w->events; 269 events |= w->events;
263 270
264 anfd->reify = 0; 271 anfd->reify = 0;
265 272
266 if (anfd->events != events) 273 if (anfd->events != events)
267 { 274 {
268 method_modify (fd, anfd->events, events); 275 method_modify (EV_A_ fd, anfd->events, events);
269 anfd->events = events; 276 anfd->events = events;
270 } 277 }
271 } 278 }
272 279
273 fdchangecnt = 0; 280 fdchangecnt = 0;
274} 281}
275 282
276static void 283static void
277fd_change (int fd) 284fd_change (EV_P_ int fd)
278{ 285{
279 if (anfds [fd].reify || fdchangecnt < 0) 286 if (anfds [fd].reify || fdchangecnt < 0)
280 return; 287 return;
281 288
282 anfds [fd].reify = 1; 289 anfds [fd].reify = 1;
285 array_needsize (fdchanges, fdchangemax, fdchangecnt, ); 292 array_needsize (fdchanges, fdchangemax, fdchangecnt, );
286 fdchanges [fdchangecnt - 1] = fd; 293 fdchanges [fdchangecnt - 1] = fd;
287} 294}
288 295
289static void 296static void
290fd_kill (int fd) 297fd_kill (EV_P_ int fd)
291{ 298{
292 struct ev_io *w; 299 struct ev_io *w;
293 300
294 printf ("killing fd %d\n", fd);//D
295 while ((w = anfds [fd].head)) 301 while ((w = (struct ev_io *)anfds [fd].head))
296 { 302 {
297 ev_io_stop (w); 303 ev_io_stop (EV_A_ w);
298 event ((W)w, EV_ERROR | EV_READ | EV_WRITE); 304 event (EV_A_ (W)w, EV_ERROR | EV_READ | EV_WRITE);
299 } 305 }
300} 306}
301 307
302/* called on EBADF to verify fds */ 308/* called on EBADF to verify fds */
303static void 309static void
304fd_ebadf (void) 310fd_ebadf (EV_P)
305{ 311{
306 int fd; 312 int fd;
307 313
308 for (fd = 0; fd < anfdmax; ++fd) 314 for (fd = 0; fd < anfdmax; ++fd)
309 if (anfds [fd].events) 315 if (anfds [fd].events)
310 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF) 316 if (fcntl (fd, F_GETFD) == -1 && errno == EBADF)
311 fd_kill (fd); 317 fd_kill (EV_A_ fd);
312} 318}
313 319
314/* called on ENOMEM in select/poll to kill some fds and retry */ 320/* called on ENOMEM in select/poll to kill some fds and retry */
315static void 321static void
316fd_enomem (void) 322fd_enomem (EV_P)
317{ 323{
318 int fd = anfdmax; 324 int fd = anfdmax;
319 325
320 while (fd--) 326 while (fd--)
321 if (anfds [fd].events) 327 if (anfds [fd].events)
322 { 328 {
323 close (fd); 329 close (fd);
324 fd_kill (fd); 330 fd_kill (EV_A_ fd);
325 return; 331 return;
326 } 332 }
327} 333}
328 334
329/*****************************************************************************/ 335/*****************************************************************************/
330
331static struct ev_timer **timers;
332static int timermax, timercnt;
333
334static struct ev_periodic **periodics;
335static int periodicmax, periodiccnt;
336 336
337static void 337static void
338upheap (WT *timers, int k) 338upheap (WT *timers, int k)
339{ 339{
340 WT w = timers [k]; 340 WT w = timers [k];
377 377
378/*****************************************************************************/ 378/*****************************************************************************/
379 379
380typedef struct 380typedef struct
381{ 381{
382 struct ev_signal *head; 382 struct ev_watcher_list *head;
383 sig_atomic_t volatile gotsig; 383 sig_atomic_t volatile gotsig;
384} ANSIG; 384} ANSIG;
385 385
386static ANSIG *signals; 386static ANSIG *signals;
387static int signalmax; 387static int signalmax;
388 388
389static int sigpipe [2]; 389static int sigpipe [2];
390static sig_atomic_t volatile gotsig; 390static sig_atomic_t volatile gotsig;
391static struct ev_io sigev;
392 391
393static void 392static void
394signals_init (ANSIG *base, int count) 393signals_init (ANSIG *base, int count)
395{ 394{
396 while (count--) 395 while (count--)
407{ 406{
408 signals [signum - 1].gotsig = 1; 407 signals [signum - 1].gotsig = 1;
409 408
410 if (!gotsig) 409 if (!gotsig)
411 { 410 {
411 int old_errno = errno;
412 gotsig = 1; 412 gotsig = 1;
413 write (sigpipe [1], &signum, 1); 413 write (sigpipe [1], &signum, 1);
414 errno = old_errno;
414 } 415 }
415} 416}
416 417
417static void 418static void
418sigcb (struct ev_io *iow, int revents) 419sigcb (EV_P_ struct ev_io *iow, int revents)
419{ 420{
420 struct ev_signal *w; 421 struct ev_watcher_list *w;
421 int signum; 422 int signum;
422 423
423 read (sigpipe [0], &revents, 1); 424 read (sigpipe [0], &revents, 1);
424 gotsig = 0; 425 gotsig = 0;
425 426
427 if (signals [signum].gotsig) 428 if (signals [signum].gotsig)
428 { 429 {
429 signals [signum].gotsig = 0; 430 signals [signum].gotsig = 0;
430 431
431 for (w = signals [signum].head; w; w = w->next) 432 for (w = signals [signum].head; w; w = w->next)
432 event ((W)w, EV_SIGNAL); 433 event (EV_A_ (W)w, EV_SIGNAL);
433 } 434 }
434} 435}
435 436
436static void 437static void
437siginit (void) 438siginit (EV_P)
438{ 439{
440#ifndef WIN32
439 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC); 441 fcntl (sigpipe [0], F_SETFD, FD_CLOEXEC);
440 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC); 442 fcntl (sigpipe [1], F_SETFD, FD_CLOEXEC);
441 443
442 /* rather than sort out wether we really need nb, set it */ 444 /* rather than sort out wether we really need nb, set it */
443 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK); 445 fcntl (sigpipe [0], F_SETFL, O_NONBLOCK);
444 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK); 446 fcntl (sigpipe [1], F_SETFL, O_NONBLOCK);
447#endif
445 448
446 ev_io_set (&sigev, sigpipe [0], EV_READ); 449 ev_io_set (&sigev, sigpipe [0], EV_READ);
447 ev_io_start (&sigev); 450 ev_io_start (&sigev);
451 ev_unref (EV_A); /* child watcher should not keep loop alive */
448} 452}
449 453
450/*****************************************************************************/ 454/*****************************************************************************/
451 455
452static struct ev_idle **idles; 456#ifndef WIN32
453static int idlemax, idlecnt;
454
455static struct ev_prepare **prepares;
456static int preparemax, preparecnt;
457
458static struct ev_check **checks;
459static int checkmax, checkcnt;
460
461/*****************************************************************************/
462
463static struct ev_child *childs [PID_HASHSIZE];
464static struct ev_signal childev;
465 457
466#ifndef WCONTINUED 458#ifndef WCONTINUED
467# define WCONTINUED 0 459# define WCONTINUED 0
468#endif 460#endif
469 461
470static void 462static void
471childcb (struct ev_signal *sw, int revents) 463child_reap (EV_P_ struct ev_signal *sw, int chain, int pid, int status)
472{ 464{
473 struct ev_child *w; 465 struct ev_child *w;
466
467 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)
469 {
470 w->priority = sw->priority; /* need to do it *now* */
471 w->rpid = pid;
472 w->rstatus = status;
473 event (EV_A_ (W)w, EV_CHILD);
474 }
475}
476
477static void
478childcb (EV_P_ struct ev_signal *sw, int revents)
479{
474 int pid, status; 480 int pid, status;
475 481
476 while ((pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)) != -1) 482 if (0 < (pid = waitpid (-1, &status, WNOHANG | WUNTRACED | WCONTINUED)))
477 for (w = childs [pid & (PID_HASHSIZE - 1)]; w; w = w->next) 483 {
478 if (w->pid == pid || !w->pid) 484 /* make sure we are called again until all childs have been reaped */
479 { 485 event (EV_A_ (W)sw, EV_SIGNAL);
480 w->status = status; 486
481 event ((W)w, EV_CHILD); 487 child_reap (EV_A_ sw, pid, pid, status);
482 } 488 child_reap (EV_A_ sw, 0, pid, status); /* this might trigger a watcher twice, but event catches that */
489 }
483} 490}
491
492#endif
484 493
485/*****************************************************************************/ 494/*****************************************************************************/
486 495
496#if EV_USE_KQUEUE
497# include "ev_kqueue.c"
498#endif
487#if EV_USE_EPOLL 499#if EV_USE_EPOLL
488# include "ev_epoll.c" 500# include "ev_epoll.c"
489#endif 501#endif
490#if EV_USE_POLL 502#if EV_USEV_POLL
491# include "ev_poll.c" 503# include "ev_poll.c"
492#endif 504#endif
493#if EV_USE_SELECT 505#if EV_USE_SELECT
494# include "ev_select.c" 506# include "ev_select.c"
495#endif 507#endif
504ev_version_minor (void) 516ev_version_minor (void)
505{ 517{
506 return EV_VERSION_MINOR; 518 return EV_VERSION_MINOR;
507} 519}
508 520
509/* return true if we are running with elevated privileges and ignore env variables */ 521/* return true if we are running with elevated privileges and should ignore env variables */
510static int 522static int
511enable_secure () 523enable_secure (void)
512{ 524{
525#ifdef WIN32
526 return 0;
527#else
513 return getuid () != geteuid () 528 return getuid () != geteuid ()
514 || getgid () != getegid (); 529 || getgid () != getegid ();
530#endif
515} 531}
516 532
533int
534ev_method (EV_P)
535{
536 return method;
537}
538
539int
517int ev_init (int methods) 540ev_init (EV_P_ int methods)
518{ 541{
542#ifdef EV_MULTIPLICITY
543 memset (loop, 0, sizeof (struct ev_loop));
544#endif
545
519 if (!ev_method) 546 if (!method)
520 { 547 {
521#if EV_USE_MONOTONIC 548#if EV_USE_MONOTONIC
522 { 549 {
523 struct timespec ts; 550 struct timespec ts;
524 if (!clock_gettime (CLOCK_MONOTONIC, &ts)) 551 if (!clock_gettime (CLOCK_MONOTONIC, &ts))
525 have_monotonic = 1; 552 have_monotonic = 1;
526 } 553 }
527#endif 554#endif
528 555
529 ev_now = ev_time (); 556 rt_now = ev_time ();
530 now = get_clock (); 557 mn_now = get_clock ();
531 now_floor = now; 558 now_floor = mn_now;
532 diff = ev_now - now; 559 diff = rt_now - mn_now;
533 560
534 if (pipe (sigpipe)) 561 if (pipe (sigpipe))
535 return 0; 562 return 0;
536 563
537 if (methods == EVMETHOD_AUTO) 564 if (methods == EVMETHOD_AUTO)
538 if (!enable_secure () && getenv ("LIBEV_METHODS")) 565 if (!enable_secure () && getenv ("LIBmethodS"))
539 methods = atoi (getenv ("LIBEV_METHODS")); 566 methods = atoi (getenv ("LIBmethodS"));
540 else 567 else
541 methods = EVMETHOD_ANY; 568 methods = EVMETHOD_ANY;
542 569
543 ev_method = 0; 570 method = 0;
571#if EV_USE_KQUEUE
572 if (!method && (methods & EVMETHOD_KQUEUE)) method = kqueue_init (EV_A_ methods);
573#endif
544#if EV_USE_EPOLL 574#if EV_USE_EPOLL
545 if (!ev_method && (methods & EVMETHOD_EPOLL )) epoll_init (methods); 575 if (!method && (methods & EVMETHOD_EPOLL )) method = epoll_init (EV_A_ methods);
546#endif 576#endif
547#if EV_USE_POLL 577#if EV_USEV_POLL
548 if (!ev_method && (methods & EVMETHOD_POLL )) poll_init (methods); 578 if (!method && (methods & EVMETHOD_POLL )) method = poll_init (EV_A_ methods);
549#endif 579#endif
550#if EV_USE_SELECT 580#if EV_USE_SELECT
551 if (!ev_method && (methods & EVMETHOD_SELECT)) select_init (methods); 581 if (!method && (methods & EVMETHOD_SELECT)) method = select_init (EV_A_ methods);
552#endif 582#endif
553 583
554 if (ev_method) 584 if (method)
555 { 585 {
556 ev_watcher_init (&sigev, sigcb); 586 ev_watcher_init (&sigev, sigcb);
587 ev_set_priority (&sigev, EV_MAXPRI);
557 siginit (); 588 siginit (EV_A);
558 589
590#ifndef WIN32
559 ev_signal_init (&childev, childcb, SIGCHLD); 591 ev_signal_init (&childev, childcb, SIGCHLD);
592 ev_set_priority (&childev, EV_MAXPRI);
560 ev_signal_start (&childev); 593 ev_signal_start (EV_A_ &childev);
594 ev_unref (EV_A); /* child watcher should not keep loop alive */
595#endif
561 } 596 }
562 } 597 }
563 598
564 return ev_method; 599 return method;
565} 600}
566 601
567/*****************************************************************************/ 602/*****************************************************************************/
568 603
569void 604void
580 615
581void 616void
582ev_fork_child (void) 617ev_fork_child (void)
583{ 618{
584#if EV_USE_EPOLL 619#if EV_USE_EPOLL
585 if (ev_method == EVMETHOD_EPOLL) 620 if (method == EVMETHOD_EPOLL)
586 epoll_postfork_child (); 621 epoll_postfork_child ();
587#endif 622#endif
588 623
589 ev_io_stop (&sigev); 624 ev_io_stop (&sigev);
590 close (sigpipe [0]); 625 close (sigpipe [0]);
594} 629}
595 630
596/*****************************************************************************/ 631/*****************************************************************************/
597 632
598static void 633static void
599call_pending (void) 634call_pending (EV_P)
600{ 635{
601 int pri; 636 int pri;
602 637
603 for (pri = NUMPRI; pri--; ) 638 for (pri = NUMPRI; pri--; )
604 while (pendingcnt [pri]) 639 while (pendingcnt [pri])
606 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 641 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
607 642
608 if (p->w) 643 if (p->w)
609 { 644 {
610 p->w->pending = 0; 645 p->w->pending = 0;
611 p->w->cb (p->w, p->events); 646 p->w->cb (EV_A_ p->w, p->events);
612 } 647 }
613 } 648 }
614} 649}
615 650
616static void 651static void
617timers_reify (void) 652timers_reify (EV_P)
618{ 653{
619 while (timercnt && timers [0]->at <= now) 654 while (timercnt && timers [0]->at <= mn_now)
620 { 655 {
621 struct ev_timer *w = timers [0]; 656 struct ev_timer *w = timers [0];
622 657
623 /* first reschedule or stop timer */ 658 /* first reschedule or stop timer */
624 if (w->repeat) 659 if (w->repeat)
625 { 660 {
626 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 661 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
627 w->at = now + w->repeat; 662 w->at = mn_now + w->repeat;
628 downheap ((WT *)timers, timercnt, 0); 663 downheap ((WT *)timers, timercnt, 0);
629 } 664 }
630 else 665 else
631 ev_timer_stop (w); /* nonrepeating: stop timer */ 666 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
632 667
633 event ((W)w, EV_TIMEOUT); 668 event ((W)w, EV_TIMEOUT);
634 } 669 }
635} 670}
636 671
637static void 672static void
638periodics_reify (void) 673periodics_reify (EV_P)
639{ 674{
640 while (periodiccnt && periodics [0]->at <= ev_now) 675 while (periodiccnt && periodics [0]->at <= rt_now)
641 { 676 {
642 struct ev_periodic *w = periodics [0]; 677 struct ev_periodic *w = periodics [0];
643 678
644 /* first reschedule or stop timer */ 679 /* first reschedule or stop timer */
645 if (w->interval) 680 if (w->interval)
646 { 681 {
647 w->at += floor ((ev_now - w->at) / w->interval + 1.) * w->interval; 682 w->at += floor ((rt_now - w->at) / w->interval + 1.) * w->interval;
648 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > ev_now)); 683 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", w->at > rt_now));
649 downheap ((WT *)periodics, periodiccnt, 0); 684 downheap ((WT *)periodics, periodiccnt, 0);
650 } 685 }
651 else 686 else
652 ev_periodic_stop (w); /* nonrepeating: stop timer */ 687 ev_periodic_stop (EV_A_ w); /* nonrepeating: stop timer */
653 688
654 event ((W)w, EV_PERIODIC); 689 event (EV_A_ (W)w, EV_PERIODIC);
655 } 690 }
656} 691}
657 692
658static void 693static void
659periodics_reschedule (ev_tstamp diff) 694periodics_reschedule (EV_P_ ev_tstamp diff)
660{ 695{
661 int i; 696 int i;
662 697
663 /* adjust periodics after time jump */ 698 /* adjust periodics after time jump */
664 for (i = 0; i < periodiccnt; ++i) 699 for (i = 0; i < periodiccnt; ++i)
665 { 700 {
666 struct ev_periodic *w = periodics [i]; 701 struct ev_periodic *w = periodics [i];
667 702
668 if (w->interval) 703 if (w->interval)
669 { 704 {
670 ev_tstamp diff = ceil ((ev_now - w->at) / w->interval) * w->interval; 705 ev_tstamp diff = ceil ((rt_now - w->at) / w->interval) * w->interval;
671 706
672 if (fabs (diff) >= 1e-4) 707 if (fabs (diff) >= 1e-4)
673 { 708 {
674 ev_periodic_stop (w); 709 ev_periodic_stop (EV_A_ w);
675 ev_periodic_start (w); 710 ev_periodic_start (EV_A_ w);
676 711
677 i = 0; /* restart loop, inefficient, but time jumps should be rare */ 712 i = 0; /* restart loop, inefficient, but time jumps should be rare */
678 } 713 }
679 } 714 }
680 } 715 }
681} 716}
682 717
683static int 718inline int
684time_update_monotonic (void) 719time_update_monotonic (EV_P)
685{ 720{
686 now = get_clock (); 721 mn_now = get_clock ();
687 722
688 if (expect_true (now - now_floor < MIN_TIMEJUMP * .5)) 723 if (expect_true (mn_now - now_floor < MIN_TIMEJUMP * .5))
689 { 724 {
690 ev_now = now + diff; 725 rt_now = mn_now + diff;
691 return 0; 726 return 0;
692 } 727 }
693 else 728 else
694 { 729 {
695 now_floor = now; 730 now_floor = mn_now;
696 ev_now = ev_time (); 731 rt_now = ev_time ();
697 return 1; 732 return 1;
698 } 733 }
699} 734}
700 735
701static void 736static void
702time_update (void) 737time_update (EV_P)
703{ 738{
704 int i; 739 int i;
705 740
706#if EV_USE_MONOTONIC 741#if EV_USE_MONOTONIC
707 if (expect_true (have_monotonic)) 742 if (expect_true (have_monotonic))
708 { 743 {
709 if (time_update_monotonic ()) 744 if (time_update_monotonic (EV_A))
710 { 745 {
711 ev_tstamp odiff = diff; 746 ev_tstamp odiff = diff;
712 747
713 for (i = 4; --i; ) /* loop a few times, before making important decisions */ 748 for (i = 4; --i; ) /* loop a few times, before making important decisions */
714 { 749 {
715 diff = ev_now - now; 750 diff = rt_now - mn_now;
716 751
717 if (fabs (odiff - diff) < MIN_TIMEJUMP) 752 if (fabs (odiff - diff) < MIN_TIMEJUMP)
718 return; /* all is well */ 753 return; /* all is well */
719 754
720 ev_now = ev_time (); 755 rt_now = ev_time ();
721 now = get_clock (); 756 mn_now = get_clock ();
722 now_floor = now; 757 now_floor = mn_now;
723 } 758 }
724 759
725 periodics_reschedule (diff - odiff); 760 periodics_reschedule (EV_A_ diff - odiff);
726 /* no timer adjustment, as the monotonic clock doesn't jump */ 761 /* no timer adjustment, as the monotonic clock doesn't jump */
727 } 762 }
728 } 763 }
729 else 764 else
730#endif 765#endif
731 { 766 {
732 ev_now = ev_time (); 767 rt_now = ev_time ();
733 768
734 if (expect_false (now > ev_now || now < ev_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 769 if (expect_false (mn_now > rt_now || mn_now < rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
735 { 770 {
736 periodics_reschedule (ev_now - now); 771 periodics_reschedule (EV_A_ rt_now - mn_now);
737 772
738 /* adjust timers. this is easy, as the offset is the same for all */ 773 /* adjust timers. this is easy, as the offset is the same for all */
739 for (i = 0; i < timercnt; ++i) 774 for (i = 0; i < timercnt; ++i)
740 timers [i]->at += diff; 775 timers [i]->at += diff;
741 } 776 }
742 777
743 now = ev_now; 778 mn_now = rt_now;
744 } 779 }
745} 780}
746 781
747int ev_loop_done; 782void
783ev_ref (EV_P)
784{
785 ++activecnt;
786}
748 787
788void
789ev_unref (EV_P)
790{
791 --activecnt;
792}
793
794static int loop_done;
795
796void
749void ev_loop (int flags) 797ev_loop (EV_P_ int flags)
750{ 798{
751 double block; 799 double block;
752 ev_loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0; 800 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) ? 1 : 0;
753 801
754 do 802 do
755 { 803 {
756 /* queue check watchers (and execute them) */ 804 /* queue check watchers (and execute them) */
757 if (expect_false (preparecnt)) 805 if (expect_false (preparecnt))
758 { 806 {
759 queue_events ((W *)prepares, preparecnt, EV_PREPARE); 807 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
760 call_pending (); 808 call_pending (EV_A);
761 } 809 }
762 810
763 /* update fd-related kernel structures */ 811 /* update fd-related kernel structures */
764 fd_reify (); 812 fd_reify (EV_A);
765 813
766 /* calculate blocking time */ 814 /* calculate blocking time */
767 815
768 /* we only need this for !monotonic clockor timers, but as we basically 816 /* we only need this for !monotonic clockor timers, but as we basically
769 always have timers, we just calculate it always */ 817 always have timers, we just calculate it always */
770#if EV_USE_MONOTONIC 818#if EV_USE_MONOTONIC
771 if (expect_true (have_monotonic)) 819 if (expect_true (have_monotonic))
772 time_update_monotonic (); 820 time_update_monotonic (EV_A);
773 else 821 else
774#endif 822#endif
775 { 823 {
776 ev_now = ev_time (); 824 rt_now = ev_time ();
777 now = ev_now; 825 mn_now = rt_now;
778 } 826 }
779 827
780 if (flags & EVLOOP_NONBLOCK || idlecnt) 828 if (flags & EVLOOP_NONBLOCK || idlecnt)
781 block = 0.; 829 block = 0.;
782 else 830 else
783 { 831 {
784 block = MAX_BLOCKTIME; 832 block = MAX_BLOCKTIME;
785 833
786 if (timercnt) 834 if (timercnt)
787 { 835 {
788 ev_tstamp to = timers [0]->at - now + method_fudge; 836 ev_tstamp to = timers [0]->at - mn_now + method_fudge;
789 if (block > to) block = to; 837 if (block > to) block = to;
790 } 838 }
791 839
792 if (periodiccnt) 840 if (periodiccnt)
793 { 841 {
794 ev_tstamp to = periodics [0]->at - ev_now + method_fudge; 842 ev_tstamp to = periodics [0]->at - rt_now + method_fudge;
795 if (block > to) block = to; 843 if (block > to) block = to;
796 } 844 }
797 845
798 if (block < 0.) block = 0.; 846 if (block < 0.) block = 0.;
799 } 847 }
800 848
801 method_poll (block); 849 method_poll (EV_A_ block);
802 850
803 /* update ev_now, do magic */ 851 /* update rt_now, do magic */
804 time_update (); 852 time_update (EV_A);
805 853
806 /* queue pending timers and reschedule them */ 854 /* queue pending timers and reschedule them */
807 timers_reify (); /* relative timers called last */ 855 timers_reify (EV_A); /* relative timers called last */
808 periodics_reify (); /* absolute timers called first */ 856 periodics_reify (EV_A); /* absolute timers called first */
809 857
810 /* queue idle watchers unless io or timers are pending */ 858 /* queue idle watchers unless io or timers are pending */
811 if (!pendingcnt) 859 if (!pendingcnt)
812 queue_events ((W *)idles, idlecnt, EV_IDLE); 860 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
813 861
814 /* queue check watchers, to be executed first */ 862 /* queue check watchers, to be executed first */
815 if (checkcnt) 863 if (checkcnt)
816 queue_events ((W *)checks, checkcnt, EV_CHECK); 864 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
817 865
818 call_pending (); 866 call_pending (EV_A);
819 } 867 }
820 while (!ev_loop_done); 868 while (activecnt && !loop_done);
821 869
822 if (ev_loop_done != 2) 870 if (loop_done != 2)
823 ev_loop_done = 0; 871 loop_done = 0;
872}
873
874void
875ev_unloop (EV_P_ int how)
876{
877 loop_done = how;
824} 878}
825 879
826/*****************************************************************************/ 880/*****************************************************************************/
827 881
828static void 882inline void
829wlist_add (WL *head, WL elem) 883wlist_add (WL *head, WL elem)
830{ 884{
831 elem->next = *head; 885 elem->next = *head;
832 *head = elem; 886 *head = elem;
833} 887}
834 888
835static void 889inline void
836wlist_del (WL *head, WL elem) 890wlist_del (WL *head, WL elem)
837{ 891{
838 while (*head) 892 while (*head)
839 { 893 {
840 if (*head == elem) 894 if (*head == elem)
845 899
846 head = &(*head)->next; 900 head = &(*head)->next;
847 } 901 }
848} 902}
849 903
850static void 904inline void
851ev_clear_pending (W w) 905ev_clear_pending (EV_P_ W w)
852{ 906{
853 if (w->pending) 907 if (w->pending)
854 { 908 {
855 pendings [ABSPRI (w)][w->pending - 1].w = 0; 909 pendings [ABSPRI (w)][w->pending - 1].w = 0;
856 w->pending = 0; 910 w->pending = 0;
857 } 911 }
858} 912}
859 913
860static void 914inline void
861ev_start (W w, int active) 915ev_start (EV_P_ W w, int active)
862{ 916{
917 if (w->priority < EV_MINPRI) w->priority = EV_MINPRI;
918 if (w->priority > EV_MAXPRI) w->priority = EV_MAXPRI;
919
863 w->active = active; 920 w->active = active;
921 ev_ref (EV_A);
864} 922}
865 923
866static void 924inline void
867ev_stop (W w) 925ev_stop (EV_P_ W w)
868{ 926{
927 ev_unref (EV_A);
869 w->active = 0; 928 w->active = 0;
870} 929}
871 930
872/*****************************************************************************/ 931/*****************************************************************************/
873 932
874void 933void
875ev_io_start (struct ev_io *w) 934ev_io_start (EV_P_ struct ev_io *w)
876{ 935{
877 int fd = w->fd; 936 int fd = w->fd;
878 937
879 if (ev_is_active (w)) 938 if (ev_is_active (w))
880 return; 939 return;
881 940
882 assert (("ev_io_start called with negative fd", fd >= 0)); 941 assert (("ev_io_start called with negative fd", fd >= 0));
883 942
884 ev_start ((W)w, 1); 943 ev_start (EV_A_ (W)w, 1);
885 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 944 array_needsize (anfds, anfdmax, fd + 1, anfds_init);
886 wlist_add ((WL *)&anfds[fd].head, (WL)w); 945 wlist_add ((WL *)&anfds[fd].head, (WL)w);
887 946
888 fd_change (fd); 947 fd_change (EV_A_ fd);
889} 948}
890 949
891void 950void
892ev_io_stop (struct ev_io *w) 951ev_io_stop (EV_P_ struct ev_io *w)
893{ 952{
894 ev_clear_pending ((W)w); 953 ev_clear_pending (EV_A_ (W)w);
895 if (!ev_is_active (w)) 954 if (!ev_is_active (w))
896 return; 955 return;
897 956
898 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 957 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
899 ev_stop ((W)w); 958 ev_stop (EV_A_ (W)w);
900 959
901 fd_change (w->fd); 960 fd_change (EV_A_ w->fd);
902} 961}
903 962
904void 963void
905ev_timer_start (struct ev_timer *w) 964ev_timer_start (EV_P_ struct ev_timer *w)
906{ 965{
907 if (ev_is_active (w)) 966 if (ev_is_active (w))
908 return; 967 return;
909 968
910 w->at += now; 969 w->at += mn_now;
911 970
912 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 971 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
913 972
914 ev_start ((W)w, ++timercnt); 973 ev_start (EV_A_ (W)w, ++timercnt);
915 array_needsize (timers, timermax, timercnt, ); 974 array_needsize (timers, timermax, timercnt, );
916 timers [timercnt - 1] = w; 975 timers [timercnt - 1] = w;
917 upheap ((WT *)timers, timercnt - 1); 976 upheap ((WT *)timers, timercnt - 1);
918} 977}
919 978
920void 979void
921ev_timer_stop (struct ev_timer *w) 980ev_timer_stop (EV_P_ struct ev_timer *w)
922{ 981{
923 ev_clear_pending ((W)w); 982 ev_clear_pending (EV_A_ (W)w);
924 if (!ev_is_active (w)) 983 if (!ev_is_active (w))
925 return; 984 return;
926 985
927 if (w->active < timercnt--) 986 if (w->active < timercnt--)
928 { 987 {
930 downheap ((WT *)timers, timercnt, w->active - 1); 989 downheap ((WT *)timers, timercnt, w->active - 1);
931 } 990 }
932 991
933 w->at = w->repeat; 992 w->at = w->repeat;
934 993
935 ev_stop ((W)w); 994 ev_stop (EV_A_ (W)w);
936} 995}
937 996
938void 997void
939ev_timer_again (struct ev_timer *w) 998ev_timer_again (EV_P_ struct ev_timer *w)
940{ 999{
941 if (ev_is_active (w)) 1000 if (ev_is_active (w))
942 { 1001 {
943 if (w->repeat) 1002 if (w->repeat)
944 { 1003 {
945 w->at = now + w->repeat; 1004 w->at = mn_now + w->repeat;
946 downheap ((WT *)timers, timercnt, w->active - 1); 1005 downheap ((WT *)timers, timercnt, w->active - 1);
947 } 1006 }
948 else 1007 else
949 ev_timer_stop (w); 1008 ev_timer_stop (EV_A_ w);
950 } 1009 }
951 else if (w->repeat) 1010 else if (w->repeat)
952 ev_timer_start (w); 1011 ev_timer_start (EV_A_ w);
953} 1012}
954 1013
955void 1014void
956ev_periodic_start (struct ev_periodic *w) 1015ev_periodic_start (EV_P_ struct ev_periodic *w)
957{ 1016{
958 if (ev_is_active (w)) 1017 if (ev_is_active (w))
959 return; 1018 return;
960 1019
961 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1020 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
962 1021
963 /* this formula differs from the one in periodic_reify because we do not always round up */ 1022 /* this formula differs from the one in periodic_reify because we do not always round up */
964 if (w->interval) 1023 if (w->interval)
965 w->at += ceil ((ev_now - w->at) / w->interval) * w->interval; 1024 w->at += ceil ((rt_now - w->at) / w->interval) * w->interval;
966 1025
967 ev_start ((W)w, ++periodiccnt); 1026 ev_start (EV_A_ (W)w, ++periodiccnt);
968 array_needsize (periodics, periodicmax, periodiccnt, ); 1027 array_needsize (periodics, periodicmax, periodiccnt, );
969 periodics [periodiccnt - 1] = w; 1028 periodics [periodiccnt - 1] = w;
970 upheap ((WT *)periodics, periodiccnt - 1); 1029 upheap ((WT *)periodics, periodiccnt - 1);
971} 1030}
972 1031
973void 1032void
974ev_periodic_stop (struct ev_periodic *w) 1033ev_periodic_stop (EV_P_ struct ev_periodic *w)
975{ 1034{
976 ev_clear_pending ((W)w); 1035 ev_clear_pending (EV_A_ (W)w);
977 if (!ev_is_active (w)) 1036 if (!ev_is_active (w))
978 return; 1037 return;
979 1038
980 if (w->active < periodiccnt--) 1039 if (w->active < periodiccnt--)
981 { 1040 {
982 periodics [w->active - 1] = periodics [periodiccnt]; 1041 periodics [w->active - 1] = periodics [periodiccnt];
983 downheap ((WT *)periodics, periodiccnt, w->active - 1); 1042 downheap ((WT *)periodics, periodiccnt, w->active - 1);
984 } 1043 }
985 1044
986 ev_stop ((W)w); 1045 ev_stop (EV_A_ (W)w);
987} 1046}
988 1047
1048#ifndef SA_RESTART
1049# define SA_RESTART 0
1050#endif
1051
989void 1052void
990ev_signal_start (struct ev_signal *w) 1053ev_signal_start (EV_P_ struct ev_signal *w)
991{ 1054{
992 if (ev_is_active (w)) 1055 if (ev_is_active (w))
993 return; 1056 return;
994 1057
995 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1058 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
996 1059
997 ev_start ((W)w, 1); 1060 ev_start (EV_A_ (W)w, 1);
998 array_needsize (signals, signalmax, w->signum, signals_init); 1061 array_needsize (signals, signalmax, w->signum, signals_init);
999 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1062 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1000 1063
1001 if (!w->next) 1064 if (!w->next)
1002 { 1065 {
1003 struct sigaction sa; 1066 struct sigaction sa;
1004 sa.sa_handler = sighandler; 1067 sa.sa_handler = sighandler;
1005 sigfillset (&sa.sa_mask); 1068 sigfillset (&sa.sa_mask);
1006 sa.sa_flags = 0; 1069 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1007 sigaction (w->signum, &sa, 0); 1070 sigaction (w->signum, &sa, 0);
1008 } 1071 }
1009} 1072}
1010 1073
1011void 1074void
1012ev_signal_stop (struct ev_signal *w) 1075ev_signal_stop (EV_P_ struct ev_signal *w)
1013{ 1076{
1014 ev_clear_pending ((W)w); 1077 ev_clear_pending (EV_A_ (W)w);
1015 if (!ev_is_active (w)) 1078 if (!ev_is_active (w))
1016 return; 1079 return;
1017 1080
1018 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w); 1081 wlist_del ((WL *)&signals [w->signum - 1].head, (WL)w);
1019 ev_stop ((W)w); 1082 ev_stop (EV_A_ (W)w);
1020 1083
1021 if (!signals [w->signum - 1].head) 1084 if (!signals [w->signum - 1].head)
1022 signal (w->signum, SIG_DFL); 1085 signal (w->signum, SIG_DFL);
1023} 1086}
1024 1087
1025void 1088void
1026ev_idle_start (struct ev_idle *w) 1089ev_idle_start (EV_P_ struct ev_idle *w)
1027{ 1090{
1028 if (ev_is_active (w)) 1091 if (ev_is_active (w))
1029 return; 1092 return;
1030 1093
1031 ev_start ((W)w, ++idlecnt); 1094 ev_start (EV_A_ (W)w, ++idlecnt);
1032 array_needsize (idles, idlemax, idlecnt, ); 1095 array_needsize (idles, idlemax, idlecnt, );
1033 idles [idlecnt - 1] = w; 1096 idles [idlecnt - 1] = w;
1034} 1097}
1035 1098
1036void 1099void
1037ev_idle_stop (struct ev_idle *w) 1100ev_idle_stop (EV_P_ struct ev_idle *w)
1038{ 1101{
1039 ev_clear_pending ((W)w); 1102 ev_clear_pending (EV_A_ (W)w);
1040 if (ev_is_active (w)) 1103 if (ev_is_active (w))
1041 return; 1104 return;
1042 1105
1043 idles [w->active - 1] = idles [--idlecnt]; 1106 idles [w->active - 1] = idles [--idlecnt];
1044 ev_stop ((W)w); 1107 ev_stop (EV_A_ (W)w);
1045} 1108}
1046 1109
1047void 1110void
1048ev_prepare_start (struct ev_prepare *w) 1111ev_prepare_start (EV_P_ struct ev_prepare *w)
1049{ 1112{
1050 if (ev_is_active (w)) 1113 if (ev_is_active (w))
1051 return; 1114 return;
1052 1115
1053 ev_start ((W)w, ++preparecnt); 1116 ev_start (EV_A_ (W)w, ++preparecnt);
1054 array_needsize (prepares, preparemax, preparecnt, ); 1117 array_needsize (prepares, preparemax, preparecnt, );
1055 prepares [preparecnt - 1] = w; 1118 prepares [preparecnt - 1] = w;
1056} 1119}
1057 1120
1058void 1121void
1059ev_prepare_stop (struct ev_prepare *w) 1122ev_prepare_stop (EV_P_ struct ev_prepare *w)
1060{ 1123{
1061 ev_clear_pending ((W)w); 1124 ev_clear_pending (EV_A_ (W)w);
1062 if (ev_is_active (w)) 1125 if (ev_is_active (w))
1063 return; 1126 return;
1064 1127
1065 prepares [w->active - 1] = prepares [--preparecnt]; 1128 prepares [w->active - 1] = prepares [--preparecnt];
1066 ev_stop ((W)w); 1129 ev_stop (EV_A_ (W)w);
1067} 1130}
1068 1131
1069void 1132void
1070ev_check_start (struct ev_check *w) 1133ev_check_start (EV_P_ struct ev_check *w)
1071{ 1134{
1072 if (ev_is_active (w)) 1135 if (ev_is_active (w))
1073 return; 1136 return;
1074 1137
1075 ev_start ((W)w, ++checkcnt); 1138 ev_start (EV_A_ (W)w, ++checkcnt);
1076 array_needsize (checks, checkmax, checkcnt, ); 1139 array_needsize (checks, checkmax, checkcnt, );
1077 checks [checkcnt - 1] = w; 1140 checks [checkcnt - 1] = w;
1078} 1141}
1079 1142
1080void 1143void
1081ev_check_stop (struct ev_check *w) 1144ev_check_stop (EV_P_ struct ev_check *w)
1082{ 1145{
1083 ev_clear_pending ((W)w); 1146 ev_clear_pending (EV_A_ (W)w);
1084 if (ev_is_active (w)) 1147 if (ev_is_active (w))
1085 return; 1148 return;
1086 1149
1087 checks [w->active - 1] = checks [--checkcnt]; 1150 checks [w->active - 1] = checks [--checkcnt];
1088 ev_stop ((W)w); 1151 ev_stop (EV_A_ (W)w);
1089} 1152}
1090 1153
1091void 1154void
1092ev_child_start (struct ev_child *w) 1155ev_child_start (EV_P_ struct ev_child *w)
1093{ 1156{
1094 if (ev_is_active (w)) 1157 if (ev_is_active (w))
1095 return; 1158 return;
1096 1159
1097 ev_start ((W)w, 1); 1160 ev_start (EV_A_ (W)w, 1);
1098 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1161 wlist_add ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1099} 1162}
1100 1163
1101void 1164void
1102ev_child_stop (struct ev_child *w) 1165ev_child_stop (EV_P_ struct ev_child *w)
1103{ 1166{
1104 ev_clear_pending ((W)w); 1167 ev_clear_pending (EV_A_ (W)w);
1105 if (ev_is_active (w)) 1168 if (ev_is_active (w))
1106 return; 1169 return;
1107 1170
1108 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1171 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1109 ev_stop ((W)w); 1172 ev_stop (EV_A_ (W)w);
1110} 1173}
1111 1174
1112/*****************************************************************************/ 1175/*****************************************************************************/
1113 1176
1114struct ev_once 1177struct ev_once
1118 void (*cb)(int revents, void *arg); 1181 void (*cb)(int revents, void *arg);
1119 void *arg; 1182 void *arg;
1120}; 1183};
1121 1184
1122static void 1185static void
1123once_cb (struct ev_once *once, int revents) 1186once_cb (EV_P_ struct ev_once *once, int revents)
1124{ 1187{
1125 void (*cb)(int revents, void *arg) = once->cb; 1188 void (*cb)(int revents, void *arg) = once->cb;
1126 void *arg = once->arg; 1189 void *arg = once->arg;
1127 1190
1128 ev_io_stop (&once->io); 1191 ev_io_stop (EV_A_ &once->io);
1129 ev_timer_stop (&once->to); 1192 ev_timer_stop (EV_A_ &once->to);
1130 free (once); 1193 free (once);
1131 1194
1132 cb (revents, arg); 1195 cb (revents, arg);
1133} 1196}
1134 1197
1135static void 1198static void
1136once_cb_io (struct ev_io *w, int revents) 1199once_cb_io (EV_P_ struct ev_io *w, int revents)
1137{ 1200{
1138 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents); 1201 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, io)), revents);
1139} 1202}
1140 1203
1141static void 1204static void
1142once_cb_to (struct ev_timer *w, int revents) 1205once_cb_to (EV_P_ struct ev_timer *w, int revents)
1143{ 1206{
1144 once_cb ((struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents); 1207 once_cb (EV_A_ (struct ev_once *)(((char *)w) - offsetof (struct ev_once, to)), revents);
1145} 1208}
1146 1209
1147void 1210void
1148ev_once (int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1211ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1149{ 1212{
1150 struct ev_once *once = malloc (sizeof (struct ev_once)); 1213 struct ev_once *once = malloc (sizeof (struct ev_once));
1151 1214
1152 if (!once) 1215 if (!once)
1153 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1216 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1158 1221
1159 ev_watcher_init (&once->io, once_cb_io); 1222 ev_watcher_init (&once->io, once_cb_io);
1160 if (fd >= 0) 1223 if (fd >= 0)
1161 { 1224 {
1162 ev_io_set (&once->io, fd, events); 1225 ev_io_set (&once->io, fd, events);
1163 ev_io_start (&once->io); 1226 ev_io_start (EV_A_ &once->io);
1164 } 1227 }
1165 1228
1166 ev_watcher_init (&once->to, once_cb_to); 1229 ev_watcher_init (&once->to, once_cb_to);
1167 if (timeout >= 0.) 1230 if (timeout >= 0.)
1168 { 1231 {
1169 ev_timer_set (&once->to, timeout, 0.); 1232 ev_timer_set (&once->to, timeout, 0.);
1170 ev_timer_start (&once->to); 1233 ev_timer_start (EV_A_ &once->to);
1171 } 1234 }
1172 } 1235 }
1173} 1236}
1174 1237
1175/*****************************************************************************/ 1238/*****************************************************************************/

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines