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

Comparing libev/ev.c (file contents):
Revision 1.72 by root, Tue Nov 6 16:09:37 2007 UTC vs.
Revision 1.76 by root, Wed Nov 7 18:47:26 2007 UTC

148typedef struct ev_watcher_list *WL; 148typedef struct ev_watcher_list *WL;
149typedef struct ev_watcher_time *WT; 149typedef struct ev_watcher_time *WT;
150 150
151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 151static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
152 152
153#if WIN32 153#include "ev_win32.c"
154/* note: the comment below could not be substantiated, but what would I care */
155/* MSDN says this is required to handle SIGFPE */
156volatile double SIGFPE_REQ = 0.0f;
157
158static int
159ev_socketpair_tcp (int filedes [2])
160{
161 struct sockaddr_in addr = { 0 };
162 int addr_size = sizeof (addr);
163 SOCKET listener;
164 SOCKET sock [2] = { -1, -1 };
165
166 if ((listener = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
167 return -1;
168
169 addr.sin_family = AF_INET;
170 addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
171 addr.sin_port = 0;
172
173 if (bind (listener, (struct sockaddr *)&addr, addr_size))
174 goto fail;
175
176 if (getsockname(listener, (struct sockaddr *)&addr, &addr_size))
177 goto fail;
178
179 if (listen (listener, 1))
180 goto fail;
181
182 if ((sock [0] = socket (AF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET)
183 goto fail;
184
185 if (connect (sock[0], (struct sockaddr *)&addr, addr_size))
186 goto fail;
187
188 if ((sock[1] = accept (listener, 0, 0)) < 0)
189 goto fail;
190
191 closesocket (listener);
192
193 filedes [0] = sock [0];
194 filedes [1] = sock [1];
195
196 return 0;
197
198fail:
199 closesocket (listener);
200
201 if (sock [0] != INVALID_SOCKET) closesocket (sock [0]);
202 if (sock [1] != INVALID_SOCKET) closesocket (sock [1]);
203
204 return -1;
205}
206
207# define ev_pipe(filedes) ev_socketpair_tcp (filedes)
208#else
209# define ev_pipe(filedes) pipe (filedes)
210#endif
211 154
212/*****************************************************************************/ 155/*****************************************************************************/
213 156
214static void (*syserr_cb)(const char *msg); 157static void (*syserr_cb)(const char *msg);
215 158
325ev_now (EV_P) 268ev_now (EV_P)
326{ 269{
327 return rt_now; 270 return rt_now;
328} 271}
329 272
330#define array_roundsize(base,n) ((n) | 4 & ~3) 273#define array_roundsize(type,n) ((n) | 4 & ~3)
331 274
332#define array_needsize(base,cur,cnt,init) \ 275#define array_needsize(type,base,cur,cnt,init) \
333 if (expect_false ((cnt) > cur)) \ 276 if (expect_false ((cnt) > cur)) \
334 { \ 277 { \
335 int newcnt = cur; \ 278 int newcnt = cur; \
336 do \ 279 do \
337 { \ 280 { \
338 newcnt = array_roundsize (base, newcnt << 1); \ 281 newcnt = array_roundsize (type, newcnt << 1); \
339 } \ 282 } \
340 while ((cnt) > newcnt); \ 283 while ((cnt) > newcnt); \
341 \ 284 \
342 base = ev_realloc (base, sizeof (*base) * (newcnt)); \ 285 base = (type *)ev_realloc (base, sizeof (type) * (newcnt));\
343 init (base + cur, newcnt - cur); \ 286 init (base + cur, newcnt - cur); \
344 cur = newcnt; \ 287 cur = newcnt; \
345 } 288 }
346 289
347#define array_slim(stem) \ 290#define array_slim(type,stem) \
348 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \ 291 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
349 { \ 292 { \
350 stem ## max = array_roundsize (stem ## cnt >> 1); \ 293 stem ## max = array_roundsize (stem ## cnt >> 1); \
351 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \ 294 base = (type *)ev_realloc (base, sizeof (type) * (stem ## max));\
352 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\ 295 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
353 } 296 }
354 297
355/* 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 */
356/* 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 */
383 pendings [ABSPRI (w)][w->pending - 1].events |= events; 326 pendings [ABSPRI (w)][w->pending - 1].events |= events;
384 return; 327 return;
385 } 328 }
386 329
387 w->pending = ++pendingcnt [ABSPRI (w)]; 330 w->pending = ++pendingcnt [ABSPRI (w)];
388 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));
389 pendings [ABSPRI (w)][w->pending - 1].w = w; 332 pendings [ABSPRI (w)][w->pending - 1].w = w;
390 pendings [ABSPRI (w)][w->pending - 1].events = events; 333 pendings [ABSPRI (w)][w->pending - 1].events = events;
391} 334}
392 335
393static void 336static void
448 return; 391 return;
449 392
450 anfds [fd].reify = 1; 393 anfds [fd].reify = 1;
451 394
452 ++fdchangecnt; 395 ++fdchangecnt;
453 array_needsize (fdchanges, fdchangemax, fdchangecnt, (void)); 396 array_needsize (int, fdchanges, fdchangemax, fdchangecnt, (void));
454 fdchanges [fdchangecnt - 1] = fd; 397 fdchanges [fdchangecnt - 1] = fd;
455} 398}
456 399
457static void 400static void
458fd_kill (EV_P_ int fd) 401fd_kill (EV_P_ int fd)
598 541
599 if (!gotsig) 542 if (!gotsig)
600 { 543 {
601 int old_errno = errno; 544 int old_errno = errno;
602 gotsig = 1; 545 gotsig = 1;
546#ifdef WIN32
547 send (sigpipe [1], &signum, 1, MSG_DONTWAIT);
548#else
603 write (sigpipe [1], &signum, 1); 549 write (sigpipe [1], &signum, 1);
550#endif
604 errno = old_errno; 551 errno = old_errno;
605 } 552 }
606} 553}
607 554
608static void 555static void
609sigcb (EV_P_ struct ev_io *iow, int revents) 556sigcb (EV_P_ struct ev_io *iow, int revents)
610{ 557{
611 WL w; 558 WL w;
612 int signum; 559 int signum;
613 560
561#ifdef WIN32
562 recv (sigpipe [0], &revents, 1, MSG_DONTWAIT);
563#else
614 read (sigpipe [0], &revents, 1); 564 read (sigpipe [0], &revents, 1);
565#endif
615 gotsig = 0; 566 gotsig = 0;
616 567
617 for (signum = signalmax; signum--; ) 568 for (signum = signalmax; signum--; )
618 if (signals [signum].gotsig) 569 if (signals [signum].gotsig)
619 { 570 {
828 ev_ref (EV_A); 779 ev_ref (EV_A);
829 ev_io_stop (EV_A_ &sigev); 780 ev_io_stop (EV_A_ &sigev);
830 close (sigpipe [0]); 781 close (sigpipe [0]);
831 close (sigpipe [1]); 782 close (sigpipe [1]);
832 783
833 while (ev_pipe (sigpipe)) 784 while (pipe (sigpipe))
834 syserr ("(libev) error creating pipe"); 785 syserr ("(libev) error creating pipe");
835 786
836 siginit (EV_A); 787 siginit (EV_A);
837 } 788 }
838 789
881int 832int
882#endif 833#endif
883ev_default_loop (int methods) 834ev_default_loop (int methods)
884{ 835{
885 if (sigpipe [0] == sigpipe [1]) 836 if (sigpipe [0] == sigpipe [1])
886 if (ev_pipe (sigpipe)) 837 if (pipe (sigpipe))
887 return 0; 838 return 0;
888 839
889 if (!default_loop) 840 if (!default_loop)
890 { 841 {
891#if EV_MULTIPLICITY 842#if EV_MULTIPLICITY
945 if (method) 896 if (method)
946 postfork = 1; 897 postfork = 1;
947} 898}
948 899
949/*****************************************************************************/ 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}
950 913
951static void 914static void
952call_pending (EV_P) 915call_pending (EV_P)
953{ 916{
954 int pri; 917 int pri;
1138 /* update fd-related kernel structures */ 1101 /* update fd-related kernel structures */
1139 fd_reify (EV_A); 1102 fd_reify (EV_A);
1140 1103
1141 /* calculate blocking time */ 1104 /* calculate blocking time */
1142 1105
1143 /* we only need this for !monotonic clockor timers, but as we basically 1106 /* we only need this for !monotonic clock or timers, but as we basically
1144 always have timers, we just calculate it always */ 1107 always have timers, we just calculate it always */
1145#if EV_USE_MONOTONIC 1108#if EV_USE_MONOTONIC
1146 if (expect_true (have_monotonic)) 1109 if (expect_true (have_monotonic))
1147 time_update_monotonic (EV_A); 1110 time_update_monotonic (EV_A);
1148 else 1111 else
1181 /* queue pending timers and reschedule them */ 1144 /* queue pending timers and reschedule them */
1182 timers_reify (EV_A); /* relative timers called last */ 1145 timers_reify (EV_A); /* relative timers called last */
1183 periodics_reify (EV_A); /* absolute timers called first */ 1146 periodics_reify (EV_A); /* absolute timers called first */
1184 1147
1185 /* queue idle watchers unless io or timers are pending */ 1148 /* queue idle watchers unless io or timers are pending */
1186 if (!pendingcnt) 1149 if (idlecnt && !any_pending (EV_A))
1187 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE); 1150 queue_events (EV_A_ (W *)idles, idlecnt, EV_IDLE);
1188 1151
1189 /* queue check watchers, to be executed first */ 1152 /* queue check watchers, to be executed first */
1190 if (checkcnt) 1153 if (checkcnt)
1191 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK); 1154 queue_events (EV_A_ (W *)checks, checkcnt, EV_CHECK);
1266 return; 1229 return;
1267 1230
1268 assert (("ev_io_start called with negative fd", fd >= 0)); 1231 assert (("ev_io_start called with negative fd", fd >= 0));
1269 1232
1270 ev_start (EV_A_ (W)w, 1); 1233 ev_start (EV_A_ (W)w, 1);
1271 array_needsize (anfds, anfdmax, fd + 1, anfds_init); 1234 array_needsize (ANFD, anfds, anfdmax, fd + 1, anfds_init);
1272 wlist_add ((WL *)&anfds[fd].head, (WL)w); 1235 wlist_add ((WL *)&anfds[fd].head, (WL)w);
1273 1236
1274 fd_change (EV_A_ fd); 1237 fd_change (EV_A_ fd);
1275} 1238}
1276 1239
1296 ((WT)w)->at += mn_now; 1259 ((WT)w)->at += mn_now;
1297 1260
1298 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.)); 1261 assert (("ev_timer_start called with negative timer repeat value", w->repeat >= 0.));
1299 1262
1300 ev_start (EV_A_ (W)w, ++timercnt); 1263 ev_start (EV_A_ (W)w, ++timercnt);
1301 array_needsize (timers, timermax, timercnt, (void)); 1264 array_needsize (struct ev_timer *, timers, timermax, timercnt, (void));
1302 timers [timercnt - 1] = w; 1265 timers [timercnt - 1] = w;
1303 upheap ((WT *)timers, timercnt - 1); 1266 upheap ((WT *)timers, timercnt - 1);
1304 1267
1305 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w)); 1268 assert (("internal timer heap corruption", timers [((W)w)->active - 1] == w));
1306} 1269}
1353 /* this formula differs from the one in periodic_reify because we do not always round up */ 1316 /* this formula differs from the one in periodic_reify because we do not always round up */
1354 if (w->interval) 1317 if (w->interval)
1355 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval; 1318 ((WT)w)->at += ceil ((rt_now - ((WT)w)->at) / w->interval) * w->interval;
1356 1319
1357 ev_start (EV_A_ (W)w, ++periodiccnt); 1320 ev_start (EV_A_ (W)w, ++periodiccnt);
1358 array_needsize (periodics, periodicmax, periodiccnt, (void)); 1321 array_needsize (struct ev_periodic *, periodics, periodicmax, periodiccnt, (void));
1359 periodics [periodiccnt - 1] = w; 1322 periodics [periodiccnt - 1] = w;
1360 upheap ((WT *)periodics, periodiccnt - 1); 1323 upheap ((WT *)periodics, periodiccnt - 1);
1361 1324
1362 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w)); 1325 assert (("internal periodic heap corruption", periodics [((W)w)->active - 1] == w));
1363} 1326}
1385{ 1348{
1386 if (ev_is_active (w)) 1349 if (ev_is_active (w))
1387 return; 1350 return;
1388 1351
1389 ev_start (EV_A_ (W)w, ++idlecnt); 1352 ev_start (EV_A_ (W)w, ++idlecnt);
1390 array_needsize (idles, idlemax, idlecnt, (void)); 1353 array_needsize (struct ev_idle *, idles, idlemax, idlecnt, (void));
1391 idles [idlecnt - 1] = w; 1354 idles [idlecnt - 1] = w;
1392} 1355}
1393 1356
1394void 1357void
1395ev_idle_stop (EV_P_ struct ev_idle *w) 1358ev_idle_stop (EV_P_ struct ev_idle *w)
1407{ 1370{
1408 if (ev_is_active (w)) 1371 if (ev_is_active (w))
1409 return; 1372 return;
1410 1373
1411 ev_start (EV_A_ (W)w, ++preparecnt); 1374 ev_start (EV_A_ (W)w, ++preparecnt);
1412 array_needsize (prepares, preparemax, preparecnt, (void)); 1375 array_needsize (struct ev_prepare *, prepares, preparemax, preparecnt, (void));
1413 prepares [preparecnt - 1] = w; 1376 prepares [preparecnt - 1] = w;
1414} 1377}
1415 1378
1416void 1379void
1417ev_prepare_stop (EV_P_ struct ev_prepare *w) 1380ev_prepare_stop (EV_P_ struct ev_prepare *w)
1429{ 1392{
1430 if (ev_is_active (w)) 1393 if (ev_is_active (w))
1431 return; 1394 return;
1432 1395
1433 ev_start (EV_A_ (W)w, ++checkcnt); 1396 ev_start (EV_A_ (W)w, ++checkcnt);
1434 array_needsize (checks, checkmax, checkcnt, (void)); 1397 array_needsize (struct ev_check *, checks, checkmax, checkcnt, (void));
1435 checks [checkcnt - 1] = w; 1398 checks [checkcnt - 1] = w;
1436} 1399}
1437 1400
1438void 1401void
1439ev_check_stop (EV_P_ struct ev_check *w) 1402ev_check_stop (EV_P_ struct ev_check *w)
1460 return; 1423 return;
1461 1424
1462 assert (("ev_signal_start called with illegal signal number", w->signum > 0)); 1425 assert (("ev_signal_start called with illegal signal number", w->signum > 0));
1463 1426
1464 ev_start (EV_A_ (W)w, 1); 1427 ev_start (EV_A_ (W)w, 1);
1465 array_needsize (signals, signalmax, w->signum, signals_init); 1428 array_needsize (ANSIG, signals, signalmax, w->signum, signals_init);
1466 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1429 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1467 1430
1468 if (!((WL)w)->next) 1431 if (!((WL)w)->next)
1469 { 1432 {
1470#if WIN32 1433#if WIN32
1553} 1516}
1554 1517
1555void 1518void
1556ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1519ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1557{ 1520{
1558 struct ev_once *once = ev_malloc (sizeof (struct ev_once)); 1521 struct ev_once *once = (struct ev_once *)ev_malloc (sizeof (struct ev_once));
1559 1522
1560 if (!once) 1523 if (!once)
1561 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1524 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1562 else 1525 else
1563 { 1526 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines