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

Comparing libev/ev.c (file contents):
Revision 1.153 by root, Wed Nov 28 11:41:18 2007 UTC vs.
Revision 1.161 by root, Sat Dec 1 23:43:45 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);
905 ev_rt_now = ev_time (); 904 ev_rt_now = ev_time ();
906 mn_now = get_clock (); 905 mn_now = get_clock ();
907 now_floor = mn_now; 906 now_floor = mn_now;
908 rtmn_diff = ev_rt_now - mn_now; 907 rtmn_diff = ev_rt_now - mn_now;
909 908
909 /* pid check not overridable via env */
910#ifndef _WIN32
911 if (flags & EVFLAG_FORKCHECK)
912 curpid = getpid ();
913#endif
914
910 if (!(flags & EVFLAG_NOENV) 915 if (!(flags & EVFLAG_NOENV)
911 && !enable_secure () 916 && !enable_secure ()
912 && getenv ("LIBEV_FLAGS")) 917 && getenv ("LIBEV_FLAGS"))
913 flags = atoi (getenv ("LIBEV_FLAGS")); 918 flags = atoi (getenv ("LIBEV_FLAGS"));
914 919
985 array_free (check, EMPTY0); 990 array_free (check, EMPTY0);
986 991
987 backend = 0; 992 backend = 0;
988} 993}
989 994
995void inline_size infy_fork (EV_P);
996
990void inline_size 997void inline_size
991loop_fork (EV_P) 998loop_fork (EV_P)
992{ 999{
993#if EV_USE_PORT 1000#if EV_USE_PORT
994 if (backend == EVBACKEND_PORT ) port_fork (EV_A); 1001 if (backend == EVBACKEND_PORT ) port_fork (EV_A);
996#if EV_USE_KQUEUE 1003#if EV_USE_KQUEUE
997 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A); 1004 if (backend == EVBACKEND_KQUEUE) kqueue_fork (EV_A);
998#endif 1005#endif
999#if EV_USE_EPOLL 1006#if EV_USE_EPOLL
1000 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A); 1007 if (backend == EVBACKEND_EPOLL ) epoll_fork (EV_A);
1008#endif
1009#if EV_USE_INOTIFY
1010 infy_fork (EV_A);
1001#endif 1011#endif
1002 1012
1003 if (ev_is_active (&sigev)) 1013 if (ev_is_active (&sigev))
1004 { 1014 {
1005 /* default loop */ 1015 /* default loop */
1265 ev_tstamp odiff = rtmn_diff; 1275 ev_tstamp odiff = rtmn_diff;
1266 1276
1267 /* loop a few times, before making important decisions. 1277 /* loop a few times, before making important decisions.
1268 * on the choice of "4": one iteration isn't enough, 1278 * on the choice of "4": one iteration isn't enough,
1269 * in case we get preempted during the calls to 1279 * in case we get preempted during the calls to
1270 * ev_time and get_clock. a second call is almost guarenteed 1280 * ev_time and get_clock. a second call is almost guaranteed
1271 * to succeed in that case, though. and looping a few more times 1281 * 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 1282 * doesn't hurt either as we only do this on time-jumps or
1273 * in the unlikely event of getting preempted here. 1283 * in the unlikely event of having been preempted here.
1274 */ 1284 */
1275 for (i = 4; --i; ) 1285 for (i = 4; --i; )
1276 { 1286 {
1277 rtmn_diff = ev_rt_now - mn_now; 1287 rtmn_diff = ev_rt_now - mn_now;
1278 1288
1300 { 1310 {
1301#if EV_PERIODIC_ENABLE 1311#if EV_PERIODIC_ENABLE
1302 periodics_reschedule (EV_A); 1312 periodics_reschedule (EV_A);
1303#endif 1313#endif
1304 1314
1305 /* adjust timers. this is easy, as the offset is the same for all */ 1315 /* adjust timers. this is easy, as the offset is the same for all of them */
1306 for (i = 0; i < timercnt; ++i) 1316 for (i = 0; i < timercnt; ++i)
1307 ((WT)timers [i])->at += ev_rt_now - mn_now; 1317 ((WT)timers [i])->at += ev_rt_now - mn_now;
1308 } 1318 }
1309 1319
1310 mn_now = ev_rt_now; 1320 mn_now = ev_rt_now;
1330{ 1340{
1331 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1341 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1332 ? EVUNLOOP_ONE 1342 ? EVUNLOOP_ONE
1333 : EVUNLOOP_CANCEL; 1343 : EVUNLOOP_CANCEL;
1334 1344
1335 while (activecnt) 1345 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1346
1347 do
1336 { 1348 {
1337 /* we might have forked, so reify kernel state if necessary */ 1349#ifndef _WIN32
1350 if (expect_false (curpid)) /* penalise the forking check even more */
1351 if (expect_false (getpid () != curpid))
1352 {
1353 curpid = getpid ();
1354 postfork = 1;
1355 }
1356#endif
1357
1338 #if EV_FORK_ENABLE 1358#if EV_FORK_ENABLE
1359 /* we might have forked, so queue fork handlers */
1339 if (expect_false (postfork)) 1360 if (expect_false (postfork))
1340 if (forkcnt) 1361 if (forkcnt)
1341 { 1362 {
1342 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK); 1363 queue_events (EV_A_ (W *)forks, forkcnt, EV_FORK);
1343 call_pending (EV_A); 1364 call_pending (EV_A);
1344 } 1365 }
1345 #endif 1366#endif
1346 1367
1347 /* queue check watchers (and execute them) */ 1368 /* queue check watchers (and execute them) */
1348 if (expect_false (preparecnt)) 1369 if (expect_false (preparecnt))
1349 { 1370 {
1350 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1371 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1351 call_pending (EV_A); 1372 call_pending (EV_A);
1352 } 1373 }
1353 1374
1375 if (expect_false (!activecnt))
1376 break;
1377
1354 /* we might have forked, so reify kernel state if necessary */ 1378 /* we might have forked, so reify kernel state if necessary */
1355 if (expect_false (postfork)) 1379 if (expect_false (postfork))
1356 loop_fork (EV_A); 1380 loop_fork (EV_A);
1357 1381
1358 /* update fd-related kernel structures */ 1382 /* update fd-related kernel structures */
1359 fd_reify (EV_A); 1383 fd_reify (EV_A);
1360 1384
1361 /* calculate blocking time */ 1385 /* calculate blocking time */
1362 { 1386 {
1363 double block; 1387 ev_tstamp block;
1364 1388
1365 if (flags & EVLOOP_NONBLOCK || idlecnt) 1389 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt))
1366 block = 0.; /* do not block at all */ 1390 block = 0.; /* do not block at all */
1367 else 1391 else
1368 { 1392 {
1369 /* update time to cancel out callback processing overhead */ 1393 /* update time to cancel out callback processing overhead */
1370#if EV_USE_MONOTONIC 1394#if EV_USE_MONOTONIC
1416 if (expect_false (checkcnt)) 1440 if (expect_false (checkcnt))
1417 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1441 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1418 1442
1419 call_pending (EV_A); 1443 call_pending (EV_A);
1420 1444
1421 if (expect_false (loop_done))
1422 break;
1423 } 1445 }
1446 while (expect_true (activecnt && !loop_done));
1424 1447
1425 if (loop_done == EVUNLOOP_ONE) 1448 if (loop_done == EVUNLOOP_ONE)
1426 loop_done = EVUNLOOP_CANCEL; 1449 loop_done = EVUNLOOP_CANCEL;
1427} 1450}
1428 1451
1713# endif 1736# endif
1714 1737
1715#define DEF_STAT_INTERVAL 5.0074891 1738#define DEF_STAT_INTERVAL 5.0074891
1716#define MIN_STAT_INTERVAL 0.1074891 1739#define MIN_STAT_INTERVAL 0.1074891
1717 1740
1718void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents); 1741static void noinline stat_timer_cb (EV_P_ ev_timer *w_, int revents);
1719 1742
1720#if EV_USE_INOTIFY 1743#if EV_USE_INOTIFY
1721# define EV_INOTIFY_BUFSIZE 8192 1744# define EV_INOTIFY_BUFSIZE 8192
1722 1745
1723static void noinline 1746static void noinline
1831 ev_set_priority (&fs_w, EV_MAXPRI); 1854 ev_set_priority (&fs_w, EV_MAXPRI);
1832 ev_io_start (EV_A_ &fs_w); 1855 ev_io_start (EV_A_ &fs_w);
1833 } 1856 }
1834} 1857}
1835 1858
1859void inline_size
1860infy_fork (EV_P)
1861{
1862 int slot;
1863
1864 if (fs_fd < 0)
1865 return;
1866
1867 close (fs_fd);
1868 fs_fd = inotify_init ();
1869
1870 for (slot = 0; slot < EV_INOTIFY_HASHSIZE; ++slot)
1871 {
1872 WL w_ = fs_hash [slot].head;
1873 fs_hash [slot].head = 0;
1874
1875 while (w_)
1876 {
1877 ev_stat *w = (ev_stat *)w_;
1878 w_ = w_->next; /* lets us add this watcher */
1879
1880 w->wd = -1;
1881
1882 if (fs_fd >= 0)
1883 infy_add (EV_A_ w); /* re-add, no matter what */
1884 else
1885 ev_timer_start (EV_A_ &w->timer);
1886 }
1887
1888 }
1889}
1890
1836#endif 1891#endif
1837 1892
1838void 1893void
1839ev_stat_stat (EV_P_ ev_stat *w) 1894ev_stat_stat (EV_P_ ev_stat *w)
1840{ 1895{
1842 w->attr.st_nlink = 0; 1897 w->attr.st_nlink = 0;
1843 else if (!w->attr.st_nlink) 1898 else if (!w->attr.st_nlink)
1844 w->attr.st_nlink = 1; 1899 w->attr.st_nlink = 1;
1845} 1900}
1846 1901
1847void noinline 1902static void noinline
1848stat_timer_cb (EV_P_ ev_timer *w_, int revents) 1903stat_timer_cb (EV_P_ ev_timer *w_, int revents)
1849{ 1904{
1850 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer)); 1905 ev_stat *w = (ev_stat *)(((char *)w_) - offsetof (ev_stat, timer));
1851 1906
1852 /* we copy this here each the time so that */ 1907 /* we copy this here each the time so that */
1853 /* prev has the old value when the callback gets invoked */ 1908 /* prev has the old value when the callback gets invoked */
1854 w->prev = w->attr; 1909 w->prev = w->attr;
1855 ev_stat_stat (EV_A_ w); 1910 ev_stat_stat (EV_A_ w);
1856 1911
1857 if (memcmp (&w->prev, &w->attr, sizeof (ev_statdata))) 1912 /* memcmp doesn't work on netbsd, they.... do stuff to their struct stat */
1913 if (
1914 w->prev.st_dev != w->attr.st_dev
1915 || w->prev.st_ino != w->attr.st_ino
1916 || w->prev.st_mode != w->attr.st_mode
1917 || w->prev.st_nlink != w->attr.st_nlink
1918 || w->prev.st_uid != w->attr.st_uid
1919 || w->prev.st_gid != w->attr.st_gid
1920 || w->prev.st_rdev != w->attr.st_rdev
1921 || w->prev.st_size != w->attr.st_size
1922 || w->prev.st_atime != w->attr.st_atime
1923 || w->prev.st_mtime != w->attr.st_mtime
1924 || w->prev.st_ctime != w->attr.st_ctime
1858 { 1925 ) {
1859 #if EV_USE_INOTIFY 1926 #if EV_USE_INOTIFY
1860 infy_del (EV_A_ w); 1927 infy_del (EV_A_ w);
1861 infy_add (EV_A_ w); 1928 infy_add (EV_A_ w);
1862 ev_stat_stat (EV_A_ w); /* avoid race... */ 1929 ev_stat_stat (EV_A_ w); /* avoid race... */
1863 #endif 1930 #endif

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines