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

Comparing libev/ev.c (file contents):
Revision 1.152 by root, Wed Nov 28 11:15:55 2007 UTC vs.
Revision 1.162 by root, Mon Dec 3 13:41:24 2007 UTC

281 perror (msg); 281 perror (msg);
282 abort (); 282 abort ();
283 } 283 }
284} 284}
285 285
286static void *(*alloc)(void *ptr, size_t size) = realloc; 286static void *(*alloc)(void *ptr, long size);
287 287
288void 288void
289ev_set_allocator (void *(*cb)(void *ptr, size_t size)) 289ev_set_allocator (void *(*cb)(void *ptr, long size))
290{ 290{
291 alloc = cb; 291 alloc = cb;
292} 292}
293 293
294inline_speed void * 294inline_speed void *
295ev_realloc (void *ptr, size_t size) 295ev_realloc (void *ptr, long size)
296{ 296{
297 ptr = alloc (ptr, size); 297 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
298 298
299 if (!ptr && size) 299 if (!ptr && size)
300 { 300 {
301 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", (long)size); 301 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
302 abort (); 302 abort ();
303 } 303 }
304 304
305 return ptr; 305 return ptr;
306} 306}
324{ 324{
325 W w; 325 W w;
326 int events; 326 int events;
327} ANPENDING; 327} ANPENDING;
328 328
329#if EV_USE_INOTIFY
329typedef struct 330typedef struct
330{ 331{
331#if EV_USE_INOTIFY
332 WL head; 332 WL head;
333#endif
334} ANFS; 333} ANFS;
334#endif
335 335
336#if EV_MULTIPLICITY 336#if EV_MULTIPLICITY
337 337
338 struct ev_loop 338 struct ev_loop
339 { 339 {
589static void noinline 589static void noinline
590fd_rearm_all (EV_P) 590fd_rearm_all (EV_P)
591{ 591{
592 int fd; 592 int fd;
593 593
594 /* this should be highly optimised to not do anything but set a flag */
595 for (fd = 0; fd < anfdmax; ++fd) 594 for (fd = 0; fd < anfdmax; ++fd)
596 if (anfds [fd].events) 595 if (anfds [fd].events)
597 { 596 {
598 anfds [fd].events = 0; 597 anfds [fd].events = 0;
599 fd_change (EV_A_ fd); 598 fd_change (EV_A_ fd);
887ev_backend (EV_P) 886ev_backend (EV_P)
888{ 887{
889 return backend; 888 return backend;
890} 889}
891 890
891unsigned int
892ev_loop_count (EV_P)
893{
894 return loop_count;
895}
896
892static void noinline 897static void noinline
893loop_init (EV_P_ unsigned int flags) 898loop_init (EV_P_ unsigned int flags)
894{ 899{
895 if (!backend) 900 if (!backend)
896 { 901 {
905 ev_rt_now = ev_time (); 910 ev_rt_now = ev_time ();
906 mn_now = get_clock (); 911 mn_now = get_clock ();
907 now_floor = mn_now; 912 now_floor = mn_now;
908 rtmn_diff = ev_rt_now - mn_now; 913 rtmn_diff = ev_rt_now - mn_now;
909 914
915 /* pid check not overridable via env */
916#ifndef _WIN32
917 if (flags & EVFLAG_FORKCHECK)
918 curpid = getpid ();
919#endif
920
910 if (!(flags & EVFLAG_NOENV) 921 if (!(flags & EVFLAG_NOENV)
911 && !enable_secure () 922 && !enable_secure ()
912 && getenv ("LIBEV_FLAGS")) 923 && getenv ("LIBEV_FLAGS"))
913 flags = atoi (getenv ("LIBEV_FLAGS")); 924 flags = atoi (getenv ("LIBEV_FLAGS"));
914 925
985 array_free (check, EMPTY0); 996 array_free (check, EMPTY0);
986 997
987 backend = 0; 998 backend = 0;
988} 999}
989 1000
1001void inline_size infy_fork (EV_P);
1002
990void inline_size 1003void inline_size
991loop_fork (EV_P) 1004loop_fork (EV_P)
992{ 1005{
993#if EV_USE_PORT 1006#if EV_USE_PORT
994 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1007 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
996#if EV_USE_KQUEUE 1009#if EV_USE_KQUEUE
997 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1010 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
998#endif 1011#endif
999#if EV_USE_EPOLL 1012#if EV_USE_EPOLL
1000 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1013 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1014#endif
1015#if EV_USE_INOTIFY
1016 infy_fork (EV_A);
1001#endif 1017#endif
1002 1018
1003 if (ev_is_active (&sigev)) 1019 if (ev_is_active (&sigev))
1004 { 1020 {
1005 /* default loop */ 1021 /* default loop */
1265 ev_tstamp odiff = rtmn_diff; 1281 ev_tstamp odiff = rtmn_diff;
1266 1282
1267 /* loop a few times, before making important decisions. 1283 /* loop a few times, before making important decisions.
1268 * on the choice of "4": one iteration isn't enough, 1284 * on the choice of "4": one iteration isn't enough,
1269 * in case we get preempted during the calls to 1285 * in case we get preempted during the calls to
1270 * ev_time and get_clock. a second call is almost guarenteed 1286 * ev_time and get_clock. a second call is almost guaranteed
1271 * to succeed in that case, though. and looping a few more times 1287 * to succeed in that case, though. and looping a few more times
1272 * doesn't hurt either as we only do this on time-jumps or 1288 * doesn't hurt either as we only do this on time-jumps or
1273 * in the unlikely event of getting preempted here. 1289 * in the unlikely event of having been preempted here.
1274 */ 1290 */
1275 for (i = 4; --i; ) 1291 for (i = 4; --i; )
1276 { 1292 {
1277 rtmn_diff = ev_rt_now - mn_now; 1293 rtmn_diff = ev_rt_now - mn_now;
1278 1294
1300 { 1316 {
1301#if EV_PERIODIC_ENABLE 1317#if EV_PERIODIC_ENABLE
1302 periodics_reschedule (EV_A); 1318 periodics_reschedule (EV_A);
1303#endif 1319#endif
1304 1320
1305 /* adjust timers. this is easy, as the offset is the same for all */ 1321 /* adjust timers. this is easy, as the offset is the same for all of them */
1306 for (i = 0; i < timercnt; ++i) 1322 for (i = 0; i < timercnt; ++i)
1307 ((WT)timers [i])->at += ev_rt_now - mn_now; 1323 ((WT)timers [i])->at += ev_rt_now - mn_now;
1308 } 1324 }
1309 1325
1310 mn_now = ev_rt_now; 1326 mn_now = ev_rt_now;
1330{ 1346{
1331 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1347 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1332 ? EVUNLOOP_ONE 1348 ? EVUNLOOP_ONE
1333 : EVUNLOOP_CANCEL; 1349 : EVUNLOOP_CANCEL;
1334 1350
1335 while (activecnt) 1351 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1352
1353 do
1336 { 1354 {
1337 /* we might have forked, so reify kernel state if necessary */ 1355#ifndef _WIN32
1356 if (expect_false (curpid)) /* penalise the forking check even more */
1357 if (expect_false (getpid () != curpid))
1358 {
1359 curpid = getpid ();
1360 postfork = 1;
1361 }
1362#endif
1363
1338 #if EV_FORK_ENABLE 1364#if EV_FORK_ENABLE
1365 /* we might have forked, so queue fork handlers */
1339 if (expect_false (postfork)) 1366 if (expect_false (postfork))
1340 if (forkcnt) 1367 if (forkcnt)
1341 { 1368 {
1342 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1369 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1343 call_pending (EV_A); 1370 call_pending (EV_A);
1344 } 1371 }
1345 #endif 1372#endif
1346 1373
1347 /* queue check watchers (and execute them) */ 1374 /* queue check watchers (and execute them) */
1348 if (expect_false (preparecnt)) 1375 if (expect_false (preparecnt))
1349 { 1376 {
1350 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1377 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1351 call_pending (EV_A); 1378 call_pending (EV_A);
1352 } 1379 }
1353 1380
1381 if (expect_false (!activecnt))
1382 break;
1383
1354 /* we might have forked, so reify kernel state if necessary */ 1384 /* we might have forked, so reify kernel state if necessary */
1355 if (expect_false (postfork)) 1385 if (expect_false (postfork))
1356 loop_fork (EV_A); 1386 loop_fork (EV_A);
1357 1387
1358 /* update fd-related kernel structures */ 1388 /* update fd-related kernel structures */
1359 fd_reify (EV_A); 1389 fd_reify (EV_A);
1360 1390
1361 /* calculate blocking time */ 1391 /* calculate blocking time */
1362 { 1392 {
1363 double block; 1393 ev_tstamp block;
1364 1394
1365 if (flags & EVLOOP_NONBLOCK || idlecnt) 1395 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt))
1366 block = 0.; /* do not block at all */ 1396 block = 0.; /* do not block at all */
1367 else 1397 else
1368 { 1398 {
1369 /* update time to cancel out callback processing overhead */ 1399 /* update time to cancel out callback processing overhead */
1370#if EV_USE_MONOTONIC 1400#if EV_USE_MONOTONIC
1394#endif 1424#endif
1395 1425
1396 if (expect_false (block < 0.)) block = 0.; 1426 if (expect_false (block < 0.)) block = 0.;
1397 } 1427 }
1398 1428
1429 ++loop_count;
1399 backend_poll (EV_A_ block); 1430 backend_poll (EV_A_ block);
1400 } 1431 }
1401 1432
1402 /* update ev_rt_now, do magic */ 1433 /* update ev_rt_now, do magic */
1403 time_update (EV_A); 1434 time_update (EV_A);
1416 if (expect_false (checkcnt)) 1447 if (expect_false (checkcnt))
1417 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1448 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1418 1449
1419 call_pending (EV_A); 1450 call_pending (EV_A);
1420 1451
1421 if (expect_false (loop_done))
1422 break;
1423 } 1452 }
1453 while (expect_true (activecnt && !loop_done));
1424 1454
1425 if (loop_done == EVUNLOOP_ONE) 1455 if (loop_done == EVUNLOOP_ONE)
1426 loop_done = EVUNLOOP_CANCEL; 1456 loop_done = EVUNLOOP_CANCEL;
1427} 1457}
1428 1458
1713# endif 1743# endif
1714 1744
1715#define DEF_STAT_INTERVAL 5.0074891 1745#define DEF_STAT_INTERVAL 5.0074891
1716#define MIN_STAT_INTERVAL 0.1074891 1746#define MIN_STAT_INTERVAL 0.1074891
1717 1747
1718void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1748static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1719 1749
1720#if EV_USE_INOTIFY 1750#if EV_USE_INOTIFY
1721# define EV_INOTIFY_BUFSIZE ((PATH_MAX + sizeof (struct inotify_event)) + 2048) 1751# define EV_INOTIFY_BUFSIZE 8192
1722 1752
1723static void noinline 1753static void noinline
1724infy_add (EV_P_ ev_stat *w) 1754infy_add (EV_P_ ev_stat *w)
1725{ 1755{
1726 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); 1756 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);
1728 if (w->wd < 0) 1758 if (w->wd < 0)
1729 { 1759 {
1730 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 1760 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
1731 1761
1732 /* monitor some parent directory for speedup hints */ 1762 /* monitor some parent directory for speedup hints */
1733 if (errno == ENOENT || errno == EACCES) 1763 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
1734 { 1764 {
1735 char path [PATH_MAX]; 1765 char path [4096];
1736 strcpy (path, w->path); 1766 strcpy (path, w->path);
1737 1767
1738 do 1768 do
1739 { 1769 {
1740 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF 1770 int mask = IN_MASK_ADD | IN_DELETE_SELF | IN_MOVE_SELF
1744 1774
1745 if (!pend) 1775 if (!pend)
1746 break; /* whoops, no '/', complain to your admin */ 1776 break; /* whoops, no '/', complain to your admin */
1747 1777
1748 *pend = 0; 1778 *pend = 0;
1749 w->wd = inotify_add_watch (fs_fd, path, IN_DELETE_SELF | IN_CREATE | IN_MOVED_TO | IN_MASK_ADD); 1779 w->wd = inotify_add_watch (fs_fd, path, mask);
1750 } 1780 }
1751 while (w->wd < 0 && (errno == ENOENT || errno == EACCES)); 1781 while (w->wd < 0 && (errno == ENOENT || errno == EACCES));
1752 } 1782 }
1753 } 1783 }
1754 else 1784 else
1759} 1789}
1760 1790
1761static void noinline 1791static void noinline
1762infy_del (EV_P_ ev_stat *w) 1792infy_del (EV_P_ ev_stat *w)
1763{ 1793{
1764 WL w_;
1765 int slot; 1794 int slot;
1766 int wd = w->wd; 1795 int wd = w->wd;
1767 1796
1768 if (wd < 0) 1797 if (wd < 0)
1769 return; 1798 return;
1798 { 1827 {
1799 w->wd = -1; 1828 w->wd = -1;
1800 infy_add (EV_A_ w); /* re-add, no matter what */ 1829 infy_add (EV_A_ w); /* re-add, no matter what */
1801 } 1830 }
1802 1831
1803 stat_timer_cb (EV_P_ &w->timer, 0); 1832 stat_timer_cb (EV_A_ &w->timer, 0);
1804 } 1833 }
1805 } 1834 }
1806 } 1835 }
1807} 1836}
1808 1837
1832 ev_set_priority (&fs_w, EV_MAXPRI); 1861 ev_set_priority (&fs_w, EV_MAXPRI);
1833 ev_io_start (EV_A_ &fs_w); 1862 ev_io_start (EV_A_ &fs_w);
1834 } 1863 }
1835} 1864}
1836 1865
1866void inline_size
1867infy_fork (EV_P)
1868{
1869 int slot;
1870
1871 if (fs_fd < 0)
1872 return;
1873
1874 close (fs_fd);
1875 fs_fd = inotify_init ();
1876
1877 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1878 {
1879 WL w_ = fs_hash [slot].head;
1880 fs_hash [slot].head = 0;
1881
1882 while (w_)
1883 {
1884 ev_stat *w = (ev_stat *)w_;
1885 w_ = w_->next; /* lets us add this watcher */
1886
1887 w->wd = -1;
1888
1889 if (fs_fd >= 0)
1890 infy_add (EV_A_ w); /* re-add, no matter what */
1891 else
1892 ev_timer_start (EV_A_ &w->timer);
1893 }
1894
1895 }
1896}
1897
1837#endif 1898#endif
1838 1899
1839void 1900void
1840ev_stat_stat (EV_P_ ev_stat *w) 1901ev_stat_stat (EV_P_ ev_stat *w)
1841{ 1902{
1843 w->attr.st_nlink = 0; 1904 w->attr.st_nlink = 0;
1844 else if (!w->attr.st_nlink) 1905 else if (!w->attr.st_nlink)
1845 w->attr.st_nlink = 1; 1906 w->attr.st_nlink = 1;
1846} 1907}
1847 1908
1848void noinline 1909static void noinline
1849stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1910stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1850{ 1911{
1851 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1912 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1852 1913
1853 /* we copy this here each the time so that */ 1914 /* we copy this here each the time so that */
1854 /* prev has the old value when the callback gets invoked */ 1915 /* prev has the old value when the callback gets invoked */
1855 w->prev = w->attr; 1916 w->prev = w->attr;
1856 ev_stat_stat (EV_A_ w); 1917 ev_stat_stat (EV_A_ w);
1857 1918
1858 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1919 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1920 if (
1921 w->prev.st_dev != w->attr.st_dev
1922 || w->prev.st_ino != w->attr.st_ino
1923 || w->prev.st_mode != w->attr.st_mode
1924 || w->prev.st_nlink != w->attr.st_nlink
1925 || w->prev.st_uid != w->attr.st_uid
1926 || w->prev.st_gid != w->attr.st_gid
1927 || w->prev.st_rdev != w->attr.st_rdev
1928 || w->prev.st_size != w->attr.st_size
1929 || w->prev.st_atime != w->attr.st_atime
1930 || w->prev.st_mtime != w->attr.st_mtime
1931 || w->prev.st_ctime != w->attr.st_ctime
1859 { 1932 ) {
1860 #if EV_USE_INOTIFY 1933 #if EV_USE_INOTIFY
1861 infy_del (EV_A_ w); 1934 infy_del (EV_A_ w);
1862 infy_add (EV_A_ w); 1935 infy_add (EV_A_ w);
1863 ev_stat_stat (EV_A_ w); /* avoid race... */ 1936 ev_stat_stat (EV_A_ w); /* avoid race... */
1864 #endif 1937 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines