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

Comparing EV/EV.xs (file contents):
Revision 1.19 by root, Wed Oct 31 11:59:54 2007 UTC vs.
Revision 1.27 by root, Wed Oct 31 21:36:01 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"
15 12
16#include "libev/ev.c" 13#include "libev/ev.c"
17 14
18typedef int Signal; 15typedef int Signal;
25 *stash_time, 22 *stash_time,
26 *stash_timer, 23 *stash_timer,
27 *stash_periodic, 24 *stash_periodic,
28 *stash_signal, 25 *stash_signal,
29 *stash_idle, 26 *stash_idle,
27 *stash_prepare,
30 *stash_check; 28 *stash_check,
29 *stash_child;
31 30
32static int 31static int
33sv_signum (SV *sig) 32sv_signum (SV *sig)
34{ 33{
35 int signum; 34 int signum;
40 for (signum = 1; signum < SIG_SIZE; ++signum) 39 for (signum = 1; signum < SIG_SIZE; ++signum)
41 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) 40 if (strEQ (SvPV_nolen (sig), PL_sig_name [signum]))
42 return signum; 41 return signum;
43 42
44 return -1; 43 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} 44}
61 45
62///////////////////////////////////////////////////////////////////////////// 46/////////////////////////////////////////////////////////////////////////////
63// Event 47// Event
64 48
120static void 104static void
121e_cb (struct ev_watcher *w, int revents) 105e_cb (struct ev_watcher *w, int revents)
122{ 106{
123 dSP; 107 dSP;
124 I32 mark = SP - PL_stack_base; 108 I32 mark = SP - PL_stack_base;
125 SV *sv_self, *sv_events; 109 SV *sv_self, *sv_events, *sv_status = 0;
126 static SV *sv_events_cache; 110 static SV *sv_events_cache;
127 111
128 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 */
129 113
130 if (sv_events_cache) 114 if (sv_events_cache)
137 121
138 PUSHMARK (SP); 122 PUSHMARK (SP);
139 EXTEND (SP, 2); 123 EXTEND (SP, 2);
140 PUSHs (sv_self); 124 PUSHs (sv_self);
141 PUSHs (sv_events); 125 PUSHs (sv_events);
126
127 if (revents & EV_CHILD)
128 XPUSHs (sv_status = newSViv (((struct ev_child *)w)->status));
129
142 PUTBACK; 130 PUTBACK;
143 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 131 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
144 SP = PL_stack_base + mark; PUTBACK; 132 SP = PL_stack_base + mark; PUTBACK;
145 133
146 SvREFCNT_dec (sv_self); 134 SvREFCNT_dec (sv_self);
135 SvREFCNT_dec (sv_status);
147 136
148 if (sv_events_cache) 137 if (sv_events_cache)
149 SvREFCNT_dec (sv_events); 138 SvREFCNT_dec (sv_events);
150 else 139 else
151 sv_events_cache = sv_events; 140 sv_events_cache = sv_events;
219///////////////////////////////////////////////////////////////////////////// 208/////////////////////////////////////////////////////////////////////////////
220// XS interface functions 209// XS interface functions
221 210
222MODULE = EV PACKAGE = EV PREFIX = ev_ 211MODULE = EV PACKAGE = EV PREFIX = ev_
223 212
213PROTOTYPES: ENABLE
214
224BOOT: 215BOOT:
225{ 216{
226 int i; 217 int i;
227 HV *stash = gv_stashpv ("EV", 1); 218 HV *stash = gv_stashpv ("EV", 1);
228 219
229 static const struct { 220 static const struct {
230 const char *name; 221 const char *name;
231 IV iv; 222 IV iv;
232 } *civ, const_iv[] = { 223 } *civ, const_iv[] = {
233# define const_iv(pfx, name) { # name, (IV) pfx ## name }, 224# define const_iv(pfx, name) { # name, (IV) pfx ## name },
225 const_iv (EV_, UNDEF)
234 const_iv (EV_, NONE) 226 const_iv (EV_, NONE)
235 const_iv (EV_, TIMEOUT) 227 const_iv (EV_, TIMEOUT)
236 const_iv (EV_, READ) 228 const_iv (EV_, READ)
237 const_iv (EV_, WRITE) 229 const_iv (EV_, WRITE)
238 const_iv (EV_, SIGNAL) 230 const_iv (EV_, SIGNAL)
239 const_iv (EV_, IDLE) 231 const_iv (EV_, IDLE)
240 const_iv (EV_, CHECK) 232 const_iv (EV_, CHECK)
233 const_iv (EV_, ERROR)
241 234
242 const_iv (EV, LOOP_ONESHOT) 235 const_iv (EV, LOOP_ONESHOT)
243 const_iv (EV, LOOP_NONBLOCK) 236 const_iv (EV, LOOP_NONBLOCK)
244 237
245 const_iv (EV, METHOD_NONE) 238 const_iv (EV, METHOD_NONE)
255 stash_time = gv_stashpv ("EV::Time" , 1); 248 stash_time = gv_stashpv ("EV::Time" , 1);
256 stash_timer = gv_stashpv ("EV::Timer" , 1); 249 stash_timer = gv_stashpv ("EV::Timer" , 1);
257 stash_periodic = gv_stashpv ("EV::Periodic", 1); 250 stash_periodic = gv_stashpv ("EV::Periodic", 1);
258 stash_signal = gv_stashpv ("EV::Signal" , 1); 251 stash_signal = gv_stashpv ("EV::Signal" , 1);
259 stash_idle = gv_stashpv ("EV::Idle" , 1); 252 stash_idle = gv_stashpv ("EV::Idle" , 1);
253 stash_prepare = gv_stashpv ("EV::Prepare" , 1);
260 stash_check = gv_stashpv ("EV::Check" , 1); 254 stash_check = gv_stashpv ("EV::Check" , 1);
255 stash_child = gv_stashpv ("EV::Child" , 1);
261 256
262 { 257 {
263 SV *sv = perl_get_sv ("EV::API", TRUE); 258 SV *sv = perl_get_sv ("EV::API", TRUE);
264 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ 259 perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */
265 260
266 /* the poor man's shared library emulator */ 261 /* the poor man's shared library emulator */
267 evapi.ver = EV_API_VERSION; 262 evapi.ver = EV_API_VERSION;
268 evapi.rev = EV_API_REVISION; 263 evapi.rev = EV_API_REVISION;
269 evapi.once = api_once;
270 evapi.sv_fileno = sv_fileno; 264 evapi.sv_fileno = sv_fileno;
271 evapi.sv_signum = sv_signum; 265 evapi.sv_signum = sv_signum;
272 evapi.now = &ev_now; 266 evapi.now = &ev_now;
273 evapi.method = &ev_method; 267 evapi.method = &ev_method;
274 evapi.loop_done = &ev_loop_done; 268 evapi.loop_done = &ev_loop_done;
275 evapi.time = ev_time; 269 evapi.time = ev_time;
276 evapi.loop = ev_loop; 270 evapi.loop = ev_loop;
271 evapi.once = ev_once;
277 evapi.io_start = evio_start; 272 evapi.io_start = evio_start;
278 evapi.io_stop = evio_stop; 273 evapi.io_stop = evio_stop;
279 evapi.timer_start = evtimer_start; 274 evapi.timer_start = evtimer_start;
280 evapi.timer_stop = evtimer_stop; 275 evapi.timer_stop = evtimer_stop;
281 evapi.timer_again = evtimer_again; 276 evapi.timer_again = evtimer_again;
283 evapi.periodic_stop = evperiodic_stop; 278 evapi.periodic_stop = evperiodic_stop;
284 evapi.signal_start = evsignal_start; 279 evapi.signal_start = evsignal_start;
285 evapi.signal_stop = evsignal_stop; 280 evapi.signal_stop = evsignal_stop;
286 evapi.idle_start = evidle_start; 281 evapi.idle_start = evidle_start;
287 evapi.idle_stop = evidle_stop; 282 evapi.idle_stop = evidle_stop;
283 evapi.prepare_start = evprepare_start;
284 evapi.prepare_stop = evprepare_stop;
288 evapi.check_start = evcheck_start; 285 evapi.check_start = evcheck_start;
289 evapi.check_stop = evcheck_stop; 286 evapi.check_stop = evcheck_stop;
287 evapi.child_start = evchild_start;
288 evapi.child_stop = evchild_stop;
290 289
291 sv_setiv (sv, (IV)&evapi); 290 sv_setiv (sv, (IV)&evapi);
292 SvREADONLY_on (sv); 291 SvREADONLY_on (sv);
293 } 292 }
294} 293}
314void ev_loop_done (int value = 1) 313void ev_loop_done (int value = 1)
315 CODE: 314 CODE:
316 ev_loop_done = value; 315 ev_loop_done = value;
317 316
318struct ev_io *io (SV *fh, int events, SV *cb) 317struct ev_io *io (SV *fh, int events, SV *cb)
319 PROTOTYPE: $$&
320 ALIAS: 318 ALIAS:
321 io_ns = 1 319 io_ns = 1
322 CODE: 320 CODE:
323 RETVAL = e_new (sizeof (struct ev_io), cb); 321 RETVAL = e_new (sizeof (struct ev_io), cb);
324 RETVAL->fh = newSVsv (fh); 322 RETVAL->fh = newSVsv (fh);
326 if (!ix) evio_start (RETVAL); 324 if (!ix) evio_start (RETVAL);
327 OUTPUT: 325 OUTPUT:
328 RETVAL 326 RETVAL
329 327
330struct ev_timer *timer (NV after, NV repeat, SV *cb) 328struct ev_timer *timer (NV after, NV repeat, SV *cb)
331 PROTOTYPE: $$&
332 ALIAS: 329 ALIAS:
333 timer_ns = 1 330 timer_ns = 1
334 INIT: 331 INIT:
335 CHECK_REPEAT (repeat); 332 CHECK_REPEAT (repeat);
336 CODE: 333 CODE:
339 if (!ix) evtimer_start (RETVAL); 336 if (!ix) evtimer_start (RETVAL);
340 OUTPUT: 337 OUTPUT:
341 RETVAL 338 RETVAL
342 339
343struct ev_periodic *periodic (NV at, NV interval, SV *cb) 340struct ev_periodic *periodic (NV at, NV interval, SV *cb)
344 PROTOTYPE: $$&
345 ALIAS: 341 ALIAS:
346 periodic_ns = 1 342 periodic_ns = 1
347 INIT: 343 INIT:
348 CHECK_REPEAT (interval); 344 CHECK_REPEAT (interval);
349 CODE: 345 CODE:
352 if (!ix) evperiodic_start (RETVAL); 348 if (!ix) evperiodic_start (RETVAL);
353 OUTPUT: 349 OUTPUT:
354 RETVAL 350 RETVAL
355 351
356struct ev_signal *signal (Signal signum, SV *cb) 352struct ev_signal *signal (Signal signum, SV *cb)
357 PROTOTYPE: $&
358 ALIAS: 353 ALIAS:
359 signal_ns = 1 354 signal_ns = 1
360 CODE: 355 CODE:
361 RETVAL = e_new (sizeof (struct ev_signal), cb); 356 RETVAL = e_new (sizeof (struct ev_signal), cb);
362 evsignal_set (RETVAL, signum); 357 evsignal_set (RETVAL, signum);
363 if (!ix) evsignal_start (RETVAL); 358 if (!ix) evsignal_start (RETVAL);
364 OUTPUT: 359 OUTPUT:
365 RETVAL 360 RETVAL
366 361
367struct ev_idle *idle (SV *cb) 362struct ev_idle *idle (SV *cb)
368 PROTOTYPE: &
369 ALIAS: 363 ALIAS:
370 idle_ns = 1 364 idle_ns = 1
371 CODE: 365 CODE:
372 RETVAL = e_new (sizeof (struct ev_idle), cb); 366 RETVAL = e_new (sizeof (struct ev_idle), cb);
373 evidle_set (RETVAL); 367 evidle_set (RETVAL);
374 if (!ix) evidle_start (RETVAL); 368 if (!ix) evidle_start (RETVAL);
375 OUTPUT: 369 OUTPUT:
376 RETVAL 370 RETVAL
377 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
378struct ev_check *check (SV *cb) 382struct ev_check *check (SV *cb)
379 PROTOTYPE: &
380 ALIAS: 383 ALIAS:
381 check_ns = 1 384 check_ns = 1
382 CODE: 385 CODE:
383 RETVAL = e_new (sizeof (struct ev_check), cb); 386 RETVAL = e_new (sizeof (struct ev_check), cb);
384 evcheck_set (RETVAL); 387 evcheck_set (RETVAL);
385 if (!ix) evcheck_start (RETVAL); 388 if (!ix) evcheck_start (RETVAL);
386 OUTPUT: 389 OUTPUT:
387 RETVAL 390 RETVAL
388 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);
399 OUTPUT:
400 RETVAL
401
389 402
390PROTOTYPES: DISABLE 403PROTOTYPES: DISABLE
391 404
392MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_ 405MODULE = EV PACKAGE = EV::Watcher PREFIX = ev_
393 406
407MODULE = EV PACKAGE = EV::IO PREFIX = evio_ 420MODULE = EV PACKAGE = EV::IO PREFIX = evio_
408 421
409void evio_start (struct ev_io *w) 422void evio_start (struct ev_io *w)
410 423
411void evio_stop (struct ev_io *w) 424void evio_stop (struct ev_io *w)
425
426void DESTROY (struct ev_io *w)
427 CODE:
428 evio_stop (w);
412 429
413void set (struct ev_io *w, SV *fh, int events) 430void set (struct ev_io *w, SV *fh, int events)
414 CODE: 431 CODE:
415{ 432{
416 int active = w->active; 433 int active = w->active;
463 480
464void evsignal_start (struct ev_signal *w) 481void evsignal_start (struct ev_signal *w)
465 482
466void evsignal_stop (struct ev_signal *w) 483void evsignal_stop (struct ev_signal *w)
467 484
485void DESTROY (struct ev_signal *w)
486 CODE:
487 evsignal_stop (w);
488
468void set (struct ev_signal *w, SV *signal = 0) 489void set (struct ev_signal *w, SV *signal = 0)
469 CODE: 490 CODE:
470{ 491{
471 Signal signum = sv_signum (signal); /* may croak here */ 492 Signal signum = sv_signum (signal); /* may croak here */
472 int active = w->active; 493 int active = w->active;
487void evtimer_stop (struct ev_timer *w) 508void evtimer_stop (struct ev_timer *w)
488 509
489void evtimer_again (struct ev_timer *w) 510void evtimer_again (struct ev_timer *w)
490 INIT: 511 INIT:
491 CHECK_REPEAT (w->repeat); 512 CHECK_REPEAT (w->repeat);
513
514void DESTROY (struct ev_timer *w)
515 CODE:
516 evtimer_stop (w);
492 517
493void set (struct ev_timer *w, NV after, NV repeat = 0.) 518void set (struct ev_timer *w, NV after, NV repeat = 0.)
494 INIT: 519 INIT:
495 CHECK_REPEAT (repeat); 520 CHECK_REPEAT (repeat);
496 CODE: 521 CODE:
507 INIT: 532 INIT:
508 CHECK_REPEAT (w->interval); 533 CHECK_REPEAT (w->interval);
509 534
510void evperiodic_stop (struct ev_periodic *w) 535void evperiodic_stop (struct ev_periodic *w)
511 536
537void DESTROY (struct ev_periodic *w)
538 CODE:
539 evperiodic_stop (w);
540
512void set (struct ev_periodic *w, NV at, NV interval = 0.) 541void set (struct ev_periodic *w, NV at, NV interval = 0.)
513 INIT: 542 INIT:
514 CHECK_REPEAT (interval); 543 CHECK_REPEAT (interval);
515 CODE: 544 CODE:
516{ 545{
524 553
525void evidle_start (struct ev_idle *w) 554void evidle_start (struct ev_idle *w)
526 555
527void evidle_stop (struct ev_idle *w) 556void evidle_stop (struct ev_idle *w)
528 557
558void DESTROY (struct ev_idle *w)
559 CODE:
560 evidle_stop (w);
561
562MODULE = EV PACKAGE = EV::Prepare PREFIX = evcheck_
563
564void evprepare_start (struct ev_prepare *w)
565
566void evprepare_stop (struct ev_prepare *w)
567
568void DESTROY (struct ev_prepare *w)
569 CODE:
570 evprepare_stop (w);
571
529MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ 572MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_
530 573
531void evcheck_start (struct ev_check *w) 574void evcheck_start (struct ev_check *w)
532 575
533void evcheck_stop (struct ev_check *w) 576void evcheck_stop (struct ev_check *w)
577
578void DESTROY (struct ev_check *w)
579 CODE:
580 evcheck_stop (w);
581
582MODULE = EV PACKAGE = EV::Child PREFIX = evchild_
583
584void evchild_start (struct ev_child *w)
585
586void evchild_stop (struct ev_child *w)
587
588void DESTROY (struct ev_child *w)
589 CODE:
590 evchild_stop (w);
591
592void set (struct ev_child *w, int pid)
593 CODE:
594{
595 int active = w->active;
596 if (active) evchild_stop (w);
597 evchild_set (w, pid);
598 if (active) evchild_start (w);
599}
600
601int status (struct ev_child *w)
602 CODE:
603 RETVAL = w->status;
604 OUTPUT:
605 RETVAL
534 606
535#if 0 607#if 0
536 608
537MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_ 609MODULE = EV PACKAGE = EV::DNS PREFIX = evdns_
538 610

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines