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

Comparing EV/EV.xs (file contents):
Revision 1.18 by root, Wed Oct 31 11:55:42 2007 UTC vs.
Revision 1.149 by root, Sun Nov 28 06:34:10 2010 UTC

1#include "EXTERN.h" 1#include "EXTERN.h"
2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <math.h> 5/* fix perl api breakage */
6#include <netinet/in.h> 6#undef signal
7#undef sigaction
7 8
8#define TIMEOUT_NONE HUGE_VAL 9#include "schmorp.h"
9 10
10#define EV_COMMON \ 11/* old API compatibility */
11 SV *self; /* contains this struct */ \ 12static int
12 SV *cb_sv, *fh; 13sv_fileno (SV *fh)
14{
15 return s_fileno (fh, 0);
16}
13 17
18#define EV_STANDALONE 1
19#define EV_PROTOTYPES 1
20#define EV_USE_NANOSLEEP EV_USE_MONOTONIC
21#define EV_H <ev.h>
22#define EV_CONFIG_H error
14#include "EV/EVAPI.h" 23#include "EV/EVAPI.h"
15 24
25#define EV_SELECT_IS_WINSOCKET 0
26#ifdef _WIN32
27# define EV_SELECT_USE_FD_SET 0
28# define NFDBITS PERL_NFDBITS
29# define fd_mask Perl_fd_mask
30#endif
31/* due to bugs in OS X we have to use libev/ explicitly here */
16#include "libev/ev.c" 32#include "libev/ev.c"
17 33
34#if !defined(_WIN32) && !defined(_MINIX)
35# include <pthread.h>
36#endif
37
38#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX (((ev_watcher *)(w))->loop))
39#define e_flags(w) ((ev_watcher *)(w))->e_flags
40#define e_self(w) ((ev_watcher *)(w))->self
41#define e_fh(w) ((ev_watcher *)(w))->fh
42#define e_data(w) ((ev_watcher *)(w))->data
43
44#define WFLAG_KEEPALIVE 1
45#define WFLAG_UNREFED 2 /* has been unref'ed */
46
47#define UNREF(w) \
48 if (!(e_flags (w) & (WFLAG_KEEPALIVE | WFLAG_UNREFED)) \
49 && ev_is_active (w)) \
50 { \
51 ev_unref (e_loop (w)); \
52 e_flags (w) |= WFLAG_UNREFED; \
53 }
54
55#define REF(w) \
56 if (e_flags (w) & WFLAG_UNREFED) \
57 { \
58 e_flags (w) &= ~WFLAG_UNREFED; \
59 ev_ref (e_loop (w)); \
60 }
61
62#define START(type,w) \
63 do { \
64 ev_ ## type ## _start (e_loop (w), w); \
65 UNREF (w); \
66 } while (0)
67
68#define STOP(type,w) \
69 do { \
70 REF (w); \
71 ev_ ## type ## _stop (e_loop (w), w); \
72 } while (0)
73
74#define RESET(type,w,seta) \
75 do { \
76 int active = ev_is_active (w); \
77 if (active) STOP (type, w); \
78 ev_ ## type ## _set seta; \
79 if (active) START (type, w); \
80 } while (0)
81
18typedef int Signal; 82typedef int Signal;
19 83
84/* horrible... */
85#define CHECK_SIGNAL_CAN_START(w) \
86 do { \
87 /* dive into the internals of libev to avoid aborting in libev */ \
88 if (signals [(w)->signum - 1].loop \
89 && signals [(w)->signum - 1].loop != e_loop (w)) \
90 croak ("unable to start signal watcher, signal %d already registered in another loop", w->signum); \
91 } while (0)
92
93#define START_SIGNAL(w) \
94 do { \
95 CHECK_SIGNAL_CAN_START (w); \
96 START (signal, w); \
97 } while (0) \
98
99#define RESET_SIGNAL(w,seta) \
100 do { \
101 int active = ev_is_active (w); \
102 if (active) STOP (signal, w); \
103 ev_ ## signal ## _set seta; \
104 if (active) START_SIGNAL (w); \
105 } while (0)
106
107static SV *default_loop_sv;
108
20static struct EVAPI evapi; 109static struct EVAPI evapi;
21 110
22static HV 111static HV
112 *stash_loop,
23 *stash_watcher, 113 *stash_watcher,
24 *stash_io, 114 *stash_io,
25 *stash_time,
26 *stash_timer, 115 *stash_timer,
27 *stash_periodic, 116 *stash_periodic,
28 *stash_signal, 117 *stash_signal,
118 *stash_child,
119 *stash_stat,
29 *stash_idle, 120 *stash_idle,
121 *stash_prepare,
30 *stash_check; 122 *stash_check,
31 123 *stash_embed,
32static int 124 *stash_fork,
33sv_signum (SV *sig) 125 *stash_cleanup,
34{ 126 *stash_async;
35 int signum;
36
37 if (SvIV (sig) > 0)
38 return SvIV (sig);
39
40 for (signum = 1; signum < SIG_SIZE; ++signum)
41 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
42 return signum;
43
44 return -1;
45}
46
47static void
48api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg)
49{
50#if 0
51 if (timeout >= 0.)
52 {
53 struct timeval tv;
54 tv_set (&tv, timeout);
55 event_once (fd, events, cb, arg, &tv);
56 }
57 else
58 event_once (fd, events, cb, arg, 0);
59#endif
60}
61 127
62///////////////////////////////////////////////////////////////////////////// 128/////////////////////////////////////////////////////////////////////////////
63// Event 129// Event
64 130
65static void e_cb (struct ev_watcher *w, int revents); 131static void e_cb (EV_P_ ev_watcher *w, int revents);
66 132
67static int 133void *
68sv_fileno (SV *fh)
69{
70 SvGETMAGIC (fh);
71
72 if (SvROK (fh))
73 fh = SvRV (fh);
74
75 if (SvTYPE (fh) == SVt_PVGV)
76 return PerlIO_fileno (IoIFP (sv_2io (fh)));
77
78 if (SvIOK (fh))
79 return SvIV (fh);
80
81 return -1;
82}
83
84static void *
85e_new (int size, SV *cb_sv) 134e_new (int size, SV *cb_sv, SV *loop)
86{ 135{
136 SV *cv = cb_sv ? s_get_cv_croak (cb_sv) : 0;
87 struct ev_watcher *w; 137 ev_watcher *w;
88 SV *self = NEWSV (0, size); 138 SV *self = NEWSV (0, size);
89 SvPOK_only (self); 139 SvPOK_only (self);
90 SvCUR_set (self, size); 140 SvCUR_set (self, size);
91 141
92 w = (struct ev_watcher *)SvPVX (self); 142 w = (ev_watcher *)SvPVX (self);
93 143
94 evw_init (w, e_cb); 144 ev_init (w, cv ? e_cb : 0);
95 145
146 w->loop = SvREFCNT_inc (SvRV (loop));
147 w->e_flags = WFLAG_KEEPALIVE;
148 w->data = 0;
96 w->fh = 0; 149 w->fh = 0;
97 w->cb_sv = newSVsv (cb_sv); 150 w->cb_sv = SvREFCNT_inc (cv);
98 w->self = self; 151 w->self = self;
99 152
100 return (void *)w; 153 return (void *)w;
101} 154}
102 155
156static void
157e_destroy (void *w_)
158{
159 ev_watcher *w = (ev_watcher *)w_;
160
161 SvREFCNT_dec (w->loop ); w->loop = 0;
162 SvREFCNT_dec (w->fh ); w->fh = 0;
163 SvREFCNT_dec (w->cb_sv); w->cb_sv = 0;
164 SvREFCNT_dec (w->data ); w->data = 0;
165}
166
103static SV * 167static SV *
104e_bless (struct ev_watcher *w, HV *stash) 168e_bless (ev_watcher *w, HV *stash)
105{ 169{
106 SV *rv; 170 SV *rv;
107 171
108 if (SvOBJECT (w->self)) 172 if (SvOBJECT (w->self))
109 rv = newRV_inc (w->self); 173 rv = newRV_inc (w->self);
115 } 179 }
116 180
117 return rv; 181 return rv;
118} 182}
119 183
184static SV *sv_self_cache, *sv_events_cache;
185
120static void 186static void
121e_cb (struct ev_watcher *w, int revents) 187e_cb (EV_P_ ev_watcher *w, int revents)
122{ 188{
123 dSP; 189 dSP;
124 I32 mark = SP - PL_stack_base; 190 I32 mark = SP - PL_stack_base;
125 SV *sv_self, *sv_events; 191 SV *sv_self, *sv_events;
126 static SV *sv_events_cache;
127 192
193 /* libev might have stopped the watcher */
194 if (expect_false (w->e_flags & WFLAG_UNREFED)
195 && !ev_is_active (w))
196 REF (w);
197
198 if (expect_true (sv_self_cache))
199 {
200 sv_self = sv_self_cache; sv_self_cache = 0;
201 SvRV_set (sv_self, SvREFCNT_inc_NN (w->self));
202 }
203 else
204 {
128 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ 205 sv_self = newRV_inc (w->self); /* e_self (w) MUST be blessed by now */
206 SvREADONLY_on (sv_self);
207 }
208
209 if (expect_true (sv_events_cache))
210 {
211 sv_events = sv_events_cache; sv_events_cache = 0;
212 SvIV_set (sv_events, revents);
213 }
214 else
215 {
216 sv_events = newSViv (revents);
217 SvREADONLY_on (sv_events);
218 }
219
220 PUSHMARK (SP);
221 EXTEND (SP, 2);
222 PUSHs (sv_self);
223 PUSHs (sv_events);
224
225 PUTBACK;
226 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
227
228 if (expect_false (SvREFCNT (sv_self) != 1 || sv_self_cache))
229 SvREFCNT_dec (sv_self);
230 else
231 {
232 SvREFCNT_dec (SvRV (sv_self));
233 SvRV_set (sv_self, &PL_sv_undef);
234 sv_self_cache = sv_self;
235 }
236
237 if (expect_false (SvREFCNT (sv_events) != 1 || sv_events_cache))
238 SvREFCNT_dec (sv_events);
239 else
240 sv_events_cache = sv_events;
241
242 if (expect_false (SvTRUE (ERRSV)))
243 {
244 SPAGAIN;
245 PUSHMARK (SP);
246 PUTBACK;
247 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
248 }
249
250 SP = PL_stack_base + mark;
251 PUTBACK;
252}
253
254static void
255e_once_cb (int revents, void *arg)
256{
257 dSP;
258 I32 mark = SP - PL_stack_base;
259 SV *sv_events;
129 260
130 if (sv_events_cache) 261 if (sv_events_cache)
131 { 262 {
132 sv_events = sv_events_cache; sv_events_cache = 0; 263 sv_events = sv_events_cache; sv_events_cache = 0;
133 SvIV_set (sv_events, revents); 264 SvIV_set (sv_events, revents);
134 } 265 }
135 else 266 else
136 sv_events = newSViv (revents); 267 sv_events = newSViv (revents);
137 268
138 PUSHMARK (SP); 269 PUSHMARK (SP);
139 EXTEND (SP, 2);
140 PUSHs (sv_self);
141 PUSHs (sv_events); 270 XPUSHs (sv_events);
271
142 PUTBACK; 272 PUTBACK;
143 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 273 call_sv ((SV *)arg, G_DISCARD | G_VOID | G_EVAL);
144 SP = PL_stack_base + mark; PUTBACK;
145 274
146 SvREFCNT_dec (sv_self); 275 SvREFCNT_dec ((SV *)arg);
147 276
148 if (sv_events_cache) 277 if (sv_events_cache)
149 SvREFCNT_dec (sv_events); 278 SvREFCNT_dec (sv_events);
150 else 279 else
151 sv_events_cache = sv_events; 280 sv_events_cache = sv_events;
281
282 if (SvTRUE (ERRSV))
283 {
284 SPAGAIN;
285 PUSHMARK (SP);
286 PUTBACK;
287 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
288 }
289
290 SP = PL_stack_base + mark;
291 PUTBACK;
292}
293
294static ev_tstamp
295e_periodic_cb (ev_periodic *w, ev_tstamp now)
296{
297 ev_tstamp retval;
298 int count;
299 dSP;
300
301 ENTER;
302 SAVETMPS;
303
304 PUSHMARK (SP);
305 EXTEND (SP, 2);
306 PUSHs (newRV_inc (e_self (w))); /* e_self (w) MUST be blessed by now */
307 PUSHs (newSVnv (now));
308
309 PUTBACK;
310 count = call_sv (w->fh, G_SCALAR | G_EVAL);
311 SPAGAIN;
152 312
153 if (SvTRUE (ERRSV)) 313 if (SvTRUE (ERRSV))
154 { 314 {
155 PUSHMARK (SP); 315 PUSHMARK (SP);
156 PUTBACK; 316 PUTBACK;
157 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR); 317 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
158 SP = PL_stack_base + mark; PUTBACK; 318 SPAGAIN;
159 } 319 }
160}
161 320
162#if 0 321 if (count > 0)
163/////////////////////////////////////////////////////////////////////////////
164// DNS
165
166static void
167dns_cb (int result, char type, int count, int ttl, void *addresses, void *arg)
168{
169 dSP;
170 SV *cb = (SV *)arg;
171
172 ENTER;
173 SAVETMPS;
174 PUSHMARK (SP);
175 EXTEND (SP, count + 3);
176 PUSHs (sv_2mortal (newSViv (result)));
177
178 if (result == DNS_ERR_NONE && ttl >= 0)
179 { 322 {
180 int i; 323 retval = SvNV (TOPs);
181 324
182 PUSHs (sv_2mortal (newSViv (type))); 325 if (retval < now)
183 PUSHs (sv_2mortal (newSViv (ttl))); 326 retval = now;
184
185 for (i = 0; i < count; ++i)
186 switch (type)
187 {
188 case DNS_IPv6_AAAA:
189 PUSHs (sv_2mortal (newSVpvn (i * 16 + (char *)addresses, 16)));
190 break;
191 case DNS_IPv4_A:
192 PUSHs (sv_2mortal (newSVpvn (i * 4 + (char *)addresses, 4)));
193 break;
194 case DNS_PTR:
195 PUSHs (sv_2mortal (newSVpv (*(char **)addresses, 0)));
196 break;
197 }
198 } 327 }
199 328 else
200 PUTBACK; 329 retval = now;
201 call_sv (sv_2mortal (cb), G_DISCARD | G_VOID | G_EVAL);
202 330
203 FREETMPS; 331 FREETMPS;
204
205 if (SvTRUE (ERRSV))
206 {
207 PUSHMARK (SP);
208 PUTBACK;
209 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
210 }
211
212 LEAVE; 332 LEAVE;
333
334 return retval;
213} 335}
214#endif
215 336
216#define CHECK_REPEAT(repeat) if (repeat < 0.) \ 337#define CHECK_REPEAT(repeat) if (repeat < 0.) \
217 croak (# repeat " value must be >= 0"); 338 croak (# repeat " value must be >= 0");
218 339
340#define CHECK_FD(fh,fd) if ((fd) < 0) \
341 croak ("illegal file descriptor or filehandle (either no attached file descriptor or illegal value): %s", SvPV_nolen (fh));
342
343#define CHECK_SIG(sv,num) if ((num) < 0) \
344 croak ("illegal signal number or name: %s", SvPV_nolen (sv));
345
346static void
347default_fork (void)
348{
349 ev_loop_fork (EV_DEFAULT_UC);
350}
351
219///////////////////////////////////////////////////////////////////////////// 352/////////////////////////////////////////////////////////////////////////////
220// XS interface functions 353// XS interface functions
221 354
222MODULE = EV PACKAGE = EV PREFIX = ev_ 355MODULE = EV PACKAGE = EV PREFIX = ev_
223 356
357PROTOTYPES: ENABLE
358
224BOOT: 359BOOT:
225{ 360{
226 int i;
227 HV *stash = gv_stashpv ("EV", 1); 361 HV *stash = gv_stashpv ("EV", 1);
228 362
229 static const struct { 363 static const struct {
230 const char *name; 364 const char *name;
231 IV iv; 365 IV iv;
232 } *civ, const_iv[] = { 366 } *civ, const_iv[] = {
233# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 367# define const_iv(pfx, name) { # name, (IV) pfx ## name },
368 const_iv (EV_, MINPRI)
369 const_iv (EV_, MAXPRI)
370
371 const_iv (EV_, UNDEF)
234 const_iv (EV_, NONE) 372 const_iv (EV_, NONE)
235 const_iv (EV_, TIMEOUT)
236 const_iv (EV_, READ) 373 const_iv (EV_, READ)
237 const_iv (EV_, WRITE) 374 const_iv (EV_, WRITE)
375 const_iv (EV_, IO)
376 const_iv (EV_, TIMER)
377 const_iv (EV_, PERIODIC)
238 const_iv (EV_, SIGNAL) 378 const_iv (EV_, SIGNAL)
379 const_iv (EV_, CHILD)
380 const_iv (EV_, STAT)
239 const_iv (EV_, IDLE) 381 const_iv (EV_, IDLE)
382 const_iv (EV_, PREPARE)
240 const_iv (EV_, CHECK) 383 const_iv (EV_, CHECK)
384 const_iv (EV_, EMBED)
385 const_iv (EV_, FORK)
386 const_iv (EV_, CLEANUP)
387 const_iv (EV_, ASYNC)
388 const_iv (EV_, CUSTOM)
389 const_iv (EV_, ERROR)
241 390
391 const_iv (EV, RUN_NOWAIT)
392 const_iv (EV, RUN_ONCE)
393
394 const_iv (EV, BREAK_CANCEL)
395 const_iv (EV, BREAK_ONE)
396 const_iv (EV, BREAK_ALL)
397 const_iv (EV, BACKEND_SELECT)
398 const_iv (EV, BACKEND_POLL)
399 const_iv (EV, BACKEND_EPOLL)
400 const_iv (EV, BACKEND_KQUEUE)
401 const_iv (EV, BACKEND_DEVPOLL)
402 const_iv (EV, BACKEND_PORT)
403 const_iv (EV, BACKEND_ALL)
404 const_iv (EV, FLAG_AUTO)
405 const_iv (EV, FLAG_FORKCHECK)
406 const_iv (EV, FLAG_SIGNALFD)
407 const_iv (EV, FLAG_NOENV)
408 const_iv (EV, FLAG_NOINOTIFY)
409
410 const_iv (EV_, VERSION_MAJOR)
411 const_iv (EV_, VERSION_MINOR)
412#if EV_COMPAT3
413 const_iv (EV, FLAG_NOSIGFD) /* compatibility, always 0 */
414 const_iv (EV_, TIMEOUT)
415 const_iv (EV, LOOP_NONBLOCK)
242 const_iv (EV, LOOP_ONESHOT) 416 const_iv (EV, LOOP_ONESHOT)
417 const_iv (EV, UNLOOP_CANCEL)
243 const_iv (EV, LOOP_NONBLOCK) 418 const_iv (EV, UNLOOP_ONE)
244
245 const_iv (EV, METHOD_NONE)
246 const_iv (EV, METHOD_SELECT)
247 const_iv (EV, METHOD_EPOLL) 419 const_iv (EV, UNLOOP_ALL)
420#endif
248 }; 421 };
249 422
250 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 423 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
251 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 424 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
252 425
426 stash_loop = gv_stashpv ("EV::Loop" , 1);
253 stash_watcher = gv_stashpv ("EV::Watcher" , 1); 427 stash_watcher = gv_stashpv ("EV::Watcher" , 1);
254 stash_io = gv_stashpv ("EV::IO" , 1); 428 stash_io = gv_stashpv ("EV::IO" , 1);
255 stash_time = gv_stashpv ("EV::Time" , 1);
256 stash_timer = gv_stashpv ("EV::Timer" , 1); 429 stash_timer = gv_stashpv ("EV::Timer" , 1);
257 stash_periodic = gv_stashpv ("EV::Periodic", 1); 430 stash_periodic = gv_stashpv ("EV::Periodic", 1);
258 stash_signal = gv_stashpv ("EV::Signal" , 1); 431 stash_signal = gv_stashpv ("EV::Signal" , 1);
259 stash_idle = gv_stashpv ("EV::Idle" , 1); 432 stash_idle = gv_stashpv ("EV::Idle" , 1);
433 stash_prepare = gv_stashpv ("EV::Prepare" , 1);
260 stash_check = gv_stashpv ("EV::Check" , 1); 434 stash_check = gv_stashpv ("EV::Check" , 1);
435 stash_child = gv_stashpv ("EV::Child" , 1);
436 stash_embed = gv_stashpv ("EV::Embed" , 1);
437 stash_stat = gv_stashpv ("EV::Stat" , 1);
438 stash_fork = gv_stashpv ("EV::Fork" , 1);
439 stash_cleanup = gv_stashpv ("EV::Cleanup" , 1);
440 stash_async = gv_stashpv ("EV::Async" , 1);
261 441
262 { 442 {
263 SV *sv = perl_get_sv ("EV::API", TRUE); 443 SV *sv = perl_get_sv ("EV::API", TRUE);
264 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ 444 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
265 445
266 /* the poor man's shared library emulator */ 446 /* the poor man's shared library emulator */
267 evapi.ver = EV_API_VERSION; 447 evapi.ver = EV_API_VERSION;
268 evapi.rev = EV_API_REVISION; 448 evapi.rev = EV_API_REVISION;
269 evapi.once = api_once;
270 evapi.sv_fileno = sv_fileno; 449 evapi.sv_fileno = sv_fileno;
271 evapi.sv_signum = sv_signum; 450 evapi.sv_signum = s_signum;
272 evapi.now = &ev_now; 451 evapi.supported_backends = ev_supported_backends ();
273 evapi.method = &ev_method; 452 evapi.recommended_backends = ev_recommended_backends ();
274 evapi.loop_done = &ev_loop_done; 453 evapi.embeddable_backends = ev_embeddable_backends ();
275 evapi.time = ev_time; 454 evapi.time_ = ev_time;
455 evapi.sleep_ = ev_sleep;
276 evapi.loop = ev_loop; 456 evapi.loop_new = ev_loop_new;
457 evapi.loop_destroy = ev_loop_destroy;
458 evapi.loop_fork = ev_loop_fork;
459 evapi.iteration = ev_iteration;
460 evapi.depth = ev_depth;
461 evapi.set_userdata = ev_set_userdata;
462 evapi.userdata = ev_userdata;
463 evapi.now = ev_now;
464 evapi.now_update = ev_now_update;
465 evapi.suspend = ev_suspend;
466 evapi.resume = ev_resume;
467 evapi.backend = ev_backend;
468 evapi.break_ = ev_break;
469 evapi.invoke_pending = ev_invoke_pending;
470 evapi.pending_count = ev_pending_count;
471 evapi.verify = ev_verify;
472 evapi.set_loop_release_cb = ev_set_loop_release_cb;
473 evapi.set_invoke_pending_cb= ev_set_invoke_pending_cb;
474 evapi.ref = ev_ref;
475 evapi.unref = ev_unref;
476 evapi.run = ev_run;
477 evapi.once = ev_once;
277 evapi.io_start = evio_start; 478 evapi.io_start = ev_io_start;
278 evapi.io_stop = evio_stop; 479 evapi.io_stop = ev_io_stop;
279 evapi.timer_start = evtimer_start; 480 evapi.timer_start = ev_timer_start;
280 evapi.timer_stop = evtimer_stop; 481 evapi.timer_stop = ev_timer_stop;
281 evapi.timer_again = evtimer_again; 482 evapi.timer_again = ev_timer_again;
483 evapi.timer_remaining = ev_timer_remaining;
282 evapi.periodic_start = evperiodic_start; 484 evapi.periodic_start = ev_periodic_start;
283 evapi.periodic_stop = evperiodic_stop; 485 evapi.periodic_stop = ev_periodic_stop;
284 evapi.signal_start = evsignal_start; 486 evapi.signal_start = ev_signal_start;
285 evapi.signal_stop = evsignal_stop; 487 evapi.signal_stop = ev_signal_stop;
286 evapi.idle_start = evidle_start; 488 evapi.idle_start = ev_idle_start;
287 evapi.idle_stop = evidle_stop; 489 evapi.idle_stop = ev_idle_stop;
490 evapi.prepare_start = ev_prepare_start;
491 evapi.prepare_stop = ev_prepare_stop;
288 evapi.check_start = evcheck_start; 492 evapi.check_start = ev_check_start;
289 evapi.check_stop = evcheck_stop; 493 evapi.check_stop = ev_check_stop;
494#if EV_CHILD_ENABLE
495 evapi.child_start = ev_child_start;
496 evapi.child_stop = ev_child_stop;
497#endif
498 evapi.stat_start = ev_stat_start;
499 evapi.stat_stop = ev_stat_stop;
500 evapi.stat_stat = ev_stat_stat;
501 evapi.embed_start = ev_embed_start;
502 evapi.embed_stop = ev_embed_stop;
503 evapi.embed_sweep = ev_embed_sweep;
504 evapi.fork_start = ev_fork_start;
505 evapi.fork_stop = ev_fork_stop;
506 evapi.cleanup_start = ev_cleanup_start;
507 evapi.cleanup_stop = ev_cleanup_stop;
508 evapi.async_start = ev_async_start;
509 evapi.async_stop = ev_async_stop;
510 evapi.async_send = ev_async_send;
511 evapi.clear_pending = ev_clear_pending;
512 evapi.invoke = ev_invoke;
290 513
291 sv_setiv (sv, (IV)&evapi); 514 sv_setiv (sv, (IV)&evapi);
292 SvREADONLY_on (sv); 515 SvREADONLY_on (sv);
293 } 516 }
517#if !defined(_WIN32) && !defined(_MINIX)
518 pthread_atfork (0, 0, default_fork);
519#endif
294} 520}
521
522SV *ev_default_loop (unsigned int flags = 0)
523 CODE:
524{
525 if (!default_loop_sv)
526 {
527 evapi.default_loop = ev_default_loop (flags);
528
529 if (!evapi.default_loop)
530 XSRETURN_UNDEF;
531
532 default_loop_sv = sv_bless (newRV_noinc (newSViv (PTR2IV (evapi.default_loop))), stash_loop);
533 }
534
535 RETVAL = newSVsv (default_loop_sv);
536}
537 OUTPUT:
538 RETVAL
539
540void ev_default_destroy ()
541 CODE:
542 ev_loop_destroy (EV_DEFAULT_UC);
543 SvREFCNT_dec (default_loop_sv);
544 default_loop_sv = 0;
545
546unsigned int ev_supported_backends ()
547
548unsigned int ev_recommended_backends ()
549
550unsigned int ev_embeddable_backends ()
551
552void ev_sleep (NV interval)
553
554NV ev_time ()
295 555
296NV ev_now () 556NV ev_now ()
297 CODE: 557 C_ARGS: evapi.default_loop
298 RETVAL = ev_now;
299 OUTPUT:
300 RETVAL
301 558
302int ev_method () 559void ev_now_update ()
303 CODE: 560 C_ARGS: evapi.default_loop
304 RETVAL = ev_method;
305 OUTPUT:
306 RETVAL
307 561
308NV ev_time () 562void ev_suspend ()
563 C_ARGS: evapi.default_loop
309 564
565void ev_resume ()
566 C_ARGS: evapi.default_loop
567
568unsigned int ev_backend ()
569 C_ARGS: evapi.default_loop
570
571void ev_verify ()
572 ALIAS:
573 loop_verify = 1
574 C_ARGS: evapi.default_loop
575
576unsigned int ev_iteration ()
577 ALIAS:
578 loop_count = 1
579 C_ARGS: evapi.default_loop
580
581unsigned int ev_depth ()
582 ALIAS:
583 loop_depth = 1
584 C_ARGS: evapi.default_loop
585
586void ev_set_io_collect_interval (NV interval)
587 C_ARGS: evapi.default_loop, interval
588
589void ev_set_timeout_collect_interval (NV interval)
590 C_ARGS: evapi.default_loop, interval
591
310void ev_init (int flags = 0) 592void ev_run (int flags = 0)
593 ALIAS:
594 loop = 1
595 C_ARGS: evapi.default_loop, flags
311 596
312void ev_loop (int flags = 0) 597void ev_break (int how = EVBREAK_ONE)
598 ALIAS:
599 unloop = 1
600 C_ARGS: evapi.default_loop, how
313 601
314void ev_loop_done (int value = 1) 602void ev_feed_fd_event (int fd, int revents = EV_NONE)
315 CODE: 603 C_ARGS: evapi.default_loop, fd, revents
316 ev_loop_done = value;
317 604
605void ev_feed_signal_event (SV *signal)
606 CODE:
607{
608 Signal signum = s_signum (signal);
609 CHECK_SIG (signal, signum);
610
611 ev_feed_signal_event (evapi.default_loop, signum);
612}
613
614unsigned int ev_pending_count ()
615 C_ARGS: evapi.default_loop
616
617void ev_invoke_pending ()
618 C_ARGS: evapi.default_loop
619
318struct ev_io *io (SV *fh, int events, SV *cb) 620ev_io *io (SV *fh, int events, SV *cb)
319 ALIAS: 621 ALIAS:
320 io_ns = 1 622 io_ns = 1
623 _ae_io = 2
321 CODE: 624 CODE:
625{
626 int fd = s_fileno (fh, events & EV_WRITE);
627 CHECK_FD (fh, fd);
628
629 if (ix == 2)
630 {
631 ix = 0;
632 events = events ? EV_WRITE : EV_READ;
633 }
634
322 RETVAL = e_new (sizeof (struct ev_io), cb); 635 RETVAL = e_new (sizeof (ev_io), cb, default_loop_sv);
323 RETVAL->fh = newSVsv (fh); 636 e_fh (RETVAL) = newSVsv (fh);
324 evio_set (RETVAL, sv_fileno (RETVAL->fh), events); 637 ev_io_set (RETVAL, fd, events);
325 if (!ix) evio_start (RETVAL); 638 if (!ix) START (io, RETVAL);
639}
326 OUTPUT: 640 OUTPUT:
327 RETVAL 641 RETVAL
328 642
329struct ev_timer *timer (NV after, NV repeat, SV *cb) 643ev_timer *timer (NV after, NV repeat, SV *cb)
330 ALIAS: 644 ALIAS:
331 timer_ns = 1 645 timer_ns = 1
332 INIT: 646 INIT:
333 CHECK_REPEAT (repeat); 647 CHECK_REPEAT (repeat);
334 CODE: 648 CODE:
335 RETVAL = e_new (sizeof (struct ev_timer), cb); 649 RETVAL = e_new (sizeof (ev_timer), cb, default_loop_sv);
336 evtimer_set (RETVAL, after, repeat); 650 ev_timer_set (RETVAL, after, repeat);
337 if (!ix) evtimer_start (RETVAL); 651 if (!ix) START (timer, RETVAL);
338 OUTPUT: 652 OUTPUT:
339 RETVAL 653 RETVAL
340 654
341struct ev_periodic *periodic (NV at, NV interval, SV *cb) 655SV *periodic (NV at, NV interval, SV *reschedule_cb, SV *cb)
342 ALIAS: 656 ALIAS:
343 periodic_ns = 1 657 periodic_ns = 1
344 INIT: 658 INIT:
345 CHECK_REPEAT (interval); 659 CHECK_REPEAT (interval);
346 CODE: 660 CODE:
347 RETVAL = e_new (sizeof (struct ev_periodic), cb); 661{
348 evperiodic_set (RETVAL, at, interval); 662 ev_periodic *w;
349 if (!ix) evperiodic_start (RETVAL); 663 w = e_new (sizeof (ev_periodic), cb, default_loop_sv);
664 e_fh (w) = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
665 ev_periodic_set (w, at, interval, e_fh (w) ? e_periodic_cb : 0);
666 RETVAL = e_bless ((ev_watcher *)w, stash_periodic);
667 if (!ix) START (periodic, w);
668}
350 OUTPUT: 669 OUTPUT:
351 RETVAL 670 RETVAL
352 671
353struct ev_signal *signal (Signal signum, SV *cb) 672ev_signal *signal (SV *signal, SV *cb)
354 ALIAS: 673 ALIAS:
355 signal_ns = 1 674 signal_ns = 1
356 CODE: 675 CODE:
676{
677 Signal signum = s_signum (signal);
678 CHECK_SIG (signal, signum);
679
357 RETVAL = e_new (sizeof (struct ev_signal), cb); 680 RETVAL = e_new (sizeof (ev_signal), cb, default_loop_sv);
358 evsignal_set (RETVAL, signum); 681 ev_signal_set (RETVAL, signum);
359 if (!ix) evsignal_start (RETVAL); 682 if (!ix) START_SIGNAL (RETVAL);
683}
360 OUTPUT: 684 OUTPUT:
361 RETVAL 685 RETVAL
362 686
363struct ev_idle *idle (SV *cb) 687ev_idle *idle (SV *cb)
364 ALIAS: 688 ALIAS:
365 idle_ns = 1 689 idle_ns = 1
366 CODE: 690 CODE:
367 RETVAL = e_new (sizeof (struct ev_idle), cb); 691 RETVAL = e_new (sizeof (ev_idle), cb, default_loop_sv);
368 evidle_set (RETVAL); 692 ev_idle_set (RETVAL);
369 if (!ix) evidle_start (RETVAL); 693 if (!ix) START (idle, RETVAL);
370 OUTPUT: 694 OUTPUT:
371 RETVAL 695 RETVAL
372 696
697ev_prepare *prepare (SV *cb)
698 ALIAS:
699 prepare_ns = 1
700 CODE:
701 RETVAL = e_new (sizeof (ev_prepare), cb, default_loop_sv);
702 ev_prepare_set (RETVAL);
703 if (!ix) START (prepare, RETVAL);
704 OUTPUT:
705 RETVAL
706
373struct ev_check *check (SV *cb) 707ev_check *check (SV *cb)
374 ALIAS: 708 ALIAS:
375 check_ns = 1 709 check_ns = 1
376 CODE: 710 CODE:
377 RETVAL = e_new (sizeof (struct ev_check), cb); 711 RETVAL = e_new (sizeof (ev_check), cb, default_loop_sv);
378 evcheck_set (RETVAL); 712 ev_check_set (RETVAL);
379 if (!ix) evcheck_start (RETVAL); 713 if (!ix) START (check, RETVAL);
380 OUTPUT: 714 OUTPUT:
381 RETVAL 715 RETVAL
382 716
717ev_fork *fork (SV *cb)
718 ALIAS:
719 fork_ns = 1
720 CODE:
721 RETVAL = e_new (sizeof (ev_fork), cb, default_loop_sv);
722 ev_fork_set (RETVAL);
723 if (!ix) START (fork, RETVAL);
724 OUTPUT:
725 RETVAL
726
727ev_cleanup *cleanup (SV *cb)
728 ALIAS:
729 cleanup_ns = 1
730 CODE:
731 RETVAL = e_new (sizeof (ev_cleanup), cb, default_loop_sv);
732 ev_cleanup_set (RETVAL);
733 if (!ix) START (cleanup, RETVAL);
734 OUTPUT:
735 RETVAL
736
737ev_child *child (int pid, int trace, SV *cb)
738 ALIAS:
739 child_ns = 1
740 CODE:
741#if EV_CHILD_ENABLE
742 RETVAL = e_new (sizeof (ev_child), cb, default_loop_sv);
743 ev_child_set (RETVAL, pid, trace);
744 if (!ix) START (child, RETVAL);
745#else
746 croak ("EV::child watchers not supported on this platform");
747#endif
748 OUTPUT:
749 RETVAL
750
751
752ev_stat *stat (SV *path, NV interval, SV *cb)
753 ALIAS:
754 stat_ns = 1
755 CODE:
756 RETVAL = e_new (sizeof (ev_stat), cb, default_loop_sv);
757 e_fh (RETVAL) = newSVsv (path);
758 ev_stat_set (RETVAL, SvPVbyte_nolen (e_fh (RETVAL)), interval);
759 if (!ix) START (stat, RETVAL);
760 OUTPUT:
761 RETVAL
762
763ev_embed *embed (struct ev_loop *loop, SV *cb = 0)
764 ALIAS:
765 embed_ns = 1
766 CODE:
767{
768 if (!(ev_backend (loop) & ev_embeddable_backends ()))
769 croak ("passed loop is not embeddable via EV::embed,");
770
771 RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv);
772 e_fh (RETVAL) = newSVsv (ST (0));
773 ev_embed_set (RETVAL, loop);
774 if (!ix) START (embed, RETVAL);
775}
776 OUTPUT:
777 RETVAL
778
779ev_async *async (SV *cb)
780 ALIAS:
781 async_ns = 1
782 CODE:
783 RETVAL = e_new (sizeof (ev_async), cb, default_loop_sv);
784 ev_async_set (RETVAL);
785 if (!ix) START (async, RETVAL);
786 OUTPUT:
787 RETVAL
788
789void once (SV *fh, int events, SV *timeout, SV *cb)
790 CODE:
791 ev_once (
792 evapi.default_loop,
793 s_fileno (fh, events & EV_WRITE), events,
794 SvOK (timeout) ? SvNV (timeout) : -1.,
795 e_once_cb,
796 newSVsv (cb)
797 );
383 798
384PROTOTYPES: DISABLE 799PROTOTYPES: DISABLE
385 800
386MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_ 801MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_
387 802
388int ev_is_active (struct ev_watcher *w) 803int ev_is_active (ev_watcher *w)
389 804
805int ev_is_pending (ev_watcher *w)
806
807void ev_invoke (ev_watcher *w, int revents = EV_NONE)
808 C_ARGS: e_loop (w), w, revents
809
810int ev_clear_pending (ev_watcher *w)
811 C_ARGS: e_loop (w), w
812
813void ev_feed_event (ev_watcher *w, int revents = EV_NONE)
814 C_ARGS: e_loop (w), w, revents
815
816int keepalive (ev_watcher *w, int new_value = 0)
817 CODE:
818{
819 RETVAL = w->e_flags & WFLAG_KEEPALIVE;
820 new_value = new_value ? WFLAG_KEEPALIVE : 0;
821
822 if (items > 1 && ((new_value ^ w->e_flags) & WFLAG_KEEPALIVE))
823 {
824 w->e_flags = (w->e_flags & ~WFLAG_KEEPALIVE) | new_value;
825 REF (w);
826 UNREF (w);
827 }
828}
829 OUTPUT:
830 RETVAL
831
390SV *cb (struct ev_watcher *w, SV *new_cb = 0) 832SV *cb (ev_watcher *w, SV *new_cb = 0)
391 CODE: 833 CODE:
392{ 834{
393 RETVAL = newSVsv (w->cb_sv);
394
395 if (items > 1)
396 sv_setsv (w->cb_sv, new_cb);
397}
398 OUTPUT:
399 RETVAL
400
401MODULE = EV PACKAGE = EV::IO PREFIX = evio_
402
403void evio_start (struct ev_io *w)
404
405void evio_stop (struct ev_io *w)
406
407void set (struct ev_io *w, SV *fh, int events)
408 CODE:
409{
410 int active = w->active;
411 if (active) evio_stop (w);
412
413 sv_setsv (w->fh, fh);
414 evio_set (w, sv_fileno (w->fh), events);
415
416 if (active) evio_start (w);
417}
418
419SV *fh (struct ev_io *w, SV *new_fh = 0)
420 CODE:
421{
422 RETVAL = newSVsv (w->fh);
423
424 if (items > 1) 835 if (items > 1)
425 { 836 {
426 int active = w->active; 837 new_cb = s_get_cv_croak (new_cb);
427 if (active) evio_stop (w); 838 RETVAL = newRV_noinc (w->cb_sv);
428 839 w->cb_sv = SvREFCNT_inc (new_cb);
429 sv_setsv (w->fh, new_fh);
430 evio_set (w, sv_fileno (w->fh), w->events);
431
432 if (active) evio_start (w);
433 } 840 }
841 else
842 RETVAL = newRV_inc (w->cb_sv);
434} 843}
435 OUTPUT: 844 OUTPUT:
436 RETVAL 845 RETVAL
437 846
438short events (struct ev_io *w, short new_events = EV_UNDEF) 847SV *data (ev_watcher *w, SV *new_data = 0)
439 CODE: 848 CODE:
440{ 849{
441 RETVAL = w->events; 850 RETVAL = w->data ? newSVsv (w->data) : &PL_sv_undef;
442 851
443 if (items > 1) 852 if (items > 1)
444 { 853 {
445 int active = w->active; 854 SvREFCNT_dec (w->data);
446 if (active) evio_stop (w); 855 w->data = newSVsv (new_data);
447
448 evio_set (w, w->fd, new_events);
449
450 if (active) evio_start (w);
451 } 856 }
452} 857}
453 OUTPUT: 858 OUTPUT:
454 RETVAL 859 RETVAL
455 860
861SV *loop (ev_watcher *w)
862 CODE:
863 RETVAL = newRV_inc (w->loop);
864 OUTPUT:
865 RETVAL
866
867int priority (ev_watcher *w, int new_priority = 0)
868 CODE:
869{
870 RETVAL = w->priority;
871
872 if (items > 1)
873 {
874 int active = ev_is_active (w);
875
876 if (active)
877 {
878 /* grrr. */
879 PUSHMARK (SP);
880 XPUSHs (ST (0));
881 PUTBACK;
882 call_method ("stop", G_DISCARD | G_VOID);
883 }
884
885 ev_set_priority (w, new_priority);
886
887 if (active)
888 {
889 PUSHMARK (SP);
890 XPUSHs (ST (0));
891 PUTBACK;
892 call_method ("start", G_DISCARD | G_VOID);
893 }
894 }
895}
896 OUTPUT:
897 RETVAL
898
899MODULE = EV PACKAGE = EV::IO PREFIX = ev_io_
900
901void ev_io_start (ev_io *w)
902 CODE:
903 START (io, w);
904
905void ev_io_stop (ev_io *w)
906 CODE:
907 STOP (io, w);
908
909void DESTROY (ev_io *w)
910 CODE:
911 STOP (io, w);
912 e_destroy (w);
913
914void set (ev_io *w, SV *fh, int events)
915 CODE:
916{
917 int fd = s_fileno (fh, events & EV_WRITE);
918 CHECK_FD (fh, fd);
919
920 sv_setsv (e_fh (w), fh);
921 RESET (io, w, (w, fd, events));
922}
923
924SV *fh (ev_io *w, SV *new_fh = 0)
925 CODE:
926{
927 if (items > 1)
928 {
929 int fd = s_fileno (new_fh, w->events & EV_WRITE);
930 CHECK_FD (new_fh, fd);
931
932 RETVAL = e_fh (w);
933 e_fh (w) = newSVsv (new_fh);
934
935 RESET (io, w, (w, fd, w->events));
936 }
937 else
938 RETVAL = newSVsv (e_fh (w));
939}
940 OUTPUT:
941 RETVAL
942
943int events (ev_io *w, int new_events = EV_UNDEF)
944 CODE:
945{
946 RETVAL = w->events;
947
948 if (items > 1)
949 RESET (io, w, (w, w->fd, new_events));
950}
951 OUTPUT:
952 RETVAL
953
456MODULE = EV PACKAGE = EV::Signal PREFIX = evsignal_ 954MODULE = EV PACKAGE = EV::Signal PREFIX = ev_signal_
457 955
458void evsignal_start (struct ev_signal *w) 956void ev_signal_start (ev_signal *w)
957 CODE:
958 START_SIGNAL (w);
459 959
460void evsignal_stop (struct ev_signal *w) 960void ev_signal_stop (ev_signal *w)
961 CODE:
962 STOP (signal, w);
461 963
964void DESTROY (ev_signal *w)
965 CODE:
966 STOP (signal, w);
967 e_destroy (w);
968
462void set (struct ev_signal *w, SV *signal = 0) 969void set (ev_signal *w, SV *signal)
463 CODE: 970 CODE:
464{ 971{
465 Signal signum = sv_signum (signal); /* may croak here */ 972 Signal signum = s_signum (signal);
466 int active = w->active; 973 CHECK_SIG (signal, signum);
467 974
468 if (active) evsignal_stop (w); 975 RESET_SIGNAL (w, (w, signum));
469 evsignal_set (w, signum);
470 if (active) evsignal_start (w);
471} 976}
472 977
473MODULE = EV PACKAGE = EV::Time 978int signal (ev_signal *w, SV *new_signal = 0)
979 CODE:
980{
981 RETVAL = w->signum;
474 982
983 if (items > 1)
984 {
985 Signal signum = s_signum (new_signal);
986 CHECK_SIG (new_signal, signum);
987
988 RESET_SIGNAL (w, (w, signum));
989 }
990}
991 OUTPUT:
992 RETVAL
993
475MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ 994MODULE = EV PACKAGE = EV::Timer PREFIX = ev_timer_
476 995
477void evtimer_start (struct ev_timer *w) 996void ev_timer_start (ev_timer *w)
478 INIT: 997 INIT:
479 CHECK_REPEAT (w->repeat); 998 CHECK_REPEAT (w->repeat);
999 CODE:
1000 START (timer, w);
480 1001
481void evtimer_stop (struct ev_timer *w) 1002void ev_timer_stop (ev_timer *w)
1003 CODE:
1004 STOP (timer, w);
482 1005
483void evtimer_again (struct ev_timer *w) 1006void ev_timer_again (ev_timer *w)
484 INIT: 1007 INIT:
485 CHECK_REPEAT (w->repeat); 1008 CHECK_REPEAT (w->repeat);
1009 CODE:
1010 ev_timer_again (e_loop (w), w);
1011 UNREF (w);
486 1012
1013NV ev_timer_remaining (ev_timer *w)
1014 C_ARGS: e_loop (w), w
1015
1016void DESTROY (ev_timer *w)
1017 CODE:
1018 STOP (timer, w);
1019 e_destroy (w);
1020
487void set (struct ev_timer *w, NV after, NV repeat = 0.) 1021void set (ev_timer *w, NV after, NV repeat = 0.)
488 INIT: 1022 INIT:
489 CHECK_REPEAT (repeat); 1023 CHECK_REPEAT (repeat);
490 CODE: 1024 CODE:
491{
492 int active = w->active;
493 if (active) evtimer_stop (w);
494 evtimer_set (w, after, repeat); 1025 RESET (timer, w, (w, after, repeat));
495 if (active) evtimer_start (w);
496}
497 1026
498MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ 1027MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_
499 1028
500void evperiodic_start (struct ev_periodic *w) 1029void ev_periodic_start (ev_periodic *w)
501 INIT: 1030 INIT:
502 CHECK_REPEAT (w->interval); 1031 CHECK_REPEAT (w->interval);
1032 CODE:
1033 START (periodic, w);
503 1034
504void evperiodic_stop (struct ev_periodic *w) 1035void ev_periodic_stop (ev_periodic *w)
1036 CODE:
1037 STOP (periodic, w);
505 1038
506void set (struct ev_periodic *w, NV at, NV interval = 0.) 1039void ev_periodic_again (ev_periodic *w)
1040 CODE:
1041 ev_periodic_again (e_loop (w), w);
1042 UNREF (w);
1043
1044void DESTROY (ev_periodic *w)
1045 CODE:
1046 STOP (periodic, w);
1047 e_destroy (w);
1048
1049void set (ev_periodic *w, NV at, NV interval = 0., SV *reschedule_cb = &PL_sv_undef)
507 INIT: 1050 INIT:
508 CHECK_REPEAT (interval); 1051 CHECK_REPEAT (interval);
509 CODE: 1052 CODE:
510{ 1053{
511 int active = w->active; 1054 SvREFCNT_dec (e_fh (w));
512 if (active) evperiodic_stop (w); 1055 e_fh (w) = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
513 evperiodic_set (w, at, interval);
514 if (active) evperiodic_start (w);
515}
516 1056
1057 RESET (periodic, w, (w, at, interval, e_fh (w) ? e_periodic_cb : 0));
1058}
1059
1060NV at (ev_periodic *w)
1061 CODE:
1062 RETVAL = ev_periodic_at (w);
1063 OUTPUT:
1064 RETVAL
1065
517MODULE = EV PACKAGE = EV::Idle PREFIX = evidle_ 1066MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
518 1067
519void evidle_start (struct ev_idle *w) 1068void ev_idle_start (ev_idle *w)
1069 CODE:
1070 START (idle, w);
520 1071
521void evidle_stop (struct ev_idle *w) 1072void ev_idle_stop (ev_idle *w)
1073 CODE:
1074 STOP (idle, w);
522 1075
1076void DESTROY (ev_idle *w)
1077 CODE:
1078 STOP (idle, w);
1079 e_destroy (w);
1080
1081MODULE = EV PACKAGE = EV::Prepare PREFIX = ev_prepare_
1082
1083void ev_prepare_start (ev_prepare *w)
1084 CODE:
1085 START (prepare, w);
1086
1087void ev_prepare_stop (ev_prepare *w)
1088 CODE:
1089 STOP (prepare, w);
1090
1091void DESTROY (ev_prepare *w)
1092 CODE:
1093 STOP (prepare, w);
1094 e_destroy (w);
1095
523MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ 1096MODULE = EV PACKAGE = EV::Check PREFIX = ev_check_
524 1097
525void evcheck_start (struct ev_check *w) 1098void ev_check_start (ev_check *w)
1099 CODE:
1100 START (check, w);
526 1101
527void evcheck_stop (struct ev_check *w) 1102void ev_check_stop (ev_check *w)
1103 CODE:
1104 STOP (check, w);
1105
1106void DESTROY (ev_check *w)
1107 CODE:
1108 STOP (check, w);
1109 e_destroy (w);
1110
1111MODULE = EV PACKAGE = EV::Fork PREFIX = ev_fork_
1112
1113void ev_fork_start (ev_fork *w)
1114 CODE:
1115 START (fork, w);
1116
1117void ev_fork_stop (ev_fork *w)
1118 CODE:
1119 STOP (fork, w);
1120
1121void DESTROY (ev_fork *w)
1122 CODE:
1123 STOP (fork, w);
1124 e_destroy (w);
1125
1126MODULE = EV PACKAGE = EV::Cleanup PREFIX = ev_cleanup_
1127
1128void ev_cleanup_start (ev_cleanup *w)
1129 CODE:
1130 START (cleanup, w);
1131
1132void ev_cleanup_stop (ev_cleanup *w)
1133 CODE:
1134 STOP (cleanup, w);
1135
1136void DESTROY (ev_cleanup *w)
1137 CODE:
1138 STOP (cleanup, w);
1139 e_destroy (w);
1140
1141int keepalive (ev_watcher *w, int new_value = 0)
1142 CODE:
1143 RETVAL = 0;
1144 OUTPUT:
1145 RETVAL
1146
1147MODULE = EV PACKAGE = EV::Child PREFIX = ev_child_
1148
1149#if EV_CHILD_ENABLE
1150
1151void ev_child_start (ev_child *w)
1152 CODE:
1153 START (child, w);
1154
1155void ev_child_stop (ev_child *w)
1156 CODE:
1157 STOP (child, w);
1158
1159void DESTROY (ev_child *w)
1160 CODE:
1161 STOP (child, w);
1162 e_destroy (w);
1163
1164void set (ev_child *w, int pid, int trace)
1165 CODE:
1166 RESET (child, w, (w, pid, trace));
1167
1168int pid (ev_child *w)
1169 ALIAS:
1170 rpid = 1
1171 rstatus = 2
1172 CODE:
1173 RETVAL = ix == 0 ? w->pid
1174 : ix == 1 ? w->rpid
1175 : w->rstatus;
1176 OUTPUT:
1177 RETVAL
1178
1179#endif
1180
1181MODULE = EV PACKAGE = EV::Stat PREFIX = ev_stat_
1182
1183void ev_stat_start (ev_stat *w)
1184 CODE:
1185 START (stat, w);
1186
1187void ev_stat_stop (ev_stat *w)
1188 CODE:
1189 STOP (stat, w);
1190
1191void DESTROY (ev_stat *w)
1192 CODE:
1193 STOP (stat, w);
1194 e_destroy (w);
1195
1196void set (ev_stat *w, SV *path, NV interval)
1197 CODE:
1198{
1199 sv_setsv (e_fh (w), path);
1200 RESET (stat, w, (w, SvPVbyte_nolen (e_fh (w)), interval));
1201}
1202
1203SV *path (ev_stat *w, SV *new_path = 0)
1204 CODE:
1205{
1206 RETVAL = SvREFCNT_inc (e_fh (w));
1207
1208 if (items > 1)
1209 {
1210 SvREFCNT_dec (e_fh (w));
1211 e_fh (w) = newSVsv (new_path);
1212 RESET (stat, w, (w, SvPVbyte_nolen (e_fh (w)), w->interval));
1213 }
1214}
1215 OUTPUT:
1216 RETVAL
1217
1218NV interval (ev_stat *w, NV new_interval = 0.)
1219 CODE:
1220{
1221 RETVAL = w->interval;
1222
1223 if (items > 1)
1224 RESET (stat, w, (w, SvPVbyte_nolen (e_fh (w)), new_interval));
1225}
1226 OUTPUT:
1227 RETVAL
1228
1229void prev (ev_stat *w)
1230 ALIAS:
1231 stat = 1
1232 attr = 2
1233 PPCODE:
1234{
1235 ev_statdata *s = ix ? &w->attr : &w->prev;
1236
1237 if (ix == 1)
1238 ev_stat_stat (e_loop (w), w);
1239 else if (!s->st_nlink)
1240 errno = ENOENT;
1241
1242 PL_statcache.st_dev = s->st_nlink;
1243 PL_statcache.st_ino = s->st_ino;
1244 PL_statcache.st_mode = s->st_mode;
1245 PL_statcache.st_nlink = s->st_nlink;
1246 PL_statcache.st_uid = s->st_uid;
1247 PL_statcache.st_gid = s->st_gid;
1248 PL_statcache.st_rdev = s->st_rdev;
1249 PL_statcache.st_size = s->st_size;
1250 PL_statcache.st_atime = s->st_atime;
1251 PL_statcache.st_mtime = s->st_mtime;
1252 PL_statcache.st_ctime = s->st_ctime;
1253
1254 if (GIMME_V == G_SCALAR)
1255 XPUSHs (boolSV (s->st_nlink));
1256 else if (GIMME_V == G_ARRAY && s->st_nlink)
1257 {
1258 EXTEND (SP, 13);
1259 PUSHs (sv_2mortal (newSViv (s->st_dev)));
1260 PUSHs (sv_2mortal (newSViv (s->st_ino)));
1261 PUSHs (sv_2mortal (newSVuv (s->st_mode)));
1262 PUSHs (sv_2mortal (newSVuv (s->st_nlink)));
1263 PUSHs (sv_2mortal (newSViv (s->st_uid)));
1264 PUSHs (sv_2mortal (newSViv (s->st_gid)));
1265 PUSHs (sv_2mortal (newSViv (s->st_rdev)));
1266 PUSHs (sv_2mortal (newSVnv ((NV)s->st_size)));
1267 PUSHs (sv_2mortal (newSVnv (s->st_atime)));
1268 PUSHs (sv_2mortal (newSVnv (s->st_mtime)));
1269 PUSHs (sv_2mortal (newSVnv (s->st_ctime)));
1270 PUSHs (sv_2mortal (newSVuv (4096)));
1271 PUSHs (sv_2mortal (newSVnv ((NV)((s->st_size + 4095) / 4096))));
1272 }
1273}
1274
1275MODULE = EV PACKAGE = EV::Embed PREFIX = ev_embed_
1276
1277void ev_embed_start (ev_embed *w)
1278 CODE:
1279 START (embed, w);
1280
1281void ev_embed_stop (ev_embed *w)
1282 CODE:
1283 STOP (embed, w);
1284
1285void DESTROY (ev_embed *w)
1286 CODE:
1287 STOP (embed, w);
1288 e_destroy (w);
1289
1290void set (ev_embed *w, struct ev_loop *loop)
1291 CODE:
1292{
1293 sv_setsv (e_fh (w), ST (1));
1294 RESET (embed, w, (w, loop));
1295}
1296
1297SV *other (ev_embed *w)
1298 CODE:
1299 RETVAL = newSVsv (e_fh (w));
1300 OUTPUT:
1301 RETVAL
1302
1303void ev_embed_sweep (ev_embed *w)
1304 C_ARGS: e_loop (w), w
1305
1306MODULE = EV PACKAGE = EV::Async PREFIX = ev_async_
1307
1308void ev_async_start (ev_async *w)
1309 CODE:
1310 START (async, w);
1311
1312void ev_async_stop (ev_async *w)
1313 CODE:
1314 STOP (async, w);
1315
1316void DESTROY (ev_async *w)
1317 CODE:
1318 STOP (async, w);
1319 e_destroy (w);
1320
1321void ev_async_send (ev_async *w)
1322 C_ARGS: e_loop (w), w
1323
1324SV *ev_async_async_pending (ev_async *w)
1325 CODE:
1326 RETVAL = boolSV (ev_async_pending (w));
1327 OUTPUT:
1328 RETVAL
1329
1330#ifndef EV_NO_LOOP
1331
1332MODULE = EV PACKAGE = EV::Loop PREFIX = ev_
1333
1334SV *new (SV *klass, unsigned int flags = 0)
1335 CODE:
1336{
1337 struct ev_loop *loop = ev_loop_new (flags);
1338
1339 if (!loop)
1340 XSRETURN_UNDEF;
1341
1342 RETVAL = sv_bless (newRV_noinc (newSViv (PTR2IV (loop))), stash_loop);
1343}
1344 OUTPUT:
1345 RETVAL
1346
1347void DESTROY (struct ev_loop *loop)
1348 CODE:
1349 /* 1. the default loop shouldn't be freed by destroying it'S pelr loop object */
1350 /* 2. not doing so helps avoid many global destruction bugs in perl, too */
1351 if (loop != evapi.default_loop)
1352 ev_loop_destroy (loop);
1353
1354void ev_loop_fork (struct ev_loop *loop)
1355
1356NV ev_now (struct ev_loop *loop)
1357
1358void ev_now_update (struct ev_loop *loop)
1359
1360void ev_suspend (struct ev_loop *loop)
1361
1362void ev_resume (struct ev_loop *loop)
1363
1364void ev_set_io_collect_interval (struct ev_loop *loop, NV interval)
1365
1366void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval)
1367
1368unsigned int ev_backend (struct ev_loop *loop)
1369
1370void ev_verify (struct ev_loop *loop)
1371 ALIAS:
1372 loop_verify = 1
1373
1374unsigned int ev_iteration (struct ev_loop *loop)
1375 ALIAS:
1376 loop_count = 1
1377
1378unsigned int ev_depth (struct ev_loop *loop)
1379 ALIAS:
1380 loop_depth = 1
1381
1382void ev_run (struct ev_loop *loop, int flags = 0)
1383 ALIAS:
1384 loop = 1
1385
1386void ev_break (struct ev_loop *loop, int how = 1)
1387 ALIAS:
1388 unloop = 1
1389
1390void ev_feed_fd_event (struct ev_loop *loop, int fd, int revents = EV_NONE)
1391
1392unsigned int ev_pending_count (struct ev_loop *loop)
1393
1394void ev_invoke_pending (struct ev_loop *loop)
528 1395
529#if 0 1396#if 0
530 1397
531MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 1398void ev_feed_signal_event (struct ev_loop *loop, SV *signal)
532 1399 CODE:
533BOOT:
534{ 1400{
535 HV *stash = gv_stashpv ("EV::DNS", 1); 1401 Signal signum = s_signum (signal);
1402 CHECK_SIG (signal, signum);
536 1403
537 static const struct { 1404 ev_feed_signal_event (loop, signum);
538 const char *name;
539 IV iv;
540 } *civ, const_iv[] = {
541# define const_iv(pfx, name) { # name, (IV) pfx ## name },
542 const_iv (DNS_, ERR_NONE)
543 const_iv (DNS_, ERR_FORMAT)
544 const_iv (DNS_, ERR_SERVERFAILED)
545 const_iv (DNS_, ERR_NOTEXIST)
546 const_iv (DNS_, ERR_NOTIMPL)
547 const_iv (DNS_, ERR_REFUSED)
548 const_iv (DNS_, ERR_TRUNCATED)
549 const_iv (DNS_, ERR_UNKNOWN)
550 const_iv (DNS_, ERR_TIMEOUT)
551 const_iv (DNS_, ERR_SHUTDOWN)
552 const_iv (DNS_, IPv4_A)
553 const_iv (DNS_, PTR)
554 const_iv (DNS_, IPv6_AAAA)
555 const_iv (DNS_, QUERY_NO_SEARCH)
556 const_iv (DNS_, OPTION_SEARCH)
557 const_iv (DNS_, OPTION_NAMESERVERS)
558 const_iv (DNS_, OPTION_MISC)
559 const_iv (DNS_, OPTIONS_ALL)
560 const_iv (DNS_, NO_SEARCH)
561 };
562
563 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
564 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
565} 1405}
566
567int evdns_init ()
568
569void evdns_shutdown (int fail_requests = 1)
570
571const char *evdns_err_to_string (int err)
572
573int evdns_nameserver_add (U32 address)
574
575int evdns_count_nameservers ()
576
577int evdns_clear_nameservers_and_suspend ()
578
579int evdns_resume ()
580
581int evdns_nameserver_ip_add (char *ip_as_string)
582
583int evdns_resolve_ipv4 (const char *name, int flags, SV *cb)
584 C_ARGS: name, flags, dns_cb, (void *)SvREFCNT_inc (cb)
585
586int evdns_resolve_ipv6 (const char *name, int flags, SV *cb)
587 C_ARGS: name, flags, dns_cb, (void *)SvREFCNT_inc (cb)
588
589int evdns_resolve_reverse (SV *addr, int flags, SV *cb)
590 ALIAS:
591 evdns_resolve_reverse_ipv6 = 1
592 CODE:
593{
594 STRLEN len;
595 char *data = SvPVbyte (addr, len);
596 if (len != (ix ? 16 : 4))
597 croak ("ipv4/ipv6 address to be resolved must be given as 4/16 byte octet string");
598
599 RETVAL = ix
600 ? evdns_resolve_reverse_ipv6 ((struct in6_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb))
601 : evdns_resolve_reverse ((struct in_addr *)data, flags, dns_cb, (void *)SvREFCNT_inc (cb));
602}
603 OUTPUT:
604 RETVAL
605
606int evdns_set_option (char *option, char *val, int flags)
607
608int evdns_resolv_conf_parse (int flags, const char *filename)
609
610#ifdef MS_WINDOWS
611
612int evdns_config_windows_nameservers ()
613 1406
614#endif 1407#endif
615 1408
616void evdns_search_clear () 1409ev_io *io (struct ev_loop *loop, SV *fh, int events, SV *cb)
617 1410 ALIAS:
618void evdns_search_add (char *domain) 1411 io_ns = 1
619 1412 CODE:
620void evdns_search_ndots_set (int ndots)
621
622
623MODULE = EV PACKAGE = EV::HTTP PREFIX = evhttp_
624
625BOOT:
626{ 1413{
627 HV *stash = gv_stashpv ("EV::HTTP", 1); 1414 int fd = s_fileno (fh, events & EV_WRITE);
1415 CHECK_FD (fh, fd);
628 1416
629 static const struct { 1417 RETVAL = e_new (sizeof (ev_io), cb, ST (0));
630 const char *name; 1418 e_fh (RETVAL) = newSVsv (fh);
631 IV iv; 1419 ev_io_set (RETVAL, fd, events);
632 } *civ, const_iv[] = { 1420 if (!ix) START (io, RETVAL);
633# define const_iv(pfx, name) { # name, (IV) pfx ## name },
634 const_iv (HTTP_, OK)
635 const_iv (HTTP_, NOCONTENT)
636 const_iv (HTTP_, MOVEPERM)
637 const_iv (HTTP_, MOVETEMP)
638 const_iv (HTTP_, NOTMODIFIED)
639 const_iv (HTTP_, BADREQUEST)
640 const_iv (HTTP_, NOTFOUND)
641 const_iv (HTTP_, SERVUNAVAIL)
642 const_iv (EVHTTP_, REQ_OWN_CONNECTION)
643 const_iv (EVHTTP_, PROXY_REQUEST)
644 const_iv (EVHTTP_, REQ_GET)
645 const_iv (EVHTTP_, REQ_POST)
646 const_iv (EVHTTP_, REQ_HEAD)
647 const_iv (EVHTTP_, REQUEST)
648 const_iv (EVHTTP_, RESPONSE)
649 };
650
651 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
652 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
653} 1421}
1422 OUTPUT:
1423 RETVAL
654 1424
655MODULE = EV PACKAGE = EV::HTTP::Request PREFIX = evhttp_request_ 1425ev_timer *timer (struct ev_loop *loop, NV after, NV repeat, SV *cb)
1426 ALIAS:
1427 timer_ns = 1
1428 INIT:
1429 CHECK_REPEAT (repeat);
1430 CODE:
1431 RETVAL = e_new (sizeof (ev_timer), cb, ST (0));
1432 ev_timer_set (RETVAL, after, repeat);
1433 if (!ix) START (timer, RETVAL);
1434 OUTPUT:
1435 RETVAL
656 1436
657#HttpRequest new (SV *klass, SV *cb) 1437SV *periodic (struct ev_loop *loop, NV at, NV interval, SV *reschedule_cb, SV *cb)
1438 ALIAS:
1439 periodic_ns = 1
1440 INIT:
1441 CHECK_REPEAT (interval);
1442 CODE:
1443{
1444 ev_periodic *w;
1445 w = e_new (sizeof (ev_periodic), cb, ST (0));
1446 e_fh (w) = SvTRUE (reschedule_cb) ? newSVsv (reschedule_cb) : 0;
1447 ev_periodic_set (w, at, interval, e_fh (w) ? e_periodic_cb : 0);
1448 RETVAL = e_bless ((ev_watcher *)w, stash_periodic);
1449 if (!ix) START (periodic, w);
1450}
1451 OUTPUT:
1452 RETVAL
658 1453
659#void DESTROY (struct evhttp_request *req); 1454ev_signal *signal (struct ev_loop *loop, SV *signal, SV *cb)
1455 ALIAS:
1456 signal_ns = 1
1457 CODE:
1458{
1459 Signal signum = s_signum (signal);
1460 CHECK_SIG (signal, signum);
660 1461
1462 RETVAL = e_new (sizeof (ev_signal), cb, ST (0));
1463 ev_signal_set (RETVAL, signum);
1464 if (!ix) START_SIGNAL (RETVAL);
1465}
1466 OUTPUT:
1467 RETVAL
1468
1469ev_idle *idle (struct ev_loop *loop, SV *cb)
1470 ALIAS:
1471 idle_ns = 1
1472 CODE:
1473 RETVAL = e_new (sizeof (ev_idle), cb, ST (0));
1474 ev_idle_set (RETVAL);
1475 if (!ix) START (idle, RETVAL);
1476 OUTPUT:
1477 RETVAL
1478
1479ev_prepare *prepare (struct ev_loop *loop, SV *cb)
1480 ALIAS:
1481 prepare_ns = 1
1482 CODE:
1483 RETVAL = e_new (sizeof (ev_prepare), cb, ST (0));
1484 ev_prepare_set (RETVAL);
1485 if (!ix) START (prepare, RETVAL);
1486 OUTPUT:
1487 RETVAL
1488
1489ev_check *check (struct ev_loop *loop, SV *cb)
1490 ALIAS:
1491 check_ns = 1
1492 CODE:
1493 RETVAL = e_new (sizeof (ev_check), cb, ST (0));
1494 ev_check_set (RETVAL);
1495 if (!ix) START (check, RETVAL);
1496 OUTPUT:
1497 RETVAL
1498
1499ev_fork *fork (struct ev_loop *loop, SV *cb)
1500 ALIAS:
1501 fork_ns = 1
1502 CODE:
1503 RETVAL = e_new (sizeof (ev_fork), cb, ST (0));
1504 ev_fork_set (RETVAL);
1505 if (!ix) START (fork, RETVAL);
1506 OUTPUT:
1507 RETVAL
1508
1509ev_cleanup *cleanup (struct ev_loop *loop, SV *cb)
1510 ALIAS:
1511 cleanup_ns = 1
1512 CODE:
1513 RETVAL = e_new (sizeof (ev_cleanup), cb, ST (0));
1514 ev_cleanup_set (RETVAL);
1515 if (!ix) START (cleanup, RETVAL);
1516 OUTPUT:
1517 RETVAL
1518
1519ev_child *child (struct ev_loop *loop, int pid, int trace, SV *cb)
1520 ALIAS:
1521 child_ns = 1
1522 CODE:
1523#if EV_CHILD_ENABLE
1524 RETVAL = e_new (sizeof (ev_child), cb, ST (0));
1525 ev_child_set (RETVAL, pid, trace);
1526 if (!ix) START (child, RETVAL);
1527#else
1528 croak ("EV::child watchers not supported on this platform");
661#endif 1529#endif
1530 OUTPUT:
1531 RETVAL
662 1532
1533ev_stat *stat (struct ev_loop *loop, SV *path, NV interval, SV *cb)
1534 ALIAS:
1535 stat_ns = 1
1536 CODE:
1537 RETVAL = e_new (sizeof (ev_stat), cb, ST (0));
1538 e_fh (RETVAL) = newSVsv (path);
1539 ev_stat_set (RETVAL, SvPVbyte_nolen (e_fh (RETVAL)), interval);
1540 if (!ix) START (stat, RETVAL);
1541 OUTPUT:
1542 RETVAL
663 1543
1544ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = 0)
1545 ALIAS:
1546 embed_ns = 1
1547 CODE:
1548{
1549 if (!(ev_backend (other) & ev_embeddable_backends ()))
1550 croak ("passed loop is not embeddable via EV::embed,");
664 1551
1552 RETVAL = e_new (sizeof (ev_embed), cb, ST (0));
1553 e_fh (RETVAL) = newSVsv (ST (1));
1554 ev_embed_set (RETVAL, other);
1555 if (!ix) START (embed, RETVAL);
1556}
1557 OUTPUT:
1558 RETVAL
665 1559
1560ev_async *async (struct ev_loop *loop, SV *cb)
1561 ALIAS:
1562 async_ns = 1
1563 CODE:
1564 RETVAL = e_new (sizeof (ev_async), cb, ST (0));
1565 ev_async_set (RETVAL);
1566 if (!ix) START (async, RETVAL);
1567 OUTPUT:
1568 RETVAL
666 1569
1570void once (struct ev_loop *loop, SV *fh, int events, SV *timeout, SV *cb)
1571 CODE:
1572 ev_once (
1573 loop,
1574 s_fileno (fh, events & EV_WRITE), events,
1575 SvOK (timeout) ? SvNV (timeout) : -1.,
1576 e_once_cb,
1577 newSVsv (cb)
1578 );
667 1579
1580#endif
668 1581
669

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines