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

Comparing libev/ev.c (file contents):
Revision 1.63 by root, Sun Nov 4 22:03:17 2007 UTC vs.
Revision 1.69 by root, Tue Nov 6 00:10:04 2007 UTC

145typedef struct ev_watcher_list *WL; 145typedef struct ev_watcher_list *WL;
146typedef struct ev_watcher_time *WT; 146typedef struct ev_watcher_time *WT;
147 147
148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */ 148static int have_monotonic; /* did clock_gettime (CLOCK_MONOTONIC) work? */
149 149
150#if WIN32
151/* note: the comment below could not be substantiated, but what would I care */
152/* MSDN says this is required to handle SIGFPE */
153volatile double SIGFPE_REQ = 0.0f;
154#endif
155
150/*****************************************************************************/ 156/*****************************************************************************/
151 157
158static void (*syserr_cb)(void);
159
160void ev_set_syserr_cb (void (*cb)(void))
161{
162 syserr_cb = cb;
163}
164
165static void
166syserr (void)
167{
168 if (syserr_cb)
169 syserr_cb ();
170 else
171 {
172 perror ("libev");
173 abort ();
174 }
175}
176
177static void *(*alloc)(void *ptr, long size);
178
179void ev_set_allocator (void *(*cb)(void *ptr, long size))
180{
181 alloc = cb;
182}
183
184static void *
185ev_realloc (void *ptr, long size)
186{
187 ptr = alloc ? alloc (ptr, size) : realloc (ptr, size);
188
189 if (!ptr && size)
190 {
191 fprintf (stderr, "libev: cannot allocate %ld bytes, aborting.", size);
192 abort ();
193 }
194
195 return ptr;
196}
197
198#define ev_malloc(size) ev_realloc (0, (size))
199#define ev_free(ptr) ev_realloc ((ptr), 0)
200
201/*****************************************************************************/
202
152typedef struct 203typedef struct
153{ 204{
154 struct ev_watcher_list *head; 205 WL head;
155 unsigned char events; 206 unsigned char events;
156 unsigned char reify; 207 unsigned char reify;
157} ANFD; 208} ANFD;
158 209
159typedef struct 210typedef struct
217 return rt_now; 268 return rt_now;
218} 269}
219 270
220#define array_roundsize(base,n) ((n) | 4 & ~3) 271#define array_roundsize(base,n) ((n) | 4 & ~3)
221 272
222#define array_needsize(base,cur,cnt,init) \ 273#define array_needsize(base,cur,cnt,init) \
223 if (expect_false ((cnt) > cur)) \ 274 if (expect_false ((cnt) > cur)) \
224 { \ 275 { \
225 int newcnt = cur; \ 276 int newcnt = cur; \
226 do \ 277 do \
227 { \ 278 { \
228 newcnt = array_roundsize (base, newcnt << 1); \ 279 newcnt = array_roundsize (base, newcnt << 1); \
229 } \ 280 } \
230 while ((cnt) > newcnt); \ 281 while ((cnt) > newcnt); \
231 \ 282 \
232 base = realloc (base, sizeof (*base) * (newcnt)); \ 283 base = ev_realloc (base, sizeof (*base) * (newcnt)); \
233 init (base + cur, newcnt - cur); \ 284 init (base + cur, newcnt - cur); \
234 cur = newcnt; \ 285 cur = newcnt; \
235 } 286 }
287
288#define array_slim(stem) \
289 if (stem ## max < array_roundsize (stem ## cnt >> 2)) \
290 { \
291 stem ## max = array_roundsize (stem ## cnt >> 1); \
292 base = ev_realloc (base, sizeof (*base) * (stem ## max)); \
293 fprintf (stderr, "slimmed down " # stem " to %d\n", stem ## max);/*D*/\
294 }
295
296#define array_free(stem, idx) \
297 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
236 298
237/*****************************************************************************/ 299/*****************************************************************************/
238 300
239static void 301static void
240anfds_init (ANFD *base, int count) 302anfds_init (ANFD *base, int count)
306 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next) 368 for (w = (struct ev_io *)anfd->head; w; w = (struct ev_io *)((WL)w)->next)
307 events |= w->events; 369 events |= w->events;
308 370
309 anfd->reify = 0; 371 anfd->reify = 0;
310 372
311 if (anfd->events != events)
312 {
313 method_modify (EV_A_ fd, anfd->events, events); 373 method_modify (EV_A_ fd, anfd->events, events);
314 anfd->events = events; 374 anfd->events = events;
315 }
316 } 375 }
317 376
318 fdchangecnt = 0; 377 fdchangecnt = 0;
319} 378}
320 379
362 int fd; 421 int fd;
363 422
364 for (fd = anfdmax; fd--; ) 423 for (fd = anfdmax; fd--; )
365 if (anfds [fd].events) 424 if (anfds [fd].events)
366 { 425 {
367 close (fd);
368 fd_kill (EV_A_ fd); 426 fd_kill (EV_A_ fd);
369 return; 427 return;
370 } 428 }
371} 429}
372 430
430 488
431/*****************************************************************************/ 489/*****************************************************************************/
432 490
433typedef struct 491typedef struct
434{ 492{
435 struct ev_watcher_list *head; 493 WL head;
436 sig_atomic_t volatile gotsig; 494 sig_atomic_t volatile gotsig;
437} ANSIG; 495} ANSIG;
438 496
439static ANSIG *signals; 497static ANSIG *signals;
440static int signalmax; 498static int signalmax;
456} 514}
457 515
458static void 516static void
459sighandler (int signum) 517sighandler (int signum)
460{ 518{
519#if WIN32
520 signal (signum, sighandler);
521#endif
522
461 signals [signum - 1].gotsig = 1; 523 signals [signum - 1].gotsig = 1;
462 524
463 if (!gotsig) 525 if (!gotsig)
464 { 526 {
465 int old_errno = errno; 527 int old_errno = errno;
470} 532}
471 533
472static void 534static void
473sigcb (EV_P_ struct ev_io *iow, int revents) 535sigcb (EV_P_ struct ev_io *iow, int revents)
474{ 536{
475 struct ev_watcher_list *w; 537 WL w;
476 int signum; 538 int signum;
477 539
478 read (sigpipe [0], &revents, 1); 540 read (sigpipe [0], &revents, 1);
479 gotsig = 0; 541 gotsig = 0;
480 542
637} 699}
638 700
639void 701void
640loop_destroy (EV_P) 702loop_destroy (EV_P)
641{ 703{
704 int i;
705
642#if EV_USE_WIN32 706#if EV_USE_WIN32
643 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 707 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
644#endif 708#endif
645#if EV_USE_KQUEUE 709#if EV_USE_KQUEUE
646 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 710 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
652 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 716 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
653#endif 717#endif
654#if EV_USE_SELECT 718#if EV_USE_SELECT
655 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 719 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
656#endif 720#endif
721
722 for (i = NUMPRI; i--; )
723 array_free (pending, [i]);
724
725 array_free (fdchange, );
726 array_free (timer, );
727 array_free (periodic, );
728 array_free (idle, );
729 array_free (prepare, );
730 array_free (check, );
657 731
658 method = 0; 732 method = 0;
659 /*TODO*/ 733 /*TODO*/
660} 734}
661 735
673 747
674#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
675struct ev_loop * 749struct ev_loop *
676ev_loop_new (int methods) 750ev_loop_new (int methods)
677{ 751{
678 struct ev_loop *loop = (struct ev_loop *)calloc (1, sizeof (struct ev_loop)); 752 struct ev_loop *loop = (struct ev_loop *)ev_malloc (sizeof (struct ev_loop));
753
754 memset (loop, 0, sizeof (struct ev_loop));
679 755
680 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
681 757
682 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
683 return loop; 759 return loop;
687 763
688void 764void
689ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
690{ 766{
691 loop_destroy (EV_A); 767 loop_destroy (EV_A);
692 free (loop); 768 ev_free (loop);
693} 769}
694 770
695void 771void
696ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
697{ 773{
796 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 872 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
797 873
798 if (p->w) 874 if (p->w)
799 { 875 {
800 p->w->pending = 0; 876 p->w->pending = 0;
801
802 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); 877 p->w->cb (EV_A_ p->w, p->events);
803 } 878 }
804 } 879 }
805} 880}
806 881
807static void 882static void
1299 array_needsize (signals, signalmax, w->signum, signals_init); 1374 array_needsize (signals, signalmax, w->signum, signals_init);
1300 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1375 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1301 1376
1302 if (!((WL)w)->next) 1377 if (!((WL)w)->next)
1303 { 1378 {
1379#if WIN32
1380 signal (w->signum, sighandler);
1381#else
1304 struct sigaction sa; 1382 struct sigaction sa;
1305 sa.sa_handler = sighandler; 1383 sa.sa_handler = sighandler;
1306 sigfillset (&sa.sa_mask); 1384 sigfillset (&sa.sa_mask);
1307 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */ 1385 sa.sa_flags = SA_RESTART; /* if restarting works we save one iteration */
1308 sigaction (w->signum, &sa, 0); 1386 sigaction (w->signum, &sa, 0);
1387#endif
1309 } 1388 }
1310} 1389}
1311 1390
1312void 1391void
1313ev_signal_stop (EV_P_ struct ev_signal *w) 1392ev_signal_stop (EV_P_ struct ev_signal *w)
1363 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1364 void *arg = once->arg; 1443 void *arg = once->arg;
1365 1444
1366 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1367 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1368 free (once); 1447 ev_free (once);
1369 1448
1370 cb (revents, arg); 1449 cb (revents, arg);
1371} 1450}
1372 1451
1373static void 1452static void
1383} 1462}
1384 1463
1385void 1464void
1386ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg) 1465ev_once (EV_P_ int fd, int events, ev_tstamp timeout, void (*cb)(int revents, void *arg), void *arg)
1387{ 1466{
1388 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1389 1468
1390 if (!once) 1469 if (!once)
1391 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1392 else 1471 else
1393 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines