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

Comparing EV/EV.xs (file contents):
Revision 1.15 by root, Wed Oct 31 10:50:05 2007 UTC vs.
Revision 1.25 by root, Wed Oct 31 20:19:20 2007 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>
6#include <netinet/in.h> 5/*#include <netinet/in.h>*/
7 6
8#define TIMEOUT_NONE HUGE_VAL 7#define TIMEOUT_NONE HUGE_VAL
8#define HAVE_EPOLL 1
9 9
10#define EV_COMMON \ 10#define EV_PROTOTYPES 1
11 SV *self; /* contains this struct */ \
12 SV *cb_sv, *fh;
13
14#include "EV/EVAPI.h" 11#include "EV/EVAPI.h"
12
13#include "libev/ev.c"
15 14
16typedef int Signal; 15typedef int Signal;
17 16
18static struct EVAPI evapi; 17static struct EVAPI evapi;
19 18
23 *stash_time, 22 *stash_time,
24 *stash_timer, 23 *stash_timer,
25 *stash_periodic, 24 *stash_periodic,
26 *stash_signal, 25 *stash_signal,
27 *stash_idle, 26 *stash_idle,
27 *stash_prepare,
28 *stash_check; 28 *stash_check,
29 *stash_child;
29 30
30static int 31static int
31sv_signum (SV *sig) 32sv_signum (SV *sig)
32{ 33{
33 int signum; 34 int signum;
38 for (signum = 1; signum < SIG_SIZE; ++signum) 39 for (signum = 1; signum < SIG_SIZE; ++signum)
39 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) 40 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
40 return signum; 41 return signum;
41 42
42 return -1; 43 return -1;
43}
44
45static void
46api_once (int fd, short events, double timeout, void (*cb)(int, short, void *), void *arg)
47{
48#if 0
49 if (timeout >= 0.)
50 {
51 struct timeval tv;
52 tv_set (&tv, timeout);
53 event_once (fd, events, cb, arg, &tv);
54 }
55 else
56 event_once (fd, events, cb, arg, 0);
57#endif
58} 44}
59 45
60///////////////////////////////////////////////////////////////////////////// 46/////////////////////////////////////////////////////////////////////////////
61// Event 47// Event
62 48
118static void 104static void
119e_cb (struct ev_watcher *w, int revents) 105e_cb (struct ev_watcher *w, int revents)
120{ 106{
121 dSP; 107 dSP;
122 I32 mark = SP - PL_stack_base; 108 I32 mark = SP - PL_stack_base;
123 SV *sv_self, *sv_events; 109 SV *sv_self, *sv_events, *sv_status = 0;
124 static SV *sv_events_cache; 110 static SV *sv_events_cache;
125 111
126 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ 112 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
127 113
128 if (sv_events_cache) 114 if (sv_events_cache)
135 121
136 PUSHMARK (SP); 122 PUSHMARK (SP);
137 EXTEND (SP, 2); 123 EXTEND (SP, 2);
138 PUSHs (sv_self); 124 PUSHs (sv_self);
139 PUSHs (sv_events); 125 PUSHs (sv_events);
126
127 if (revents & EV_CHILD)
128 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
129
140 PUTBACK; 130 PUTBACK;
141 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 131 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
142 SP = PL_stack_base + mark; PUTBACK; 132 SP = PL_stack_base + mark; PUTBACK;
143 133
144 SvREFCNT_dec (sv_self); 134 SvREFCNT_dec (sv_self);
135 SvREFCNT_dec (sv_status);
145 136
146 if (sv_events_cache) 137 if (sv_events_cache)
147 SvREFCNT_dec (sv_events); 138 SvREFCNT_dec (sv_events);
148 else 139 else
149 sv_events_cache = sv_events; 140 sv_events_cache = sv_events;
209 200
210 LEAVE; 201 LEAVE;
211} 202}
212#endif 203#endif
213 204
205#define CHECK_REPEAT(repeat) if (repeat < 0.) \
206 croak (# repeat " value must be >= 0");
207
214///////////////////////////////////////////////////////////////////////////// 208/////////////////////////////////////////////////////////////////////////////
215// XS interface functions 209// XS interface functions
216 210
217MODULE = EV PACKAGE = EV PREFIX = ev_ 211MODULE = EV PACKAGE = EV PREFIX = ev_
212
213PROTOTYPES: ENABLE
218 214
219BOOT: 215BOOT:
220{ 216{
221 int i; 217 int i;
222 HV *stash = gv_stashpv ("EV", 1); 218 HV *stash = gv_stashpv ("EV", 1);
224 static const struct { 220 static const struct {
225 const char *name; 221 const char *name;
226 IV iv; 222 IV iv;
227 } *civ, const_iv[] = { 223 } *civ, const_iv[] = {
228# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 224# define const_iv(pfx, name) { # name, (IV) pfx ## name },
225 const_iv (EV_, UNDEF)
229 const_iv (EV_, NONE) 226 const_iv (EV_, NONE)
230 const_iv (EV_, TIMEOUT) 227 const_iv (EV_, TIMEOUT)
231 const_iv (EV_, READ) 228 const_iv (EV_, READ)
232 const_iv (EV_, WRITE) 229 const_iv (EV_, WRITE)
233 const_iv (EV_, SIGNAL) 230 const_iv (EV_, SIGNAL)
234 const_iv (EV_, IDLE) 231 const_iv (EV_, IDLE)
235 const_iv (EV_, CHECK) 232 const_iv (EV_, CHECK)
233 const_iv (EV_, ERROR)
236 234
237 const_iv (EV, LOOP_ONESHOT) 235 const_iv (EV, LOOP_ONESHOT)
238 const_iv (EV, LOOP_NONBLOCK) 236 const_iv (EV, LOOP_NONBLOCK)
239 237
240 const_iv (EV, METHOD_NONE) 238 const_iv (EV, METHOD_NONE)
250 stash_time = gv_stashpv ("EV::Time" , 1); 248 stash_time = gv_stashpv ("EV::Time" , 1);
251 stash_timer = gv_stashpv ("EV::Timer" , 1); 249 stash_timer = gv_stashpv ("EV::Timer" , 1);
252 stash_periodic = gv_stashpv ("EV::Periodic", 1); 250 stash_periodic = gv_stashpv ("EV::Periodic", 1);
253 stash_signal = gv_stashpv ("EV::Signal" , 1); 251 stash_signal = gv_stashpv ("EV::Signal" , 1);
254 stash_idle = gv_stashpv ("EV::Idle" , 1); 252 stash_idle = gv_stashpv ("EV::Idle" , 1);
253 stash_prepare = gv_stashpv ("EV::Prepare" , 1);
255 stash_check = gv_stashpv ("EV::Check" , 1); 254 stash_check = gv_stashpv ("EV::Check" , 1);
255 stash_child = gv_stashpv ("EV::Child" , 1);
256 256
257 { 257 {
258 SV *sv = perl_get_sv ("EV::API", TRUE); 258 SV *sv = perl_get_sv ("EV::API", TRUE);
259 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ 259 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
260 260
261 /* the poor man's shared library emulator */ 261 /* the poor man's shared library emulator */
262 evapi.ver = EV_API_VERSION; 262 evapi.ver = EV_API_VERSION;
263 evapi.rev = EV_API_REVISION; 263 evapi.rev = EV_API_REVISION;
264 evapi.once = api_once;
265 evapi.sv_fileno = sv_fileno; 264 evapi.sv_fileno = sv_fileno;
266 evapi.sv_signum = sv_signum; 265 evapi.sv_signum = sv_signum;
267 evapi.now = &ev_now; 266 evapi.now = &ev_now;
268 evapi.method = &ev_method; 267 evapi.method = &ev_method;
269 evapi.loop_done = &ev_loop_done; 268 evapi.loop_done = &ev_loop_done;
270 evapi.time = ev_time; 269 evapi.time = ev_time;
271 evapi.loop = ev_loop; 270 evapi.loop = ev_loop;
271 evapi.once = ev_once;
272 evapi.io_start = evio_start; 272 evapi.io_start = evio_start;
273 evapi.io_stop = evio_stop; 273 evapi.io_stop = evio_stop;
274 evapi.timer_start = evtimer_start; 274 evapi.timer_start = evtimer_start;
275 evapi.timer_stop = evtimer_stop; 275 evapi.timer_stop = evtimer_stop;
276 evapi.timer_again = evtimer_again;
276 evapi.periodic_start = evperiodic_start; 277 evapi.periodic_start = evperiodic_start;
277 evapi.periodic_stop = evperiodic_stop; 278 evapi.periodic_stop = evperiodic_stop;
278 evapi.signal_start = evsignal_start; 279 evapi.signal_start = evsignal_start;
279 evapi.signal_stop = evsignal_stop; 280 evapi.signal_stop = evsignal_stop;
280 evapi.idle_start = evidle_start; 281 evapi.idle_start = evidle_start;
281 evapi.idle_stop = evidle_stop; 282 evapi.idle_stop = evidle_stop;
283 evapi.prepare_start = evprepare_start;
284 evapi.prepare_stop = evprepare_stop;
282 evapi.check_start = evcheck_start; 285 evapi.check_start = evcheck_start;
283 evapi.check_stop = evcheck_stop; 286 evapi.check_stop = evcheck_stop;
287 evapi.child_start = evchild_start;
288 evapi.child_stop = evchild_stop;
284 289
285 sv_setiv (sv, (IV)&evapi); 290 sv_setiv (sv, (IV)&evapi);
286 SvREADONLY_on (sv); 291 SvREADONLY_on (sv);
287 } 292 }
288} 293}
321 RETVAL 326 RETVAL
322 327
323struct ev_timer *timer (NV after, NV repeat, SV *cb) 328struct ev_timer *timer (NV after, NV repeat, SV *cb)
324 ALIAS: 329 ALIAS:
325 timer_ns = 1 330 timer_ns = 1
331 INIT:
332 CHECK_REPEAT (repeat);
326 CODE: 333 CODE:
327 RETVAL = e_new (sizeof (struct ev_timer), cb); 334 RETVAL = e_new (sizeof (struct ev_timer), cb);
328 evtimer_set (RETVAL, after, repeat); 335 evtimer_set (RETVAL, after, repeat);
329 if (!ix) evtimer_start (RETVAL); 336 if (!ix) evtimer_start (RETVAL);
330 OUTPUT: 337 OUTPUT:
331 RETVAL 338 RETVAL
332 339
333struct ev_periodic *periodic (NV at, NV interval, SV *cb) 340struct ev_periodic *periodic (NV at, NV interval, SV *cb)
334 ALIAS: 341 ALIAS:
335 periodic_ns = 1 342 periodic_ns = 1
343 INIT:
344 CHECK_REPEAT (interval);
336 CODE: 345 CODE:
337 RETVAL = e_new (sizeof (struct ev_periodic), cb); 346 RETVAL = e_new (sizeof (struct ev_periodic), cb);
338 evperiodic_set (RETVAL, at, interval); 347 evperiodic_set (RETVAL, at, interval);
339 if (!ix) evperiodic_start (RETVAL); 348 if (!ix) evperiodic_start (RETVAL);
340 OUTPUT: 349 OUTPUT:
358 evidle_set (RETVAL); 367 evidle_set (RETVAL);
359 if (!ix) evidle_start (RETVAL); 368 if (!ix) evidle_start (RETVAL);
360 OUTPUT: 369 OUTPUT:
361 RETVAL 370 RETVAL
362 371
372struct ev_prepare *prepare (SV *cb)
373 ALIAS:
374 prepare_ns = 1
375 CODE:
376 RETVAL = e_new (sizeof (struct ev_prepare), cb);
377 evprepare_set (RETVAL);
378 if (!ix) evprepare_start (RETVAL);
379 OUTPUT:
380 RETVAL
381
363struct ev_check *check (SV *cb) 382struct ev_check *check (SV *cb)
364 ALIAS: 383 ALIAS:
365 check_ns = 1 384 check_ns = 1
366 CODE: 385 CODE:
367 RETVAL = e_new (sizeof (struct ev_check), cb); 386 RETVAL = e_new (sizeof (struct ev_check), cb);
368 evcheck_set (RETVAL); 387 evcheck_set (RETVAL);
369 if (!ix) evcheck_start (RETVAL); 388 if (!ix) evcheck_start (RETVAL);
389 OUTPUT:
390 RETVAL
391
392struct ev_child *child (int pid, SV *cb)
393 ALIAS:
394 check_ns = 1
395 CODE:
396 RETVAL = e_new (sizeof (struct ev_check), cb);
397 evchild_set (RETVAL, pid);
398 if (!ix) evchild_start (RETVAL);
370 OUTPUT: 399 OUTPUT:
371 RETVAL 400 RETVAL
372 401
373 402
374PROTOTYPES: DISABLE 403PROTOTYPES: DISABLE
463MODULE = EV PACKAGE = EV::Time 492MODULE = EV PACKAGE = EV::Time
464 493
465MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ 494MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_
466 495
467void evtimer_start (struct ev_timer *w) 496void evtimer_start (struct ev_timer *w)
497 INIT:
498 CHECK_REPEAT (w->repeat);
468 499
469void evtimer_stop (struct ev_timer *w) 500void evtimer_stop (struct ev_timer *w)
470 501
502void evtimer_again (struct ev_timer *w)
503 INIT:
504 CHECK_REPEAT (w->repeat);
505
471void set (struct ev_timer *w, NV after, NV repeat = 0.) 506void set (struct ev_timer *w, NV after, NV repeat = 0.)
507 INIT:
508 CHECK_REPEAT (repeat);
472 CODE: 509 CODE:
473{ 510{
474 int active = w->active; 511 int active = w->active;
475 if (active) evtimer_stop (w); 512 if (active) evtimer_stop (w);
476 evtimer_set (w, after, repeat); 513 evtimer_set (w, after, repeat);
478} 515}
479 516
480MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ 517MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_
481 518
482void evperiodic_start (struct ev_periodic *w) 519void evperiodic_start (struct ev_periodic *w)
520 INIT:
521 CHECK_REPEAT (w->interval);
483 522
484void evperiodic_stop (struct ev_periodic *w) 523void evperiodic_stop (struct ev_periodic *w)
485 524
486void set (struct ev_periodic *w, NV at, NV interval = 0.) 525void set (struct ev_periodic *w, NV at, NV interval = 0.)
526 INIT:
527 CHECK_REPEAT (interval);
487 CODE: 528 CODE:
488{ 529{
489 int active = w->active; 530 int active = w->active;
490 if (active) evperiodic_stop (w); 531 if (active) evperiodic_stop (w);
491 evperiodic_set (w, at, interval); 532 evperiodic_set (w, at, interval);
496 537
497void evidle_start (struct ev_idle *w) 538void evidle_start (struct ev_idle *w)
498 539
499void evidle_stop (struct ev_idle *w) 540void evidle_stop (struct ev_idle *w)
500 541
542MODULE = EV PACKAGE = EV::Prepare PREFIX = evcheck_
543
544void evprepare_start (struct ev_prepare *w)
545
546void evprepare_stop (struct ev_prepare *w)
547
501MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ 548MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_
502 549
503void evcheck_start (struct ev_check *w) 550void evcheck_start (struct ev_check *w)
504 551
505void evcheck_stop (struct ev_check *w) 552void evcheck_stop (struct ev_check *w)
553
554MODULE = EV PACKAGE = EV::Child PREFIX = evchild_
555
556void evchild_start (struct ev_child *w)
557
558void evchild_stop (struct ev_child *w)
559
560void set (struct ev_child *w, int pid)
561 CODE:
562{
563 int active = w->active;
564 if (active) evchild_stop (w);
565 evchild_set (w, pid);
566 if (active) evchild_start (w);
567}
568
569int status (struct ev_child *w)
570 CODE:
571 RETVAL = w->status;
572 OUTPUT:
573 RETVAL
506 574
507#if 0 575#if 0
508 576
509MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 577MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
510 578

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines