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

Comparing libev/ev.c (file contents):
Revision 1.73 by root, Tue Nov 6 16:27:10 2007 UTC vs.
Revision 1.77 by root, Thu Nov 8 00:44:17 2007 UTC

268ev_now (EV_P) 268ev_now (EV_P)
269{ 269{
270 return rt_now; 270 return rt_now;
271} 271}
272 272
273#define array_roundsize(base,n) ((n) | 4 & ~3) 273#define array_roundsize(type,n) ((n) | 4 & ~3)
274 274
275#define array_needsize(base,cur,cnt,init) \ 275#define array_needsize(type,base,cur,cnt,init) \
276 if (expect_false ((cnt) > cur)) \ 276 if (expect_false ((cnt) > cur)) \
277 { \ 277 { \
278 int newcnt = cur; \ 278 int newcnt = cur; \
279 do \ 279 do \
280 { \ 280 { \
281 newcnt = array_roundsize (base, newcnt << 1); \ 281 newcnt = array_roundsize (type, newcnt << 1); \
282 } \ 282 } \
283 while ((cnt) > newcnt); \ 283 while ((cnt) > newcnt); \
284 \ 284 \
285 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 285 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
286 init (base + cur, newcnt - cur); \ 286 init (base + cur, newcnt - cur); \
287 cur = newcnt; \ 287 cur = newcnt; \
288 } 288 }
289 289
290#define array_slim(stem) \ 290#define array_slim(type,stem) \
291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
292 { \ 292 { \
293 stem ## max = array_roundsize (stem ## cnt >> 1); \ 293 stem ## max = array_roundsize (stem ## cnt >> 1); \
294 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
296 } 296 }
297 297
298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */ 298/* microsoft's pseudo-c is quite far from C as the rest of the world and the standard knows it */
299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */ 299/* bringing us everlasting joy in form of stupid extra macros that are not required in C */
326 pendings [ABSPRI (w)][w->pending - 1].events |= events; 326 pendings [ABSPRI (w)][w->pending - 1].events |= events;
327 return; 327 return;
328 } 328 }
329 329
330 w->pending = ++pendingcnt [ABSPRI (w)]; 330 w->pending = ++pendingcnt [ABSPRI (w)];
331 array_needsize (pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void)); 331 array_needsize (ANPENDING, pendings [ABSPRI (w)], pendingmax [ABSPRI (w)], pendingcnt [ABSPRI (w)], (void));
332 pendings [ABSPRI (w)][w->pending - 1].w = w; 332 pendings [ABSPRI (w)][w->pending - 1].w = w;
333 pendings [ABSPRI (w)][w->pending - 1].events = events; 333 pendings [ABSPRI (w)][w->pending - 1].events = events;
334} 334}
335 335
336static void 336static void
391 return; 391 return;
392 392
393 anfds [fd].reify = 1; 393 anfds [fd].reify = 1;
394 394
395 ++fdchangecnt; 395 ++fdchangecnt;
396 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 396 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
397 fdchanges [fdchangecnt - 1] = fd; 397 fdchanges [fdchangecnt - 1] = fd;
398} 398}
399 399
400static void 400static void
401fd_kill (EV_P_ int fd) 401fd_kill (EV_P_ int fd)
541 541
542 if (!gotsig) 542 if (!gotsig)
543 { 543 {
544 int old_errno = errno; 544 int old_errno = errno;
545 gotsig = 1; 545 gotsig = 1;
546#ifdef WIN32
547 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
548#else
546 write (sigpipe [1], &signum, 1); 549 write (sigpipe [1], &signum, 1);
550#endif
547 errno = old_errno; 551 errno = old_errno;
548 } 552 }
549} 553}
550 554
551static void 555static void
552sigcb (EV_P_ struct ev_io *iow, int revents) 556sigcb (EV_P_ struct ev_io *iow, int revents)
553{ 557{
554 WL w; 558 WL w;
555 int signum; 559 int signum;
556 560
561#ifdef WIN32
562 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
563#else
557 read (sigpipe [0], &revents, 1); 564 read (sigpipe [0], &revents, 1);
565#endif
558 gotsig = 0; 566 gotsig = 0;
559 567
560 for (signum = signalmax; signum--; ) 568 for (signum = signalmax; signum--; )
561 if (signals [signum].gotsig) 569 if (signals [signum].gotsig)
562 { 570 {
888 if (method) 896 if (method)
889 postfork = 1; 897 postfork = 1;
890} 898}
891 899
892/*****************************************************************************/ 900/*****************************************************************************/
901
902static int
903any_pending (EV_P)
904{
905 int pri;
906
907 for (pri = NUMPRI; pri--; )
908 if (pendingcnt [pri])
909 return 1;
910
911 return 0;
912}
893 913
894static void 914static void
895call_pending (EV_P) 915call_pending (EV_P)
896{ 916{
897 int pri; 917 int pri;
940 struct ev_periodic *w = periodics [0]; 960 struct ev_periodic *w = periodics [0];
941 961
942 assert (("inactive timer on periodic heap detected", ev_is_active (w))); 962 assert (("inactive timer on periodic heap detected", ev_is_active (w)));
943 963
944 /* first reschedule or stop timer */ 964 /* first reschedule or stop timer */
965 if (w->reschedule_cb)
966 {
967 ev_tstamp at = ((WT)w)->at = w->reschedule_cb (w, rt_now + 0.0001);
968
969 assert (("ev_periodic reschedule callback returned time in the past", ((WT)w)->at > rt_now));
970 downheap ((WT *)periodics, periodiccnt, 0);
971 }
945 if (w->interval) 972 else if (w->interval)
946 { 973 {
947 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval; 974 ((WT)w)->at += floor ((rt_now - ((WT)w)->at) / w->interval + 1.) * w->interval;
948 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now)); 975 assert (("ev_periodic timeout in the past detected while processing timers, negative interval?", ((WT)w)->at > rt_now));
949 downheap ((WT *)periodics, periodiccnt, 0); 976 downheap ((WT *)periodics, periodiccnt, 0);
950 } 977 }
963 /* adjust periodics after time jump */ 990 /* adjust periodics after time jump */
964 for (i = 0; i < periodiccnt; ++i) 991 for (i = 0; i < periodiccnt; ++i)
965 { 992 {
966 struct ev_periodic *w = periodics [i]; 993 struct ev_periodic *w = periodics [i];
967 994
995 if (w->reschedule_cb)
996 ((WT)w)->at = w->reschedule_cb (w, rt_now);
968 if (w->interval) 997 else if (w->interval)
969 {
970 ev_tstamp diff = ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 998 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
971
972 if (fabs (diff) >= 1e-4)
973 {
974 ev_periodic_stop (EV_A_ w);
975 ev_periodic_start (EV_A_ w);
976
977 i = 0; /* restart loop, inefficient, but time jumps should be rare */
978 }
979 }
980 } 999 }
1000
1001 /* now rebuild the heap */
1002 for (i = periodiccnt >> 1; i--; )
1003 downheap ((WT *)periodics, periodiccnt, i);
981} 1004}
982 1005
983inline int 1006inline int
984time_update_monotonic (EV_P) 1007time_update_monotonic (EV_P)
985{ 1008{
1081 /* update fd-related kernel structures */ 1104 /* update fd-related kernel structures */
1082 fd_reify (EV_A); 1105 fd_reify (EV_A);
1083 1106
1084 /* calculate blocking time */ 1107 /* calculate blocking time */
1085 1108
1086 /* we only need this for !monotonic clockor timers, but as we basically 1109 /* we only need this for !monotonic clock or timers, but as we basically
1087 always have timers, we just calculate it always */ 1110 always have timers, we just calculate it always */
1088#if EV_USE_MONOTONIC 1111#if EV_USE_MONOTONIC
1089 if (expect_true (have_monotonic)) 1112 if (expect_true (have_monotonic))
1090 time_update_monotonic (EV_A); 1113 time_update_monotonic (EV_A);
1091 else 1114 else
1124 /* queue pending timers and reschedule them */ 1147 /* queue pending timers and reschedule them */
1125 timers_reify (EV_A); /* relative timers called last */ 1148 timers_reify (EV_A); /* relative timers called last */
1126 periodics_reify (EV_A); /* absolute timers called first */ 1149 periodics_reify (EV_A); /* absolute timers called first */
1127 1150
1128 /* queue idle watchers unless io or timers are pending */ 1151 /* queue idle watchers unless io or timers are pending */
1129 if (!pendingcnt) 1152 if (idlecnt && !any_pending (EV_A))
1130 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1153 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1131 1154
1132 /* queue check watchers, to be executed first */ 1155 /* queue check watchers, to be executed first */
1133 if (checkcnt) 1156 if (checkcnt)
1134 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1157 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1209 return; 1232 return;
1210 1233
1211 assert (("ev_io_start called with negative fd", fd >= 0)); 1234 assert (("ev_io_start called with negative fd", fd >= 0));
1212 1235
1213 ev_start (EV_A_ (W)w, 1); 1236 ev_start (EV_A_ (W)w, 1);
1214 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1237 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1215 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1238 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1216 1239
1217 fd_change (EV_A_ fd); 1240 fd_change (EV_A_ fd);
1218} 1241}
1219 1242
1239 ((WT)w)->at += mn_now; 1262 ((WT)w)->at += mn_now;
1240 1263
1241 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1264 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1242 1265
1243 ev_start (EV_A_ (W)w, ++timercnt); 1266 ev_start (EV_A_ (W)w, ++timercnt);
1244 array_needsize (timers, timermax, timercnt, (void)); 1267 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1245 timers [timercnt - 1] = w; 1268 timers [timercnt - 1] = w;
1246 upheap ((WT *)timers, timercnt - 1); 1269 upheap ((WT *)timers, timercnt - 1);
1247 1270
1248 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1271 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1249} 1272}
1289ev_periodic_start (EV_P_ struct ev_periodic *w) 1312ev_periodic_start (EV_P_ struct ev_periodic *w)
1290{ 1313{
1291 if (ev_is_active (w)) 1314 if (ev_is_active (w))
1292 return; 1315 return;
1293 1316
1317 if (w->reschedule_cb)
1318 ((WT)w)->at = w->reschedule_cb (w, rt_now);
1319 else if (w->interval)
1320 {
1294 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.)); 1321 assert (("ev_periodic_start called with negative interval value", w->interval >= 0.));
1295
1296 /* this formula differs from the one in periodic_reify because we do not always round up */ 1322 /* this formula differs from the one in periodic_reify because we do not always round up */
1297 if (w->interval)
1298 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1323 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1324 }
1299 1325
1300 ev_start (EV_A_ (W)w, ++periodiccnt); 1326 ev_start (EV_A_ (W)w, ++periodiccnt);
1301 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1327 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1302 periodics [periodiccnt - 1] = w; 1328 periodics [periodiccnt - 1] = w;
1303 upheap ((WT *)periodics, periodiccnt - 1); 1329 upheap ((WT *)periodics, periodiccnt - 1);
1304 1330
1305 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1331 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1306} 1332}
1322 1348
1323 ev_stop (EV_A_ (W)w); 1349 ev_stop (EV_A_ (W)w);
1324} 1350}
1325 1351
1326void 1352void
1353ev_periodic_again (EV_P_ struct ev_periodic *w)
1354{
1355 ev_periodic_stop (EV_A_ w);
1356 ev_periodic_start (EV_A_ w);
1357}
1358
1359void
1327ev_idle_start (EV_P_ struct ev_idle *w) 1360ev_idle_start (EV_P_ struct ev_idle *w)
1328{ 1361{
1329 if (ev_is_active (w)) 1362 if (ev_is_active (w))
1330 return; 1363 return;
1331 1364
1332 ev_start (EV_A_ (W)w, ++idlecnt); 1365 ev_start (EV_A_ (W)w, ++idlecnt);
1333 array_needsize (idles, idlemax, idlecnt, (void)); 1366 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1334 idles [idlecnt - 1] = w; 1367 idles [idlecnt - 1] = w;
1335} 1368}
1336 1369
1337void 1370void
1338ev_idle_stop (EV_P_ struct ev_idle *w) 1371ev_idle_stop (EV_P_ struct ev_idle *w)
1350{ 1383{
1351 if (ev_is_active (w)) 1384 if (ev_is_active (w))
1352 return; 1385 return;
1353 1386
1354 ev_start (EV_A_ (W)w, ++preparecnt); 1387 ev_start (EV_A_ (W)w, ++preparecnt);
1355 array_needsize (prepares, preparemax, preparecnt, (void)); 1388 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1356 prepares [preparecnt - 1] = w; 1389 prepares [preparecnt - 1] = w;
1357} 1390}
1358 1391
1359void 1392void
1360ev_prepare_stop (EV_P_ struct ev_prepare *w) 1393ev_prepare_stop (EV_P_ struct ev_prepare *w)
1372{ 1405{
1373 if (ev_is_active (w)) 1406 if (ev_is_active (w))
1374 return; 1407 return;
1375 1408
1376 ev_start (EV_A_ (W)w, ++checkcnt); 1409 ev_start (EV_A_ (W)w, ++checkcnt);
1377 array_needsize (checks, checkmax, checkcnt, (void)); 1410 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1378 checks [checkcnt - 1] = w; 1411 checks [checkcnt - 1] = w;
1379} 1412}
1380 1413
1381void 1414void
1382ev_check_stop (EV_P_ struct ev_check *w) 1415ev_check_stop (EV_P_ struct ev_check *w)
1403 return; 1436 return;
1404 1437
1405 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1438 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1406 1439
1407 ev_start (EV_A_ (W)w, 1); 1440 ev_start (EV_A_ (W)w, 1);
1408 array_needsize (signals, signalmax, w->signum, signals_init); 1441 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1409 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1442 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1410 1443
1411 if (!((WL)w)->next) 1444 if (!((WL)w)->next)
1412 { 1445 {
1413#if WIN32 1446#if WIN32
1496} 1529}
1497 1530
1498void 1531void
1499ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1532ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1500{ 1533{
1501 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1534 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1502 1535
1503 if (!once) 1536 if (!once)
1504 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1537 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1505 else 1538 else
1506 { 1539 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines