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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines