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

Comparing libev/ev.c (file contents):
Revision 1.229 by root, Fri May 2 08:08:45 2008 UTC vs.
Revision 1.233 by root, Tue May 6 23:34:16 2008 UTC

520 } 520 }
521} 521}
522 522
523/*****************************************************************************/ 523/*****************************************************************************/
524 524
525#define MALLOC_ROUND 4096 /* prefer to allocate in chunks of this size, must be 2**n and >> 4 longs */
526
525int inline_size 527int inline_size
526array_nextsize (int elem, int cur, int cnt) 528array_nextsize (int elem, int cur, int cnt)
527{ 529{
528 int ncur = cur + 1; 530 int ncur = cur + 1;
529 531
530 do 532 do
531 ncur <<= 1; 533 ncur <<= 1;
532 while (cnt > ncur); 534 while (cnt > ncur);
533 535
534 /* if size > 4096, round to 4096 - 4 * longs to accomodate malloc overhead */ 536 /* if size is large, round to MALLOC_ROUND - 4 * longs to accomodate malloc overhead */
535 if (elem * ncur > 4096) 537 if (elem * ncur > MALLOC_ROUND - sizeof (void *) * 4)
536 { 538 {
537 ncur *= elem; 539 ncur *= elem;
538 ncur = (ncur + elem + 4095 + sizeof (void *) * 4) & ~4095; 540 ncur = (ncur + elem + (MALLOC_ROUND - 1) + sizeof (void *) * 4) & ~(MALLOC_ROUND - 1);
539 ncur = ncur - sizeof (void *) * 4; 541 ncur = ncur - sizeof (void *) * 4;
540 ncur /= elem; 542 ncur /= elem;
541 } 543 }
542 544
543 return ncur; 545 return ncur;
772 /* maybe we could use a dummy element at heap [0]? */ 774 /* maybe we could use a dummy element at heap [0]? */
773 if (!p || heap [p]->at <= w->at) 775 if (!p || heap [p]->at <= w->at)
774 break; 776 break;
775 777
776 heap [k] = heap [p]; 778 heap [k] = heap [p];
777 ((W)heap [k])->active = k; 779 ev_active (heap [k]) = k;
778 k = p; 780 k = p;
779 } 781 }
780 782
781 heap [k] = w; 783 heap [k] = w;
782 ((W)heap [k])->active = k; 784 ev_active (heap [k]) = k;
783} 785}
784 786
785/* away from the root */ 787/* away from the root */
786void inline_speed 788void inline_speed
787downheap (WT *heap, int N, int k) 789downheap (WT *heap, int N, int k)
800 802
801 if (w->at <= heap [c]->at) 803 if (w->at <= heap [c]->at)
802 break; 804 break;
803 805
804 heap [k] = heap [c]; 806 heap [k] = heap [c];
805 ((W)heap [k])->active = k; 807 ev_active (heap [k]) = k;
806 808
807 k = c; 809 k = c;
808 } 810 }
809 811
810 heap [k] = w; 812 heap [k] = w;
811 ((W)heap [k])->active = k; 813 ev_active (heap [k]) = k;
812} 814}
813 815
814void inline_size 816void inline_size
815adjustheap (WT *heap, int N, int k) 817adjustheap (WT *heap, int N, int k)
816{ 818{
912pipecb (EV_P_ ev_io *iow, int revents) 914pipecb (EV_P_ ev_io *iow, int revents)
913{ 915{
914#if EV_USE_EVENTFD 916#if EV_USE_EVENTFD
915 if (evfd >= 0) 917 if (evfd >= 0)
916 { 918 {
917 uint64_t counter = 1; 919 uint64_t counter;
918 read (evfd, &counter, sizeof (uint64_t)); 920 read (evfd, &counter, sizeof (uint64_t));
919 } 921 }
920 else 922 else
921#endif 923#endif
922 { 924 {
1523periodics_reschedule (EV_P) 1525periodics_reschedule (EV_P)
1524{ 1526{
1525 int i; 1527 int i;
1526 1528
1527 /* adjust periodics after time jump */ 1529 /* adjust periodics after time jump */
1528 for (i = 0; i < periodiccnt; ++i) 1530 for (i = 1; i <= periodiccnt; ++i)
1529 { 1531 {
1530 ev_periodic *w = (ev_periodic *)periodics [i]; 1532 ev_periodic *w = (ev_periodic *)periodics [i];
1531 1533
1532 if (w->reschedule_cb) 1534 if (w->reschedule_cb)
1533 ev_at (w) = w->reschedule_cb (w, ev_rt_now); 1535 ev_at (w) = w->reschedule_cb (w, ev_rt_now);
1895 ev_start (EV_A_ (W)w, ++timercnt); 1897 ev_start (EV_A_ (W)w, ++timercnt);
1896 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2); 1898 array_needsize (WT, timers, timermax, timercnt + 1, EMPTY2);
1897 timers [timercnt] = (WT)w; 1899 timers [timercnt] = (WT)w;
1898 upheap (timers, timercnt); 1900 upheap (timers, timercnt);
1899 1901
1900 /*assert (("internal timer heap corruption", timers [((W)w)->active] == w));*/ 1902 /*assert (("internal timer heap corruption", timers [ev_active (w)] == w));*/
1901} 1903}
1902 1904
1903void noinline 1905void noinline
1904ev_timer_stop (EV_P_ ev_timer *w) 1906ev_timer_stop (EV_P_ ev_timer *w)
1905{ 1907{
1906 clear_pending (EV_A_ (W)w); 1908 clear_pending (EV_A_ (W)w);
1907 if (expect_false (!ev_is_active (w))) 1909 if (expect_false (!ev_is_active (w)))
1908 return; 1910 return;
1909 1911
1910 assert (("internal timer heap corruption", timers [((W)w)->active] == (WT)w));
1911
1912 { 1912 {
1913 int active = ((W)w)->active; 1913 int active = ev_active (w);
1914
1915 assert (("internal timer heap corruption", timers [active] == (WT)w));
1914 1916
1915 if (expect_true (active < timercnt)) 1917 if (expect_true (active < timercnt))
1916 { 1918 {
1917 timers [active] = timers [timercnt]; 1919 timers [active] = timers [timercnt];
1918 adjustheap (timers, timercnt, active); 1920 adjustheap (timers, timercnt, active);
1932 if (ev_is_active (w)) 1934 if (ev_is_active (w))
1933 { 1935 {
1934 if (w->repeat) 1936 if (w->repeat)
1935 { 1937 {
1936 ev_at (w) = mn_now + w->repeat; 1938 ev_at (w) = mn_now + w->repeat;
1937 adjustheap (timers, timercnt, ((W)w)->active); 1939 adjustheap (timers, timercnt, ev_active (w));
1938 } 1940 }
1939 else 1941 else
1940 ev_timer_stop (EV_A_ w); 1942 ev_timer_stop (EV_A_ w);
1941 } 1943 }
1942 else if (w->repeat) 1944 else if (w->repeat)
1967 ev_start (EV_A_ (W)w, ++periodiccnt); 1969 ev_start (EV_A_ (W)w, ++periodiccnt);
1968 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2); 1970 array_needsize (WT, periodics, periodicmax, periodiccnt + 1, EMPTY2);
1969 periodics [periodiccnt] = (WT)w; 1971 periodics [periodiccnt] = (WT)w;
1970 upheap (periodics, periodiccnt); 1972 upheap (periodics, periodiccnt);
1971 1973
1972 /*assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));*/ 1974 /*assert (("internal periodic heap corruption", periodics [ev_active (w)] == w));*/
1973} 1975}
1974 1976
1975void noinline 1977void noinline
1976ev_periodic_stop (EV_P_ ev_periodic *w) 1978ev_periodic_stop (EV_P_ ev_periodic *w)
1977{ 1979{
1978 clear_pending (EV_A_ (W)w); 1980 clear_pending (EV_A_ (W)w);
1979 if (expect_false (!ev_is_active (w))) 1981 if (expect_false (!ev_is_active (w)))
1980 return; 1982 return;
1981 1983
1982 assert (("internal periodic heap corruption", periodics [((W)w)->active] == (WT)w));
1983
1984 { 1984 {
1985 int active = ((W)w)->active; 1985 int active = ev_active (w);
1986
1987 assert (("internal periodic heap corruption", periodics [active] == (WT)w));
1986 1988
1987 if (expect_true (active < periodiccnt)) 1989 if (expect_true (active < periodiccnt))
1988 { 1990 {
1989 periodics [active] = periodics [periodiccnt]; 1991 periodics [active] = periodics [periodiccnt];
1990 adjustheap (periodics, periodiccnt, active); 1992 adjustheap (periodics, periodiccnt, active);
2114 if (w->wd < 0) 2116 if (w->wd < 0)
2115 { 2117 {
2116 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */ 2118 ev_timer_start (EV_A_ &w->timer); /* this is not race-free, so we still need to recheck periodically */
2117 2119
2118 /* monitor some parent directory for speedup hints */ 2120 /* monitor some parent directory for speedup hints */
2121 /* note that exceeding the hardcoded limit is not a correctness issue, */
2122 /* but an efficiency issue only */
2119 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096) 2123 if ((errno == ENOENT || errno == EACCES) && strlen (w->path) < 4096)
2120 { 2124 {
2121 char path [4096]; 2125 char path [4096];
2122 strcpy (path, w->path); 2126 strcpy (path, w->path);
2123 2127
2368 clear_pending (EV_A_ (W)w); 2372 clear_pending (EV_A_ (W)w);
2369 if (expect_false (!ev_is_active (w))) 2373 if (expect_false (!ev_is_active (w)))
2370 return; 2374 return;
2371 2375
2372 { 2376 {
2373 int active = ((W)w)->active; 2377 int active = ev_active (w);
2374 2378
2375 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]]; 2379 idles [ABSPRI (w)][active - 1] = idles [ABSPRI (w)][--idlecnt [ABSPRI (w)]];
2376 ((W)idles [ABSPRI (w)][active - 1])->active = active; 2380 ev_active (idles [ABSPRI (w)][active - 1]) = active;
2377 2381
2378 ev_stop (EV_A_ (W)w); 2382 ev_stop (EV_A_ (W)w);
2379 --idleall; 2383 --idleall;
2380 } 2384 }
2381} 2385}
2398 clear_pending (EV_A_ (W)w); 2402 clear_pending (EV_A_ (W)w);
2399 if (expect_false (!ev_is_active (w))) 2403 if (expect_false (!ev_is_active (w)))
2400 return; 2404 return;
2401 2405
2402 { 2406 {
2403 int active = ((W)w)->active; 2407 int active = ev_active (w);
2408
2404 prepares [active - 1] = prepares [--preparecnt]; 2409 prepares [active - 1] = prepares [--preparecnt];
2405 ((W)prepares [active - 1])->active = active; 2410 ev_active (prepares [active - 1]) = active;
2406 } 2411 }
2407 2412
2408 ev_stop (EV_A_ (W)w); 2413 ev_stop (EV_A_ (W)w);
2409} 2414}
2410 2415
2425 clear_pending (EV_A_ (W)w); 2430 clear_pending (EV_A_ (W)w);
2426 if (expect_false (!ev_is_active (w))) 2431 if (expect_false (!ev_is_active (w)))
2427 return; 2432 return;
2428 2433
2429 { 2434 {
2430 int active = ((W)w)->active; 2435 int active = ev_active (w);
2436
2431 checks [active - 1] = checks [--checkcnt]; 2437 checks [active - 1] = checks [--checkcnt];
2432 ((W)checks [active - 1])->active = active; 2438 ev_active (checks [active - 1]) = active;
2433 } 2439 }
2434 2440
2435 ev_stop (EV_A_ (W)w); 2441 ev_stop (EV_A_ (W)w);
2436} 2442}
2437 2443
2533 clear_pending (EV_A_ (W)w); 2539 clear_pending (EV_A_ (W)w);
2534 if (expect_false (!ev_is_active (w))) 2540 if (expect_false (!ev_is_active (w)))
2535 return; 2541 return;
2536 2542
2537 { 2543 {
2538 int active = ((W)w)->active; 2544 int active = ev_active (w);
2545
2539 forks [active - 1] = forks [--forkcnt]; 2546 forks [active - 1] = forks [--forkcnt];
2540 ((W)forks [active - 1])->active = active; 2547 ev_active (forks [active - 1]) = active;
2541 } 2548 }
2542 2549
2543 ev_stop (EV_A_ (W)w); 2550 ev_stop (EV_A_ (W)w);
2544} 2551}
2545#endif 2552#endif
2564 clear_pending (EV_A_ (W)w); 2571 clear_pending (EV_A_ (W)w);
2565 if (expect_false (!ev_is_active (w))) 2572 if (expect_false (!ev_is_active (w)))
2566 return; 2573 return;
2567 2574
2568 { 2575 {
2569 int active = ((W)w)->active; 2576 int active = ev_active (w);
2577
2570 asyncs [active - 1] = asyncs [--asynccnt]; 2578 asyncs [active - 1] = asyncs [--asynccnt];
2571 ((W)asyncs [active - 1])->active = active; 2579 ev_active (asyncs [active - 1]) = active;
2572 } 2580 }
2573 2581
2574 ev_stop (EV_A_ (W)w); 2582 ev_stop (EV_A_ (W)w);
2575} 2583}
2576 2584

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines