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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines