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

Comparing libev/ev.c (file contents):
Revision 1.325 by root, Sun Jan 24 12:31:55 2010 UTC vs.
Revision 1.332 by root, Tue Mar 9 08:58:17 2010 UTC

1/* 1/*
2 * libev event processing core, watcher management 2 * libev event processing core, watcher management
3 * 3 *
4 * Copyright (c) 2007,2008,2009 Marc Alexander Lehmann <libev@schmorp.de> 4 * Copyright (c) 2007,2008,2009,2010 Marc Alexander Lehmann <libev@schmorp.de>
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without modifica- 7 * Redistribution and use in source and binary forms, with or without modifica-
8 * tion, are permitted provided that the following conditions are met: 8 * tion, are permitted provided that the following conditions are met:
9 * 9 *
163 163
164#include <assert.h> 164#include <assert.h>
165#include <errno.h> 165#include <errno.h>
166#include <sys/types.h> 166#include <sys/types.h>
167#include <time.h> 167#include <time.h>
168#include <limits.h>
168 169
169#include <signal.h> 170#include <signal.h>
170 171
171#ifdef EV_H 172#ifdef EV_H
172# include EV_H 173# include EV_H
183# define WIN32_LEAN_AND_MEAN 184# define WIN32_LEAN_AND_MEAN
184# include <windows.h> 185# include <windows.h>
185# ifndef EV_SELECT_IS_WINSOCKET 186# ifndef EV_SELECT_IS_WINSOCKET
186# define EV_SELECT_IS_WINSOCKET 1 187# define EV_SELECT_IS_WINSOCKET 1
187# endif 188# endif
189# undef EV_AVOID_STDIO
188#endif 190#endif
189 191
190/* this block tries to deduce configuration from header-defined symbols and defaults */ 192/* this block tries to deduce configuration from header-defined symbols and defaults */
191 193
192/* try to deduce the maximum number of signals on this platform */ 194/* try to deduce the maximum number of signals on this platform */
402# endif 404# endif
403# endif 405# endif
404# ifdef __cplusplus 406# ifdef __cplusplus
405extern "C" { 407extern "C" {
406# endif 408# endif
407int eventfd (unsigned int initval, int flags); 409int (eventfd) (unsigned int initval, int flags);
408# ifdef __cplusplus 410# ifdef __cplusplus
409} 411}
410# endif 412# endif
411#endif 413#endif
412 414
523# include "ev_win32.c" 525# include "ev_win32.c"
524#endif 526#endif
525 527
526/*****************************************************************************/ 528/*****************************************************************************/
527 529
530#if EV_AVOID_STDIO
531static void noinline
532ev_printerr (const char *msg)
533{
534 write (STDERR_FILENO, msg, strlen (msg));
535}
536#endif
537
528static void (*syserr_cb)(const char *msg); 538static void (*syserr_cb)(const char *msg);
529 539
530void 540void
531ev_set_syserr_cb (void (*cb)(const char *msg)) 541ev_set_syserr_cb (void (*cb)(const char *msg))
532{ 542{
541 551
542 if (syserr_cb) 552 if (syserr_cb)
543 syserr_cb (msg); 553 syserr_cb (msg);
544 else 554 else
545 { 555 {
556#if EV_AVOID_STDIO
557 const char *err = strerror (errno);
558
559 ev_printerr (msg);
560 ev_printerr (": ");
561 ev_printerr (err);
562 ev_printerr ("\n");
563#else
546 perror (msg); 564 perror (msg);
565#endif
547 abort (); 566 abort ();
548 } 567 }
549} 568}
550 569
551static void * 570static void *
553{ 572{
554 /* some systems, notably openbsd and darwin, fail to properly 573 /* some systems, notably openbsd and darwin, fail to properly
555 * implement realloc (x, 0) (as required by both ansi c-98 and 574 * implement realloc (x, 0) (as required by both ansi c-98 and
556 * the single unix specification, so work around them here. 575 * the single unix specification, so work around them here.
557 */ 576 */
558
559 if (size) 577 if (size)
560 return realloc (ptr, size); 578 return realloc (ptr, size);
561 579
562 free (ptr); 580 free (ptr);
563 return 0; 581 return 0;
576{ 594{
577 ptr = alloc (ptr, size); 595 ptr = alloc (ptr, size);
578 596
579 if (!ptr && size) 597 if (!ptr && size)
580 { 598 {
599#if EV_AVOID_STDIO
600 ev_printerr ("libev: memory allocation failed, aborting.\n");
601#else
581 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size); 602 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
603#endif
582 abort (); 604 abort ();
583 } 605 }
584 606
585 return ptr; 607 return ptr;
586} 608}
2507 2529
2508 if (expect_false (ev_is_active (w))) 2530 if (expect_false (ev_is_active (w)))
2509 return; 2531 return;
2510 2532
2511 assert (("libev: ev_io_start called with negative fd", fd >= 0)); 2533 assert (("libev: ev_io_start called with negative fd", fd >= 0));
2512 assert (("libev: ev_io start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE)))); 2534 assert (("libev: ev_io_start called with illegal event mask", !(w->events & ~(EV__IOFDSET | EV_READ | EV_WRITE))));
2513 2535
2514 EV_FREQUENT_CHECK; 2536 EV_FREQUENT_CHECK;
2515 2537
2516 ev_start (EV_A_ (W)w, 1); 2538 ev_start (EV_A_ (W)w, 1);
2517 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero); 2539 array_needsize (ANFD, anfds, anfdmax, fd + 1, array_init_zero);
2587 timers [active] = timers [timercnt + HEAP0]; 2609 timers [active] = timers [timercnt + HEAP0];
2588 adjustheap (timers, timercnt, active); 2610 adjustheap (timers, timercnt, active);
2589 } 2611 }
2590 } 2612 }
2591 2613
2592 EV_FREQUENT_CHECK;
2593
2594 ev_at (w) -= mn_now; 2614 ev_at (w) -= mn_now;
2595 2615
2596 ev_stop (EV_A_ (W)w); 2616 ev_stop (EV_A_ (W)w);
2617
2618 EV_FREQUENT_CHECK;
2597} 2619}
2598 2620
2599void noinline 2621void noinline
2600ev_timer_again (EV_P_ ev_timer *w) 2622ev_timer_again (EV_P_ ev_timer *w)
2601{ 2623{
2680 periodics [active] = periodics [periodiccnt + HEAP0]; 2702 periodics [active] = periodics [periodiccnt + HEAP0];
2681 adjustheap (periodics, periodiccnt, active); 2703 adjustheap (periodics, periodiccnt, active);
2682 } 2704 }
2683 } 2705 }
2684 2706
2685 EV_FREQUENT_CHECK;
2686
2687 ev_stop (EV_A_ (W)w); 2707 ev_stop (EV_A_ (W)w);
2708
2709 EV_FREQUENT_CHECK;
2688} 2710}
2689 2711
2690void noinline 2712void noinline
2691ev_periodic_again (EV_P_ ev_periodic *w) 2713ev_periodic_again (EV_P_ ev_periodic *w)
2692{ 2714{
2859#define MIN_STAT_INTERVAL 0.1074891 2881#define MIN_STAT_INTERVAL 0.1074891
2860 2882
2861static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 2883static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
2862 2884
2863#if EV_USE_INOTIFY 2885#if EV_USE_INOTIFY
2864# define EV_INOTIFY_BUFSIZE 8192 2886
2887/* the * 2 is to allow for alignment padding, which for some reason is >> 8 */
2888# define EV_INOTIFY_BUFSIZE (sizeof (struct inotify_event) * 2 + NAME_MAX)
2865 2889
2866static void noinline 2890static void noinline
2867infy_add (EV_P_ ev_stat *w) 2891infy_add (EV_P_ ev_stat *w)
2868{ 2892{
2869 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD); 2893 w->wd = inotify_add_watch (fs_fd, w->path, IN_ATTRIB | IN_DELETE_SELF | IN_MOVE_SELF | IN_MODIFY | IN_DONT_FOLLOW | IN_MASK_ADD);
2978 3002
2979static void 3003static void
2980infy_cb (EV_P_ ev_io *w, int revents) 3004infy_cb (EV_P_ ev_io *w, int revents)
2981{ 3005{
2982 char buf [EV_INOTIFY_BUFSIZE]; 3006 char buf [EV_INOTIFY_BUFSIZE];
2983 struct inotify_event *ev = (struct inotify_event *)buf;
2984 int ofs; 3007 int ofs;
2985 int len = read (fs_fd, buf, sizeof (buf)); 3008 int len = read (fs_fd, buf, sizeof (buf));
2986 3009
2987 for (ofs = 0; ofs < len; ofs += sizeof (struct inotify_event) + ev->len) 3010 for (ofs = 0; ofs < len; )
3011 {
3012 struct inotify_event *ev = (struct inotify_event *)(buf + ofs);
2988 infy_wd (EV_A_ ev->wd, ev->wd, ev); 3013 infy_wd (EV_A_ ev->wd, ev->wd, ev);
3014 ofs += sizeof (struct inotify_event) + ev->len;
3015 }
3016}
3017
3018inline_size unsigned int
3019ev_linux_version (void)
3020{
3021 struct utsname buf;
3022 unsigned int v;
3023 int i;
3024 char *p = buf.release;
3025
3026 if (uname (&buf))
3027 return 0;
3028
3029 for (i = 3+1; --i; )
3030 {
3031 unsigned int c = 0;
3032
3033 for (;;)
3034 {
3035 if (*p >= '0' && *p <= '9')
3036 c = c * 10 + *p++ - '0';
3037 else
3038 {
3039 p += *p == '.';
3040 break;
3041 }
3042 }
3043
3044 v = (v << 8) | c;
3045 }
3046
3047 return v;
2989} 3048}
2990 3049
2991inline_size void 3050inline_size void
2992check_2625 (EV_P) 3051ev_check_2625 (EV_P)
2993{ 3052{
2994 /* kernels < 2.6.25 are borked 3053 /* kernels < 2.6.25 are borked
2995 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html 3054 * http://www.ussg.indiana.edu/hypermail/linux/kernel/0711.3/1208.html
2996 */ 3055 */
2997 struct utsname buf; 3056 if (ev_linux_version () < 0x020619)
2998 int major, minor, micro;
2999
3000 if (uname (&buf))
3001 return;
3002
3003 if (sscanf (buf.release, "%d.%d.%d", &major, &minor, &micro) != 3)
3004 return;
3005
3006 if (major < 2
3007 || (major == 2 && minor < 6)
3008 || (major == 2 && minor == 6 && micro < 25))
3009 return; 3057 return;
3010 3058
3011 fs_2625 = 1; 3059 fs_2625 = 1;
3012} 3060}
3013 3061
3028 if (fs_fd != -2) 3076 if (fs_fd != -2)
3029 return; 3077 return;
3030 3078
3031 fs_fd = -1; 3079 fs_fd = -1;
3032 3080
3033 check_2625 (EV_A); 3081 ev_check_2625 (EV_A);
3034 3082
3035 fs_fd = infy_newfd (); 3083 fs_fd = infy_newfd ();
3036 3084
3037 if (fs_fd >= 0) 3085 if (fs_fd >= 0)
3038 { 3086 {
3423 3471
3424 ev_io_stop (EV_A_ &w->io); 3472 ev_io_stop (EV_A_ &w->io);
3425 ev_prepare_stop (EV_A_ &w->prepare); 3473 ev_prepare_stop (EV_A_ &w->prepare);
3426 ev_fork_stop (EV_A_ &w->fork); 3474 ev_fork_stop (EV_A_ &w->fork);
3427 3475
3476 ev_stop (EV_A_ (W)w);
3477
3428 EV_FREQUENT_CHECK; 3478 EV_FREQUENT_CHECK;
3429} 3479}
3430#endif 3480#endif
3431 3481
3432#if EV_FORK_ENABLE 3482#if EV_FORK_ENABLE

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines