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

Comparing EV/EV.xs (file contents):
Revision 1.110 by root, Tue Apr 15 04:41:57 2008 UTC vs.
Revision 1.124 by root, Mon Jun 29 18:46:52 2009 UTC

24 24
25#ifndef _WIN32 25#ifndef _WIN32
26# include <pthread.h> 26# include <pthread.h>
27#endif 27#endif
28 28
29/* 5.10.0 */
30#ifndef SvREFCNT_inc_NN
31# define SvREFCNT_inc_NN(sv) SvREFCNT_inc (sv)
32#endif
33
34/* 5.6.x */
35#ifndef SvRV_set
36# define SvRV_set(a,b) SvRV ((a)) = (b)
37#endif
38
29#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) 39#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
30 40
31#define WFLAG_KEEPALIVE 1 41#define WFLAG_KEEPALIVE 1
42#define WFLAG_UNREFED 2 /* has been unref'ed */
32 43
33#define UNREF(w) \ 44#define UNREF(w) \
34 if (!((w)->e_flags & WFLAG_KEEPALIVE) \ 45 if (!((w)->e_flags & (WFLAG_KEEPALIVE | WFLAG_UNREFED)) \
35 && !ev_is_active (w)) \ 46 && ev_is_active (w)) \
47 { \
36 ev_unref (e_loop (w)); 48 ev_unref (e_loop (w)); \
49 (w)->e_flags |= WFLAG_UNREFED; \
50 }
37 51
38#define REF(w) \ 52#define REF(w) \
39 if (!((w)->e_flags & WFLAG_KEEPALIVE) \ 53 if ((w)->e_flags & WFLAG_UNREFED) \
40 && ev_is_active (w)) \ 54 { \
55 (w)->e_flags &= ~WFLAG_UNREFED; \
41 ev_ref (e_loop (w)); 56 ev_ref (e_loop (w)); \
57 }
42 58
43#define START(type,w) \ 59#define START(type,w) \
44 do { \ 60 do { \
61 ev_ ## type ## _start (e_loop (w), w); \
45 UNREF (w); \ 62 UNREF (w); \
46 ev_ ## type ## _start (e_loop (w), w); \
47 } while (0) 63 } while (0)
48 64
49#define STOP(type,w) \ 65#define STOP(type,w) \
50 do { \ 66 do { \
51 REF (w); \ 67 REF (w); \
127 return SvIV (fh); 143 return SvIV (fh);
128 144
129 return -1; 145 return -1;
130} 146}
131 147
148static SV *
149e_get_cv (SV *cb_sv)
150{
151 HV *st;
152 GV *gvp;
153 CV *cv = sv_2cv (cb_sv, &st, &gvp, 0);
154
155 if (!cv)
156 croak ("EV watcher callback must be a CODE reference");
157
158 return (SV *)cv;
159}
160
132static void * 161static void *
133e_new (int size, SV *cb_sv, SV *loop) 162e_new (int size, SV *cb_sv, SV *loop)
134{ 163{
164 SV *cv = cb_sv ? e_get_cv (cb_sv) : 0;
135 ev_watcher *w; 165 ev_watcher *w;
136 SV *self = NEWSV (0, size); 166 SV *self = NEWSV (0, size);
137 SvPOK_only (self); 167 SvPOK_only (self);
138 SvCUR_set (self, size); 168 SvCUR_set (self, size);
139 169
140 w = (ev_watcher *)SvPVX (self); 170 w = (ev_watcher *)SvPVX (self);
141 171
142 ev_init (w, e_cb); 172 ev_init (w, cv ? e_cb : 0);
143 173
144 w->loop = SvREFCNT_inc (SvRV (loop)); 174 w->loop = SvREFCNT_inc (SvRV (loop));
145 w->e_flags = WFLAG_KEEPALIVE; 175 w->e_flags = WFLAG_KEEPALIVE;
146 w->data = 0; 176 w->data = 0;
147 w->fh = 0; 177 w->fh = 0;
148 w->cb_sv = SvTEMP (cb_sv) && SvREFCNT (cb_sv) == 1 ? SvREFCNT_inc (cb_sv) : newSVsv (cb_sv); 178 w->cb_sv = SvREFCNT_inc (cv);
149 w->self = self; 179 w->self = self;
150 180
151 return (void *)w; 181 return (void *)w;
152} 182}
153 183
177 } 207 }
178 208
179 return rv; 209 return rv;
180} 210}
181 211
182static SV *sv_events_cache; 212static SV *sv_self_cache, *sv_events_cache;
183 213
184static void 214static void
185e_cb (EV_P_ ev_watcher *w, int revents) 215e_cb (EV_P_ ev_watcher *w, int revents)
186{ 216{
187 dSP; 217 dSP;
188 I32 mark = SP - PL_stack_base; 218 I32 mark = SP - PL_stack_base;
189 SV *sv_self, *sv_events; 219 SV *sv_self, *sv_events;
190 220
221 /* libev might have stopped the watcher */
222 if (expect_false (w->e_flags & WFLAG_UNREFED)
223 && !ev_is_active (w))
224 REF (w);
225
226 if (expect_true (sv_self_cache))
227 {
228 sv_self = sv_self_cache; sv_self_cache = 0;
229 SvRV_set (sv_self, SvREFCNT_inc_NN (w->self));
230 }
231 else
232 {
191 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ 233 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
234 SvREADONLY_on (sv_self);
235 }
236
237 if (expect_true (sv_events_cache))
238 {
239 sv_events = sv_events_cache; sv_events_cache = 0;
240 SvIV_set (sv_events, revents);
241 }
242 else
243 {
244 sv_events = newSViv (revents);
245 SvREADONLY_on (sv_events);
246 }
247
248 PUSHMARK (SP);
249 EXTEND (SP, 2);
250 PUSHs (sv_self);
251 PUSHs (sv_events);
252
253 PUTBACK;
254 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
255
256 if (expect_false (SvREFCNT (sv_self) != 1 || sv_self_cache))
257 SvREFCNT_dec (sv_self);
258 else
259 {
260 SvREFCNT_dec (SvRV (sv_self));
261 SvRV_set (sv_self, &PL_sv_undef);
262 sv_self_cache = sv_self;
263 }
264
265 if (expect_false (SvREFCNT (sv_events) != 1 || sv_events_cache))
266 SvREFCNT_dec (sv_events);
267 else
268 sv_events_cache = sv_events;
269
270 if (expect_false (SvTRUE (ERRSV)))
271 {
272 SPAGAIN;
273 PUSHMARK (SP);
274 PUTBACK;
275 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
276 }
277
278 SP = PL_stack_base + mark;
279 PUTBACK;
280}
281
282static void
283e_once_cb (int revents, void *arg)
284{
285 dSP;
286 I32 mark = SP - PL_stack_base;
287 SV *sv_events;
192 288
193 if (sv_events_cache) 289 if (sv_events_cache)
194 { 290 {
195 sv_events = sv_events_cache; sv_events_cache = 0; 291 sv_events = sv_events_cache; sv_events_cache = 0;
196 SvIV_set (sv_events, revents); 292 SvIV_set (sv_events, revents);
197 } 293 }
198 else 294 else
199 sv_events = newSViv (revents); 295 sv_events = newSViv (revents);
200 296
201 PUSHMARK (SP); 297 PUSHMARK (SP);
202 EXTEND (SP, 2);
203 PUSHs (sv_self);
204 PUSHs (sv_events); 298 XPUSHs (sv_events);
205 299
206 PUTBACK; 300 PUTBACK;
207 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 301 call_sv ((SV *)arg, G_DISCARD | G_VOID | G_EVAL);
208 302
209 SvREFCNT_dec (sv_self); 303 SvREFCNT_dec ((SV *)arg);
210 304
211 if (sv_events_cache) 305 if (sv_events_cache)
212 SvREFCNT_dec (sv_events); 306 SvREFCNT_dec (sv_events);
213 else 307 else
214 sv_events_cache = sv_events; 308 sv_events_cache = sv_events;
223 317
224 SP = PL_stack_base + mark; 318 SP = PL_stack_base + mark;
225 PUTBACK; 319 PUTBACK;
226} 320}
227 321
228static void
229e_once_cb (int revents, void *arg)
230{
231 dSP;
232 I32 mark = SP - PL_stack_base;
233 SV *sv_events;
234
235 if (sv_events_cache)
236 {
237 sv_events = sv_events_cache; sv_events_cache = 0;
238 SvIV_set (sv_events, revents);
239 }
240 else
241 sv_events = newSViv (revents);
242
243 PUSHMARK (SP);
244 XPUSHs (sv_events);
245
246 PUTBACK;
247 call_sv ((SV *)arg, G_DISCARD | G_VOID | G_EVAL);
248
249 SvREFCNT_dec ((SV *)arg);
250
251 if (sv_events_cache)
252 SvREFCNT_dec (sv_events);
253 else
254 sv_events_cache = sv_events;
255
256 if (SvTRUE (ERRSV))
257 {
258 SPAGAIN;
259 PUSHMARK (SP);
260 PUTBACK;
261 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
262 }
263
264 SP = PL_stack_base + mark;
265 PUTBACK;
266}
267
268static ev_tstamp 322static ev_tstamp
269e_periodic_cb (ev_periodic *w, ev_tstamp now) 323e_periodic_cb (ev_periodic *w, ev_tstamp now)
270{ 324{
271 ev_tstamp retval; 325 ev_tstamp retval;
272 int count; 326 int count;
336 const_iv (EV_, MINPRI) 390 const_iv (EV_, MINPRI)
337 const_iv (EV_, MAXPRI) 391 const_iv (EV_, MAXPRI)
338 392
339 const_iv (EV_, UNDEF) 393 const_iv (EV_, UNDEF)
340 const_iv (EV_, NONE) 394 const_iv (EV_, NONE)
341 const_iv (EV_, TIMEOUT)
342 const_iv (EV_, READ) 395 const_iv (EV_, READ)
343 const_iv (EV_, WRITE) 396 const_iv (EV_, WRITE)
397 const_iv (EV_, IO)
398 const_iv (EV_, TIMEOUT)
399 const_iv (EV_, TIMER)
400 const_iv (EV_, PERIODIC)
344 const_iv (EV_, SIGNAL) 401 const_iv (EV_, SIGNAL)
402 const_iv (EV_, CHILD)
403 const_iv (EV_, STAT)
345 const_iv (EV_, IDLE) 404 const_iv (EV_, IDLE)
405 const_iv (EV_, PREPARE)
346 const_iv (EV_, CHECK) 406 const_iv (EV_, CHECK)
407 const_iv (EV_, EMBED)
408 const_iv (EV_, FORK)
409 const_iv (EV_, ASYNC)
410 const_iv (EV_, CUSTOM)
347 const_iv (EV_, ERROR) 411 const_iv (EV_, ERROR)
348 412
413 const_iv (EV, LOOP_NONBLOCK)
349 const_iv (EV, LOOP_ONESHOT) 414 const_iv (EV, LOOP_ONESHOT)
415
350 const_iv (EV, LOOP_NONBLOCK) 416 const_iv (EV, UNLOOP_CANCEL)
351 const_iv (EV, UNLOOP_ONE) 417 const_iv (EV, UNLOOP_ONE)
352 const_iv (EV, UNLOOP_ALL) 418 const_iv (EV, UNLOOP_ALL)
353 419
354 const_iv (EV, BACKEND_SELECT) 420 const_iv (EV, BACKEND_SELECT)
355 const_iv (EV, BACKEND_POLL) 421 const_iv (EV, BACKEND_POLL)
358 const_iv (EV, BACKEND_DEVPOLL) 424 const_iv (EV, BACKEND_DEVPOLL)
359 const_iv (EV, BACKEND_PORT) 425 const_iv (EV, BACKEND_PORT)
360 const_iv (EV, FLAG_AUTO) 426 const_iv (EV, FLAG_AUTO)
361 const_iv (EV, FLAG_NOENV) 427 const_iv (EV, FLAG_NOENV)
362 const_iv (EV, FLAG_FORKCHECK) 428 const_iv (EV, FLAG_FORKCHECK)
429
430 const_iv (EV_, VERSION_MAJOR)
431 const_iv (EV_, VERSION_MINOR)
363 }; 432 };
364 433
365 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 434 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
366 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 435 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
367 436
397 evapi.loop_new = ev_loop_new; 466 evapi.loop_new = ev_loop_new;
398 evapi.loop_destroy = ev_loop_destroy; 467 evapi.loop_destroy = ev_loop_destroy;
399 evapi.loop_fork = ev_loop_fork; 468 evapi.loop_fork = ev_loop_fork;
400 evapi.loop_count = ev_loop_count; 469 evapi.loop_count = ev_loop_count;
401 evapi.now = ev_now; 470 evapi.now = ev_now;
471 evapi.now_update = ev_now_update;
472 evapi.suspend = ev_suspend;
473 evapi.resume = ev_resume;
402 evapi.backend = ev_backend; 474 evapi.backend = ev_backend;
403 evapi.unloop = ev_unloop; 475 evapi.unloop = ev_unloop;
404 evapi.ref = ev_ref; 476 evapi.ref = ev_ref;
405 evapi.unref = ev_unref; 477 evapi.unref = ev_unref;
406 evapi.loop = ev_loop; 478 evapi.loop = ev_loop;
472 544
473unsigned int ev_recommended_backends () 545unsigned int ev_recommended_backends ()
474 546
475unsigned int ev_embeddable_backends () 547unsigned int ev_embeddable_backends ()
476 548
549void ev_sleep (NV interval)
550
477NV ev_time () 551NV ev_time ()
478 552
479NV ev_now () 553NV ev_now ()
554 C_ARGS: evapi.default_loop
555
556void ev_now_update ()
557 C_ARGS: evapi.default_loop
558
559void ev_suspend ()
560 C_ARGS: evapi.default_loop
561
562void ev_resume ()
480 C_ARGS: evapi.default_loop 563 C_ARGS: evapi.default_loop
481 564
482unsigned int ev_backend () 565unsigned int ev_backend ()
483 C_ARGS: evapi.default_loop 566 C_ARGS: evapi.default_loop
484 567
628 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); 711 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval);
629 if (!ix) START (stat, RETVAL); 712 if (!ix) START (stat, RETVAL);
630 OUTPUT: 713 OUTPUT:
631 RETVAL 714 RETVAL
632 715
633ev_embed *embed (struct ev_loop *loop, SV *cb = &PL_sv_undef) 716ev_embed *embed (struct ev_loop *loop, SV *cb = 0)
634 ALIAS: 717 ALIAS:
635 embed_ns = 1 718 embed_ns = 1
636 CODE: 719 CODE:
637{ 720{
638 if (!(ev_backend (loop) & ev_embeddable_backends ())) 721 if (!(ev_backend (loop) & ev_embeddable_backends ()))
639 croak ("passed loop is not embeddable via EV::embed,"); 722 croak ("passed loop is not embeddable via EV::embed,");
640 723
641 RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv); 724 RETVAL = e_new (sizeof (ev_embed), cb, default_loop_sv);
642 RETVAL->fh = newSVsv (ST (0)); 725 RETVAL->fh = newSVsv (ST (0));
643 ev_embed_set (RETVAL, loop); 726 ev_embed_set (RETVAL, loop);
644
645 if (!SvOK (cb)) ev_set_cb (RETVAL, 0);
646
647 if (!ix) START (embed, RETVAL); 727 if (!ix) START (embed, RETVAL);
648} 728}
649 OUTPUT: 729 OUTPUT:
650 RETVAL 730 RETVAL
651 731
692 RETVAL = w->e_flags & WFLAG_KEEPALIVE; 772 RETVAL = w->e_flags & WFLAG_KEEPALIVE;
693 new_value = new_value ? WFLAG_KEEPALIVE : 0; 773 new_value = new_value ? WFLAG_KEEPALIVE : 0;
694 774
695 if (items > 1 && ((new_value ^ w->e_flags) & WFLAG_KEEPALIVE)) 775 if (items > 1 && ((new_value ^ w->e_flags) & WFLAG_KEEPALIVE))
696 { 776 {
777 w->e_flags = (w->e_flags & ~WFLAG_KEEPALIVE) | new_value;
697 REF (w); 778 REF (w);
698 w->e_flags = (w->e_flags & ~WFLAG_KEEPALIVE) | new_value;
699 UNREF (w); 779 UNREF (w);
700 } 780 }
701} 781}
702 OUTPUT: 782 OUTPUT:
703 RETVAL 783 RETVAL
704 784
705SV *cb (ev_watcher *w, SV *new_cb = 0) 785SV *cb (ev_watcher *w, SV *new_cb = 0)
706 CODE: 786 CODE:
707{ 787{
708 RETVAL = newSVsv (w->cb_sv);
709
710 if (items > 1) 788 if (items > 1)
711 sv_setsv (w->cb_sv, new_cb); 789 {
790 new_cb = e_get_cv (new_cb);
791 RETVAL = newRV_noinc (w->cb_sv);
792 w->cb_sv = SvREFCNT_inc (new_cb);
793 }
794 else
795 RETVAL = newRV_inc (w->cb_sv);
712} 796}
713 OUTPUT: 797 OUTPUT:
714 RETVAL 798 RETVAL
715 799
716SV *data (ev_watcher *w, SV *new_data = 0) 800SV *data (ev_watcher *w, SV *new_data = 0)
874 958
875void ev_timer_again (ev_timer *w) 959void ev_timer_again (ev_timer *w)
876 INIT: 960 INIT:
877 CHECK_REPEAT (w->repeat); 961 CHECK_REPEAT (w->repeat);
878 CODE: 962 CODE:
879 REF (w);
880 ev_timer_again (e_loop (w), w); 963 ev_timer_again (e_loop (w), w);
881 UNREF (w); 964 UNREF (w);
882 965
883void DESTROY (ev_timer *w) 966void DESTROY (ev_timer *w)
884 CODE: 967 CODE:
889 INIT: 972 INIT:
890 CHECK_REPEAT (repeat); 973 CHECK_REPEAT (repeat);
891 CODE: 974 CODE:
892 RESET (timer, w, (w, after, repeat)); 975 RESET (timer, w, (w, after, repeat));
893 976
894NV at (ev_timer *w)
895 CODE:
896 RETVAL = w->at;
897 OUTPUT:
898 RETVAL
899
900MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_ 977MODULE = EV PACKAGE = EV::Periodic PREFIX = ev_periodic_
901 978
902void ev_periodic_start (ev_periodic *w) 979void ev_periodic_start (ev_periodic *w)
903 INIT: 980 INIT:
904 CHECK_REPEAT (w->interval); 981 CHECK_REPEAT (w->interval);
909 CODE: 986 CODE:
910 STOP (periodic, w); 987 STOP (periodic, w);
911 988
912void ev_periodic_again (ev_periodic *w) 989void ev_periodic_again (ev_periodic *w)
913 CODE: 990 CODE:
914 REF (w);
915 ev_periodic_again (e_loop (w), w); 991 ev_periodic_again (e_loop (w), w);
916 UNREF (w); 992 UNREF (w);
917 993
918void DESTROY (ev_periodic *w) 994void DESTROY (ev_periodic *w)
919 CODE: 995 CODE:
931 RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0)); 1007 RESET (periodic, w, (w, at, interval, w->fh ? e_periodic_cb : 0));
932} 1008}
933 1009
934NV at (ev_periodic *w) 1010NV at (ev_periodic *w)
935 CODE: 1011 CODE:
936 RETVAL = w->at; 1012 RETVAL = ev_periodic_at (w);
937 OUTPUT: 1013 OUTPUT:
938 RETVAL 1014 RETVAL
939 1015
940MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_ 1016MODULE = EV PACKAGE = EV::Idle PREFIX = ev_idle_
941 1017
1196 if (loop != evapi.default_loop) /* global destruction sucks */ 1272 if (loop != evapi.default_loop) /* global destruction sucks */
1197 ev_loop_destroy (loop); 1273 ev_loop_destroy (loop);
1198 1274
1199void ev_loop_fork (struct ev_loop *loop) 1275void ev_loop_fork (struct ev_loop *loop)
1200 1276
1277void ev_loop_verify (struct ev_loop *loop)
1278
1201NV ev_now (struct ev_loop *loop) 1279NV ev_now (struct ev_loop *loop)
1280
1281void ev_now_update (struct ev_loop *loop)
1282
1283void ev_suspend (struct ev_loop *loop)
1284
1285void ev_resume (struct ev_loop *loop)
1202 1286
1203void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) 1287void ev_set_io_collect_interval (struct ev_loop *loop, NV interval)
1204 1288
1205void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) 1289void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval)
1206 1290
1350 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval); 1434 ev_stat_set (RETVAL, SvPVbyte_nolen (RETVAL->fh), interval);
1351 if (!ix) START (stat, RETVAL); 1435 if (!ix) START (stat, RETVAL);
1352 OUTPUT: 1436 OUTPUT:
1353 RETVAL 1437 RETVAL
1354 1438
1355ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = &PL_sv_undef) 1439ev_embed *embed (struct ev_loop *loop, struct ev_loop *other, SV *cb = 0)
1356 ALIAS: 1440 ALIAS:
1357 embed_ns = 1 1441 embed_ns = 1
1358 CODE: 1442 CODE:
1359{ 1443{
1360 if (!(ev_backend (other) & ev_embeddable_backends ())) 1444 if (!(ev_backend (other) & ev_embeddable_backends ()))
1361 croak ("passed loop is not embeddable via EV::embed,"); 1445 croak ("passed loop is not embeddable via EV::embed,");
1362 1446
1363 RETVAL = e_new (sizeof (ev_embed), cb, ST (0)); 1447 RETVAL = e_new (sizeof (ev_embed), cb, ST (0));
1364 RETVAL->fh = newSVsv (ST (1)); 1448 RETVAL->fh = newSVsv (ST (1));
1365 ev_embed_set (RETVAL, other); 1449 ev_embed_set (RETVAL, other);
1366
1367 if (!SvOK (cb)) ev_set_cb (RETVAL, 0);
1368
1369 if (!ix) START (embed, RETVAL); 1450 if (!ix) START (embed, RETVAL);
1370} 1451}
1371 OUTPUT: 1452 OUTPUT:
1372 RETVAL 1453 RETVAL
1373 1454

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines