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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines