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

Comparing libev/ev.c (file contents):
Revision 1.158 by root, Thu Nov 29 17:28:13 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/*****************************************************************************/
884 906
885unsigned int 907unsigned int
886ev_backend (EV_P) 908ev_backend (EV_P)
887{ 909{
888 return backend; 910 return backend;
911}
912
913unsigned int
914ev_loop_count (EV_P)
915{
916 return loop_count;
889} 917}
890 918
891static void noinline 919static void noinline
892loop_init (EV_P_ unsigned int flags) 920loop_init (EV_P_ unsigned int flags)
893{ 921{
1342 ? EVUNLOOP_ONE 1370 ? EVUNLOOP_ONE
1343 : EVUNLOOP_CANCEL; 1371 : EVUNLOOP_CANCEL;
1344 1372
1345 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */ 1373 call_pending (EV_A); /* in case we recurse, ensure ordering stays nice and clean */
1346 1374
1347 while (activecnt) 1375 do
1348 { 1376 {
1349#ifndef _WIN32 1377#ifndef _WIN32
1350 if (expect_false (curpid)) /* penalise the forking check even more */ 1378 if (expect_false (curpid)) /* penalise the forking check even more */
1351 if (expect_false (getpid () != curpid)) 1379 if (expect_false (getpid () != curpid))
1352 { 1380 {
1370 { 1398 {
1371 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE); 1399 queue_events (EV_A_ (W *)prepares, preparecnt, EV_PREPARE);
1372 call_pending (EV_A); 1400 call_pending (EV_A);
1373 } 1401 }
1374 1402
1403 if (expect_false (!activecnt))
1404 break;
1405
1375 /* we might have forked, so reify kernel state if necessary */ 1406 /* we might have forked, so reify kernel state if necessary */
1376 if (expect_false (postfork)) 1407 if (expect_false (postfork))
1377 loop_fork (EV_A); 1408 loop_fork (EV_A);
1378 1409
1379 /* update fd-related kernel structures */ 1410 /* update fd-related kernel structures */
1381 1412
1382 /* calculate blocking time */ 1413 /* calculate blocking time */
1383 { 1414 {
1384 ev_tstamp block; 1415 ev_tstamp block;
1385 1416
1386 if (flags & EVLOOP_NONBLOCK || idlecnt) 1417 if (expect_false (flags & EVLOOP_NONBLOCK || idlecnt || !activecnt))
1387 block = 0.; /* do not block at all */ 1418 block = 0.; /* do not block at all */
1388 else 1419 else
1389 { 1420 {
1390 /* update time to cancel out callback processing overhead */ 1421 /* update time to cancel out callback processing overhead */
1391#if EV_USE_MONOTONIC 1422#if EV_USE_MONOTONIC
1415#endif 1446#endif
1416 1447
1417 if (expect_false (block < 0.)) block = 0.; 1448 if (expect_false (block < 0.)) block = 0.;
1418 } 1449 }
1419 1450
1451 ++loop_count;
1420 backend_poll (EV_A_ block); 1452 backend_poll (EV_A_ block);
1421 } 1453 }
1422 1454
1423 /* update ev_rt_now, do magic */ 1455 /* update ev_rt_now, do magic */
1424 time_update (EV_A); 1456 time_update (EV_A);
1437 if (expect_false (checkcnt)) 1469 if (expect_false (checkcnt))
1438 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1470 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1439 1471
1440 call_pending (EV_A); 1472 call_pending (EV_A);
1441 1473
1442 if (expect_false (loop_done))
1443 break;
1444 } 1474 }
1475 while (expect_true (activecnt && !loop_done));
1445 1476
1446 if (loop_done == EVUNLOOP_ONE) 1477 if (loop_done == EVUNLOOP_ONE)
1447 loop_done = EVUNLOOP_CANCEL; 1478 loop_done = EVUNLOOP_CANCEL;
1448} 1479}
1449 1480

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines