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

Comparing EV/EV.xs (file contents):
Revision 1.114 by root, Sat Jul 12 22:19:22 2008 UTC vs.
Revision 1.123 by root, Sat Apr 25 14:12:48 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
39#if __GNUC__ >= 3
40# define expect(expr,value) __builtin_expect ((expr),(value))
41#else
42# define expect(expr,value) (expr)
43#endif
44
45#define expect_false(expr) expect ((expr) != 0, 0)
46#define expect_true(expr) expect ((expr) != 0, 1)
47
29#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop)) 48#define e_loop(w) INT2PTR (struct ev_loop *, SvIVX ((w)->loop))
30 49
31#define WFLAG_KEEPALIVE 1 50#define WFLAG_KEEPALIVE 1
51#define WFLAG_UNREFED 2 /* has been unref'ed */
32 52
33#define UNREF(w) \ 53#define UNREF(w) \
34 if (!((w)->e_flags & WFLAG_KEEPALIVE) \ 54 if (!((w)->e_flags & (WFLAG_KEEPALIVE | WFLAG_UNREFED)) \
35 && !ev_is_active (w)) \ 55 && ev_is_active (w)) \
56 { \
36 ev_unref (e_loop (w)); 57 ev_unref (e_loop (w)); \
58 (w)->e_flags |= WFLAG_UNREFED; \
59 }
37 60
38#define REF(w) \ 61#define REF(w) \
39 if (!((w)->e_flags & WFLAG_KEEPALIVE) \ 62 if ((w)->e_flags & WFLAG_UNREFED) \
40 && ev_is_active (w)) \ 63 { \
64 (w)->e_flags &= ~WFLAG_UNREFED; \
41 ev_ref (e_loop (w)); 65 ev_ref (e_loop (w)); \
66 }
42 67
43#define START(type,w) \ 68#define START(type,w) \
44 do { \ 69 do { \
70 ev_ ## type ## _start (e_loop (w), w); \
45 UNREF (w); \ 71 UNREF (w); \
46 ev_ ## type ## _start (e_loop (w), w); \
47 } while (0) 72 } while (0)
48 73
49#define STOP(type,w) \ 74#define STOP(type,w) \
50 do { \ 75 do { \
51 REF (w); \ 76 REF (w); \
191 } 216 }
192 217
193 return rv; 218 return rv;
194} 219}
195 220
196static SV *sv_events_cache; 221static SV *sv_self_cache, *sv_events_cache;
197 222
198static void 223static void
199e_cb (EV_P_ ev_watcher *w, int revents) 224e_cb (EV_P_ ev_watcher *w, int revents)
200{ 225{
201 dSP; 226 dSP;
202 I32 mark = SP - PL_stack_base; 227 I32 mark = SP - PL_stack_base;
203 SV *sv_self, *sv_events; 228 SV *sv_self, *sv_events;
204 229
230 /* libev might have stopped the watcher */
231 if (expect_false (w->e_flags & WFLAG_UNREFED)
232 && !ev_is_active (w))
233 REF (w);
234
235 if (expect_true (sv_self_cache))
236 {
237 sv_self = sv_self_cache; sv_self_cache = 0;
238 SvRV_set (sv_self, SvREFCNT_inc_NN (w->self));
239 }
240 else
241 {
205 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */ 242 sv_self = newRV_inc (w->self); /* w->self MUST be blessed by now */
243 SvREADONLY_on (sv_self);
244 }
245
246 if (expect_true (sv_events_cache))
247 {
248 sv_events = sv_events_cache; sv_events_cache = 0;
249 SvIV_set (sv_events, revents);
250 }
251 else
252 {
253 sv_events = newSViv (revents);
254 SvREADONLY_on (sv_events);
255 }
256
257 PUSHMARK (SP);
258 EXTEND (SP, 2);
259 PUSHs (sv_self);
260 PUSHs (sv_events);
261
262 PUTBACK;
263 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL);
264
265 if (expect_false (SvREFCNT (sv_self) != 1 || sv_self_cache))
266 SvREFCNT_dec (sv_self);
267 else
268 {
269 SvREFCNT_dec (SvRV (sv_self));
270 SvRV_set (sv_self, &PL_sv_undef);
271 sv_self_cache = sv_self;
272 }
273
274 if (expect_false (SvREFCNT (sv_events) != 1 || sv_events_cache))
275 SvREFCNT_dec (sv_events);
276 else
277 sv_events_cache = sv_events;
278
279 if (expect_false (SvTRUE (ERRSV)))
280 {
281 SPAGAIN;
282 PUSHMARK (SP);
283 PUTBACK;
284 call_sv (get_sv ("EV::DIED", 1), G_DISCARD | G_VOID | G_EVAL | G_KEEPERR);
285 }
286
287 SP = PL_stack_base + mark;
288 PUTBACK;
289}
290
291static void
292e_once_cb (int revents, void *arg)
293{
294 dSP;
295 I32 mark = SP - PL_stack_base;
296 SV *sv_events;
206 297
207 if (sv_events_cache) 298 if (sv_events_cache)
208 { 299 {
209 sv_events = sv_events_cache; sv_events_cache = 0; 300 sv_events = sv_events_cache; sv_events_cache = 0;
210 SvIV_set (sv_events, revents); 301 SvIV_set (sv_events, revents);
211 } 302 }
212 else 303 else
213 sv_events = newSViv (revents); 304 sv_events = newSViv (revents);
214 305
215 PUSHMARK (SP); 306 PUSHMARK (SP);
216 EXTEND (SP, 2);
217 PUSHs (sv_self);
218 PUSHs (sv_events); 307 XPUSHs (sv_events);
219 308
220 PUTBACK; 309 PUTBACK;
221 call_sv (w->cb_sv, G_DISCARD | G_VOID | G_EVAL); 310 call_sv ((SV *)arg, G_DISCARD | G_VOID | G_EVAL);
222 311
223 SvREFCNT_dec (sv_self); 312 SvREFCNT_dec ((SV *)arg);
224 313
225 if (sv_events_cache) 314 if (sv_events_cache)
226 SvREFCNT_dec (sv_events); 315 SvREFCNT_dec (sv_events);
227 else 316 else
228 sv_events_cache = sv_events; 317 sv_events_cache = sv_events;
237 326
238 SP = PL_stack_base + mark; 327 SP = PL_stack_base + mark;
239 PUTBACK; 328 PUTBACK;
240} 329}
241 330
242static void
243e_once_cb (int revents, void *arg)
244{
245 dSP;
246 I32 mark = SP - PL_stack_base;
247 SV *sv_events;
248
249 if (sv_events_cache)
250 {
251 sv_events = sv_events_cache; sv_events_cache = 0;
252 SvIV_set (sv_events, revents);
253 }
254 else
255 sv_events = newSViv (revents);
256
257 PUSHMARK (SP);
258 XPUSHs (sv_events);
259
260 PUTBACK;
261 call_sv ((SV *)arg, G_DISCARD | G_VOID | G_EVAL);
262
263 SvREFCNT_dec ((SV *)arg);
264
265 if (sv_events_cache)
266 SvREFCNT_dec (sv_events);
267 else
268 sv_events_cache = sv_events;
269
270 if (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 ev_tstamp 331static ev_tstamp
283e_periodic_cb (ev_periodic *w, ev_tstamp now) 332e_periodic_cb (ev_periodic *w, ev_tstamp now)
284{ 333{
285 ev_tstamp retval; 334 ev_tstamp retval;
286 int count; 335 int count;
350 const_iv (EV_, MINPRI) 399 const_iv (EV_, MINPRI)
351 const_iv (EV_, MAXPRI) 400 const_iv (EV_, MAXPRI)
352 401
353 const_iv (EV_, UNDEF) 402 const_iv (EV_, UNDEF)
354 const_iv (EV_, NONE) 403 const_iv (EV_, NONE)
355 const_iv (EV_, TIMEOUT)
356 const_iv (EV_, READ) 404 const_iv (EV_, READ)
357 const_iv (EV_, WRITE) 405 const_iv (EV_, WRITE)
406 const_iv (EV_, IO)
407 const_iv (EV_, TIMEOUT)
408 const_iv (EV_, TIMER)
409 const_iv (EV_, PERIODIC)
358 const_iv (EV_, SIGNAL) 410 const_iv (EV_, SIGNAL)
411 const_iv (EV_, CHILD)
412 const_iv (EV_, STAT)
359 const_iv (EV_, IDLE) 413 const_iv (EV_, IDLE)
414 const_iv (EV_, PREPARE)
360 const_iv (EV_, CHECK) 415 const_iv (EV_, CHECK)
416 const_iv (EV_, EMBED)
417 const_iv (EV_, FORK)
418 const_iv (EV_, ASYNC)
419 const_iv (EV_, CUSTOM)
361 const_iv (EV_, ERROR) 420 const_iv (EV_, ERROR)
362 421
422 const_iv (EV, LOOP_NONBLOCK)
363 const_iv (EV, LOOP_ONESHOT) 423 const_iv (EV, LOOP_ONESHOT)
424
364 const_iv (EV, LOOP_NONBLOCK) 425 const_iv (EV, UNLOOP_CANCEL)
365 const_iv (EV, UNLOOP_ONE) 426 const_iv (EV, UNLOOP_ONE)
366 const_iv (EV, UNLOOP_ALL) 427 const_iv (EV, UNLOOP_ALL)
367 428
368 const_iv (EV, BACKEND_SELECT) 429 const_iv (EV, BACKEND_SELECT)
369 const_iv (EV, BACKEND_POLL) 430 const_iv (EV, BACKEND_POLL)
372 const_iv (EV, BACKEND_DEVPOLL) 433 const_iv (EV, BACKEND_DEVPOLL)
373 const_iv (EV, BACKEND_PORT) 434 const_iv (EV, BACKEND_PORT)
374 const_iv (EV, FLAG_AUTO) 435 const_iv (EV, FLAG_AUTO)
375 const_iv (EV, FLAG_NOENV) 436 const_iv (EV, FLAG_NOENV)
376 const_iv (EV, FLAG_FORKCHECK) 437 const_iv (EV, FLAG_FORKCHECK)
438
439 const_iv (EV_, VERSION_MAJOR)
440 const_iv (EV_, VERSION_MINOR)
377 }; 441 };
378 442
379 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; ) 443 for (civ = const_iv + sizeof (const_iv) / sizeof (const_iv [0]); civ-- > const_iv; )
380 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv)); 444 newCONSTSUB (stash, (char *)civ->name, newSViv (civ->iv));
381 445
411 evapi.loop_new = ev_loop_new; 475 evapi.loop_new = ev_loop_new;
412 evapi.loop_destroy = ev_loop_destroy; 476 evapi.loop_destroy = ev_loop_destroy;
413 evapi.loop_fork = ev_loop_fork; 477 evapi.loop_fork = ev_loop_fork;
414 evapi.loop_count = ev_loop_count; 478 evapi.loop_count = ev_loop_count;
415 evapi.now = ev_now; 479 evapi.now = ev_now;
480 evapi.now_update = ev_now_update;
481 evapi.suspend = ev_suspend;
482 evapi.resume = ev_resume;
416 evapi.backend = ev_backend; 483 evapi.backend = ev_backend;
417 evapi.unloop = ev_unloop; 484 evapi.unloop = ev_unloop;
418 evapi.ref = ev_ref; 485 evapi.ref = ev_ref;
419 evapi.unref = ev_unref; 486 evapi.unref = ev_unref;
420 evapi.loop = ev_loop; 487 evapi.loop = ev_loop;
486 553
487unsigned int ev_recommended_backends () 554unsigned int ev_recommended_backends ()
488 555
489unsigned int ev_embeddable_backends () 556unsigned int ev_embeddable_backends ()
490 557
558void ev_sleep (NV interval)
559
491NV ev_time () 560NV ev_time ()
492 561
493NV ev_now () 562NV ev_now ()
563 C_ARGS: evapi.default_loop
564
565void ev_now_update ()
566 C_ARGS: evapi.default_loop
567
568void ev_suspend ()
569 C_ARGS: evapi.default_loop
570
571void ev_resume ()
494 C_ARGS: evapi.default_loop 572 C_ARGS: evapi.default_loop
495 573
496unsigned int ev_backend () 574unsigned int ev_backend ()
497 C_ARGS: evapi.default_loop 575 C_ARGS: evapi.default_loop
498 576
703 RETVAL = w->e_flags & WFLAG_KEEPALIVE; 781 RETVAL = w->e_flags & WFLAG_KEEPALIVE;
704 new_value = new_value ? WFLAG_KEEPALIVE : 0; 782 new_value = new_value ? WFLAG_KEEPALIVE : 0;
705 783
706 if (items > 1 && ((new_value ^ w->e_flags) & WFLAG_KEEPALIVE)) 784 if (items > 1 && ((new_value ^ w->e_flags) & WFLAG_KEEPALIVE))
707 { 785 {
786 w->e_flags = (w->e_flags & ~WFLAG_KEEPALIVE) | new_value;
708 REF (w); 787 REF (w);
709 w->e_flags = (w->e_flags & ~WFLAG_KEEPALIVE) | new_value;
710 UNREF (w); 788 UNREF (w);
711 } 789 }
712} 790}
713 OUTPUT: 791 OUTPUT:
714 RETVAL 792 RETVAL
889 967
890void ev_timer_again (ev_timer *w) 968void ev_timer_again (ev_timer *w)
891 INIT: 969 INIT:
892 CHECK_REPEAT (w->repeat); 970 CHECK_REPEAT (w->repeat);
893 CODE: 971 CODE:
894 REF (w);
895 ev_timer_again (e_loop (w), w); 972 ev_timer_again (e_loop (w), w);
896 UNREF (w); 973 UNREF (w);
897 974
898void DESTROY (ev_timer *w) 975void DESTROY (ev_timer *w)
899 CODE: 976 CODE:
918 CODE: 995 CODE:
919 STOP (periodic, w); 996 STOP (periodic, w);
920 997
921void ev_periodic_again (ev_periodic *w) 998void ev_periodic_again (ev_periodic *w)
922 CODE: 999 CODE:
923 REF (w);
924 ev_periodic_again (e_loop (w), w); 1000 ev_periodic_again (e_loop (w), w);
925 UNREF (w); 1001 UNREF (w);
926 1002
927void DESTROY (ev_periodic *w) 1003void DESTROY (ev_periodic *w)
928 CODE: 1004 CODE:
1209 1285
1210void ev_loop_verify (struct ev_loop *loop) 1286void ev_loop_verify (struct ev_loop *loop)
1211 1287
1212NV ev_now (struct ev_loop *loop) 1288NV ev_now (struct ev_loop *loop)
1213 1289
1290void ev_now_update (struct ev_loop *loop)
1291
1292void ev_suspend (struct ev_loop *loop)
1293
1294void ev_resume (struct ev_loop *loop)
1295
1214void ev_set_io_collect_interval (struct ev_loop *loop, NV interval) 1296void ev_set_io_collect_interval (struct ev_loop *loop, NV interval)
1215 1297
1216void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval) 1298void ev_set_timeout_collect_interval (struct ev_loop *loop, NV interval)
1217 1299
1218unsigned int ev_backend (struct ev_loop *loop) 1300unsigned int ev_backend (struct ev_loop *loop)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines