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

Comparing libev/ev.c (file contents):
Revision 1.157 by root, Wed Nov 28 20:58:32 2007 UTC vs.
Revision 1.163 by root, Wed Dec 5 13:54:36 2007 UTC

396{ 396{
397 return ev_rt_now; 397 return ev_rt_now;
398} 398}
399#endif 399#endif
400 400
401#define array_roundsize(type,n) (((n) | 4) & ~3) 401int inline_size
402array_nextsize (int elem, int cur, int cnt)
403{
404 int ncur = cur + 1;
405
406 do
407 ncur <<= 1;
408 while (cnt > ncur);
409
410 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */
411 if (elem * ncur > 4096)
412 {
413 ncur *= elem;
414 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095;
415 ncur = ncur - sizeof (void *) * 4;
416 ncur /= elem;
417 }
418
419 return ncur;
420}
421
422inline_speed void *
423array_realloc (int elem, void *base, int *cur, int cnt)
424{
425 *cur = array_nextsize (elem, *cur, cnt);
426 return ev_realloc (base, elem * *cur);
427}
402 428
403#define array_needsize(type,base,cur,cnt,init) \ 429#define array_needsize(type,base,cur,cnt,init) \
404 if (expect_false ((cnt) > cur)) \ 430 if (expect_false ((cnt) > (cur))) \
405 { \ 431 { \
406 int newcnt = cur; \ 432 int ocur_ = (cur); \
407 do \ 433 (base) = (type *)array_realloc \
408 { \ 434 (sizeof (type), (base), &(cur), (cnt)); \
409 newcnt = array_roundsize (type, newcnt << 1); \ 435 init ((base) + (ocur_), (cur) - ocur_); \
410 } \
411 while ((cnt) > newcnt); \
412 \
413 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
414 init (base + cur, newcnt - cur); \
415 cur = newcnt; \
416 } 436 }
417 437
438#if 0
418#define array_slim(type,stem) \ 439#define array_slim(type,stem) \
419 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 440 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
420 { \ 441 { \
421 stem ## max = array_roundsize (stem ## cnt >> 1); \ 442 stem ## max = array_roundsize (stem ## cnt >> 1); \
422 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\ 443 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
423 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 444 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
424 } 445 }
446#endif
425 447
426#define array_free(stem, idx) \ 448#define array_free(stem, idx) \
427 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 449 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
428 450
429/*****************************************************************************/ 451/*****************************************************************************/
886ev_backend (EV_P) 908ev_backend (EV_P)
887{ 909{
888 return backend; 910 return backend;
889} 911}
890 912
913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
917}
918
891static void noinline 919static void noinline
892loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
893{ 921{
894 if (!backend) 922 if (!backend)
895 { 923 {
903 931
904 ev_rt_now = ev_time (); 932 ev_rt_now = ev_time ();
905 mn_now = get_clock (); 933 mn_now = get_clock ();
906 now_floor = mn_now; 934 now_floor = mn_now;
907 rtmn_diff = ev_rt_now - mn_now; 935 rtmn_diff = ev_rt_now - mn_now;
936
937 /* pid check not overridable via env */
938#ifndef _WIN32
939 if (flags & EVFLAG_FORKCHECK)
940 curpid = getpid ();
941#endif
908 942
909 if (!(flags & EVFLAG_NOENV) 943 if (!(flags & EVFLAG_NOENV)
910 && !enable_secure () 944 && !enable_secure ()
911 && getenv ("LIBEV_FLAGS")) 945 && getenv ("LIBEV_FLAGS"))
912 flags = atoi (getenv ("LIBEV_FLAGS")); 946 flags = atoi (getenv ("LIBEV_FLAGS"));
1334{ 1368{
1335 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK) 1369 loop_done = flags & (EVLOOP_ONESHOT | EVLOOP_NONBLOCK)
1336 ? EVUNLOOP_ONE 1370 ? EVUNLOOP_ONE
1337 : EVUNLOOP_CANCEL; 1371 : EVUNLOOP_CANCEL;
1338 1372
1339 while (activecnt) 1373 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1374
1375 do
1340 { 1376 {
1377#ifndef _WIN32
1378 if (expect_false (curpid)) /* penalise the forking check even more */
1379 if (expect_false (getpid () != curpid))
1380 {
1381 curpid = getpid ();
1382 postfork = 1;
1383 }
1384#endif
1385
1341#if EV_FORK_ENABLE 1386#if EV_FORK_ENABLE
1342 /* we might have forked, so queue fork handlers */ 1387 /* we might have forked, so queue fork handlers */
1343 if (expect_false (postfork)) 1388 if (expect_false (postfork))
1344 if (forkcnt) 1389 if (forkcnt)
1345 { 1390 {
1353 { 1398 {
1354 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1399 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1355 call_pending (EV_A); 1400 call_pending (EV_A);
1356 } 1401 }
1357 1402
1403 if (expect_false (!activecnt))
1404 break;
1405
1358 /* we might have forked, so reify kernel state if necessary */ 1406 /* we might have forked, so reify kernel state if necessary */
1359 if (expect_false (postfork)) 1407 if (expect_false (postfork))
1360 loop_fork (EV_A); 1408 loop_fork (EV_A);
1361 1409
1362 /* update fd-related kernel structures */ 1410 /* update fd-related kernel structures */
1364 1412
1365 /* calculate blocking time */ 1413 /* calculate blocking time */
1366 { 1414 {
1367 ev_tstamp block; 1415 ev_tstamp block;
1368 1416
1369 if (flags & EVLOOP_NONBLOCK || idlecnt) 1417 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt))
1370 block = 0.; /* do not block at all */ 1418 block = 0.; /* do not block at all */
1371 else 1419 else
1372 { 1420 {
1373 /* update time to cancel out callback processing overhead */ 1421 /* update time to cancel out callback processing overhead */
1374#if EV_USE_MONOTONIC 1422#if EV_USE_MONOTONIC
1398#endif 1446#endif
1399 1447
1400 if (expect_false (block < 0.)) block = 0.; 1448 if (expect_false (block < 0.)) block = 0.;
1401 } 1449 }
1402 1450
1451 ++loop_count;
1403 backend_poll (EV_A_ block); 1452 backend_poll (EV_A_ block);
1404 } 1453 }
1405 1454
1406 /* update ev_rt_now, do magic */ 1455 /* update ev_rt_now, do magic */
1407 time_update (EV_A); 1456 time_update (EV_A);
1420 if (expect_false (checkcnt)) 1469 if (expect_false (checkcnt))
1421 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1470 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1422 1471
1423 call_pending (EV_A); 1472 call_pending (EV_A);
1424 1473
1425 if (expect_false (loop_done))
1426 break;
1427 } 1474 }
1475 while (expect_true (activecnt && !loop_done));
1428 1476
1429 if (loop_done == EVUNLOOP_ONE) 1477 if (loop_done == EVUNLOOP_ONE)
1430 loop_done = EVUNLOOP_CANCEL; 1478 loop_done = EVUNLOOP_CANCEL;
1431} 1479}
1432 1480

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines