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

Comparing libev/ev.c (file contents):
Revision 1.90 by root, Sun Nov 11 00:05:59 2007 UTC vs.
Revision 1.102 by root, Sun Nov 11 17:56:11 2007 UTC

35 35
36#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
37# include "config.h" 37# include "config.h"
38 38
39# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
40# ifndef EV_USE_MONOTONIC
40# define EV_USE_MONOTONIC 1 41# define EV_USE_MONOTONIC 1
42# endif
43# ifndef EV_USE_REALTIME
41# define EV_USE_REALTIME 1 44# define EV_USE_REALTIME 1
45# endif
42# endif 46# endif
43 47
44# if HAVE_SELECT && HAVE_SYS_SELECT_H 48# if HAVE_SELECT && HAVE_SYS_SELECT_H && !defined (EV_USE_SELECT)
45# define EV_USE_SELECT 1 49# define EV_USE_SELECT 1
46# endif 50# endif
47 51
48# if HAVE_POLL && HAVE_POLL_H 52# if HAVE_POLL && HAVE_POLL_H && !defined (EV_USE_POLL)
49# define EV_USE_POLL 1 53# define EV_USE_POLL 1
50# endif 54# endif
51 55
52# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 56# if HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H && !defined (EV_USE_EPOLL)
53# define EV_USE_EPOLL 1 57# define EV_USE_EPOLL 1
54# endif 58# endif
55 59
56# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 60# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H && !defined (EV_USE_KQUEUE)
57# define EV_USE_KQUEUE 1 61# define EV_USE_KQUEUE 1
58# endif 62# endif
59 63
60#endif 64#endif
61 65
157typedef struct ev_watcher_list *WL; 161typedef struct ev_watcher_list *WL;
158typedef struct ev_watcher_time *WT; 162typedef struct ev_watcher_time *WT;
159 163
160static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 164static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
161 165
166#ifdef WIN32
162#include "ev_win32.c" 167# include "ev_win32.c"
168#endif
163 169
164/*****************************************************************************/ 170/*****************************************************************************/
165 171
166static void (*syserr_cb)(const char *msg); 172static void (*syserr_cb)(const char *msg);
167 173
227#if EV_MULTIPLICITY 233#if EV_MULTIPLICITY
228 234
229 struct ev_loop 235 struct ev_loop
230 { 236 {
231 ev_tstamp ev_rt_now; 237 ev_tstamp ev_rt_now;
238 #define ev_rt_now ((loop)->ev_rt_now)
232 #define VAR(name,decl) decl; 239 #define VAR(name,decl) decl;
233 #include "ev_vars.h" 240 #include "ev_vars.h"
234 #undef VAR 241 #undef VAR
235 }; 242 };
236 #include "ev_wrap.h" 243 #include "ev_wrap.h"
249 256
250#endif 257#endif
251 258
252/*****************************************************************************/ 259/*****************************************************************************/
253 260
254inline ev_tstamp 261ev_tstamp
255ev_time (void) 262ev_time (void)
256{ 263{
257#if EV_USE_REALTIME 264#if EV_USE_REALTIME
258 struct timespec ts; 265 struct timespec ts;
259 clock_gettime (CLOCK_REALTIME, &ts); 266 clock_gettime (CLOCK_REALTIME, &ts);
528 heap [k] = w; 535 heap [k] = w;
529 ((W)heap [k])->active = k + 1; 536 ((W)heap [k])->active = k + 1;
530} 537}
531 538
532inline void 539inline void
533adjustheap (WT *heap, int N, int k, ev_tstamp at) 540adjustheap (WT *heap, int N, int k)
534{ 541{
535 ev_tstamp old_at = heap [k]->at; 542 upheap (heap, k);
536 heap [k]->at = at;
537
538 if (old_at < at)
539 downheap (heap, N, k); 543 downheap (heap, N, k);
540 else
541 upheap (heap, k);
542} 544}
543 545
544/*****************************************************************************/ 546/*****************************************************************************/
545 547
546typedef struct 548typedef struct
804 array_free (pending, [i]); 806 array_free (pending, [i]);
805 807
806 /* have to use the microsoft-never-gets-it-right macro */ 808 /* have to use the microsoft-never-gets-it-right macro */
807 array_free_microshit (fdchange); 809 array_free_microshit (fdchange);
808 array_free_microshit (timer); 810 array_free_microshit (timer);
811#if EV_PERIODICS
809 array_free_microshit (periodic); 812 array_free_microshit (periodic);
813#endif
810 array_free_microshit (idle); 814 array_free_microshit (idle);
811 array_free_microshit (prepare); 815 array_free_microshit (prepare);
812 array_free_microshit (check); 816 array_free_microshit (check);
813 817
814 method = 0; 818 method = 0;
1001 1005
1002 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1006 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
1003 } 1007 }
1004} 1008}
1005 1009
1010#if EV_PERIODICS
1006static void 1011static void
1007periodics_reify (EV_P) 1012periodics_reify (EV_P)
1008{ 1013{
1009 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1014 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1010 { 1015 {
1051 1056
1052 /* now rebuild the heap */ 1057 /* now rebuild the heap */
1053 for (i = periodiccnt >> 1; i--; ) 1058 for (i = periodiccnt >> 1; i--; )
1054 downheap ((WT *)periodics, periodiccnt, i); 1059 downheap ((WT *)periodics, periodiccnt, i);
1055} 1060}
1061#endif
1056 1062
1057inline int 1063inline int
1058time_update_monotonic (EV_P) 1064time_update_monotonic (EV_P)
1059{ 1065{
1060 mn_now = get_clock (); 1066 mn_now = get_clock ();
1094 ev_rt_now = ev_time (); 1100 ev_rt_now = ev_time ();
1095 mn_now = get_clock (); 1101 mn_now = get_clock ();
1096 now_floor = mn_now; 1102 now_floor = mn_now;
1097 } 1103 }
1098 1104
1105# if EV_PERIODICS
1099 periodics_reschedule (EV_A); 1106 periodics_reschedule (EV_A);
1107# endif
1100 /* no timer adjustment, as the monotonic clock doesn't jump */ 1108 /* no timer adjustment, as the monotonic clock doesn't jump */
1101 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1109 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1102 } 1110 }
1103 } 1111 }
1104 else 1112 else
1106 { 1114 {
1107 ev_rt_now = ev_time (); 1115 ev_rt_now = ev_time ();
1108 1116
1109 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1117 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1110 { 1118 {
1119#if EV_PERIODICS
1111 periodics_reschedule (EV_A); 1120 periodics_reschedule (EV_A);
1121#endif
1112 1122
1113 /* adjust timers. this is easy, as the offset is the same for all */ 1123 /* adjust timers. this is easy, as the offset is the same for all */
1114 for (i = 0; i < timercnt; ++i) 1124 for (i = 0; i < timercnt; ++i)
1115 ((WT)timers [i])->at += ev_rt_now - mn_now; 1125 ((WT)timers [i])->at += ev_rt_now - mn_now;
1116 } 1126 }
1179 { 1189 {
1180 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1190 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1181 if (block > to) block = to; 1191 if (block > to) block = to;
1182 } 1192 }
1183 1193
1194#if EV_PERIODICS
1184 if (periodiccnt) 1195 if (periodiccnt)
1185 { 1196 {
1186 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1197 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1187 if (block > to) block = to; 1198 if (block > to) block = to;
1188 } 1199 }
1200#endif
1189 1201
1190 if (block < 0.) block = 0.; 1202 if (block < 0.) block = 0.;
1191 } 1203 }
1192 1204
1193 method_poll (EV_A_ block); 1205 method_poll (EV_A_ block);
1195 /* update ev_rt_now, do magic */ 1207 /* update ev_rt_now, do magic */
1196 time_update (EV_A); 1208 time_update (EV_A);
1197 1209
1198 /* queue pending timers and reschedule them */ 1210 /* queue pending timers and reschedule them */
1199 timers_reify (EV_A); /* relative timers called last */ 1211 timers_reify (EV_A); /* relative timers called last */
1212#if EV_PERIODICS
1200 periodics_reify (EV_A); /* absolute timers called first */ 1213 periodics_reify (EV_A); /* absolute timers called first */
1214#endif
1201 1215
1202 /* queue idle watchers unless io or timers are pending */ 1216 /* queue idle watchers unless io or timers are pending */
1203 if (idlecnt && !any_pending (EV_A)) 1217 if (idlecnt && !any_pending (EV_A))
1204 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1218 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1205 1219
1334 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1348 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1335 1349
1336 if (((W)w)->active < timercnt--) 1350 if (((W)w)->active < timercnt--)
1337 { 1351 {
1338 timers [((W)w)->active - 1] = timers [timercnt]; 1352 timers [((W)w)->active - 1] = timers [timercnt];
1339 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1353 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1340 } 1354 }
1341 1355
1342 ((WT)w)->at = w->repeat; 1356 ((WT)w)->at -= mn_now;
1343 1357
1344 ev_stop (EV_A_ (W)w); 1358 ev_stop (EV_A_ (W)w);
1345} 1359}
1346 1360
1347void 1361void
1348ev_timer_again (EV_P_ struct ev_timer *w) 1362ev_timer_again (EV_P_ struct ev_timer *w)
1349{ 1363{
1350 if (ev_is_active (w)) 1364 if (ev_is_active (w))
1351 { 1365 {
1352 if (w->repeat) 1366 if (w->repeat)
1367 {
1368 ((WT)w)->at = mn_now + w->repeat;
1353 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1, mn_now + w->repeat); 1369 adjustheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1370 }
1354 else 1371 else
1355 ev_timer_stop (EV_A_ w); 1372 ev_timer_stop (EV_A_ w);
1356 } 1373 }
1357 else if (w->repeat) 1374 else if (w->repeat)
1358 ev_timer_start (EV_A_ w); 1375 ev_timer_start (EV_A_ w);
1359} 1376}
1360 1377
1378#if EV_PERIODICS
1361void 1379void
1362ev_periodic_start (EV_P_ struct ev_periodic *w) 1380ev_periodic_start (EV_P_ struct ev_periodic *w)
1363{ 1381{
1364 if (ev_is_active (w)) 1382 if (ev_is_active (w))
1365 return; 1383 return;
1391 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1409 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1392 1410
1393 if (((W)w)->active < periodiccnt--) 1411 if (((W)w)->active < periodiccnt--)
1394 { 1412 {
1395 periodics [((W)w)->active - 1] = periodics [periodiccnt]; 1413 periodics [((W)w)->active - 1] = periodics [periodiccnt];
1396 downheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1); 1414 adjustheap ((WT *)periodics, periodiccnt, ((W)w)->active - 1);
1397 } 1415 }
1398 1416
1399 ev_stop (EV_A_ (W)w); 1417 ev_stop (EV_A_ (W)w);
1400} 1418}
1401 1419
1404{ 1422{
1405 /* TODO: use adjustheap and recalculation */ 1423 /* TODO: use adjustheap and recalculation */
1406 ev_periodic_stop (EV_A_ w); 1424 ev_periodic_stop (EV_A_ w);
1407 ev_periodic_start (EV_A_ w); 1425 ev_periodic_start (EV_A_ w);
1408} 1426}
1427#endif
1409 1428
1410void 1429void
1411ev_idle_start (EV_P_ struct ev_idle *w) 1430ev_idle_start (EV_P_ struct ev_idle *w)
1412{ 1431{
1413 if (ev_is_active (w)) 1432 if (ev_is_active (w))
1420 1439
1421void 1440void
1422ev_idle_stop (EV_P_ struct ev_idle *w) 1441ev_idle_stop (EV_P_ struct ev_idle *w)
1423{ 1442{
1424 ev_clear_pending (EV_A_ (W)w); 1443 ev_clear_pending (EV_A_ (W)w);
1425 if (ev_is_active (w)) 1444 if (!ev_is_active (w))
1426 return; 1445 return;
1427 1446
1428 idles [((W)w)->active - 1] = idles [--idlecnt]; 1447 idles [((W)w)->active - 1] = idles [--idlecnt];
1429 ev_stop (EV_A_ (W)w); 1448 ev_stop (EV_A_ (W)w);
1430} 1449}
1442 1461
1443void 1462void
1444ev_prepare_stop (EV_P_ struct ev_prepare *w) 1463ev_prepare_stop (EV_P_ struct ev_prepare *w)
1445{ 1464{
1446 ev_clear_pending (EV_A_ (W)w); 1465 ev_clear_pending (EV_A_ (W)w);
1447 if (ev_is_active (w)) 1466 if (!ev_is_active (w))
1448 return; 1467 return;
1449 1468
1450 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1469 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1451 ev_stop (EV_A_ (W)w); 1470 ev_stop (EV_A_ (W)w);
1452} 1471}
1464 1483
1465void 1484void
1466ev_check_stop (EV_P_ struct ev_check *w) 1485ev_check_stop (EV_P_ struct ev_check *w)
1467{ 1486{
1468 ev_clear_pending (EV_A_ (W)w); 1487 ev_clear_pending (EV_A_ (W)w);
1469 if (ev_is_active (w)) 1488 if (!ev_is_active (w))
1470 return; 1489 return;
1471 1490
1472 checks [((W)w)->active - 1] = checks [--checkcnt]; 1491 checks [((W)w)->active - 1] = checks [--checkcnt];
1473 ev_stop (EV_A_ (W)w); 1492 ev_stop (EV_A_ (W)w);
1474} 1493}
1535 1554
1536void 1555void
1537ev_child_stop (EV_P_ struct ev_child *w) 1556ev_child_stop (EV_P_ struct ev_child *w)
1538{ 1557{
1539 ev_clear_pending (EV_A_ (W)w); 1558 ev_clear_pending (EV_A_ (W)w);
1540 if (ev_is_active (w)) 1559 if (!ev_is_active (w))
1541 return; 1560 return;
1542 1561
1543 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w); 1562 wlist_del ((WL *)&childs [w->pid & (PID_HASHSIZE - 1)], (WL)w);
1544 ev_stop (EV_A_ (W)w); 1563 ev_stop (EV_A_ (W)w);
1545} 1564}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines