ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
(Generate patch)

Comparing EV/EV.xs (file contents):
Revision 1.41 by root, Fri Nov 2 20:21:33 2007 UTC vs.
Revision 1.70 by root, Fri Nov 16 10:37:28 2007 UTC

5/*#include <netinet/in.h>*/ 5/*#include <netinet/in.h>*/
6 6
7#define EV_PROTOTYPES 1 7#define EV_PROTOTYPES 1
8#include "EV/EVAPI.h" 8#include "EV/EVAPI.h"
9 9
10/* fix perl api breakage */
11#undef signal
12#undef sigaction
13
14#define EV_USE_MONOTONIC 1
15#define EV_USE_REALTIME 1
16#define EV_SELECT_IS_WINSOCKET 0
17#ifdef _WIN32
18# define EV_SELECT_USE_FD_SET 0
19# define NFDBITS PERL_NFDBITS
20# define fd_mask Perl_fd_mask
21#endif
22/* due to bugs in OS X we have to use libev/ explicitly here */
10#include "libev/ev.c" 23#include "libev/ev.c"
11#include "libev/event.h"
12#include "libev/event.c" 24#include "event.c"
25
26#ifndef _WIN32
27#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
28#if !defined (WIN32) && !defined(__CYGWIN__)
29# define HAVE_STRUCT_IN6_ADDR 1
30#endif
31#undef HAVE_STRTOK_R
32#undef strtok_r
33#define strtok_r fake_strtok_r
34#include "evdns.h"
13#include "libev/evdns.c" 35#include "evdns.c"
36#endif
37
38#ifndef _WIN32
39# include <pthread.h>
40#endif
14 41
15typedef int Signal; 42typedef int Signal;
16 43
17static struct EVAPI evapi; 44static struct EVAPI evapi;
18 45
25 *stash_idle, 52 *stash_idle,
26 *stash_prepare, 53 *stash_prepare,
27 *stash_check, 54 *stash_check,
28 *stash_child; 55 *stash_child;
29 56
57#ifndef SIG_SIZE
58/* kudos to Slaven Rezic for the idea */
59static char sig_size [] = { SIG_NUM };
60# define SIG_SIZE (sizeof (sig_size) + 1)
61#endif
62
30static int 63static int
31sv_signum (SV *sig) 64sv_signum (SV *sig)
32{ 65{
33 int signum; 66 int signum;
34 67
74 SvPOK_only (self); 107 SvPOK_only (self);
75 SvCUR_set (self, size); 108 SvCUR_set (self, size);
76 109
77 w = (struct ev_watcher *)SvPVX (self); 110 w = (struct ev_watcher *)SvPVX (self);
78 111
79 ev_watcher_init (w, e_cb); 112 ev_init (w, e_cb);
80 113
114 w->data = 0;
81 w->fh = 0; 115 w->fh = 0;
82 w->cb_sv = newSVsv (cb_sv); 116 w->cb_sv = newSVsv (cb_sv);
83 w->self = self; 117 w->self = self;
84 118
85 return (void *)w; 119 return (void *)w;
86} 120}
87 121
88static void * 122static void
89e_destroy (void *w_) 123e_destroy (void *w_)
90{ 124{
91 struct ev_watcher *w = w_; 125 struct ev_watcher *w = (struct ev_watcher *)w_;
92 126
93 SvREFCNT_dec (w->fh ); w->fh = 0; 127 SvREFCNT_dec (w->fh ); w->fh = 0;
94 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0; 128 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
129 SvREFCNT_dec (w->data ); w->data = 0;
95} 130}
96 131
97static SV * 132static SV *
98e_bless (struct ev_watcher *w, HV *stash) 133e_bless (struct ev_watcher *w, HV *stash)
99{ 134{
132 PUSHMARK (SP); 167 PUSHMARK (SP);
133 EXTEND (SP, 2); 168 EXTEND (SP, 2);
134 PUSHs (sv_self); 169 PUSHs (sv_self);
135 PUSHs (sv_events); 170 PUSHs (sv_events);
136 171
137 if (revents & EV_CHILD)
138 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
139
140 PUTBACK; 172 PUTBACK;
141 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 173 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
142 SP = PL_stack_base + mark; PUTBACK;
143 174
144 SvREFCNT_dec (sv_self); 175 SvREFCNT_dec (sv_self);
145 SvREFCNT_dec (sv_status); 176 SvREFCNT_dec (sv_status);
146 177
147 if (sv_events_cache) 178 if (sv_events_cache)
152 if (SvTRUE (ERRSV)) 183 if (SvTRUE (ERRSV))
153 { 184 {
154 PUSHMARK (SP); 185 PUSHMARK (SP);
155 PUTBACK; 186 PUTBACK;
156 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 187 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
157 SP = PL_stack_base + mark; PUTBACK;
158 } 188 }
189
190 SP = PL_stack_base + mark;
191 PUTBACK;
192}
193
194static ev_tstamp
195e_periodic_cb (struct ev_periodic *w, ev_tstamp now)
196{
197 ev_tstamp retval;
198 int count;
199 dSP;
200
201 ENTER;
202 SAVETMPS;
203
204 PUSHMARK (SP);
205 EXTEND (SP, 2);
206 PUSHs (newRV_inc (w->self)); /* w->self MUST be blessed by now */
207 PUSHs (newSVnv (now));
208
209 PUTBACK;
210 count = call_sv (w->fh, G_SCALAR | G_EVAL);
211 SPAGAIN;
212
213 if (SvTRUE (ERRSV))
214 {
215 PUSHMARK (SP);
216 PUTBACK;
217 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
218 SPAGAIN;
219 }
220
221 if (count > 0)
222 {
223 retval = SvNV (TOPs);
224
225 if (retval < now)
226 retval = now;
227 }
228 else
229 retval = now;
230
231 FREETMPS;
232 LEAVE;
233
234 return retval;
159} 235}
160 236
161///////////////////////////////////////////////////////////////////////////// 237/////////////////////////////////////////////////////////////////////////////
162// DNS 238// DNS
163 239
240#ifndef _WIN32
164static void 241static void
165dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg) 242dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
166{ 243{
167 dSP; 244 dSP;
168 SV *cb = (SV *)arg; 245 SV *cb = (SV *)arg;
207 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 284 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
208 } 285 }
209 286
210 LEAVE; 287 LEAVE;
211} 288}
289#endif
212 290
213#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 291#define CHECK_REPEAT(repeat) if (repeat < 0.) \
214 croak (# repeat " value must be >= 0"); 292 croak (# repeat " value must be >= 0");
215 293
216#define CHECK_FD(fh,fd) if ((fd) < 0) \ 294#define CHECK_FD(fh,fd) if ((fd) < 0) \
223 301
224PROTOTYPES: ENABLE 302PROTOTYPES: ENABLE
225 303
226BOOT: 304BOOT:
227{ 305{
228 int i;
229 HV *stash = gv_stashpv ("EV", 1); 306 HV *stash = gv_stashpv ("EV", 1);
230 307
231 static const struct { 308 static const struct {
232 const char *name; 309 const char *name;
233 IV iv; 310 IV iv;
246 const_iv (EV_, CHECK) 323 const_iv (EV_, CHECK)
247 const_iv (EV_, ERROR) 324 const_iv (EV_, ERROR)
248 325
249 const_iv (EV, LOOP_ONESHOT) 326 const_iv (EV, LOOP_ONESHOT)
250 const_iv (EV, LOOP_NONBLOCK) 327 const_iv (EV, LOOP_NONBLOCK)
328 const_iv (EV, UNLOOP_ONE)
329 const_iv (EV, UNLOOP_ALL)
251 330
252 const_iv (EV, METHOD_AUTO)
253 const_iv (EV, METHOD_SELECT) 331 const_iv (EV, METHOD_SELECT)
254 const_iv (EV, METHOD_POLL) 332 const_iv (EV, METHOD_POLL)
255 const_iv (EV, METHOD_EPOLL) 333 const_iv (EV, METHOD_EPOLL)
334 const_iv (EV, METHOD_KQUEUE)
335 const_iv (EV, METHOD_DEVPOLL)
256 const_iv (EV, METHOD_ANY) 336 const_iv (EV, METHOD_PORT)
337 const_iv (EV, FLAG_AUTO)
338 const_iv (EV, FLAG_NOENV)
257 }; 339 };
258 340
259 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 341 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
260 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 342 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
261 343
276 /* the poor man's shared library emulator */ 358 /* the poor man's shared library emulator */
277 evapi.ver = EV_API_VERSION; 359 evapi.ver = EV_API_VERSION;
278 evapi.rev = EV_API_REVISION; 360 evapi.rev = EV_API_REVISION;
279 evapi.sv_fileno = sv_fileno; 361 evapi.sv_fileno = sv_fileno;
280 evapi.sv_signum = sv_signum; 362 evapi.sv_signum = sv_signum;
281 evapi.now = &ev_now; 363 evapi.now = ev_now;
282 evapi.method = &ev_method; 364 evapi.method = ev_method;
283 evapi.loop_done = &ev_loop_done; 365 evapi.unloop = ev_unloop;
284 evapi.time = ev_time; 366 evapi.time = ev_time;
285 evapi.loop = ev_loop; 367 evapi.loop = ev_loop;
286 evapi.once = ev_once; 368 evapi.once = ev_once;
287 evapi.io_start = ev_io_start; 369 evapi.io_start = ev_io_start;
288 evapi.io_stop = ev_io_stop; 370 evapi.io_stop = ev_io_stop;
303 evapi.child_stop = ev_child_stop; 385 evapi.child_stop = ev_child_stop;
304 386
305 sv_setiv (sv, (IV)&evapi); 387 sv_setiv (sv, (IV)&evapi);
306 SvREADONLY_on (sv); 388 SvREADONLY_on (sv);
307 } 389 }
308 390#ifndef _WIN32
309 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 391 pthread_atfork (0, 0, ev_default_fork);
392#endif
310} 393}
311 394
312NV ev_now () 395NV ev_now ()
313 CODE:
314 RETVAL = ev_now;
315 OUTPUT:
316 RETVAL
317 396
318int ev_method () 397int ev_method ()
319 CODE:
320 RETVAL = ev_method;
321 OUTPUT:
322 RETVAL
323 398
324NV ev_time () 399NV ev_time ()
325 400
326void ev_init (int methods = EVMETHOD_AUTO) 401int ev_default_loop (int methods = EVFLAG_AUTO)
327 402
328void ev_loop (int flags = 0) 403void ev_loop (int flags = 0)
329 404
330void ev_loop_done (int value = 1) 405void ev_unloop (int how = 1)
331 CODE:
332 ev_loop_done = value;
333 406
334struct ev_io *io (SV *fh, int events, SV *cb) 407struct ev_io *io (SV *fh, int events, SV *cb)
335 ALIAS: 408 ALIAS:
336 io_ns = 1 409 io_ns = 1
337 CODE: 410 CODE:
357 ev_timer_set (RETVAL, after, repeat); 430 ev_timer_set (RETVAL, after, repeat);
358 if (!ix) ev_timer_start (RETVAL); 431 if (!ix) ev_timer_start (RETVAL);
359 OUTPUT: 432 OUTPUT:
360 RETVAL 433 RETVAL
361 434
362struct ev_periodic *periodic (NV at, NV interval, SV *cb) 435SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
363 ALIAS: 436 ALIAS:
364 periodic_ns = 1 437 periodic_ns = 1
365 INIT: 438 INIT:
366 CHECK_REPEAT (interval); 439 CHECK_REPEAT (interval);
367 CODE: 440 CODE:
441{
442 struct ev_periodic *w;
368 RETVAL = e_new (sizeof (struct ev_periodic), cb); 443 w = e_new (sizeof (struct ev_periodic), cb);
369 ev_periodic_set (RETVAL, at, interval); 444 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
445 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
446 RETVAL = e_bless ((struct ev_watcher *)w, stash_periodic);
370 if (!ix) ev_periodic_start (RETVAL); 447 if (!ix) ev_periodic_start (w);
448}
371 OUTPUT: 449 OUTPUT:
372 RETVAL 450 RETVAL
373 451
374struct ev_signal *signal (Signal signum, SV *cb) 452struct ev_signal *signal (Signal signum, SV *cb)
375 ALIAS: 453 ALIAS:
411 OUTPUT: 489 OUTPUT:
412 RETVAL 490 RETVAL
413 491
414struct ev_child *child (int pid, SV *cb) 492struct ev_child *child (int pid, SV *cb)
415 ALIAS: 493 ALIAS:
416 check_ns = 1 494 child_ns = 1
417 CODE: 495 CODE:
418 RETVAL = e_new (sizeof (struct ev_check), cb); 496 RETVAL = e_new (sizeof (struct ev_child), cb);
419 ev_child_set (RETVAL, pid); 497 ev_child_set (RETVAL, pid);
420 if (!ix) ev_child_start (RETVAL); 498 if (!ix) ev_child_start (RETVAL);
421 OUTPUT: 499 OUTPUT:
422 RETVAL 500 RETVAL
423 501
433{ 511{
434 RETVAL = newSVsv (w->cb_sv); 512 RETVAL = newSVsv (w->cb_sv);
435 513
436 if (items > 1) 514 if (items > 1)
437 sv_setsv (w->cb_sv, new_cb); 515 sv_setsv (w->cb_sv, new_cb);
516}
517 OUTPUT:
518 RETVAL
519
520SV *data (struct ev_watcher *w, SV *new_data = 0)
521 CODE:
522{
523 RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef;
438} 524}
439 OUTPUT: 525 OUTPUT:
440 RETVAL 526 RETVAL
441 527
442void trigger (struct ev_watcher *w, int revents = EV_NONE) 528void trigger (struct ev_watcher *w, int revents = EV_NONE)
580 } 666 }
581} 667}
582 OUTPUT: 668 OUTPUT:
583 RETVAL 669 RETVAL
584 670
585MODULE = EV PACKAGE = EV::Time
586
587MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 671MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
588 672
589void ev_timer_start (struct ev_timer *w) 673void ev_timer_start (struct ev_timer *w)
590 INIT: 674 INIT:
591 CHECK_REPEAT (w->repeat); 675 CHECK_REPEAT (w->repeat);
618 INIT: 702 INIT:
619 CHECK_REPEAT (w->interval); 703 CHECK_REPEAT (w->interval);
620 704
621void ev_periodic_stop (struct ev_periodic *w) 705void ev_periodic_stop (struct ev_periodic *w)
622 706
707void ev_periodic_again (struct ev_periodic *w)
708
623void DESTROY (struct ev_periodic *w) 709void DESTROY (struct ev_periodic *w)
624 CODE: 710 CODE:
625 ev_periodic_stop (w); 711 ev_periodic_stop (w);
626 e_destroy (w); 712 e_destroy (w);
627 713
628void set (struct ev_periodic *w, NV at, NV interval = 0.) 714void set (struct ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef)
629 INIT: 715 INIT:
630 CHECK_REPEAT (interval); 716 CHECK_REPEAT (interval);
631 CODE: 717 CODE:
632{ 718{
633 int active = ev_is_active (w); 719 int active = ev_is_active (w);
634 if (active) ev_periodic_stop (w); 720 if (active) ev_periodic_stop (w);
635 721
722 SvREFCNT_dec (w->fh);
723 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
636 ev_periodic_set (w, at, interval); 724 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
637 725
638 if (active) ev_periodic_start (w); 726 if (active) ev_periodic_start (w);
639} 727}
640 728
641MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 729MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
710} 798}
711 OUTPUT: 799 OUTPUT:
712 RETVAL 800 RETVAL
713 801
714 802
715int status (struct ev_child *w) 803int rstatus (struct ev_child *w)
804 ALIAS:
805 rpid = 1
716 CODE: 806 CODE:
717 RETVAL = w->status; 807 RETVAL = ix ? w->rpid : w->rstatus;
718 OUTPUT: 808 OUTPUT:
719 RETVAL 809 RETVAL
810
811#ifndef _WIN32
720 812
721MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 813MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
722 814
723BOOT: 815BOOT:
724{ 816{
795 887
796int evdns_set_option (char *option, char *val, int flags) 888int evdns_set_option (char *option, char *val, int flags)
797 889
798int evdns_resolv_conf_parse (int flags, const char *filename) 890int evdns_resolv_conf_parse (int flags, const char *filename)
799 891
800#ifdef MS_WINDOWS 892#ifdef _WIN32
801 893
802int evdns_config_windows_nameservers () 894int evdns_config_windows_nameservers ()
803 895
804#endif 896#endif
805 897
849 941
850#void DESTROY (struct evhttp_request *req); 942#void DESTROY (struct evhttp_request *req);
851 943
852#endif 944#endif
853 945
946#endif
854 947
855 948
856 949
857 950
858 951

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines