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 | |
16 | typedef int Signal; |
15 | typedef int Signal; |
17 | |
16 | |
18 | static struct EVAPI evapi; |
17 | static 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 | |
29 | |
30 | static int |
30 | static int |
31 | sv_signum (SV *sig) |
31 | sv_signum (SV *sig) |
32 | { |
32 | { |
… | |
… | |
38 | for (signum = 1; signum < SIG_SIZE; ++signum) |
38 | for (signum = 1; signum < SIG_SIZE; ++signum) |
39 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
39 | if (strEQ (SvPV_nolen (sig), PL_sig_name [signum])) |
40 | return signum; |
40 | return signum; |
41 | |
41 | |
42 | return -1; |
42 | return -1; |
43 | } |
|
|
44 | |
|
|
45 | static void |
|
|
46 | api_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 | } |
43 | } |
59 | |
44 | |
60 | ///////////////////////////////////////////////////////////////////////////// |
45 | ///////////////////////////////////////////////////////////////////////////// |
61 | // Event |
46 | // Event |
62 | |
47 | |
… | |
… | |
209 | |
194 | |
210 | LEAVE; |
195 | LEAVE; |
211 | } |
196 | } |
212 | #endif |
197 | #endif |
213 | |
198 | |
|
|
199 | #define CHECK_REPEAT(repeat) if (repeat < 0.) \ |
|
|
200 | croak (# repeat " value must be >= 0"); |
|
|
201 | |
214 | ///////////////////////////////////////////////////////////////////////////// |
202 | ///////////////////////////////////////////////////////////////////////////// |
215 | // XS interface functions |
203 | // XS interface functions |
216 | |
204 | |
217 | MODULE = EV PACKAGE = EV PREFIX = ev_ |
205 | MODULE = EV PACKAGE = EV PREFIX = ev_ |
|
|
206 | |
|
|
207 | PROTOTYPES: ENABLE |
218 | |
208 | |
219 | BOOT: |
209 | BOOT: |
220 | { |
210 | { |
221 | int i; |
211 | int i; |
222 | HV *stash = gv_stashpv ("EV", 1); |
212 | HV *stash = gv_stashpv ("EV", 1); |
… | |
… | |
224 | static const struct { |
214 | static const struct { |
225 | const char *name; |
215 | const char *name; |
226 | IV iv; |
216 | IV iv; |
227 | } *civ, const_iv[] = { |
217 | } *civ, const_iv[] = { |
228 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
218 | # define const_iv(pfx, name) { # name, (IV) pfx ## name }, |
|
|
219 | const_iv (EV_, UNDEF) |
229 | const_iv (EV_, NONE) |
220 | const_iv (EV_, NONE) |
230 | const_iv (EV_, TIMEOUT) |
221 | const_iv (EV_, TIMEOUT) |
231 | const_iv (EV_, READ) |
222 | const_iv (EV_, READ) |
232 | const_iv (EV_, WRITE) |
223 | const_iv (EV_, WRITE) |
233 | const_iv (EV_, SIGNAL) |
224 | const_iv (EV_, SIGNAL) |
234 | const_iv (EV_, IDLE) |
225 | const_iv (EV_, IDLE) |
235 | const_iv (EV_, CHECK) |
226 | const_iv (EV_, CHECK) |
|
|
227 | const_iv (EV_, ERROR) |
236 | |
228 | |
237 | const_iv (EV, LOOP_ONESHOT) |
229 | const_iv (EV, LOOP_ONESHOT) |
238 | const_iv (EV, LOOP_NONBLOCK) |
230 | const_iv (EV, LOOP_NONBLOCK) |
239 | |
231 | |
240 | const_iv (EV, METHOD_NONE) |
232 | const_iv (EV, METHOD_NONE) |
… | |
… | |
250 | stash_time = gv_stashpv ("EV::Time" , 1); |
242 | stash_time = gv_stashpv ("EV::Time" , 1); |
251 | stash_timer = gv_stashpv ("EV::Timer" , 1); |
243 | stash_timer = gv_stashpv ("EV::Timer" , 1); |
252 | stash_periodic = gv_stashpv ("EV::Periodic", 1); |
244 | stash_periodic = gv_stashpv ("EV::Periodic", 1); |
253 | stash_signal = gv_stashpv ("EV::Signal" , 1); |
245 | stash_signal = gv_stashpv ("EV::Signal" , 1); |
254 | stash_idle = gv_stashpv ("EV::Idle" , 1); |
246 | stash_idle = gv_stashpv ("EV::Idle" , 1); |
|
|
247 | stash_prepare = gv_stashpv ("EV::Prepare" , 1); |
255 | stash_check = gv_stashpv ("EV::Check" , 1); |
248 | stash_check = gv_stashpv ("EV::Check" , 1); |
256 | |
249 | |
257 | { |
250 | { |
258 | SV *sv = perl_get_sv ("EV::API", TRUE); |
251 | SV *sv = perl_get_sv ("EV::API", TRUE); |
259 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
252 | perl_get_sv ("EV::API", TRUE); /* silence 5.10 warning */ |
260 | |
253 | |
261 | /* the poor man's shared library emulator */ |
254 | /* the poor man's shared library emulator */ |
262 | evapi.ver = EV_API_VERSION; |
255 | evapi.ver = EV_API_VERSION; |
263 | evapi.rev = EV_API_REVISION; |
256 | evapi.rev = EV_API_REVISION; |
264 | evapi.once = api_once; |
|
|
265 | evapi.sv_fileno = sv_fileno; |
257 | evapi.sv_fileno = sv_fileno; |
266 | evapi.sv_signum = sv_signum; |
258 | evapi.sv_signum = sv_signum; |
267 | evapi.now = &ev_now; |
259 | evapi.now = &ev_now; |
268 | evapi.method = &ev_method; |
260 | evapi.method = &ev_method; |
269 | evapi.loop_done = &ev_loop_done; |
261 | evapi.loop_done = &ev_loop_done; |
270 | evapi.time = ev_time; |
262 | evapi.time = ev_time; |
271 | evapi.loop = ev_loop; |
263 | evapi.loop = ev_loop; |
|
|
264 | evapi.once = ev_once; |
272 | evapi.io_start = evio_start; |
265 | evapi.io_start = evio_start; |
273 | evapi.io_stop = evio_stop; |
266 | evapi.io_stop = evio_stop; |
274 | evapi.timer_start = evtimer_start; |
267 | evapi.timer_start = evtimer_start; |
275 | evapi.timer_stop = evtimer_stop; |
268 | evapi.timer_stop = evtimer_stop; |
|
|
269 | evapi.timer_again = evtimer_again; |
276 | evapi.periodic_start = evperiodic_start; |
270 | evapi.periodic_start = evperiodic_start; |
277 | evapi.periodic_stop = evperiodic_stop; |
271 | evapi.periodic_stop = evperiodic_stop; |
278 | evapi.signal_start = evsignal_start; |
272 | evapi.signal_start = evsignal_start; |
279 | evapi.signal_stop = evsignal_stop; |
273 | evapi.signal_stop = evsignal_stop; |
280 | evapi.idle_start = evidle_start; |
274 | evapi.idle_start = evidle_start; |
281 | evapi.idle_stop = evidle_stop; |
275 | evapi.idle_stop = evidle_stop; |
|
|
276 | evapi.prepare_start = evprepare_start; |
|
|
277 | evapi.prepare_stop = evprepare_stop; |
282 | evapi.check_start = evcheck_start; |
278 | evapi.check_start = evcheck_start; |
283 | evapi.check_stop = evcheck_stop; |
279 | evapi.check_stop = evcheck_stop; |
284 | |
280 | |
285 | sv_setiv (sv, (IV)&evapi); |
281 | sv_setiv (sv, (IV)&evapi); |
286 | SvREADONLY_on (sv); |
282 | SvREADONLY_on (sv); |
… | |
… | |
321 | RETVAL |
317 | RETVAL |
322 | |
318 | |
323 | struct ev_timer *timer (NV after, NV repeat, SV *cb) |
319 | struct ev_timer *timer (NV after, NV repeat, SV *cb) |
324 | ALIAS: |
320 | ALIAS: |
325 | timer_ns = 1 |
321 | timer_ns = 1 |
|
|
322 | INIT: |
|
|
323 | CHECK_REPEAT (repeat); |
326 | CODE: |
324 | CODE: |
327 | RETVAL = e_new (sizeof (struct ev_timer), cb); |
325 | RETVAL = e_new (sizeof (struct ev_timer), cb); |
328 | evtimer_set (RETVAL, after, repeat); |
326 | evtimer_set (RETVAL, after, repeat); |
329 | if (!ix) evtimer_start (RETVAL); |
327 | if (!ix) evtimer_start (RETVAL); |
330 | OUTPUT: |
328 | OUTPUT: |
331 | RETVAL |
329 | RETVAL |
332 | |
330 | |
333 | struct ev_periodic *periodic (NV at, NV interval, SV *cb) |
331 | struct ev_periodic *periodic (NV at, NV interval, SV *cb) |
334 | ALIAS: |
332 | ALIAS: |
335 | periodic_ns = 1 |
333 | periodic_ns = 1 |
|
|
334 | INIT: |
|
|
335 | CHECK_REPEAT (interval); |
336 | CODE: |
336 | CODE: |
337 | RETVAL = e_new (sizeof (struct ev_periodic), cb); |
337 | RETVAL = e_new (sizeof (struct ev_periodic), cb); |
338 | evperiodic_set (RETVAL, at, interval); |
338 | evperiodic_set (RETVAL, at, interval); |
339 | if (!ix) evperiodic_start (RETVAL); |
339 | if (!ix) evperiodic_start (RETVAL); |
340 | OUTPUT: |
340 | OUTPUT: |
… | |
… | |
355 | idle_ns = 1 |
355 | idle_ns = 1 |
356 | CODE: |
356 | CODE: |
357 | RETVAL = e_new (sizeof (struct ev_idle), cb); |
357 | RETVAL = e_new (sizeof (struct ev_idle), cb); |
358 | evidle_set (RETVAL); |
358 | evidle_set (RETVAL); |
359 | if (!ix) evidle_start (RETVAL); |
359 | if (!ix) evidle_start (RETVAL); |
|
|
360 | OUTPUT: |
|
|
361 | RETVAL |
|
|
362 | |
|
|
363 | struct ev_prepare *prepare (SV *cb) |
|
|
364 | ALIAS: |
|
|
365 | prepare_ns = 1 |
|
|
366 | CODE: |
|
|
367 | RETVAL = e_new (sizeof (struct ev_prepare), cb); |
|
|
368 | evprepare_set (RETVAL); |
|
|
369 | if (!ix) evprepare_start (RETVAL); |
360 | OUTPUT: |
370 | OUTPUT: |
361 | RETVAL |
371 | RETVAL |
362 | |
372 | |
363 | struct ev_check *check (SV *cb) |
373 | struct ev_check *check (SV *cb) |
364 | ALIAS: |
374 | ALIAS: |
… | |
… | |
463 | MODULE = EV PACKAGE = EV::Time |
473 | MODULE = EV PACKAGE = EV::Time |
464 | |
474 | |
465 | MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ |
475 | MODULE = EV PACKAGE = EV::Timer PREFIX = evtimer_ |
466 | |
476 | |
467 | void evtimer_start (struct ev_timer *w) |
477 | void evtimer_start (struct ev_timer *w) |
|
|
478 | INIT: |
|
|
479 | CHECK_REPEAT (w->repeat); |
468 | |
480 | |
469 | void evtimer_stop (struct ev_timer *w) |
481 | void evtimer_stop (struct ev_timer *w) |
470 | |
482 | |
|
|
483 | void evtimer_again (struct ev_timer *w) |
|
|
484 | INIT: |
|
|
485 | CHECK_REPEAT (w->repeat); |
|
|
486 | |
471 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
487 | void set (struct ev_timer *w, NV after, NV repeat = 0.) |
|
|
488 | INIT: |
|
|
489 | CHECK_REPEAT (repeat); |
472 | CODE: |
490 | CODE: |
473 | { |
491 | { |
474 | int active = w->active; |
492 | int active = w->active; |
475 | if (active) evtimer_stop (w); |
493 | if (active) evtimer_stop (w); |
476 | evtimer_set (w, after, repeat); |
494 | evtimer_set (w, after, repeat); |
… | |
… | |
478 | } |
496 | } |
479 | |
497 | |
480 | MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ |
498 | MODULE = EV PACKAGE = EV::Periodic PREFIX = evperiodic_ |
481 | |
499 | |
482 | void evperiodic_start (struct ev_periodic *w) |
500 | void evperiodic_start (struct ev_periodic *w) |
|
|
501 | INIT: |
|
|
502 | CHECK_REPEAT (w->interval); |
483 | |
503 | |
484 | void evperiodic_stop (struct ev_periodic *w) |
504 | void evperiodic_stop (struct ev_periodic *w) |
485 | |
505 | |
486 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
506 | void set (struct ev_periodic *w, NV at, NV interval = 0.) |
|
|
507 | INIT: |
|
|
508 | CHECK_REPEAT (interval); |
487 | CODE: |
509 | CODE: |
488 | { |
510 | { |
489 | int active = w->active; |
511 | int active = w->active; |
490 | if (active) evperiodic_stop (w); |
512 | if (active) evperiodic_stop (w); |
491 | evperiodic_set (w, at, interval); |
513 | evperiodic_set (w, at, interval); |
… | |
… | |
495 | MODULE = EV PACKAGE = EV::Idle PREFIX = evidle_ |
517 | MODULE = EV PACKAGE = EV::Idle PREFIX = evidle_ |
496 | |
518 | |
497 | void evidle_start (struct ev_idle *w) |
519 | void evidle_start (struct ev_idle *w) |
498 | |
520 | |
499 | void evidle_stop (struct ev_idle *w) |
521 | void evidle_stop (struct ev_idle *w) |
|
|
522 | |
|
|
523 | MODULE = EV PACKAGE = EV::Prepare PREFIX = evcheck_ |
|
|
524 | |
|
|
525 | void evprepare_start (struct ev_prepare *w) |
|
|
526 | |
|
|
527 | void evprepare_stop (struct ev_prepare *w) |
500 | |
528 | |
501 | MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ |
529 | MODULE = EV PACKAGE = EV::Check PREFIX = evcheck_ |
502 | |
530 | |
503 | void evcheck_start (struct ev_check *w) |
531 | void evcheck_start (struct ev_check *w) |
504 | |
532 | |