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

Comparing libev/ev.c (file contents):
Revision 1.306 by root, Sun Jul 19 06:35:25 2009 UTC vs.
Revision 1.312 by root, Wed Aug 12 18:48:17 2009 UTC

187#endif 187#endif
188 188
189/* this block tries to deduce configuration from header-defined symbols and defaults */ 189/* this block tries to deduce configuration from header-defined symbols and defaults */
190 190
191/* try to deduce the maximum number of signals on this platform */ 191/* try to deduce the maximum number of signals on this platform */
192/* one some platforms, NSIG is one too large. we do not bother */
193#if defined (EV_NSIG) 192#if defined (EV_NSIG)
194/* use what's provided */ 193/* use what's provided */
195#elif defined (NSIG) 194#elif defined (NSIG)
196# define EV_NSIG (NSIG) 195# define EV_NSIG (NSIG)
197#elif defined(_NSIG) 196#elif defined(_NSIG)
387# include <stdint.h> 386# include <stdint.h>
388# ifndef EFD_NONBLOCK 387# ifndef EFD_NONBLOCK
389# define EFD_NONBLOCK O_NONBLOCK 388# define EFD_NONBLOCK O_NONBLOCK
390# endif 389# endif
391# ifndef EFD_CLOEXEC 390# ifndef EFD_CLOEXEC
391# ifdef O_CLOEXEC
392# define EFD_CLOEXEC O_CLOEXEC 392# define EFD_CLOEXEC O_CLOEXEC
393# else
394# define EFD_CLOEXEC 02000000
395# endif
393# endif 396# endif
394# ifdef __cplusplus 397# ifdef __cplusplus
395extern "C" { 398extern "C" {
396# endif 399# endif
397int eventfd (unsigned int initval, int flags); 400int eventfd (unsigned int initval, int flags);
958 961
959 for (fd = anfdmax; fd--; ) 962 for (fd = anfdmax; fd--; )
960 if (anfds [fd].events) 963 if (anfds [fd].events)
961 { 964 {
962 fd_kill (EV_A_ fd); 965 fd_kill (EV_A_ fd);
963 return; 966 break;
964 } 967 }
965} 968}
966 969
967/* usually called after fork if backend needs to re-arm all fds from scratch */ 970/* usually called after fork if backend needs to re-arm all fds from scratch */
968static void noinline 971static void noinline
1058 1061
1059 for (;;) 1062 for (;;)
1060 { 1063 {
1061 int c = k << 1; 1064 int c = k << 1;
1062 1065
1063 if (c > N + HEAP0 - 1) 1066 if (c >= N + HEAP0)
1064 break; 1067 break;
1065 1068
1066 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1]) 1069 c += c + 1 < N + HEAP0 && ANHE_at (heap [c]) > ANHE_at (heap [c + 1])
1067 ? 1 : 0; 1070 ? 1 : 0;
1068 1071
1104 1107
1105/* move an element suitably so it is in a correct place */ 1108/* move an element suitably so it is in a correct place */
1106inline_size void 1109inline_size void
1107adjustheap (ANHE *heap, int N, int k) 1110adjustheap (ANHE *heap, int N, int k)
1108{ 1111{
1109 if (k > HEAP0 && ANHE_at (heap [HPARENT (k)]) >= ANHE_at (heap [k])) 1112 if (k > HEAP0 && ANHE_at (heap [k]) <= ANHE_at (heap [HPARENT (k)]))
1110 upheap (heap, k); 1113 upheap (heap, k);
1111 else 1114 else
1112 downheap (heap, N, k); 1115 downheap (heap, N, k);
1113} 1116}
1114 1117
1127/*****************************************************************************/ 1130/*****************************************************************************/
1128 1131
1129/* associate signal watchers to a signal signal */ 1132/* associate signal watchers to a signal signal */
1130typedef struct 1133typedef struct
1131{ 1134{
1135 EV_ATOMIC_T pending;
1132#if EV_MULTIPLICITY 1136#if EV_MULTIPLICITY
1133 EV_P; 1137 EV_P;
1134#endif 1138#endif
1135 WL head; 1139 WL head;
1136 EV_ATOMIC_T gotsig;
1137} ANSIG; 1140} ANSIG;
1138 1141
1139static ANSIG signals [EV_NSIG - 1]; 1142static ANSIG signals [EV_NSIG - 1];
1140static EV_ATOMIC_T gotsig;
1141 1143
1142/*****************************************************************************/ 1144/*****************************************************************************/
1143 1145
1144/* used to prepare libev internal fd's */ 1146/* used to prepare libev internal fd's */
1145/* this is not fork-safe */ 1147/* this is not fork-safe */
1213/* called whenever the libev signal pipe */ 1215/* called whenever the libev signal pipe */
1214/* got some events (signal, async) */ 1216/* got some events (signal, async) */
1215static void 1217static void
1216pipecb (EV_P_ ev_io *iow, int revents) 1218pipecb (EV_P_ ev_io *iow, int revents)
1217{ 1219{
1220 int i;
1221
1218#if EV_USE_EVENTFD 1222#if EV_USE_EVENTFD
1219 if (evfd >= 0) 1223 if (evfd >= 0)
1220 { 1224 {
1221 uint64_t counter; 1225 uint64_t counter;
1222 read (evfd, &counter, sizeof (uint64_t)); 1226 read (evfd, &counter, sizeof (uint64_t));
1226 { 1230 {
1227 char dummy; 1231 char dummy;
1228 read (evpipe [0], &dummy, 1); 1232 read (evpipe [0], &dummy, 1);
1229 } 1233 }
1230 1234
1231 if (gotsig && ev_is_default_loop (EV_A)) 1235 if (sig_pending)
1232 { 1236 {
1233 int signum; 1237 sig_pending = 0;
1234 gotsig = 0;
1235 1238
1236 for (signum = EV_NSIG - 1; signum--; ) 1239 for (i = EV_NSIG - 1; i--; )
1237 if (signals [signum].gotsig) 1240 if (expect_false (signals [i].pending))
1238 ev_feed_signal_event (EV_A_ signum + 1); 1241 ev_feed_signal_event (EV_A_ i + 1);
1239 } 1242 }
1240 1243
1241#if EV_ASYNC_ENABLE 1244#if EV_ASYNC_ENABLE
1242 if (gotasync) 1245 if (async_pending)
1243 { 1246 {
1244 int i; 1247 async_pending = 0;
1245 gotasync = 0;
1246 1248
1247 for (i = asynccnt; i--; ) 1249 for (i = asynccnt; i--; )
1248 if (asyncs [i]->sent) 1250 if (asyncs [i]->sent)
1249 { 1251 {
1250 asyncs [i]->sent = 0; 1252 asyncs [i]->sent = 0;
1265 1267
1266#if _WIN32 1268#if _WIN32
1267 signal (signum, ev_sighandler); 1269 signal (signum, ev_sighandler);
1268#endif 1270#endif
1269 1271
1270 signals [signum - 1].gotsig = 1; 1272 signals [signum - 1].pending = 1;
1271 evpipe_write (EV_A_ &gotsig); 1273 evpipe_write (EV_A_ &sig_pending);
1272} 1274}
1273 1275
1274void noinline 1276void noinline
1275ev_feed_signal_event (EV_P_ int signum) 1277ev_feed_signal_event (EV_P_ int signum)
1276{ 1278{
1277 WL w; 1279 WL w;
1278 1280
1281 if (expect_false (signum <= 0 || signum > EV_NSIG))
1282 return;
1283
1284 --signum;
1285
1279#if EV_MULTIPLICITY 1286#if EV_MULTIPLICITY
1280 assert (("libev: feeding signal events is only supported in the default loop", loop == ev_default_loop_ptr)); 1287 /* it is permissible to try to feed a signal to the wrong loop */
1281#endif 1288 /* or, likely more useful, feeding a signal nobody is waiting for */
1282 1289
1283 if (signum <= 0 || signum > EV_NSIG) 1290 if (expect_false (signals [signum].loop != EV_A))
1284 return; 1291 return;
1292#endif
1285 1293
1286 --signum;
1287
1288 signals [signum].gotsig = 0; 1294 signals [signum].pending = 0;
1289 1295
1290 for (w = signals [signum].head; w; w = w->next) 1296 for (w = signals [signum].head; w; w = w->next)
1291 ev_feed_event (EV_A_ (W)w, EV_SIGNAL); 1297 ev_feed_event (EV_A_ (W)w, EV_SIGNAL);
1292} 1298}
1293 1299
1560 1566
1561 io_blocktime = 0.; 1567 io_blocktime = 0.;
1562 timeout_blocktime = 0.; 1568 timeout_blocktime = 0.;
1563 backend = 0; 1569 backend = 0;
1564 backend_fd = -1; 1570 backend_fd = -1;
1565 gotasync = 0; 1571 sig_pending = 0;
1572#if EV_ASYNC_ENABLE
1573 async_pending = 0;
1574#endif
1566#if EV_USE_INOTIFY 1575#if EV_USE_INOTIFY
1567 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2; 1576 fs_fd = flags & EVFLAG_NOINOTIFY ? -1 : -2;
1568#endif 1577#endif
1569#if EV_USE_SIGNALFD 1578#if EV_USE_SIGNALFD
1570 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2; 1579 sigfd = flags & EVFLAG_NOSIGFD ? -1 : -2;
1704 1713
1705 if (ev_is_active (&pipe_w)) 1714 if (ev_is_active (&pipe_w))
1706 { 1715 {
1707 /* this "locks" the handlers against writing to the pipe */ 1716 /* this "locks" the handlers against writing to the pipe */
1708 /* while we modify the fd vars */ 1717 /* while we modify the fd vars */
1709 gotsig = 1; 1718 sig_pending = 1;
1710#if EV_ASYNC_ENABLE 1719#if EV_ASYNC_ENABLE
1711 gotasync = 1; 1720 async_pending = 1;
1712#endif 1721#endif
1713 1722
1714 ev_ref (EV_A); 1723 ev_ref (EV_A);
1715 ev_io_stop (EV_A_ &pipe_w); 1724 ev_io_stop (EV_A_ &pipe_w);
1716 1725
1856 assert (checkmax >= checkcnt); 1865 assert (checkmax >= checkcnt);
1857 array_verify (EV_A_ (W *)checks, checkcnt); 1866 array_verify (EV_A_ (W *)checks, checkcnt);
1858 1867
1859# if 0 1868# if 0
1860 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next) 1869 for (w = (ev_child *)childs [chain & (EV_PID_HASHSIZE - 1)]; w; w = (ev_child *)((WL)w)->next)
1861 for (signum = EV_NSIG; signum--; ) if (signals [signum].gotsig) 1870 for (signum = EV_NSIG; signum--; ) if (signals [signum].pending)
1862# endif 1871# endif
1863#endif 1872#endif
1864} 1873}
1865#endif 1874#endif
1866 1875
2393inline_size void 2402inline_size void
2394wlist_del (WL *head, WL elem) 2403wlist_del (WL *head, WL elem)
2395{ 2404{
2396 while (*head) 2405 while (*head)
2397 { 2406 {
2398 if (*head == elem) 2407 if (expect_true (*head == elem))
2399 { 2408 {
2400 *head = elem->next; 2409 *head = elem->next;
2401 return; 2410 break;
2402 } 2411 }
2403 2412
2404 head = &(*head)->next; 2413 head = &(*head)->next;
2405 } 2414 }
2406} 2415}
2666 return; 2675 return;
2667 2676
2668 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG)); 2677 assert (("libev: ev_signal_start called with illegal signal number", w->signum > 0 && w->signum < EV_NSIG));
2669 2678
2670#if EV_MULTIPLICITY 2679#if EV_MULTIPLICITY
2671 assert (("libev: tried to attach to a signal from two different loops", 2680 assert (("libev: a signal must not be attached to two different loops",
2672 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop)); 2681 !signals [w->signum - 1].loop || signals [w->signum - 1].loop == loop));
2673 2682
2674 signals [w->signum - 1].loop = EV_A; 2683 signals [w->signum - 1].loop = EV_A;
2675#endif 2684#endif
2676 2685
2747 wlist_del (&signals [w->signum - 1].head, (WL)w); 2756 wlist_del (&signals [w->signum - 1].head, (WL)w);
2748 ev_stop (EV_A_ (W)w); 2757 ev_stop (EV_A_ (W)w);
2749 2758
2750 if (!signals [w->signum - 1].head) 2759 if (!signals [w->signum - 1].head)
2751 { 2760 {
2752 #if EV_MULTIPLICITY 2761#if EV_MULTIPLICITY
2753 signals [w->signum - 1].loop = 0; /* unattach from signal */ 2762 signals [w->signum - 1].loop = 0; /* unattach from signal */
2754 #endif 2763#endif
2755 #if EV_USE_SIGNALFD 2764#if EV_USE_SIGNALFD
2756 if (sigfd >= 0) 2765 if (sigfd >= 0)
2757 { 2766 {
2758 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D 2767 sigprocmask (SIG_UNBLOCK, &sigfd_set, 0);//D
2759 sigdelset (&sigfd_set, w->signum); 2768 sigdelset (&sigfd_set, w->signum);
2760 signalfd (sigfd, &sigfd_set, 0); 2769 signalfd (sigfd, &sigfd_set, 0);
2761 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D 2770 sigprocmask (SIG_BLOCK, &sigfd_set, 0);//D
2762 /*TODO: maybe unblock signal? */ 2771 /*TODO: maybe unblock signal? */
2763 } 2772 }
2764 else 2773 else
2765 #endif 2774#endif
2766 signal (w->signum, SIG_DFL); 2775 signal (w->signum, SIG_DFL);
2767 } 2776 }
2768 2777
2769 EV_FREQUENT_CHECK; 2778 EV_FREQUENT_CHECK;
2770} 2779}
2852 } 2861 }
2853 } 2862 }
2854 2863
2855 if (w->wd >= 0) 2864 if (w->wd >= 0)
2856 { 2865 {
2866 struct statfs sfs;
2867
2857 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w); 2868 wlist_add (&fs_hash [w->wd & (EV_INOTIFY_HASHSIZE - 1)].head, (WL)w);
2858 2869
2859 /* now local changes will be tracked by inotify, but remote changes won't */ 2870 /* now local changes will be tracked by inotify, but remote changes won't */
2860 /* unless the filesystem it known to be local, we therefore still poll */ 2871 /* unless the filesystem it known to be local, we therefore still poll */
2861 /* also do poll on <2.6.25, but with normal frequency */ 2872 /* also do poll on <2.6.25, but with normal frequency */
2862 struct statfs sfs;
2863 2873
2864 if (fs_2625 && !statfs (w->path, &sfs)) 2874 if (fs_2625 && !statfs (w->path, &sfs))
2865 if (sfs.f_type == 0x1373 /* devfs */ 2875 if (sfs.f_type == 0x1373 /* devfs */
2866 || sfs.f_type == 0xEF53 /* ext2/3 */ 2876 || sfs.f_type == 0xEF53 /* ext2/3 */
2867 || sfs.f_type == 0x3153464a /* jfs */ 2877 || sfs.f_type == 0x3153464a /* jfs */
3416 3426
3417void 3427void
3418ev_async_send (EV_P_ ev_async *w) 3428ev_async_send (EV_P_ ev_async *w)
3419{ 3429{
3420 w->sent = 1; 3430 w->sent = 1;
3421 evpipe_write (EV_A_ &gotasync); 3431 evpipe_write (EV_A_ &async_pending);
3422} 3432}
3423#endif 3433#endif
3424 3434
3425/*****************************************************************************/ 3435/*****************************************************************************/
3426 3436

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines