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

Comparing EV/EV.xs (file contents):
Revision 1.47 by root, Sat Nov 3 21:58:51 2007 UTC vs.
Revision 1.70 by root, Fri Nov 16 10:37:28 2007 UTC

3#include "XSUB.h" 3#include "XSUB.h"
4 4
5/*#include <netinet/in.h>*/ 5/*#include <netinet/in.h>*/
6 6
7#define EV_PROTOTYPES 1 7#define EV_PROTOTYPES 1
8#define EV_STANDALONE 1
9#include "EV/EVAPI.h" 8#include "EV/EVAPI.h"
10 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 */
11#include "libev/ev.c" 23#include "libev/ev.c"
12#include "libev/event.h"
13#include "libev/event.c" 24#include "event.c"
25
26#ifndef _WIN32
14#define DNS_USE_GETTIMEOFDAY_FOR_ID 1 27#define DNS_USE_GETTIMEOFDAY_FOR_ID 1
28#if !defined (WIN32) && !defined(__CYGWIN__)
15#define HAVE_STRUCT_IN6_ADDR 1 29# define HAVE_STRUCT_IN6_ADDR 1
30#endif
16#undef HAVE_STRTOK_R 31#undef HAVE_STRTOK_R
17#undef strtok_r 32#undef strtok_r
18#define strtok_r fake_strtok_r 33#define strtok_r fake_strtok_r
34#include "evdns.h"
19#include "libev/evdns.c" 35#include "evdns.c"
36#endif
37
38#ifndef _WIN32
39# include <pthread.h>
40#endif
20 41
21typedef int Signal; 42typedef int Signal;
22 43
23static struct EVAPI evapi; 44static struct EVAPI evapi;
24 45
86 SvPOK_only (self); 107 SvPOK_only (self);
87 SvCUR_set (self, size); 108 SvCUR_set (self, size);
88 109
89 w = (struct ev_watcher *)SvPVX (self); 110 w = (struct ev_watcher *)SvPVX (self);
90 111
91 ev_watcher_init (w, e_cb); 112 ev_init (w, e_cb);
92 113
114 w->data = 0;
93 w->fh = 0; 115 w->fh = 0;
94 w->cb_sv = newSVsv (cb_sv); 116 w->cb_sv = newSVsv (cb_sv);
95 w->self = self; 117 w->self = self;
96 118
97 return (void *)w; 119 return (void *)w;
98} 120}
99 121
100static void * 122static void
101e_destroy (void *w_) 123e_destroy (void *w_)
102{ 124{
103 struct ev_watcher *w = w_; 125 struct ev_watcher *w = (struct ev_watcher *)w_;
104 126
105 SvREFCNT_dec (w->fh ); w->fh = 0; 127 SvREFCNT_dec (w->fh ); w->fh = 0;
106 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;
107} 130}
108 131
109static SV * 132static SV *
110e_bless (struct ev_watcher *w, HV *stash) 133e_bless (struct ev_watcher *w, HV *stash)
111{ 134{
146 PUSHs (sv_self); 169 PUSHs (sv_self);
147 PUSHs (sv_events); 170 PUSHs (sv_events);
148 171
149 PUTBACK; 172 PUTBACK;
150 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 173 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
151 SP = PL_stack_base + mark; PUTBACK;
152 174
153 SvREFCNT_dec (sv_self); 175 SvREFCNT_dec (sv_self);
154 SvREFCNT_dec (sv_status); 176 SvREFCNT_dec (sv_status);
155 177
156 if (sv_events_cache) 178 if (sv_events_cache)
161 if (SvTRUE (ERRSV)) 183 if (SvTRUE (ERRSV))
162 { 184 {
163 PUSHMARK (SP); 185 PUSHMARK (SP);
164 PUTBACK; 186 PUTBACK;
165 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);
166 SP = PL_stack_base + mark; PUTBACK;
167 } 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;
168} 235}
169 236
170///////////////////////////////////////////////////////////////////////////// 237/////////////////////////////////////////////////////////////////////////////
171// DNS 238// DNS
172 239
240#ifndef _WIN32
173static void 241static void
174dns_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)
175{ 243{
176 dSP; 244 dSP;
177 SV *cb = (SV *)arg; 245 SV *cb = (SV *)arg;
216 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);
217 } 285 }
218 286
219 LEAVE; 287 LEAVE;
220} 288}
289#endif
221 290
222#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 291#define CHECK_REPEAT(repeat) if (repeat < 0.) \
223 croak (# repeat " value must be >= 0"); 292 croak (# repeat " value must be >= 0");
224 293
225#define CHECK_FD(fh,fd) if ((fd) < 0) \ 294#define CHECK_FD(fh,fd) if ((fd) < 0) \
232 301
233PROTOTYPES: ENABLE 302PROTOTYPES: ENABLE
234 303
235BOOT: 304BOOT:
236{ 305{
237 int i;
238 HV *stash = gv_stashpv ("EV", 1); 306 HV *stash = gv_stashpv ("EV", 1);
239 307
240 static const struct { 308 static const struct {
241 const char *name; 309 const char *name;
242 IV iv; 310 IV iv;
255 const_iv (EV_, CHECK) 323 const_iv (EV_, CHECK)
256 const_iv (EV_, ERROR) 324 const_iv (EV_, ERROR)
257 325
258 const_iv (EV, LOOP_ONESHOT) 326 const_iv (EV, LOOP_ONESHOT)
259 const_iv (EV, LOOP_NONBLOCK) 327 const_iv (EV, LOOP_NONBLOCK)
328 const_iv (EV, UNLOOP_ONE)
329 const_iv (EV, UNLOOP_ALL)
260 330
261 const_iv (EV, METHOD_AUTO)
262 const_iv (EV, METHOD_SELECT) 331 const_iv (EV, METHOD_SELECT)
263 const_iv (EV, METHOD_POLL) 332 const_iv (EV, METHOD_POLL)
264 const_iv (EV, METHOD_EPOLL) 333 const_iv (EV, METHOD_EPOLL)
265 const_iv (EV, METHOD_KQUEUE) 334 const_iv (EV, METHOD_KQUEUE)
266 const_iv (EV, METHOD_DEVPOLL) 335 const_iv (EV, METHOD_DEVPOLL)
267 const_iv (EV, METHOD_PORT) 336 const_iv (EV, METHOD_PORT)
268 const_iv (EV, METHOD_ANY) 337 const_iv (EV, FLAG_AUTO)
338 const_iv (EV, FLAG_NOENV)
269 }; 339 };
270 340
271 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; )
272 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 342 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
273 343
315 evapi.child_stop = ev_child_stop; 385 evapi.child_stop = ev_child_stop;
316 386
317 sv_setiv (sv, (IV)&evapi); 387 sv_setiv (sv, (IV)&evapi);
318 SvREADONLY_on (sv); 388 SvREADONLY_on (sv);
319 } 389 }
320 390#ifndef _WIN32
321 pthread_atfork (ev_fork_prepare, ev_fork_parent, ev_fork_child); 391 pthread_atfork (0, 0, ev_default_fork);
392#endif
322} 393}
323 394
324NV ev_now () 395NV ev_now ()
325 396
326int ev_method () 397int ev_method ()
327 398
328NV ev_time () 399NV ev_time ()
329 400
330int ev_init (int methods = EVMETHOD_AUTO) 401int ev_default_loop (int methods = EVFLAG_AUTO)
331 402
332void ev_loop (int flags = 0) 403void ev_loop (int flags = 0)
333 404
334void ev_unloop (int how = 1) 405void ev_unloop (int how = 1)
335 406
359 ev_timer_set (RETVAL, after, repeat); 430 ev_timer_set (RETVAL, after, repeat);
360 if (!ix) ev_timer_start (RETVAL); 431 if (!ix) ev_timer_start (RETVAL);
361 OUTPUT: 432 OUTPUT:
362 RETVAL 433 RETVAL
363 434
364struct ev_periodic *periodic (NV at, NV interval, SV *cb) 435SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
365 ALIAS: 436 ALIAS:
366 periodic_ns = 1 437 periodic_ns = 1
367 INIT: 438 INIT:
368 CHECK_REPEAT (interval); 439 CHECK_REPEAT (interval);
369 CODE: 440 CODE:
441{
442 struct ev_periodic *w;
370 RETVAL = e_new (sizeof (struct ev_periodic), cb); 443 w = e_new (sizeof (struct ev_periodic), cb);
371 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);
372 if (!ix) ev_periodic_start (RETVAL); 447 if (!ix) ev_periodic_start (w);
448}
373 OUTPUT: 449 OUTPUT:
374 RETVAL 450 RETVAL
375 451
376struct ev_signal *signal (Signal signum, SV *cb) 452struct ev_signal *signal (Signal signum, SV *cb)
377 ALIAS: 453 ALIAS:
413 OUTPUT: 489 OUTPUT:
414 RETVAL 490 RETVAL
415 491
416struct ev_child *child (int pid, SV *cb) 492struct ev_child *child (int pid, SV *cb)
417 ALIAS: 493 ALIAS:
418 check_ns = 1 494 child_ns = 1
419 CODE: 495 CODE:
420 RETVAL = e_new (sizeof (struct ev_child), cb); 496 RETVAL = e_new (sizeof (struct ev_child), cb);
421 ev_child_set (RETVAL, pid); 497 ev_child_set (RETVAL, pid);
422 if (!ix) ev_child_start (RETVAL); 498 if (!ix) ev_child_start (RETVAL);
423 OUTPUT: 499 OUTPUT:
435{ 511{
436 RETVAL = newSVsv (w->cb_sv); 512 RETVAL = newSVsv (w->cb_sv);
437 513
438 if (items > 1) 514 if (items > 1)
439 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;
440} 524}
441 OUTPUT: 525 OUTPUT:
442 RETVAL 526 RETVAL
443 527
444void trigger (struct ev_watcher *w, int revents = EV_NONE) 528void trigger (struct ev_watcher *w, int revents = EV_NONE)
582 } 666 }
583} 667}
584 OUTPUT: 668 OUTPUT:
585 RETVAL 669 RETVAL
586 670
587MODULE = EV PACKAGE = EV::Time
588
589MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_ 671MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
590 672
591void ev_timer_start (struct ev_timer *w) 673void ev_timer_start (struct ev_timer *w)
592 INIT: 674 INIT:
593 CHECK_REPEAT (w->repeat); 675 CHECK_REPEAT (w->repeat);
620 INIT: 702 INIT:
621 CHECK_REPEAT (w->interval); 703 CHECK_REPEAT (w->interval);
622 704
623void ev_periodic_stop (struct ev_periodic *w) 705void ev_periodic_stop (struct ev_periodic *w)
624 706
707void ev_periodic_again (struct ev_periodic *w)
708
625void DESTROY (struct ev_periodic *w) 709void DESTROY (struct ev_periodic *w)
626 CODE: 710 CODE:
627 ev_periodic_stop (w); 711 ev_periodic_stop (w);
628 e_destroy (w); 712 e_destroy (w);
629 713
630void 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)
631 INIT: 715 INIT:
632 CHECK_REPEAT (interval); 716 CHECK_REPEAT (interval);
633 CODE: 717 CODE:
634{ 718{
635 int active = ev_is_active (w); 719 int active = ev_is_active (w);
636 if (active) ev_periodic_stop (w); 720 if (active) ev_periodic_stop (w);
637 721
722 SvREFCNT_dec (w->fh);
723 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
638 ev_periodic_set (w, at, interval); 724 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
639 725
640 if (active) ev_periodic_start (w); 726 if (active) ev_periodic_start (w);
641} 727}
642 728
643MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 729MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
719 rpid = 1 805 rpid = 1
720 CODE: 806 CODE:
721 RETVAL = ix ? w->rpid : w->rstatus; 807 RETVAL = ix ? w->rpid : w->rstatus;
722 OUTPUT: 808 OUTPUT:
723 RETVAL 809 RETVAL
810
811#ifndef _WIN32
724 812
725MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 813MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
726 814
727BOOT: 815BOOT:
728{ 816{
799 887
800int evdns_set_option (char *option, char *val, int flags) 888int evdns_set_option (char *option, char *val, int flags)
801 889
802int evdns_resolv_conf_parse (int flags, const char *filename) 890int evdns_resolv_conf_parse (int flags, const char *filename)
803 891
804#ifdef MS_WINDOWS 892#ifdef _WIN32
805 893
806int evdns_config_windows_nameservers () 894int evdns_config_windows_nameservers ()
807 895
808#endif 896#endif
809 897
853 941
854#void DESTROY (struct evhttp_request *req); 942#void DESTROY (struct evhttp_request *req);
855 943
856#endif 944#endif
857 945
946#endif
858 947
859 948
860 949
861 950
862 951

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines