ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/EV/EV.xs
Revision: 1.93
Committed: Thu Dec 20 09:26:40 2007 UTC (16 years, 4 months ago) by root
Branch: MAIN
Changes since 1.92: +82 -66 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.91 SV *ev_default_loop (unsigned int flags = ev_supported_backends ())
441     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.93 ev_embed *embed (struct ev_loop *loop, SV *cb)
609     ALIAS:
610     embed_ns = 1
611     CODE:
612     RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv);
613     RETVAL->fh = newSVsv (ST (0));
614     ev_embed_set (RETVAL, loop);
615     if (!ix) START (embed, RETVAL);
616     OUTPUT:
617     RETVAL
618    
619 root 1.76 void once (SV *fh, int events, SV *timeout, SV *cb)
620 root 1.75 CODE:
621 root 1.76 ev_once (
622 root 1.91 evapi.default_loop,
623 root 1.76 sv_fileno (fh), events,
624     SvOK (timeout) ? SvNV (timeout) : -1.,
625     e_once_cb,
626     newSVsv (cb)
627     );
628 root 1.15
629 root 1.1 PROTOTYPES: DISABLE
630    
631 root 1.15 MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_
632 root 1.1
633 root 1.78 int ev_is_active (ev_watcher *w)
634 root 1.1
635 root 1.78 int ev_is_pending (ev_watcher *w)
636    
637 root 1.89 void ev_invoke (ev_watcher *w, int revents = EV_NONE)
638 root 1.93 C_ARGS: e_loop (w), w, revents
639 root 1.89
640     int ev_clear_pending (ev_watcher *w)
641 root 1.93 C_ARGS: e_loop (w), w
642 root 1.89
643     void ev_feed_event (ev_watcher *w, int revents = EV_NONE)
644 root 1.93 C_ARGS: e_loop (w), w, revents
645 root 1.89
646 root 1.78 int keepalive (ev_watcher *w, int new_value = 0)
647     CODE:
648     {
649     RETVAL = w->flags & WFLAG_KEEPALIVE;
650     new_value = new_value ? WFLAG_KEEPALIVE : 0;
651    
652     if (items > 1 && ((new_value ^ w->flags) & WFLAG_KEEPALIVE))
653     {
654     REF (w);
655     w->flags = (w->flags & ~WFLAG_KEEPALIVE) | new_value;
656     UNREF (w);
657     }
658     }
659     OUTPUT:
660     RETVAL
661    
662     SV *cb (ev_watcher *w, SV *new_cb = 0)
663 root 1.1 CODE:
664 root 1.15 {
665     RETVAL = newSVsv (w->cb_sv);
666    
667     if (items > 1)
668     sv_setsv (w->cb_sv, new_cb);
669     }
670 root 1.1 OUTPUT:
671     RETVAL
672    
673 root 1.78 SV *data (ev_watcher *w, SV *new_data = 0)
674 root 1.60 CODE:
675     {
676     RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef;
677 root 1.79
678     if (items > 1)
679     {
680     SvREFCNT_dec (w->data);
681     w->data = newSVsv (new_data);
682     }
683 root 1.60 }
684     OUTPUT:
685     RETVAL
686    
687 root 1.78 int priority (ev_watcher *w, int new_priority = 0)
688 root 1.41 CODE:
689     {
690     RETVAL = w->priority;
691    
692     if (items > 1)
693     {
694     int active = ev_is_active (w);
695    
696     if (active)
697     {
698     /* grrr. */
699     PUSHMARK (SP);
700     XPUSHs (ST (0));
701 root 1.87 PUTBACK;
702 root 1.41 call_method ("stop", G_DISCARD | G_VOID);
703     }
704    
705     ev_set_priority (w, new_priority);
706    
707     if (active)
708     {
709     PUSHMARK (SP);
710     XPUSHs (ST (0));
711 root 1.87 PUTBACK;
712 root 1.41 call_method ("start", G_DISCARD | G_VOID);
713     }
714     }
715     }
716     OUTPUT:
717     RETVAL
718    
719 root 1.78 MODULE = EV PACKAGE = EV::IO PREFIX = ev_io_
720 root 1.1
721 root 1.78 void ev_io_start (ev_io *w)
722     CODE:
723     START (io, w);
724 root 1.1
725 root 1.78 void ev_io_stop (ev_io *w)
726     CODE:
727     STOP (io, w);
728 root 1.15
729 root 1.78 void DESTROY (ev_io *w)
730 root 1.26 CODE:
731 root 1.78 STOP (io, w);
732 root 1.34 e_destroy (w);
733 root 1.26
734 root 1.78 void set (ev_io *w, SV *fh, int events)
735 root 1.1 CODE:
736     {
737 root 1.31 int fd = sv_fileno (fh);
738     CHECK_FD (fh, fd);
739    
740 root 1.15 sv_setsv (w->fh, fh);
741 root 1.80 RESET (io, w, (w, fd, events));
742 root 1.15 }
743 root 1.1
744 root 1.78 SV *fh (ev_io *w, SV *new_fh = 0)
745 root 1.1 CODE:
746 root 1.15 {
747     if (items > 1)
748     {
749 root 1.80 int fd = sv_fileno (new_fh);
750     CHECK_FD (new_fh, fd);
751 root 1.1
752 root 1.80 RETVAL = w->fh;
753     w->fh = newSVsv (new_fh);
754 root 1.1
755 root 1.80 RESET (io, w, (w, fd, w->events));
756 root 1.15 }
757 root 1.80 else
758     RETVAL = newSVsv (w->fh);
759 root 1.15 }
760 root 1.1 OUTPUT:
761     RETVAL
762    
763 root 1.78 int events (ev_io *w, int new_events = EV_UNDEF)
764 root 1.1 CODE:
765 root 1.15 {
766     RETVAL = w->events;
767    
768     if (items > 1)
769 root 1.80 RESET (io, w, (w, w->fd, new_events));
770 root 1.15 }
771 root 1.1 OUTPUT:
772     RETVAL
773    
774 root 1.29 MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
775 root 1.15
776 root 1.78 void ev_signal_start (ev_signal *w)
777     CODE:
778     START (signal, w);
779 root 1.15
780 root 1.78 void ev_signal_stop (ev_signal *w)
781     CODE:
782     STOP (signal, w);
783 root 1.15
784 root 1.78 void DESTROY (ev_signal *w)
785 root 1.26 CODE:
786 root 1.78 STOP (signal, w);
787 root 1.34 e_destroy (w);
788 root 1.26
789 root 1.78 void set (ev_signal *w, SV *signal)
790 root 1.1 CODE:
791 root 1.15 {
792 root 1.80 Signal signum = sv_signum (signal);
793     CHECK_SIG (signal, signum);
794 root 1.39
795 root 1.80 RESET (signal, w, (w, signum));
796 root 1.15 }
797    
798 root 1.78 int signal (ev_signal *w, SV *new_signal = 0)
799 root 1.39 CODE:
800     {
801     RETVAL = w->signum;
802    
803     if (items > 1)
804     {
805 root 1.80 Signal signum = sv_signum (new_signal);
806     CHECK_SIG (new_signal, signum);
807 root 1.39
808 root 1.80 RESET (signal, w, (w, signum));
809 root 1.39 }
810     }
811     OUTPUT:
812     RETVAL
813    
814 root 1.29 MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
815 root 1.15
816 root 1.78 void ev_timer_start (ev_timer *w)
817 root 1.18 INIT:
818     CHECK_REPEAT (w->repeat);
819 root 1.78 CODE:
820     START (timer, w);
821 root 1.15
822 root 1.78 void ev_timer_stop (ev_timer *w)
823     CODE:
824     STOP (timer, w);
825 root 1.1
826 root 1.78 void ev_timer_again (ev_timer *w)
827 root 1.18 INIT:
828     CHECK_REPEAT (w->repeat);
829 root 1.78 CODE:
830     REF (w);
831 root 1.93 ev_timer_again (e_loop (w), w);
832 root 1.78 UNREF (w);
833 root 1.17
834 root 1.78 void DESTROY (ev_timer *w)
835 root 1.26 CODE:
836 root 1.78 STOP (timer, w);
837 root 1.34 e_destroy (w);
838 root 1.26
839 root 1.78 void set (ev_timer *w, NV after, NV repeat = 0.)
840 root 1.18 INIT:
841     CHECK_REPEAT (repeat);
842 root 1.1 CODE:
843 root 1.80 RESET (timer, w, (w, after, repeat));
844 root 1.1
845 root 1.90 NV at (ev_timer *w)
846     CODE:
847     RETVAL = w->at;
848     OUTPUT:
849     RETVAL
850    
851 root 1.29 MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_
852 root 1.15
853 root 1.78 void ev_periodic_start (ev_periodic *w)
854 root 1.18 INIT:
855     CHECK_REPEAT (w->interval);
856 root 1.78 CODE:
857     START (periodic, w);
858 root 1.1
859 root 1.78 void ev_periodic_stop (ev_periodic *w)
860     CODE:
861     STOP (periodic, w);
862 root 1.1
863 root 1.78 void ev_periodic_again (ev_periodic *w)
864     CODE:
865     REF (w);
866 root 1.93 ev_periodic_again (e_loop (w), w);
867 root 1.78 UNREF (w);
868 root 1.59
869 root 1.78 void DESTROY (ev_periodic *w)
870 root 1.26 CODE:
871 root 1.78 STOP (periodic, w);
872 root 1.34 e_destroy (w);
873 root 1.26
874 root 1.78 void set (ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef)
875 root 1.18 INIT:
876     CHECK_REPEAT (interval);
877 root 1.10 CODE:
878     {
879 root 1.59 SvREFCNT_dec (w->fh);
880     w->fh = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
881 root 1.39
882 root 1.80 RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0));
883 root 1.15 }
884 root 1.10
885 root 1.90 NV at (ev_periodic *w)
886     CODE:
887     RETVAL = w->at;
888     OUTPUT:
889     RETVAL
890    
891 root 1.29 MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
892 root 1.10
893 root 1.78 void ev_idle_start (ev_idle *w)
894     CODE:
895     START (idle, w);
896 root 1.10
897 root 1.78 void ev_idle_stop (ev_idle *w)
898     CODE:
899     STOP (idle, w);
900 root 1.10
901 root 1.78 void DESTROY (ev_idle *w)
902 root 1.26 CODE:
903 root 1.78 STOP (idle, w);
904 root 1.34 e_destroy (w);
905 root 1.26
906 root 1.29 MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_check_
907 root 1.22
908 root 1.78 void ev_prepare_start (ev_prepare *w)
909     CODE:
910     START (prepare, w);
911 root 1.22
912 root 1.78 void ev_prepare_stop (ev_prepare *w)
913     CODE:
914     STOP (prepare, w);
915 root 1.22
916 root 1.78 void DESTROY (ev_prepare *w)
917 root 1.26 CODE:
918 root 1.78 STOP (prepare, w);
919 root 1.34 e_destroy (w);
920 root 1.26
921 root 1.29 MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
922 root 1.1
923 root 1.78 void ev_check_start (ev_check *w)
924     CODE:
925     START (check, w);
926 root 1.1
927 root 1.78 void ev_check_stop (ev_check *w)
928     CODE:
929     STOP (check, w);
930 root 1.1
931 root 1.78 void DESTROY (ev_check *w)
932 root 1.26 CODE:
933 root 1.78 STOP (check, w);
934 root 1.34 e_destroy (w);
935 root 1.26
936 root 1.91 MODULE = EV PACKAGE = EV::Fork PREFIX = ev_fork_
937    
938     void ev_fork_start (ev_fork *w)
939     CODE:
940     START (fork, w);
941    
942     void ev_fork_stop (ev_fork *w)
943     CODE:
944     STOP (fork, w);
945    
946     void DESTROY (ev_fork *w)
947     CODE:
948     STOP (fork, w);
949     e_destroy (w);
950    
951 root 1.29 MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
952 root 1.23
953 root 1.78 void ev_child_start (ev_child *w)
954     CODE:
955     START (child, w);
956 root 1.23
957 root 1.78 void ev_child_stop (ev_child *w)
958     CODE:
959     STOP (child, w);
960 root 1.23
961 root 1.78 void DESTROY (ev_child *w)
962 root 1.26 CODE:
963 root 1.78 STOP (child, w);
964 root 1.34 e_destroy (w);
965 root 1.26
966 root 1.78 void set (ev_child *w, int pid)
967 root 1.23 CODE:
968 root 1.80 RESET (child, w, (w, pid));
969 root 1.23
970 root 1.78 int pid (ev_child *w, int new_pid = 0)
971 root 1.39 CODE:
972     {
973 root 1.40 RETVAL = w->pid;
974 root 1.39
975     if (items > 1)
976 root 1.80 RESET (child, w, (w, new_pid));
977 root 1.39 }
978     OUTPUT:
979     RETVAL
980    
981    
982 root 1.78 int rstatus (ev_child *w)
983 root 1.45 ALIAS:
984     rpid = 1
985 root 1.24 CODE:
986 root 1.45 RETVAL = ix ? w->rpid : w->rstatus;
987 root 1.24 OUTPUT:
988     RETVAL
989    
990 root 1.80 MODULE = EV PACKAGE = EV::Stat PREFIX = ev_stat_
991    
992     void ev_stat_start (ev_stat *w)
993     CODE:
994     START (stat, w);
995    
996     void ev_stat_stop (ev_stat *w)
997     CODE:
998     STOP (stat, w);
999    
1000     void DESTROY (ev_stat *w)
1001     CODE:
1002     STOP (stat, w);
1003     e_destroy (w);
1004    
1005     void set (ev_stat *w, SV *path, NV interval)
1006     CODE:
1007     {
1008     sv_setsv (w->fh, path);
1009     RESET (stat, w, (w, SvPVbyte_nolen (w->fh), interval));
1010     }
1011    
1012     SV *path (ev_stat *w, SV *new_path = 0)
1013     CODE:
1014     {
1015     RETVAL = SvREFCNT_inc (w->fh);
1016    
1017     if (items > 1)
1018     {
1019     SvREFCNT_dec (w->fh);
1020     w->fh = newSVsv (new_path);
1021     RESET (stat, w, (w, SvPVbyte_nolen (w->fh), w->interval));
1022     }
1023     }
1024     OUTPUT:
1025     RETVAL
1026    
1027     NV interval (ev_stat *w, NV new_interval = 0.)
1028     CODE:
1029     {
1030     RETVAL = w->interval;
1031    
1032     if (items > 1)
1033     RESET (stat, w, (w, SvPVbyte_nolen (w->fh), new_interval));
1034     }
1035     OUTPUT:
1036     RETVAL
1037    
1038 root 1.82 void prev (ev_stat *w)
1039     ALIAS:
1040     stat = 1
1041     attr = 2
1042     PPCODE:
1043     {
1044     ev_statdata *s = ix ? &w->attr : &w->prev;
1045    
1046     if (ix == 1)
1047 root 1.93 ev_stat_stat (e_loop (w), w);
1048 root 1.82 else if (!s->st_nlink)
1049     errno = ENOENT;
1050    
1051     PL_statcache.st_dev = s->st_nlink;
1052     PL_statcache.st_ino = s->st_ino;
1053     PL_statcache.st_mode = s->st_mode;
1054     PL_statcache.st_nlink = s->st_nlink;
1055     PL_statcache.st_uid = s->st_uid;
1056     PL_statcache.st_gid = s->st_gid;
1057     PL_statcache.st_rdev = s->st_rdev;
1058     PL_statcache.st_size = s->st_size;
1059     PL_statcache.st_atime = s->st_atime;
1060     PL_statcache.st_mtime = s->st_mtime;
1061     PL_statcache.st_ctime = s->st_ctime;
1062    
1063     if (GIMME_V == G_SCALAR)
1064     XPUSHs (boolSV (s->st_nlink));
1065     else if (GIMME_V == G_ARRAY && s->st_nlink)
1066     {
1067     EXTEND (SP, 13);
1068     PUSHs (sv_2mortal (newSViv (s->st_dev)));
1069     PUSHs (sv_2mortal (newSViv (s->st_ino)));
1070     PUSHs (sv_2mortal (newSVuv (s->st_mode)));
1071     PUSHs (sv_2mortal (newSVuv (s->st_nlink)));
1072     PUSHs (sv_2mortal (newSViv (s->st_uid)));
1073     PUSHs (sv_2mortal (newSViv (s->st_gid)));
1074     PUSHs (sv_2mortal (newSViv (s->st_rdev)));
1075     PUSHs (sv_2mortal (newSVnv ((NV)s->st_size)));
1076     PUSHs (sv_2mortal (newSVnv (s->st_atime)));
1077     PUSHs (sv_2mortal (newSVnv (s->st_mtime)));
1078     PUSHs (sv_2mortal (newSVnv (s->st_ctime)));
1079     PUSHs (sv_2mortal (newSVuv (4096)));
1080     PUSHs (sv_2mortal (newSVnv ((NV)((s->st_size + 4095) / 4096))));
1081     }
1082     }
1083    
1084 root 1.93 MODULE = EV PACKAGE = EV::Embed PREFIX = ev_embed_
1085 root 1.5
1086 root 1.93 void ev_embed_start (ev_embed *w)
1087     CODE:
1088     START (embed, w);
1089 root 1.5
1090 root 1.93 void ev_embed_stop (ev_embed *w)
1091     CODE:
1092     STOP (embed, w);
1093 root 1.5
1094 root 1.93 void DESTROY (ev_embed *w)
1095     CODE:
1096     STOP (embed, w);
1097     e_destroy (w);
1098 root 1.5
1099 root 1.93 void set (ev_embed *w, struct ev_loop *loop)
1100     CODE:
1101     {
1102     sv_setsv (w->fh, ST (1));
1103     RESET (embed, w, (w, loop));
1104 root 1.5 }
1105    
1106 root 1.93 MODULE = EV PACKAGE = EV::Loop PREFIX = ev_loop_
1107 root 1.5
1108 root 1.93 SV *new (SV *klass, unsigned int flags = ev_supported_backends ())
1109     CODE:
1110     {
1111     struct ev_loop *loop = ev_loop_new (flags);
1112 root 1.5
1113 root 1.93 if (!loop)
1114     XSRETURN_UNDEF;
1115 root 1.5
1116 root 1.93 RETVAL = sv_bless (newRV_noinc (newSViv (PTR2IV (evapi.default_loop))), stash_loop);
1117     }
1118     OUTPUT:
1119     RETVAL
1120 root 1.5
1121 root 1.93 void DESTROY (struct ev_loop *loop)
1122     CODE:
1123     if (loop != evapi.default_loop) /* global destruction sucks */
1124     ev_loop_destroy (loop);
1125 root 1.5