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

Comparing libev/ev.c (file contents):
Revision 1.86 by root, Sat Nov 10 03:19:21 2007 UTC vs.
Revision 1.94 by root, Sun Nov 11 01:29:49 2007 UTC

26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31
32#ifdef __cplusplus
33extern "C" {
34#endif
35
31#ifndef EV_STANDALONE 36#ifndef EV_STANDALONE
32# include "config.h" 37# include "config.h"
33 38
34# if HAVE_CLOCK_GETTIME 39# if HAVE_CLOCK_GETTIME
35# define EV_USE_MONOTONIC 1 40# define EV_USE_MONOTONIC 1
46 51
47# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H 52# if HAVE_EPOLL && HAVE_EPOLL_CTL && HAVE_SYS_EPOLL_H
48# define EV_USE_EPOLL 1 53# define EV_USE_EPOLL 1
49# endif 54# endif
50 55
51# if HAVE_KQUEUE && HAVE_WORKING_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H 56# if HAVE_KQUEUE && HAVE_SYS_EVENT_H && HAVE_SYS_QUEUE_H
52# define EV_USE_KQUEUE 1 57# define EV_USE_KQUEUE 1
53# endif 58# endif
54 59
55#endif 60#endif
56 61
244 249
245#endif 250#endif
246 251
247/*****************************************************************************/ 252/*****************************************************************************/
248 253
249inline ev_tstamp 254ev_tstamp
250ev_time (void) 255ev_time (void)
251{ 256{
252#if EV_USE_REALTIME 257#if EV_USE_REALTIME
253 struct timespec ts; 258 struct timespec ts;
254 clock_gettime (CLOCK_REALTIME, &ts); 259 clock_gettime (CLOCK_REALTIME, &ts);
799 array_free (pending, [i]); 804 array_free (pending, [i]);
800 805
801 /* have to use the microsoft-never-gets-it-right macro */ 806 /* have to use the microsoft-never-gets-it-right macro */
802 array_free_microshit (fdchange); 807 array_free_microshit (fdchange);
803 array_free_microshit (timer); 808 array_free_microshit (timer);
809#if EV_PERIODICS
804 array_free_microshit (periodic); 810 array_free_microshit (periodic);
811#endif
805 array_free_microshit (idle); 812 array_free_microshit (idle);
806 array_free_microshit (prepare); 813 array_free_microshit (prepare);
807 array_free_microshit (check); 814 array_free_microshit (check);
808 815
809 method = 0; 816 method = 0;
982 989
983 /* first reschedule or stop timer */ 990 /* first reschedule or stop timer */
984 if (w->repeat) 991 if (w->repeat)
985 { 992 {
986 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.)); 993 assert (("negative ev_timer repeat value found while processing timers", w->repeat > 0.));
994
987 ((WT)w)->at = mn_now + w->repeat; 995 ((WT)w)->at += w->repeat;
996 if (((WT)w)->at < mn_now)
997 ((WT)w)->at = mn_now;
998
988 downheap ((WT *)timers, timercnt, 0); 999 downheap ((WT *)timers, timercnt, 0);
989 } 1000 }
990 else 1001 else
991 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */ 1002 ev_timer_stop (EV_A_ w); /* nonrepeating: stop timer */
992 1003
993 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT); 1004 ev_feed_event (EV_A_ (W)w, EV_TIMEOUT);
994 } 1005 }
995} 1006}
996 1007
1008#if EV_PERIODICS
997static void 1009static void
998periodics_reify (EV_P) 1010periodics_reify (EV_P)
999{ 1011{
1000 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now) 1012 while (periodiccnt && ((WT)periodics [0])->at <= ev_rt_now)
1001 { 1013 {
1042 1054
1043 /* now rebuild the heap */ 1055 /* now rebuild the heap */
1044 for (i = periodiccnt >> 1; i--; ) 1056 for (i = periodiccnt >> 1; i--; )
1045 downheap ((WT *)periodics, periodiccnt, i); 1057 downheap ((WT *)periodics, periodiccnt, i);
1046} 1058}
1059#endif
1047 1060
1048inline int 1061inline int
1049time_update_monotonic (EV_P) 1062time_update_monotonic (EV_P)
1050{ 1063{
1051 mn_now = get_clock (); 1064 mn_now = get_clock ();
1085 ev_rt_now = ev_time (); 1098 ev_rt_now = ev_time ();
1086 mn_now = get_clock (); 1099 mn_now = get_clock ();
1087 now_floor = mn_now; 1100 now_floor = mn_now;
1088 } 1101 }
1089 1102
1103# if EV_PERIODICS
1090 periodics_reschedule (EV_A); 1104 periodics_reschedule (EV_A);
1105# endif
1091 /* no timer adjustment, as the monotonic clock doesn't jump */ 1106 /* no timer adjustment, as the monotonic clock doesn't jump */
1092 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */ 1107 /* timers_reschedule (EV_A_ rtmn_diff - odiff) */
1093 } 1108 }
1094 } 1109 }
1095 else 1110 else
1097 { 1112 {
1098 ev_rt_now = ev_time (); 1113 ev_rt_now = ev_time ();
1099 1114
1100 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP)) 1115 if (expect_false (mn_now > ev_rt_now || mn_now < ev_rt_now - MAX_BLOCKTIME - MIN_TIMEJUMP))
1101 { 1116 {
1117#if EV_PERIODICS
1102 periodics_reschedule (EV_A); 1118 periodics_reschedule (EV_A);
1119#endif
1103 1120
1104 /* adjust timers. this is easy, as the offset is the same for all */ 1121 /* adjust timers. this is easy, as the offset is the same for all */
1105 for (i = 0; i < timercnt; ++i) 1122 for (i = 0; i < timercnt; ++i)
1106 ((WT)timers [i])->at += ev_rt_now - mn_now; 1123 ((WT)timers [i])->at += ev_rt_now - mn_now;
1107 } 1124 }
1170 { 1187 {
1171 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge; 1188 ev_tstamp to = ((WT)timers [0])->at - mn_now + method_fudge;
1172 if (block > to) block = to; 1189 if (block > to) block = to;
1173 } 1190 }
1174 1191
1192#if EV_PERIODICS
1175 if (periodiccnt) 1193 if (periodiccnt)
1176 { 1194 {
1177 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge; 1195 ev_tstamp to = ((WT)periodics [0])->at - ev_rt_now + method_fudge;
1178 if (block > to) block = to; 1196 if (block > to) block = to;
1179 } 1197 }
1198#endif
1180 1199
1181 if (block < 0.) block = 0.; 1200 if (block < 0.) block = 0.;
1182 } 1201 }
1183 1202
1184 method_poll (EV_A_ block); 1203 method_poll (EV_A_ block);
1186 /* update ev_rt_now, do magic */ 1205 /* update ev_rt_now, do magic */
1187 time_update (EV_A); 1206 time_update (EV_A);
1188 1207
1189 /* queue pending timers and reschedule them */ 1208 /* queue pending timers and reschedule them */
1190 timers_reify (EV_A); /* relative timers called last */ 1209 timers_reify (EV_A); /* relative timers called last */
1210#if EV_PERIODICS
1191 periodics_reify (EV_A); /* absolute timers called first */ 1211 periodics_reify (EV_A); /* absolute timers called first */
1212#endif
1192 1213
1193 /* queue idle watchers unless io or timers are pending */ 1214 /* queue idle watchers unless io or timers are pending */
1194 if (idlecnt && !any_pending (EV_A)) 1215 if (idlecnt && !any_pending (EV_A))
1195 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1216 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1196 1217
1287{ 1308{
1288 ev_clear_pending (EV_A_ (W)w); 1309 ev_clear_pending (EV_A_ (W)w);
1289 if (!ev_is_active (w)) 1310 if (!ev_is_active (w))
1290 return; 1311 return;
1291 1312
1313 assert (("ev_io_start called with illegal fd (must stay constant after start!)", w->fd >= 0 && w->fd < anfdmax));
1314
1292 wlist_del ((WL *)&anfds[w->fd].head, (WL)w); 1315 wlist_del ((WL *)&anfds[w->fd].head, (WL)w);
1293 ev_stop (EV_A_ (W)w); 1316 ev_stop (EV_A_ (W)w);
1294 1317
1295 fd_change (EV_A_ w->fd); 1318 fd_change (EV_A_ w->fd);
1296} 1319}
1326 { 1349 {
1327 timers [((W)w)->active - 1] = timers [timercnt]; 1350 timers [((W)w)->active - 1] = timers [timercnt];
1328 downheap ((WT *)timers, timercnt, ((W)w)->active - 1); 1351 downheap ((WT *)timers, timercnt, ((W)w)->active - 1);
1329 } 1352 }
1330 1353
1331 ((WT)w)->at = w->repeat; 1354 ((WT)w)->at -= mn_now;
1332 1355
1333 ev_stop (EV_A_ (W)w); 1356 ev_stop (EV_A_ (W)w);
1334} 1357}
1335 1358
1336void 1359void
1345 } 1368 }
1346 else if (w->repeat) 1369 else if (w->repeat)
1347 ev_timer_start (EV_A_ w); 1370 ev_timer_start (EV_A_ w);
1348} 1371}
1349 1372
1373#if EV_PERIODICS
1350void 1374void
1351ev_periodic_start (EV_P_ struct ev_periodic *w) 1375ev_periodic_start (EV_P_ struct ev_periodic *w)
1352{ 1376{
1353 if (ev_is_active (w)) 1377 if (ev_is_active (w))
1354 return; 1378 return;
1393{ 1417{
1394 /* TODO: use adjustheap and recalculation */ 1418 /* TODO: use adjustheap and recalculation */
1395 ev_periodic_stop (EV_A_ w); 1419 ev_periodic_stop (EV_A_ w);
1396 ev_periodic_start (EV_A_ w); 1420 ev_periodic_start (EV_A_ w);
1397} 1421}
1422#endif
1398 1423
1399void 1424void
1400ev_idle_start (EV_P_ struct ev_idle *w) 1425ev_idle_start (EV_P_ struct ev_idle *w)
1401{ 1426{
1402 if (ev_is_active (w)) 1427 if (ev_is_active (w))
1409 1434
1410void 1435void
1411ev_idle_stop (EV_P_ struct ev_idle *w) 1436ev_idle_stop (EV_P_ struct ev_idle *w)
1412{ 1437{
1413 ev_clear_pending (EV_A_ (W)w); 1438 ev_clear_pending (EV_A_ (W)w);
1414 if (ev_is_active (w)) 1439 if (!ev_is_active (w))
1415 return; 1440 return;
1416 1441
1417 idles [((W)w)->active - 1] = idles [--idlecnt]; 1442 idles [((W)w)->active - 1] = idles [--idlecnt];
1418 ev_stop (EV_A_ (W)w); 1443 ev_stop (EV_A_ (W)w);
1419} 1444}
1431 1456
1432void 1457void
1433ev_prepare_stop (EV_P_ struct ev_prepare *w) 1458ev_prepare_stop (EV_P_ struct ev_prepare *w)
1434{ 1459{
1435 ev_clear_pending (EV_A_ (W)w); 1460 ev_clear_pending (EV_A_ (W)w);
1436 if (ev_is_active (w)) 1461 if (!ev_is_active (w))
1437 return; 1462 return;
1438 1463
1439 prepares [((W)w)->active - 1] = prepares [--preparecnt]; 1464 prepares [((W)w)->active - 1] = prepares [--preparecnt];
1440 ev_stop (EV_A_ (W)w); 1465 ev_stop (EV_A_ (W)w);
1441} 1466}
1453 1478
1454void 1479void
1455ev_check_stop (EV_P_ struct ev_check *w) 1480ev_check_stop (EV_P_ struct ev_check *w)
1456{ 1481{
1457 ev_clear_pending (EV_A_ (W)w); 1482 ev_clear_pending (EV_A_ (W)w);
1458 if (ev_is_active (w)) 1483 if (!ev_is_active (w))
1459 return; 1484 return;
1460 1485
1461 checks [((W)w)->active - 1] = checks [--checkcnt]; 1486 checks [((W)w)->active - 1] = checks [--checkcnt];
1462 ev_stop (EV_A_ (W)w); 1487 ev_stop (EV_A_ (W)w);
1463} 1488}
1594 ev_timer_start (EV_A_ &once->to); 1619 ev_timer_start (EV_A_ &once->to);
1595 } 1620 }
1596 } 1621 }
1597} 1622}
1598 1623
1624#ifdef __cplusplus
1625}
1626#endif
1627

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines