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

Comparing libev/ev.c (file contents):
Revision 1.350 by root, Sat Oct 16 00:59:56 2010 UTC vs.
Revision 1.356 by root, Fri Oct 22 11:21:52 2010 UTC

35 * and other provisions required by the GPL. If you do not delete the 35 * and other provisions required by the GPL. If you do not delete the
36 * provisions above, a recipient may use your version of this file under 36 * provisions above, a recipient may use your version of this file under
37 * either the BSD or the GPL. 37 * either the BSD or the GPL.
38 */ 38 */
39 39
40#ifdef __cplusplus
41extern "C" {
42#endif
43
44/* this big block deduces configuration from config.h */ 40/* this big block deduces configuration from config.h */
45#ifndef EV_STANDALONE 41#ifndef EV_STANDALONE
46# ifdef EV_CONFIG_H 42# ifdef EV_CONFIG_H
47# include EV_CONFIG_H 43# include EV_CONFIG_H
48# else 44# else
182# include EV_H 178# include EV_H
183#else 179#else
184# include "ev.h" 180# include "ev.h"
185#endif 181#endif
186 182
183EV_CPP(extern "C" {)
184
187#ifndef _WIN32 185#ifndef _WIN32
188# include <sys/time.h> 186# include <sys/time.h>
189# include <sys/wait.h> 187# include <sys/wait.h>
190# include <unistd.h> 188# include <unistd.h>
191#else 189#else
384# include <sys/select.h> 382# include <sys/select.h>
385# endif 383# endif
386#endif 384#endif
387 385
388#if EV_USE_INOTIFY 386#if EV_USE_INOTIFY
389# include <sys/utsname.h>
390# include <sys/statfs.h> 387# include <sys/statfs.h>
391# include <sys/inotify.h> 388# include <sys/inotify.h>
392/* some very old inotify.h headers don't have IN_DONT_FOLLOW */ 389/* some very old inotify.h headers don't have IN_DONT_FOLLOW */
393# ifndef IN_DONT_FOLLOW 390# ifndef IN_DONT_FOLLOW
394# undef EV_USE_INOTIFY 391# undef EV_USE_INOTIFY
411# define EFD_CLOEXEC O_CLOEXEC 408# define EFD_CLOEXEC O_CLOEXEC
412# else 409# else
413# define EFD_CLOEXEC 02000000 410# define EFD_CLOEXEC 02000000
414# endif 411# endif
415# endif 412# endif
416# ifdef __cplusplus
417extern "C" {
418# endif
419int (eventfd) (unsigned int initval, int flags); 413EV_CPP(extern "C") int (eventfd) (unsigned int initval, int flags);
420# ifdef __cplusplus
421}
422# endif
423#endif 414#endif
424 415
425#if EV_USE_SIGNALFD 416#if EV_USE_SIGNALFD
426/* our minimum requirement is glibc 2.7 which has the stub, but not the header */ 417/* our minimum requirement is glibc 2.7 which has the stub, but not the header */
427# include <stdint.h> 418# include <stdint.h>
433# define SFD_CLOEXEC O_CLOEXEC 424# define SFD_CLOEXEC O_CLOEXEC
434# else 425# else
435# define SFD_CLOEXEC 02000000 426# define SFD_CLOEXEC 02000000
436# endif 427# endif
437# endif 428# endif
438# ifdef __cplusplus
439extern "C" {
440# endif
441int signalfd (int fd, const sigset_t *mask, int flags); 429EV_CPP (extern "C") int signalfd (int fd, const sigset_t *mask, int flags);
442 430
443struct signalfd_siginfo 431struct signalfd_siginfo
444{ 432{
445 uint32_t ssi_signo; 433 uint32_t ssi_signo;
446 char pad[128 - sizeof (uint32_t)]; 434 char pad[128 - sizeof (uint32_t)];
447}; 435};
448# ifdef __cplusplus
449}
450# endif
451#endif 436#endif
452 437
453/**/ 438/**/
454 439
455#if EV_VERIFY >= 3 440#if EV_VERIFY >= 3
537# include "ev_win32.c" 522# include "ev_win32.c"
538#endif 523#endif
539 524
540/*****************************************************************************/ 525/*****************************************************************************/
541 526
527#ifdef __linux
528# include <sys/utsname.h>
529#endif
530
531static unsigned int noinline
532ev_linux_version (void)
533{
534#ifdef __linux
535 struct utsname buf;
536 unsigned int v;
537 int i;
538 char *p = buf.release;
539
540 if (uname (&buf))
541 return 0;
542
543 for (i = 3+1; --i; )
544 {
545 unsigned int c = 0;
546
547 for (;;)
548 {
549 if (*p >= '0' && *p <= '9')
550 c = c * 10 + *p++ - '0';
551 else
552 {
553 p += *p == '.';
554 break;
555 }
556 }
557
558 v = (v << 8) | c;
559 }
560
561 return v;
562#else
563 return 0;
564#endif
565}
566
567/*****************************************************************************/
568
542#if EV_AVOID_STDIO 569#if EV_AVOID_STDIO
543static void noinline 570static void noinline
544ev_printerr (const char *msg) 571ev_printerr (const char *msg)
545{ 572{
546 write (STDERR_FILENO, msg, strlen (msg)); 573 write (STDERR_FILENO, msg, strlen (msg));
717# define EV_RELEASE_CB (void)0 744# define EV_RELEASE_CB (void)0
718# define EV_ACQUIRE_CB (void)0 745# define EV_ACQUIRE_CB (void)0
719# define EV_INVOKE_PENDING ev_invoke_pending (EV_A) 746# define EV_INVOKE_PENDING ev_invoke_pending (EV_A)
720#endif 747#endif
721 748
722#define EVUNLOOP_RECURSE 0x80 749#define EVBREAK_RECURSE 0x80
723 750
724/*****************************************************************************/ 751/*****************************************************************************/
725 752
726#ifndef EV_HAVE_EV_TIME 753#ifndef EV_HAVE_EV_TIME
727ev_tstamp 754ev_tstamp
964 anfd->events = 0; 991 anfd->events = 0;
965 992
966 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next) 993 for (w = (ev_io *)anfd->head; w; w = (ev_io *)((WL)w)->next)
967 anfd->events |= (unsigned char)w->events; 994 anfd->events |= (unsigned char)w->events;
968 995
969 if (o_events !=anfd-> events) 996 if (o_events != anfd->events)
970 o_reify = EV__IOFDSET; /* actually |= */ 997 o_reify = EV__IOFDSET; /* actually |= */
971 } 998 }
972 999
973 if (o_reify & EV__IOFDSET) 1000 if (o_reify & EV__IOFDSET)
974 backend_modify (EV_A_ fd, o_events, anfd->events); 1001 backend_modify (EV_A_ fd, o_events, anfd->events);
1544ev_embeddable_backends (void) 1571ev_embeddable_backends (void)
1545{ 1572{
1546 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT; 1573 int flags = EVBACKEND_EPOLL | EVBACKEND_KQUEUE | EVBACKEND_PORT;
1547 1574
1548 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */ 1575 /* epoll embeddability broken on all linux versions up to at least 2.6.23 */
1549 /* please fix it and tell me how to detect the fix */ 1576 if (ev_linux_version () < 0x020620) /* disable it on linux < 2.6.32 */
1550 flags &= ~EVBACKEND_EPOLL; 1577 flags &= ~EVBACKEND_EPOLL;
1551 1578
1552 return flags; 1579 return flags;
1553} 1580}
1554 1581
1555unsigned int 1582unsigned int
2292 mn_now = ev_rt_now; 2319 mn_now = ev_rt_now;
2293 } 2320 }
2294} 2321}
2295 2322
2296void 2323void
2297ev_loop (EV_P_ int flags) 2324ev_run (EV_P_ int flags)
2298{ 2325{
2299#if EV_FEATURE_API 2326#if EV_FEATURE_API
2300 ++loop_depth; 2327 ++loop_depth;
2301#endif 2328#endif
2302 2329
2303 assert (("libev: ev_loop recursion during release detected", loop_done != EVUNLOOP_RECURSE)); 2330 assert (("libev: ev_loop recursion during release detected", loop_done != EVBREAK_RECURSE));
2304 2331
2305 loop_done = EVUNLOOP_CANCEL; 2332 loop_done = EVBREAK_CANCEL;
2306 2333
2307 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */ 2334 EV_INVOKE_PENDING; /* in case we recurse, ensure ordering stays nice and clean */
2308 2335
2309 do 2336 do
2310 { 2337 {
2353 /* calculate blocking time */ 2380 /* calculate blocking time */
2354 { 2381 {
2355 ev_tstamp waittime = 0.; 2382 ev_tstamp waittime = 0.;
2356 ev_tstamp sleeptime = 0.; 2383 ev_tstamp sleeptime = 0.;
2357 2384
2385 /* remember old timestamp for io_blocktime calculation */
2386 ev_tstamp prev_mn_now = mn_now;
2387
2388 /* update time to cancel out callback processing overhead */
2389 time_update (EV_A_ 1e100);
2390
2358 if (expect_true (!(flags & EVLOOP_NONBLOCK || idleall || !activecnt))) 2391 if (expect_true (!(flags & EVRUN_NOWAIT || idleall || !activecnt)))
2359 { 2392 {
2360 /* remember old timestamp for io_blocktime calculation */
2361 ev_tstamp prev_mn_now = mn_now;
2362
2363 /* update time to cancel out callback processing overhead */
2364 time_update (EV_A_ 1e100);
2365
2366 waittime = MAX_BLOCKTIME; 2393 waittime = MAX_BLOCKTIME;
2367 2394
2368 if (timercnt) 2395 if (timercnt)
2369 { 2396 {
2370 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge; 2397 ev_tstamp to = ANHE_at (timers [HEAP0]) - mn_now + backend_fudge;
2400 } 2427 }
2401 2428
2402#if EV_FEATURE_API 2429#if EV_FEATURE_API
2403 ++loop_count; 2430 ++loop_count;
2404#endif 2431#endif
2405 assert ((loop_done = EVUNLOOP_RECURSE, 1)); /* assert for side effect */ 2432 assert ((loop_done = EVBREAK_RECURSE, 1)); /* assert for side effect */
2406 backend_poll (EV_A_ waittime); 2433 backend_poll (EV_A_ waittime);
2407 assert ((loop_done = EVUNLOOP_CANCEL, 1)); /* assert for side effect */ 2434 assert ((loop_done = EVBREAK_CANCEL, 1)); /* assert for side effect */
2408 2435
2409 /* update ev_rt_now, do magic */ 2436 /* update ev_rt_now, do magic */
2410 time_update (EV_A_ waittime + sleeptime); 2437 time_update (EV_A_ waittime + sleeptime);
2411 } 2438 }
2412 2439
2430 EV_INVOKE_PENDING; 2457 EV_INVOKE_PENDING;
2431 } 2458 }
2432 while (expect_true ( 2459 while (expect_true (
2433 activecnt 2460 activecnt
2434 && !loop_done 2461 && !loop_done
2435 && !(flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)) 2462 && !(flags & (EVRUN_ONCE | EVRUN_NOWAIT))
2436 )); 2463 ));
2437 2464
2438 if (loop_done == EVUNLOOP_ONE) 2465 if (loop_done == EVBREAK_ONE)
2439 loop_done = EVUNLOOP_CANCEL; 2466 loop_done = EVBREAK_CANCEL;
2440 2467
2441#if EV_FEATURE_API 2468#if EV_FEATURE_API
2442 --loop_depth; 2469 --loop_depth;
2443#endif 2470#endif
2444} 2471}
2445 2472
2446void 2473void
2447ev_unloop (EV_P_ int how) 2474ev_break (EV_P_ int how)
2448{ 2475{
2449 loop_done = how; 2476 loop_done = how;
2450} 2477}
2451 2478
2452void 2479void
3062 { 3089 {
3063 struct inotify_event *ev = (struct inotify_event *)(buf + ofs); 3090 struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
3064 infy_wd (EV_A_ ev->wd, ev->wd, ev); 3091 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3065 ofs += sizeof (struct inotify_event) + ev->len; 3092 ofs += sizeof (struct inotify_event) + ev->len;
3066 } 3093 }
3067}
3068
3069inline_size unsigned int
3070ev_linux_version (void)
3071{
3072 struct utsname buf;
3073 unsigned int v;
3074 int i;
3075 char *p = buf.release;
3076
3077 if (uname (&buf))
3078 return 0;
3079
3080 for (i = 3+1; --i; )
3081 {
3082 unsigned int c = 0;
3083
3084 for (;;)
3085 {
3086 if (*p >= '0' && *p <= '9')
3087 c = c * 10 + *p++ - '0';
3088 else
3089 {
3090 p += *p == '.';
3091 break;
3092 }
3093 }
3094
3095 v = (v << 8) | c;
3096 }
3097
3098 return v;
3099} 3094}
3100 3095
3101inline_size void 3096inline_size void
3102ev_check_2625 (EV_P) 3097ev_check_2625 (EV_P)
3103{ 3098{
3427 3422
3428#if EV_EMBED_ENABLE 3423#if EV_EMBED_ENABLE
3429void noinline 3424void noinline
3430ev_embed_sweep (EV_P_ ev_embed *w) 3425ev_embed_sweep (EV_P_ ev_embed *w)
3431{ 3426{
3432 ev_loop (w->other, EVLOOP_NONBLOCK); 3427 ev_run (w->other, EVRUN_NOWAIT);
3433} 3428}
3434 3429
3435static void 3430static void
3436embed_io_cb (EV_P_ ev_io *io, int revents) 3431embed_io_cb (EV_P_ ev_io *io, int revents)
3437{ 3432{
3438 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io)); 3433 ev_embed *w = (ev_embed *)(((char *)io) - offsetof (ev_embed, io));
3439 3434
3440 if (ev_cb (w)) 3435 if (ev_cb (w))
3441 ev_feed_event (EV_A_ (W)w, EV_EMBED); 3436 ev_feed_event (EV_A_ (W)w, EV_EMBED);
3442 else 3437 else
3443 ev_loop (w->other, EVLOOP_NONBLOCK); 3438 ev_run (w->other, EVRUN_NOWAIT);
3444} 3439}
3445 3440
3446static void 3441static void
3447embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents) 3442embed_prepare_cb (EV_P_ ev_prepare *prepare, int revents)
3448{ 3443{
3452 EV_P = w->other; 3447 EV_P = w->other;
3453 3448
3454 while (fdchangecnt) 3449 while (fdchangecnt)
3455 { 3450 {
3456 fd_reify (EV_A); 3451 fd_reify (EV_A);
3457 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3452 ev_run (EV_A_ EVRUN_NOWAIT);
3458 } 3453 }
3459 } 3454 }
3460} 3455}
3461 3456
3462static void 3457static void
3468 3463
3469 { 3464 {
3470 EV_P = w->other; 3465 EV_P = w->other;
3471 3466
3472 ev_loop_fork (EV_A); 3467 ev_loop_fork (EV_A);
3473 ev_loop (EV_A_ EVLOOP_NONBLOCK); 3468 ev_run (EV_A_ EVRUN_NOWAIT);
3474 } 3469 }
3475 3470
3476 ev_embed_start (EV_A_ w); 3471 ev_embed_start (EV_A_ w);
3477} 3472}
3478 3473
3576void 3571void
3577ev_async_start (EV_P_ ev_async *w) 3572ev_async_start (EV_P_ ev_async *w)
3578{ 3573{
3579 if (expect_false (ev_is_active (w))) 3574 if (expect_false (ev_is_active (w)))
3580 return; 3575 return;
3576
3577 w->sent = 0;
3581 3578
3582 evpipe_init (EV_A); 3579 evpipe_init (EV_A);
3583 3580
3584 EV_FREQUENT_CHECK; 3581 EV_FREQUENT_CHECK;
3585 3582
3805 3802
3806#if EV_MULTIPLICITY 3803#if EV_MULTIPLICITY
3807 #include "ev_wrap.h" 3804 #include "ev_wrap.h"
3808#endif 3805#endif
3809 3806
3810#ifdef __cplusplus 3807EV_CPP(})
3811}
3812#endif
3813 3808

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines