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

Comparing libev/ev.c (file contents):
Revision 1.66 by root, Sun Nov 4 23:30:53 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; \
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*/\
235 } 294 }
236 295
237#define array_free(stem, idx) \ 296#define array_free(stem, idx) \
238 free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0; 297 ev_free (stem ## s idx); stem ## cnt idx = stem ## max idx = 0;
239 298
240/*****************************************************************************/ 299/*****************************************************************************/
241 300
242static void 301static void
243anfds_init (ANFD *base, int count) 302anfds_init (ANFD *base, int count)
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
685 747
686#if EV_MULTIPLICITY 748#if EV_MULTIPLICITY
687struct ev_loop * 749struct ev_loop *
688ev_loop_new (int methods) 750ev_loop_new (int methods)
689{ 751{
690 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));
691 755
692 loop_init (EV_A_ methods); 756 loop_init (EV_A_ methods);
693 757
694 if (ev_method (EV_A)) 758 if (ev_method (EV_A))
695 return loop; 759 return loop;
699 763
700void 764void
701ev_loop_destroy (EV_P) 765ev_loop_destroy (EV_P)
702{ 766{
703 loop_destroy (EV_A); 767 loop_destroy (EV_A);
704 free (loop); 768 ev_free (loop);
705} 769}
706 770
707void 771void
708ev_loop_fork (EV_P) 772ev_loop_fork (EV_P)
709{ 773{
1310 array_needsize (signals, signalmax, w->signum, signals_init); 1374 array_needsize (signals, signalmax, w->signum, signals_init);
1311 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w); 1375 wlist_add ((WL *)&signals [w->signum - 1].head, (WL)w);
1312 1376
1313 if (!((WL)w)->next) 1377 if (!((WL)w)->next)
1314 { 1378 {
1379#if WIN32
1380 signal (w->signum, sighandler);
1381#else
1315 struct sigaction sa; 1382 struct sigaction sa;
1316 sa.sa_handler = sighandler; 1383 sa.sa_handler = sighandler;
1317 sigfillset (&sa.sa_mask); 1384 sigfillset (&sa.sa_mask);
1318 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 */
1319 sigaction (w->signum, &sa, 0); 1386 sigaction (w->signum, &sa, 0);
1387#endif
1320 } 1388 }
1321} 1389}
1322 1390
1323void 1391void
1324ev_signal_stop (EV_P_ struct ev_signal *w) 1392ev_signal_stop (EV_P_ struct ev_signal *w)
1374 void (*cb)(int revents, void *arg) = once->cb; 1442 void (*cb)(int revents, void *arg) = once->cb;
1375 void *arg = once->arg; 1443 void *arg = once->arg;
1376 1444
1377 ev_io_stop (EV_A_ &once->io); 1445 ev_io_stop (EV_A_ &once->io);
1378 ev_timer_stop (EV_A_ &once->to); 1446 ev_timer_stop (EV_A_ &once->to);
1379 free (once); 1447 ev_free (once);
1380 1448
1381 cb (revents, arg); 1449 cb (revents, arg);
1382} 1450}
1383 1451
1384static void 1452static void
1394} 1462}
1395 1463
1396void 1464void
1397ev_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)
1398{ 1466{
1399 struct ev_once *once = malloc (sizeof (struct ev_once)); 1467 struct ev_once *once = ev_malloc (sizeof (struct ev_once));
1400 1468
1401 if (!once) 1469 if (!once)
1402 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg); 1470 cb (EV_ERROR | EV_READ | EV_WRITE | EV_TIMEOUT, arg);
1403 else 1471 else
1404 { 1472 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines