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

Comparing libev/ev.c (file contents):
Revision 1.64 by root, Sun Nov 4 23:14:11 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)
359 int fd; 421 int fd;
360 422
361 for (fd = anfdmax; fd--; ) 423 for (fd = anfdmax; fd--; )
362 if (anfds [fd].events) 424 if (anfds [fd].events)
363 { 425 {
364 close (fd);
365 fd_kill (EV_A_ fd); 426 fd_kill (EV_A_ fd);
366 return; 427 return;
367 } 428 }
368} 429}
369 430
427 488
428/*****************************************************************************/ 489/*****************************************************************************/
429 490
430typedef struct 491typedef struct
431{ 492{
432 struct ev_watcher_list *head; 493 WL head;
433 sig_atomic_t volatile gotsig; 494 sig_atomic_t volatile gotsig;
434} ANSIG; 495} ANSIG;
435 496
436static ANSIG *signals; 497static ANSIG *signals;
437static int signalmax; 498static int signalmax;
453} 514}
454 515
455static void 516static void
456sighandler (int signum) 517sighandler (int signum)
457{ 518{
519#if WIN32
520 signal (signum, sighandler);
521#endif
522
458 signals [signum - 1].gotsig = 1; 523 signals [signum - 1].gotsig = 1;
459 524
460 if (!gotsig) 525 if (!gotsig)
461 { 526 {
462 int old_errno = errno; 527 int old_errno = errno;
467} 532}
468 533
469static void 534static void
470sigcb (EV_P_ struct ev_io *iow, int revents) 535sigcb (EV_P_ struct ev_io *iow, int revents)
471{ 536{
472 struct ev_watcher_list *w; 537 WL w;
473 int signum; 538 int signum;
474 539
475 read (sigpipe [0], &revents, 1); 540 read (sigpipe [0], &revents, 1);
476 gotsig = 0; 541 gotsig = 0;
477 542
634} 699}
635 700
636void 701void
637loop_destroy (EV_P) 702loop_destroy (EV_P)
638{ 703{
704 int i;
705
639#if EV_USE_WIN32 706#if EV_USE_WIN32
640 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A); 707 if (method == EVMETHOD_WIN32 ) win32_destroy (EV_A);
641#endif 708#endif
642#if EV_USE_KQUEUE 709#if EV_USE_KQUEUE
643 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A); 710 if (method == EVMETHOD_KQUEUE) kqueue_destroy (EV_A);
649 if (method == EVMETHOD_POLL ) poll_destroy (EV_A); 716 if (method == EVMETHOD_POLL ) poll_destroy (EV_A);
650#endif 717#endif
651#if EV_USE_SELECT 718#if EV_USE_SELECT
652 if (method == EVMETHOD_SELECT) select_destroy (EV_A); 719 if (method == EVMETHOD_SELECT) select_destroy (EV_A);
653#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, );
654 731
655 method = 0; 732 method = 0;
656 /*TODO*/ 733 /*TODO*/
657} 734}
658 735
670 747
671#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
672struct ev_loop * 749struct ev_loop *
673ev_loop_new (int methods) 750ev_loop_new (int methods)
674{ 751{
675 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));
676 755
677 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
678 757
679 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
680 return loop; 759 return loop;
684 763
685void 764void
686ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
687{ 766{
688 loop_destroy (EV_A); 767 loop_destroy (EV_A);
689 free (loop); 768 ev_free (loop);
690} 769}
691 770
692void 771void
693ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
694{ 773{
793 ANPENDING *p = pendings [pri] + --pendingcnt [pri]; 872 ANPENDING *p = pendings [pri] + --pendingcnt [pri];
794 873
795 if (p->w) 874 if (p->w)
796 { 875 {
797 p->w->pending = 0; 876 p->w->pending = 0;
798
799 (*(void (**)(EV_P_ W, int))&p->w->cb) (EV_A_ p->w, p->events); 877 p->w->cb (EV_A_ p->w, p->events);
800 } 878 }
801 } 879 }
802} 880}
803 881
804static void 882static void
1296 array_needsize (signals, signalmax, w->signum, signals_init); 1374 array_needsize (signals, signalmax, w->signum, signals_init);
1297 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1375 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1298 1376
1299 if (!((WL)w)->next) 1377 if (!((WL)w)->next)
1300 { 1378 {
1379#if WIN32
1380 signal (w->signum, sighandler);
1381#else
1301 struct sigaction sa; 1382 struct sigaction sa;
1302 sa.sa_handler = sighandler; 1383 sa.sa_handler = sighandler;
1303 sigfillset (&sa.sa_mask); 1384 sigfillset (&sa.sa_mask);
1304 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 */
1305 sigaction (w->signum, &sa, 0); 1386 sigaction (w->signum, &sa, 0);
1387#endif
1306 } 1388 }
1307} 1389}
1308 1390
1309void 1391void
1310ev_signal_stop (EV_P_ struct ev_signal *w) 1392ev_signal_stop (EV_P_ struct ev_signal *w)
1360 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1361 void *arg = once->arg; 1443 void *arg = once->arg;
1362 1444
1363 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1364 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1365 free (once); 1447 ev_free (once);
1366 1448
1367 cb (revents, arg); 1449 cb (revents, arg);
1368} 1450}
1369 1451
1370static void 1452static void
1380} 1462}
1381 1463
1382void 1464void
1383ev_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)
1384{ 1466{
1385 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1386 1468
1387 if (!once) 1469 if (!once)
1388 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1389 else 1471 else
1390 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines