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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines