ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
Revision: 1.97
Committed: Fri Dec 21 04:41:12 2007 UTC (16 years, 4 months ago) by root
Branch: MAIN
Changes since 1.96: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 #include "EXTERN.h"
2     #include "perl.h"
3     #include "XSUB.h"
4    
5 root 1.20 /*#include <netinet/in.h>*/
6 root 1.1
7 root 1.20 #define EV_PROTOTYPES 1
8 root 1.92 #define EV_H <ev.h>
9 root 1.11 #include "EV/EVAPI.h"
10    
11 root 1.56 /* fix perl api breakage */
12     #undef signal
13     #undef sigaction
14    
15 root 1.64 #define EV_SELECT_IS_WINSOCKET 0
16     #ifdef _WIN32
17     # define EV_SELECT_USE_FD_SET 0
18     # define NFDBITS PERL_NFDBITS
19     # define fd_mask Perl_fd_mask
20     #endif
21 root 1.53 /* due to bugs in OS X we have to use libev/ explicitly here */
22     #include "libev/ev.c"
23 root 1.56
24 root 1.64 #ifndef _WIN32
25 root 1.55 # include <pthread.h>
26     #endif
27    
28 root 1.93 #define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
29    
30 root 1.78 #define WFLAG_KEEPALIVE 1
31    
32     #define UNREF(w) \
33     if (!((w)->flags & WFLAG_KEEPALIVE) \
34     && !ev_is_active (w)) \
35 root 1.93 ev_unref (e_loop (w));
36 root 1.78
37     #define REF(w) \
38     if (!((w)->flags & WFLAG_KEEPALIVE) \
39     && ev_is_active (w)) \
40 root 1.93 ev_ref (e_loop (w));
41 root 1.78
42     #define START(type,w) \
43     do { \
44     UNREF (w); \
45 root 1.93 ev_ ## type ## _start (e_loop (w), w); \
46 root 1.78 } while (0)
47    
48     #define STOP(type,w) \
49     do { \
50     REF (w); \
51 root 1.93 ev_ ## type ## _stop (e_loop (w), w); \
52 root 1.78 } while (0)
53    
54 root 1.80 #define RESET(type,w,seta) \
55     do { \
56     int active = ev_is_active (w); \
57     if (active) STOP (type, w); \
58     ev_ ## type ## _set seta; \
59     if (active) START (type, w); \
60     } while (0)
61    
62 root 1.10 typedef int Signal;
63 root 1.1
64 root 1.93 static SV *default_loop_sv;
65    
66 root 1.11 static struct EVAPI evapi;
67    
68 root 1.15 static HV
69 root 1.93 *stash_loop,
70 root 1.15 *stash_watcher,
71     *stash_io,
72     *stash_timer,
73     *stash_periodic,
74     *stash_signal,
75 root 1.81 *stash_child,
76     *stash_stat,
77 root 1.15 *stash_idle,
78 root 1.22 *stash_prepare,
79 root 1.23 *stash_check,
80 root 1.80 *stash_embed,
81 root 1.81 *stash_fork;
82 root 1.1
83 root 1.43 #ifndef SIG_SIZE
84     /* kudos to Slaven Rezic for the idea */
85     static char sig_size [] = { SIG_NUM };
86     # define SIG_SIZE (sizeof (sig_size) + 1)
87     #endif
88    
89 root 1.80 static Signal
90 root 1.10 sv_signum (SV *sig)
91     {
92 root 1.80 Signal signum;
93 root 1.10
94 root 1.31 SvGETMAGIC (sig);
95 root 1.10
96     for (signum = 1; signum < SIG_SIZE; ++signum)
97     if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
98     return signum;
99    
100 root 1.80 signum = SvIV (sig);
101    
102     if (signum > 0 && signum < SIG_SIZE)
103     return signum;
104 root 1.31
105 root 1.10 return -1;
106     }
107    
108 root 1.1 /////////////////////////////////////////////////////////////////////////////
109     // Event
110    
111 root 1.91 static void e_cb (EV_P_ ev_watcher *w, int revents);
112 root 1.1
113 root 1.15 static int
114     sv_fileno (SV *fh)
115 root 1.1 {
116 root 1.3 SvGETMAGIC (fh);
117 root 1.1
118 root 1.3 if (SvROK (fh))
119     fh = SvRV (fh);
120 root 1.1
121 root 1.3 if (SvTYPE (fh) == SVt_PVGV)
122     return PerlIO_fileno (IoIFP (sv_2io (fh)));
123 root 1.1
124 root 1.75 if (SvOK (fh) && (SvIV (fh) >= 0) && (SvIV (fh) < 0x7fffffffL))
125 root 1.3 return SvIV (fh);
126 root 1.1
127     return -1;
128     }
129    
130 root 1.15 static void *
131 root 1.93 e_new (int size, SV *cb_sv, SV *loop)
132 root 1.1 {
133 root 1.78 ev_watcher *w;
134 root 1.15 SV *self = NEWSV (0, size);
135 root 1.1 SvPOK_only (self);
136 root 1.15 SvCUR_set (self, size);
137 root 1.1
138 root 1.78 w = (ev_watcher *)SvPVX (self);
139 root 1.1
140 root 1.63 ev_init (w, e_cb);
141 root 1.1
142 root 1.93 w->loop = SvREFCNT_inc (SvRV (loop));
143 root 1.78 w->flags = WFLAG_KEEPALIVE;
144 root 1.61 w->data = 0;
145 root 1.15 w->fh = 0;
146 root 1.93 w->cb_sv = SvTEMP (cb_sv) && SvREFCNT (cb_sv) == 1 ? SvREFCNT_inc (cb_sv) : newSVsv (cb_sv);
147 root 1.15 w->self = self;
148 root 1.1
149 root 1.15 return (void *)w;
150 root 1.1 }
151    
152 root 1.56 static void
153 root 1.34 e_destroy (void *w_)
154     {
155 root 1.78 ev_watcher *w = (ev_watcher *)w_;
156 root 1.34
157 root 1.93 SvREFCNT_dec (w->loop ); w->loop = 0;
158 root 1.34 SvREFCNT_dec (w->fh ); w->fh = 0;
159     SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
160 root 1.60 SvREFCNT_dec (w->data ); w->data = 0;
161 root 1.34 }
162    
163 root 1.15 static SV *
164 root 1.78 e_bless (ev_watcher *w, HV *stash)
165 root 1.1 {
166     SV *rv;
167    
168 root 1.15 if (SvOBJECT (w->self))
169     rv = newRV_inc (w->self);
170 root 1.1 else
171     {
172 root 1.15 rv = newRV_noinc (w->self);
173     sv_bless (rv, stash);
174     SvREADONLY_on (w->self);
175 root 1.1 }
176    
177     return rv;
178     }
179    
180 root 1.75 static SV *sv_events_cache;
181    
182 root 1.1 static void
183 root 1.91 e_cb (EV_P_ ev_watcher *w, int revents)
184 root 1.1 {
185     dSP;
186 root 1.14 I32 mark = SP - PL_stack_base;
187 root 1.75 SV *sv_self, *sv_events;
188 root 1.1
189 root 1.15 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
190 root 1.14
191     if (sv_events_cache)
192     {
193     sv_events = sv_events_cache; sv_events_cache = 0;
194 root 1.15 SvIV_set (sv_events, revents);
195 root 1.14 }
196     else
197 root 1.15 sv_events = newSViv (revents);
198 root 1.14
199 root 1.1 PUSHMARK (SP);
200     EXTEND (SP, 2);
201 root 1.14 PUSHs (sv_self);
202     PUSHs (sv_events);
203 root 1.24
204 root 1.1 PUTBACK;
205 root 1.15 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
206 root 1.14
207     SvREFCNT_dec (sv_self);
208 root 1.75
209     if (sv_events_cache)
210     SvREFCNT_dec (sv_events);
211     else
212     sv_events_cache = sv_events;
213    
214     if (SvTRUE (ERRSV))
215     {
216 root 1.84 SPAGAIN;
217 root 1.75 PUSHMARK (SP);
218     PUTBACK;
219     call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
220     }
221    
222     SP = PL_stack_base + mark;
223     PUTBACK;
224     }
225    
226     static void
227     e_once_cb (int revents, void *arg)
228     {
229     dSP;
230     I32 mark = SP - PL_stack_base;
231     SV *sv_events;
232    
233     if (sv_events_cache)
234     {
235     sv_events = sv_events_cache; sv_events_cache = 0;
236     SvIV_set (sv_events, revents);
237     }
238     else
239     sv_events = newSViv (revents);
240    
241     PUSHMARK (SP);
242     XPUSHs (sv_events);
243    
244     PUTBACK;
245     call_sv ((SV *)arg, G_DISCARD | G_VOID | G_EVAL);
246    
247     SvREFCNT_dec ((SV *)arg);
248 root 1.14
249     if (sv_events_cache)
250     SvREFCNT_dec (sv_events);
251     else
252     sv_events_cache = sv_events;
253 root 1.1
254 root 1.8 if (SvTRUE (ERRSV))
255     {
256 root 1.86 SPAGAIN;
257 root 1.8 PUSHMARK (SP);
258     PUTBACK;
259     call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
260     }
261 root 1.68
262     SP = PL_stack_base + mark;
263     PUTBACK;
264 root 1.1 }
265    
266 root 1.59 static ev_tstamp
267 root 1.78 e_periodic_cb (ev_periodic *w, ev_tstamp now)
268 root 1.59 {
269     ev_tstamp retval;
270     int count;
271     dSP;
272    
273     ENTER;
274     SAVETMPS;
275    
276     PUSHMARK (SP);
277     EXTEND (SP, 2);
278     PUSHs (newRV_inc (w->self)); /* w->self MUST be blessed by now */
279     PUSHs (newSVnv (now));
280    
281     PUTBACK;
282     count = call_sv (w->fh, G_SCALAR | G_EVAL);
283     SPAGAIN;
284    
285     if (SvTRUE (ERRSV))
286     {
287     PUSHMARK (SP);
288     PUTBACK;
289     call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
290     SPAGAIN;
291     }
292    
293     if (count > 0)
294     {
295     retval = SvNV (TOPs);
296    
297     if (retval < now)
298     retval = now;
299     }
300     else
301     retval = now;
302    
303     FREETMPS;
304     LEAVE;
305    
306     return retval;
307     }
308    
309 root 1.18 #define CHECK_REPEAT(repeat) if (repeat < 0.) \
310     croak (# repeat " value must be >= 0");
311    
312 root 1.31 #define CHECK_FD(fh,fd) if ((fd) < 0) \
313     croak ("illegal file descriptor or filehandle (either no attached file descriptor or illegal value): %s", SvPV_nolen (fh));
314    
315 root 1.80 #define CHECK_SIG(sv,num) if ((num) < 0) \
316     croak ("illegal signal number or name: %s", SvPV_nolen (sv));
317    
318 root 1.1 /////////////////////////////////////////////////////////////////////////////
319     // XS interface functions
320    
321 root 1.15 MODULE = EV PACKAGE = EV PREFIX = ev_
322 root 1.1
323 root 1.21 PROTOTYPES: ENABLE
324    
325 root 1.1 BOOT:
326     {
327     HV *stash = gv_stashpv ("EV", 1);
328    
329     static const struct {
330     const char *name;
331     IV iv;
332     } *civ, const_iv[] = {
333     # define const_iv(pfx, name) { # name, (IV) pfx ## name },
334 root 1.41 const_iv (EV_, MINPRI)
335     const_iv (EV_, MAXPRI)
336    
337 root 1.20 const_iv (EV_, UNDEF)
338 root 1.1 const_iv (EV_, NONE)
339     const_iv (EV_, TIMEOUT)
340     const_iv (EV_, READ)
341     const_iv (EV_, WRITE)
342     const_iv (EV_, SIGNAL)
343 root 1.15 const_iv (EV_, IDLE)
344     const_iv (EV_, CHECK)
345 root 1.20 const_iv (EV_, ERROR)
346 root 1.15
347     const_iv (EV, LOOP_ONESHOT)
348 root 1.1 const_iv (EV, LOOP_NONBLOCK)
349 root 1.67 const_iv (EV, UNLOOP_ONE)
350     const_iv (EV, UNLOOP_ALL)
351 root 1.15
352 root 1.73 const_iv (EV, BACKEND_SELECT)
353     const_iv (EV, BACKEND_POLL)
354     const_iv (EV, BACKEND_EPOLL)
355     const_iv (EV, BACKEND_KQUEUE)
356     const_iv (EV, BACKEND_DEVPOLL)
357     const_iv (EV, BACKEND_PORT)
358 root 1.66 const_iv (EV, FLAG_AUTO)
359     const_iv (EV, FLAG_NOENV)
360 root 1.83 const_iv (EV, FLAG_FORKCHECK)
361 root 1.1 };
362    
363     for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
364     newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
365    
366 root 1.93 stash_loop = gv_stashpv ("EV::Loop" , 1);
367 root 1.15 stash_watcher = gv_stashpv ("EV::Watcher" , 1);
368 root 1.78 stash_io = gv_stashpv ("EV::IO" , 1);
369 root 1.15 stash_timer = gv_stashpv ("EV::Timer" , 1);
370     stash_periodic = gv_stashpv ("EV::Periodic", 1);
371     stash_signal = gv_stashpv ("EV::Signal" , 1);
372     stash_idle = gv_stashpv ("EV::Idle" , 1);
373 root 1.22 stash_prepare = gv_stashpv ("EV::Prepare" , 1);
374 root 1.15 stash_check = gv_stashpv ("EV::Check" , 1);
375 root 1.23 stash_child = gv_stashpv ("EV::Child" , 1);
376 root 1.80 stash_embed = gv_stashpv ("EV::Embed" , 1);
377     stash_stat = gv_stashpv ("EV::Stat" , 1);
378 root 1.91 stash_fork = gv_stashpv ("EV::Fork" , 1);
379 root 1.11
380     {
381     SV *sv = perl_get_sv ("EV::API", TRUE);
382     perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
383    
384 root 1.15 /* the poor man's shared library emulator */
385 root 1.91 evapi.ver = EV_API_VERSION;
386     evapi.rev = EV_API_REVISION;
387     evapi.sv_fileno = sv_fileno;
388     evapi.sv_signum = sv_signum;
389     evapi.supported_backends = ev_supported_backends ();
390     evapi.recommended_backends = ev_recommended_backends ();
391     evapi.embeddable_backends = ev_embeddable_backends ();
392     evapi.time = ev_time;
393     evapi.loop_new = ev_loop_new;
394     evapi.loop_destroy = ev_loop_destroy;
395     evapi.loop_fork = ev_loop_fork;
396     evapi.loop_count = ev_loop_count;
397     evapi.now = ev_now;
398     evapi.backend = ev_backend;
399     evapi.unloop = ev_unloop;
400     evapi.ref = ev_ref;
401     evapi.unref = ev_unref;
402     evapi.loop = ev_loop;
403     evapi.once = ev_once;
404     evapi.io_start = ev_io_start;
405     evapi.io_stop = ev_io_stop;
406     evapi.timer_start = ev_timer_start;
407     evapi.timer_stop = ev_timer_stop;
408     evapi.timer_again = ev_timer_again;
409     evapi.periodic_start = ev_periodic_start;
410     evapi.periodic_stop = ev_periodic_stop;
411     evapi.signal_start = ev_signal_start;
412     evapi.signal_stop = ev_signal_stop;
413     evapi.idle_start = ev_idle_start;
414     evapi.idle_stop = ev_idle_stop;
415     evapi.prepare_start = ev_prepare_start;
416     evapi.prepare_stop = ev_prepare_stop;
417     evapi.check_start = ev_check_start;
418     evapi.check_stop = ev_check_stop;
419     evapi.child_start = ev_child_start;
420     evapi.child_stop = ev_child_stop;
421     evapi.stat_start = ev_stat_start;
422     evapi.stat_stop = ev_stat_stop;
423     evapi.stat_stat = ev_stat_stat;
424     evapi.embed_start = ev_embed_start;
425     evapi.embed_stop = ev_embed_stop;
426     evapi.embed_sweep = ev_embed_sweep;
427     evapi.fork_start = ev_fork_start;
428     evapi.fork_stop = ev_fork_stop;
429     evapi.clear_pending = ev_clear_pending;
430     evapi.invoke = ev_invoke;
431 root 1.11
432     sv_setiv (sv, (IV)&evapi);
433     SvREADONLY_on (sv);
434     }
435 root 1.64 #ifndef _WIN32
436 root 1.56 pthread_atfork (0, 0, ev_default_fork);
437     #endif
438 root 1.1 }
439    
440 root 1.97 SV *ev_default_loop (unsigned int flags = 0)
441 root 1.91 CODE:
442     {
443 root 1.93 if (!default_loop_sv)
444     {
445     evapi.default_loop = ev_default_loop (flags);
446    
447     if (!evapi.default_loop)
448     XSRETURN_UNDEF;
449    
450     default_loop_sv = sv_bless (newRV_noinc (newSViv (PTR2IV (evapi.default_loop))), stash_loop);
451     }
452 root 1.1
453 root 1.93 RETVAL = newSVsv (default_loop_sv);
454 root 1.91 }
455     OUTPUT:
456     RETVAL
457 root 1.1
458 root 1.15 NV ev_time ()
459 root 1.1
460 root 1.91 NV ev_now ()
461     C_ARGS: evapi.default_loop
462    
463     unsigned int ev_backend ()
464     C_ARGS: evapi.default_loop
465 root 1.1
466 root 1.86 unsigned int ev_loop_count ()
467 root 1.91 C_ARGS: evapi.default_loop
468 root 1.86
469 root 1.15 void ev_loop (int flags = 0)
470 root 1.91 C_ARGS: evapi.default_loop, flags
471 root 1.1
472 root 1.47 void ev_unloop (int how = 1)
473 root 1.91 C_ARGS: evapi.default_loop, how
474 root 1.1
475 root 1.89 void ev_feed_fd_event (int fd, int revents = EV_NONE)
476 root 1.91 C_ARGS: evapi.default_loop, fd, revents
477 root 1.89
478     void ev_feed_signal_event (SV *signal)
479     CODE:
480     {
481     Signal signum = sv_signum (signal);
482     CHECK_SIG (signal, signum);
483    
484 root 1.91 ev_feed_signal_event (evapi.default_loop, signum);
485 root 1.89 }
486    
487 root 1.78 ev_io *io (SV *fh, int events, SV *cb)
488 root 1.15 ALIAS:
489     io_ns = 1
490 root 1.1 CODE:
491 root 1.31 {
492     int fd = sv_fileno (fh);
493     CHECK_FD (fh, fd);
494    
495 root 1.93 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
496 root 1.15 RETVAL->fh = newSVsv (fh);
497 root 1.31 ev_io_set (RETVAL, fd, events);
498 root 1.78 if (!ix) START (io, RETVAL);
499 root 1.31 }
500 root 1.1 OUTPUT:
501     RETVAL
502    
503 root 1.78 ev_timer *timer (NV after, NV repeat, SV *cb)
504 root 1.1 ALIAS:
505 root 1.15 timer_ns = 1
506 root 1.18 INIT:
507     CHECK_REPEAT (repeat);
508 root 1.1 CODE:
509 root 1.93 RETVAL = e_new (sizeof (ev_timer), cb, default_loop_sv);
510 root 1.29 ev_timer_set (RETVAL, after, repeat);
511 root 1.78 if (!ix) START (timer, RETVAL);
512 root 1.1 OUTPUT:
513     RETVAL
514    
515 root 1.59 SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
516 root 1.9 ALIAS:
517 root 1.15 periodic_ns = 1
518 root 1.18 INIT:
519     CHECK_REPEAT (interval);
520 root 1.9 CODE:
521 root 1.59 {
522 root 1.78 ev_periodic *w;
523 root 1.93 w = e_new (sizeof (ev_periodic), cb, default_loop_sv);
524 root 1.60 w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
525 root 1.59 ev_periodic_set (w, at, interval, w->fh ? e_periodic_cb : 0);
526 root 1.78 RETVAL = e_bless ((ev_watcher *)w, stash_periodic);
527     if (!ix) START (periodic, w);
528 root 1.59 }
529 root 1.9 OUTPUT:
530     RETVAL
531    
532 root 1.80 ev_signal *signal (SV *signal, SV *cb)
533 root 1.1 ALIAS:
534 root 1.15 signal_ns = 1
535 root 1.1 CODE:
536 root 1.80 {
537     Signal signum = sv_signum (signal);
538     CHECK_SIG (signal, signum);
539    
540 root 1.93 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
541 root 1.29 ev_signal_set (RETVAL, signum);
542 root 1.78 if (!ix) START (signal, RETVAL);
543 root 1.80 }
544 root 1.1 OUTPUT:
545     RETVAL
546    
547 root 1.78 ev_idle *idle (SV *cb)
548 root 1.1 ALIAS:
549 root 1.15 idle_ns = 1
550 root 1.1 CODE:
551 root 1.93 RETVAL = e_new (sizeof (ev_idle), cb, default_loop_sv);
552 root 1.29 ev_idle_set (RETVAL);
553 root 1.78 if (!ix) START (idle, RETVAL);
554 root 1.1 OUTPUT:
555     RETVAL
556    
557 root 1.78 ev_prepare *prepare (SV *cb)
558 root 1.22 ALIAS:
559     prepare_ns = 1
560     CODE:
561 root 1.93 RETVAL = e_new (sizeof (ev_prepare), cb, default_loop_sv);
562 root 1.29 ev_prepare_set (RETVAL);
563 root 1.78 if (!ix) START (prepare, RETVAL);
564 root 1.22 OUTPUT:
565     RETVAL
566    
567 root 1.78 ev_check *check (SV *cb)
568 root 1.1 ALIAS:
569 root 1.15 check_ns = 1
570 root 1.1 CODE:
571 root 1.93 RETVAL = e_new (sizeof (ev_check), cb, default_loop_sv);
572 root 1.29 ev_check_set (RETVAL);
573 root 1.78 if (!ix) START (check, RETVAL);
574 root 1.1 OUTPUT:
575     RETVAL
576    
577 root 1.91 ev_fork *fork (SV *cb)
578     ALIAS:
579     fork_ns = 1
580     CODE:
581 root 1.93 RETVAL = e_new (sizeof (ev_fork), cb, default_loop_sv);
582 root 1.91 ev_fork_set (RETVAL);
583     if (!ix) START (fork, RETVAL);
584     OUTPUT:
585     RETVAL
586    
587 root 1.78 ev_child *child (int pid, SV *cb)
588 root 1.23 ALIAS:
589 root 1.69 child_ns = 1
590 root 1.23 CODE:
591 root 1.93 RETVAL = e_new (sizeof (ev_child), cb, default_loop_sv);
592 root 1.29 ev_child_set (RETVAL, pid);
593 root 1.78 if (!ix) START (child, RETVAL);
594 root 1.23 OUTPUT:
595     RETVAL
596    
597 root 1.80 ev_stat *stat (SV *path, NV interval, SV *cb)
598     ALIAS:
599     stat_ns = 1
600     CODE:
601 root 1.93 RETVAL = e_new (sizeof (ev_stat), cb, default_loop_sv);
602 root 1.80 RETVAL->fh = newSVsv (path);
603     ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval);
604     if (!ix) START (stat, RETVAL);
605     OUTPUT:
606     RETVAL
607    
608 root 1.95 ev_embed *embed (struct ev_loop *loop, SV *cb = &PL_sv_undef)
609 root 1.93 ALIAS:
610     embed_ns = 1
611     CODE:
612 root 1.95 {
613     if (!(ev_backend (loop) & ev_embeddable_backends ()))
614     croak ("passed loop is not embeddable via EV::embed,");
615    
616 root 1.93 RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv);
617     RETVAL->fh = newSVsv (ST (0));
618     ev_embed_set (RETVAL, loop);
619 root 1.95
620     if (!SvOK (cb)) ev_set_cb (RETVAL, 0);
621    
622 root 1.93 if (!ix) START (embed, RETVAL);
623 root 1.95 }
624 root 1.93 OUTPUT:
625     RETVAL
626    
627 root 1.76 void once (SV *fh, int events, SV *timeout, SV *cb)
628 root 1.75 CODE:
629 root 1.76 ev_once (
630 root 1.91 evapi.default_loop,
631 root 1.76 sv_fileno (fh), events,
632     SvOK (timeout) ? SvNV (timeout) : -1.,
633     e_once_cb,
634     newSVsv (cb)
635     );
636 root 1.15
637 root 1.1 PROTOTYPES: DISABLE
638    
639 root 1.15 MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_
640 root 1.1
641 root 1.78 int ev_is_active (ev_watcher *w)
642 root 1.1
643 root 1.78 int ev_is_pending (ev_watcher *w)
644    
645 root 1.89 void ev_invoke (ev_watcher *w, int revents = EV_NONE)
646 root 1.93 C_ARGS: e_loop (w), w, revents
647 root 1.89
648     int ev_clear_pending (ev_watcher *w)
649 root 1.93 C_ARGS: e_loop (w), w
650 root 1.89
651     void ev_feed_event (ev_watcher *w, int revents = EV_NONE)
652 root 1.93 C_ARGS: e_loop (w), w, revents
653 root 1.89
654 root 1.78 int keepalive (ev_watcher *w, int new_value = 0)
655     CODE:
656     {
657     RETVAL = w->flags & WFLAG_KEEPALIVE;
658     new_value = new_value ? WFLAG_KEEPALIVE : 0;
659    
660     if (items > 1 && ((new_value ^ w->flags) & WFLAG_KEEPALIVE))
661     {
662     REF (w);
663     w->flags = (w->flags & ~WFLAG_KEEPALIVE) | new_value;
664     UNREF (w);
665     }
666     }
667     OUTPUT:
668     RETVAL
669    
670     SV *cb (ev_watcher *w, SV *new_cb = 0)
671 root 1.1 CODE:
672 root 1.15 {
673     RETVAL = newSVsv (w->cb_sv);
674    
675     if (items > 1)
676     sv_setsv (w->cb_sv, new_cb);
677     }
678 root 1.1 OUTPUT:
679     RETVAL
680    
681 root 1.78 SV *data (ev_watcher *w, SV *new_data = 0)
682 root 1.60 CODE:
683     {
684     RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef;
685 root 1.79
686     if (items > 1)
687     {
688     SvREFCNT_dec (w->data);
689     w->data = newSVsv (new_data);
690     }
691 root 1.60 }
692     OUTPUT:
693     RETVAL
694    
695 root 1.78 int priority (ev_watcher *w, int new_priority = 0)
696 root 1.41 CODE:
697     {
698     RETVAL = w->priority;
699    
700     if (items > 1)
701     {
702     int active = ev_is_active (w);
703    
704     if (active)
705     {
706     /* grrr. */
707     PUSHMARK (SP);
708     XPUSHs (ST (0));
709 root 1.87 PUTBACK;
710 root 1.41 call_method ("stop", G_DISCARD | G_VOID);
711     }
712    
713     ev_set_priority (w, new_priority);
714    
715     if (active)
716     {
717     PUSHMARK (SP);
718     XPUSHs (ST (0));
719 root 1.87 PUTBACK;
720 root 1.41 call_method ("start", G_DISCARD | G_VOID);
721     }
722     }
723     }
724     OUTPUT:
725     RETVAL
726    
727 root 1.78 MODULE = EV PACKAGE = EV::IO PREFIX = ev_io_
728 root 1.1
729 root 1.78 void ev_io_start (ev_io *w)
730     CODE:
731     START (io, w);
732 root 1.1
733 root 1.78 void ev_io_stop (ev_io *w)
734     CODE:
735     STOP (io, w);
736 root 1.15
737 root 1.78 void DESTROY (ev_io *w)
738 root 1.26 CODE:
739 root 1.78 STOP (io, w);
740 root 1.34 e_destroy (w);
741 root 1.26
742 root 1.78 void set (ev_io *w, SV *fh, int events)
743 root 1.1 CODE:
744     {
745 root 1.31 int fd = sv_fileno (fh);
746     CHECK_FD (fh, fd);
747    
748 root 1.15 sv_setsv (w->fh, fh);
749 root 1.80 RESET (io, w, (w, fd, events));
750 root 1.15 }
751 root 1.1
752 root 1.78 SV *fh (ev_io *w, SV *new_fh = 0)
753 root 1.1 CODE:
754 root 1.15 {
755     if (items > 1)
756     {
757 root 1.80 int fd = sv_fileno (new_fh);
758     CHECK_FD (new_fh, fd);
759 root 1.1
760 root 1.80 RETVAL = w->fh;
761     w->fh = newSVsv (new_fh);
762 root 1.1
763 root 1.80 RESET (io, w, (w, fd, w->events));
764 root 1.15 }
765 root 1.80 else
766     RETVAL = newSVsv (w->fh);
767 root 1.15 }
768 root 1.1 OUTPUT:
769     RETVAL
770    
771 root 1.78 int events (ev_io *w, int new_events = EV_UNDEF)
772 root 1.1 CODE:
773 root 1.15 {
774     RETVAL = w->events;
775    
776     if (items > 1)
777 root 1.80 RESET (io, w, (w, w->fd, new_events));
778 root 1.15 }
779 root 1.1 OUTPUT:
780     RETVAL
781    
782 root 1.29 MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
783 root 1.15
784 root 1.78 void ev_signal_start (ev_signal *w)
785     CODE:
786     START (signal, w);
787 root 1.15
788 root 1.78 void ev_signal_stop (ev_signal *w)
789     CODE:
790     STOP (signal, w);
791 root 1.15
792 root 1.78 void DESTROY (ev_signal *w)
793 root 1.26 CODE:
794 root 1.78 STOP (signal, w);
795 root 1.34 e_destroy (w);
796 root 1.26
797 root 1.78 void set (ev_signal *w, SV *signal)
798 root 1.1 CODE:
799 root 1.15 {
800 root 1.80 Signal signum = sv_signum (signal);
801     CHECK_SIG (signal, signum);
802 root 1.39
803 root 1.80 RESET (signal, w, (w, signum));
804 root 1.15 }
805    
806 root 1.78 int signal (ev_signal *w, SV *new_signal = 0)
807 root 1.39 CODE:
808     {
809     RETVAL = w->signum;
810    
811     if (items > 1)
812     {
813 root 1.80 Signal signum = sv_signum (new_signal);
814     CHECK_SIG (new_signal, signum);
815 root 1.39
816 root 1.80 RESET (signal, w, (w, signum));
817 root 1.39 }
818     }
819     OUTPUT:
820     RETVAL
821    
822 root 1.29 MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
823 root 1.15
824 root 1.78 void ev_timer_start (ev_timer *w)
825 root 1.18 INIT:
826     CHECK_REPEAT (w->repeat);
827 root 1.78 CODE:
828     START (timer, w);
829 root 1.15
830 root 1.78 void ev_timer_stop (ev_timer *w)
831     CODE:
832     STOP (timer, w);
833 root 1.1
834 root 1.78 void ev_timer_again (ev_timer *w)
835 root 1.18 INIT:
836     CHECK_REPEAT (w->repeat);
837 root 1.78 CODE:
838     REF (w);
839 root 1.93 ev_timer_again (e_loop (w), w);
840 root 1.78 UNREF (w);
841 root 1.17
842 root 1.78 void DESTROY (ev_timer *w)
843 root 1.26 CODE:
844 root 1.78 STOP (timer, w);
845 root 1.34 e_destroy (w);
846 root 1.26
847 root 1.78 void set (ev_timer *w, NV after, NV repeat = 0.)
848 root 1.18 INIT:
849     CHECK_REPEAT (repeat);
850 root 1.1 CODE:
851 root 1.80 RESET (timer, w, (w, after, repeat));
852 root 1.1
853 root 1.90 NV at (ev_timer *w)
854     CODE:
855     RETVAL = w->at;
856     OUTPUT:
857     RETVAL
858    
859 root 1.29 MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_
860 root 1.15
861 root 1.78 void ev_periodic_start (ev_periodic *w)
862 root 1.18 INIT:
863     CHECK_REPEAT (w->interval);
864 root 1.78 CODE:
865     START (periodic, w);
866 root 1.1
867 root 1.78 void ev_periodic_stop (ev_periodic *w)
868     CODE:
869     STOP (periodic, w);
870 root 1.1
871 root 1.78 void ev_periodic_again (ev_periodic *w)
872     CODE:
873     REF (w);
874 root 1.93 ev_periodic_again (e_loop (w), w);
875 root 1.78 UNREF (w);
876 root 1.59
877 root 1.78 void DESTROY (ev_periodic *w)
878 root 1.26 CODE:
879 root 1.78 STOP (periodic, w);
880 root 1.34 e_destroy (w);
881 root 1.26
882 root 1.78 void set (ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef)
883 root 1.18 INIT:
884     CHECK_REPEAT (interval);
885 root 1.10 CODE:
886     {
887 root 1.59 SvREFCNT_dec (w->fh);
888     w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
889 root 1.39
890 root 1.80 RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0));
891 root 1.15 }
892 root 1.10
893 root 1.90 NV at (ev_periodic *w)
894     CODE:
895     RETVAL = w->at;
896     OUTPUT:
897     RETVAL
898    
899 root 1.29 MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
900 root 1.10
901 root 1.78 void ev_idle_start (ev_idle *w)
902     CODE:
903     START (idle, w);
904 root 1.10
905 root 1.78 void ev_idle_stop (ev_idle *w)
906     CODE:
907     STOP (idle, w);
908 root 1.10
909 root 1.78 void DESTROY (ev_idle *w)
910 root 1.26 CODE:
911 root 1.78 STOP (idle, w);
912 root 1.34 e_destroy (w);
913 root 1.26
914 root 1.29 MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
915 root 1.22
916 root 1.78 void ev_prepare_start (ev_prepare *w)
917     CODE:
918     START (prepare, w);
919 root 1.22
920 root 1.78 void ev_prepare_stop (ev_prepare *w)
921     CODE:
922     STOP (prepare, w);
923 root 1.22
924 root 1.78 void DESTROY (ev_prepare *w)
925 root 1.26 CODE:
926 root 1.78 STOP (prepare, w);
927 root 1.34 e_destroy (w);
928 root 1.26
929 root 1.29 MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
930 root 1.1
931 root 1.78 void ev_check_start (ev_check *w)
932     CODE:
933     START (check, w);
934 root 1.1
935 root 1.78 void ev_check_stop (ev_check *w)
936     CODE:
937     STOP (check, w);
938 root 1.1
939 root 1.78 void DESTROY (ev_check *w)
940 root 1.26 CODE:
941 root 1.78 STOP (check, w);
942 root 1.34 e_destroy (w);
943 root 1.26
944 root 1.91 MODULE = EV PACKAGE = EV::Fork PREFIX = ev_fork_
945    
946     void ev_fork_start (ev_fork *w)
947     CODE:
948     START (fork, w);
949    
950     void ev_fork_stop (ev_fork *w)
951     CODE:
952     STOP (fork, w);
953    
954     void DESTROY (ev_fork *w)
955     CODE:
956     STOP (fork, w);
957     e_destroy (w);
958    
959 root 1.29 MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
960 root 1.23
961 root 1.78 void ev_child_start (ev_child *w)
962     CODE:
963     START (child, w);
964 root 1.23
965 root 1.78 void ev_child_stop (ev_child *w)
966     CODE:
967     STOP (child, w);
968 root 1.23
969 root 1.78 void DESTROY (ev_child *w)
970 root 1.26 CODE:
971 root 1.78 STOP (child, w);
972 root 1.34 e_destroy (w);
973 root 1.26
974 root 1.78 void set (ev_child *w, int pid)
975 root 1.23 CODE:
976 root 1.80 RESET (child, w, (w, pid));
977 root 1.23
978 root 1.78 int pid (ev_child *w, int new_pid = 0)
979 root 1.39 CODE:
980     {
981 root 1.40 RETVAL = w->pid;
982 root 1.39
983     if (items > 1)
984 root 1.80 RESET (child, w, (w, new_pid));
985 root 1.39 }
986     OUTPUT:
987     RETVAL
988    
989    
990 root 1.78 int rstatus (ev_child *w)
991 root 1.45 ALIAS:
992     rpid = 1
993 root 1.24 CODE:
994 root 1.45 RETVAL = ix ? w->rpid : w->rstatus;
995 root 1.24 OUTPUT:
996     RETVAL
997    
998 root 1.80 MODULE = EV PACKAGE = EV::Stat PREFIX = ev_stat_
999    
1000     void ev_stat_start (ev_stat *w)
1001     CODE:
1002     START (stat, w);
1003    
1004     void ev_stat_stop (ev_stat *w)
1005     CODE:
1006     STOP (stat, w);
1007    
1008     void DESTROY (ev_stat *w)
1009     CODE:
1010     STOP (stat, w);
1011     e_destroy (w);
1012    
1013     void set (ev_stat *w, SV *path, NV interval)
1014     CODE:
1015     {
1016     sv_setsv (w->fh, path);
1017     RESET (stat, w, (w, SvPVbyte_nolen (w->fh), interval));
1018     }
1019    
1020     SV *path (ev_stat *w, SV *new_path = 0)
1021     CODE:
1022     {
1023     RETVAL = SvREFCNT_inc (w->fh);
1024    
1025     if (items > 1)
1026     {
1027     SvREFCNT_dec (w->fh);
1028     w->fh = newSVsv (new_path);
1029     RESET (stat, w, (w, SvPVbyte_nolen (w->fh), w->interval));
1030     }
1031     }
1032     OUTPUT:
1033     RETVAL
1034    
1035     NV interval (ev_stat *w, NV new_interval = 0.)
1036     CODE:
1037     {
1038     RETVAL = w->interval;
1039    
1040     if (items > 1)
1041     RESET (stat, w, (w, SvPVbyte_nolen (w->fh), new_interval));
1042     }
1043     OUTPUT:
1044     RETVAL
1045    
1046 root 1.82 void prev (ev_stat *w)
1047     ALIAS:
1048     stat = 1
1049     attr = 2
1050     PPCODE:
1051     {
1052     ev_statdata *s = ix ? &w->attr : &w->prev;
1053    
1054     if (ix == 1)
1055 root 1.93 ev_stat_stat (e_loop (w), w);
1056 root 1.82 else if (!s->st_nlink)
1057     errno = ENOENT;
1058    
1059     PL_statcache.st_dev = s->st_nlink;
1060     PL_statcache.st_ino = s->st_ino;
1061     PL_statcache.st_mode = s->st_mode;
1062     PL_statcache.st_nlink = s->st_nlink;
1063     PL_statcache.st_uid = s->st_uid;
1064     PL_statcache.st_gid = s->st_gid;
1065     PL_statcache.st_rdev = s->st_rdev;
1066     PL_statcache.st_size = s->st_size;
1067     PL_statcache.st_atime = s->st_atime;
1068     PL_statcache.st_mtime = s->st_mtime;
1069     PL_statcache.st_ctime = s->st_ctime;
1070    
1071     if (GIMME_V == G_SCALAR)
1072     XPUSHs (boolSV (s->st_nlink));
1073     else if (GIMME_V == G_ARRAY && s->st_nlink)
1074     {
1075     EXTEND (SP, 13);
1076     PUSHs (sv_2mortal (newSViv (s->st_dev)));
1077     PUSHs (sv_2mortal (newSViv (s->st_ino)));
1078     PUSHs (sv_2mortal (newSVuv (s->st_mode)));
1079     PUSHs (sv_2mortal (newSVuv (s->st_nlink)));
1080     PUSHs (sv_2mortal (newSViv (s->st_uid)));
1081     PUSHs (sv_2mortal (newSViv (s->st_gid)));
1082     PUSHs (sv_2mortal (newSViv (s->st_rdev)));
1083     PUSHs (sv_2mortal (newSVnv ((NV)s->st_size)));
1084     PUSHs (sv_2mortal (newSVnv (s->st_atime)));
1085     PUSHs (sv_2mortal (newSVnv (s->st_mtime)));
1086     PUSHs (sv_2mortal (newSVnv (s->st_ctime)));
1087     PUSHs (sv_2mortal (newSVuv (4096)));
1088     PUSHs (sv_2mortal (newSVnv ((NV)((s->st_size + 4095) / 4096))));
1089     }
1090     }
1091    
1092 root 1.93 MODULE = EV PACKAGE = EV::Embed PREFIX = ev_embed_
1093 root 1.5
1094 root 1.93 void ev_embed_start (ev_embed *w)
1095     CODE:
1096     START (embed, w);
1097 root 1.5
1098 root 1.93 void ev_embed_stop (ev_embed *w)
1099     CODE:
1100     STOP (embed, w);
1101 root 1.5
1102 root 1.93 void DESTROY (ev_embed *w)
1103     CODE:
1104     STOP (embed, w);
1105     e_destroy (w);
1106 root 1.5
1107 root 1.93 void set (ev_embed *w, struct ev_loop *loop)
1108     CODE:
1109     {
1110     sv_setsv (w->fh, ST (1));
1111     RESET (embed, w, (w, loop));
1112 root 1.5 }
1113    
1114 root 1.96 SV *other (ev_embed *w)
1115     CODE:
1116     RETVAL = newSVsv (w->fh);
1117     OUTPUT:
1118     RETVAL
1119    
1120 root 1.93 MODULE = EV PACKAGE = EV::Loop PREFIX = ev_loop_
1121 root 1.5
1122 root 1.97 SV *new (SV *klass, unsigned int flags = 0)
1123 root 1.93 CODE:
1124     {
1125     struct ev_loop *loop = ev_loop_new (flags);
1126 root 1.5
1127 root 1.93 if (!loop)
1128     XSRETURN_UNDEF;
1129 root 1.5
1130 root 1.95 RETVAL = sv_bless (newRV_noinc (newSViv (PTR2IV (loop))), stash_loop);
1131 root 1.93 }
1132     OUTPUT:
1133     RETVAL
1134 root 1.5
1135 root 1.93 void DESTROY (struct ev_loop *loop)
1136     CODE:
1137     if (loop != evapi.default_loop) /* global destruction sucks */
1138     ev_loop_destroy (loop);
1139 root 1.5
1140 root 1.94 ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb)
1141     ALIAS:
1142     io_ns = 1
1143     CODE:
1144     {
1145     int fd = sv_fileno (fh);
1146     CHECK_FD (fh, fd);
1147    
1148     RETVAL = e_new (sizeof (ev_io), cb, ST (0));
1149     RETVAL->fh = newSVsv (fh);
1150     ev_io_set (RETVAL, fd, events);
1151     if (!ix) START (io, RETVAL);
1152     }
1153     OUTPUT:
1154     RETVAL
1155