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

Comparing Coro/Event/Event.xs (file contents):
Revision 1.7 by root, Mon Sep 24 01:36:20 2001 UTC vs.
Revision 1.10 by root, Thu Nov 23 23:54:02 2006 UTC

9confess (const char *msg) 9confess (const char *msg)
10{ 10{
11 dSP; 11 dSP;
12 12
13 PUSHMARK(SP); 13 PUSHMARK(SP);
14 XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event",0))); 14 XPUSHs (sv_2mortal(newSVpv("only one coroutine can wait for an event at any given time",0)));
15 PUTBACK; 15 PUTBACK;
16 call_pv ("Carp::confess", G_VOID); 16 call_pv ("Carp::confess", G_VOID);
17} 17}
18 18
19#include "EventAPI.h" 19#include "EventAPI.h"
20#include "../Coro/CoroAPI.h" 20#include "../Coro/CoroAPI.h"
21
22#ifndef PE_PERLCB
23# define PE_PERLCB 0x020 /* not public, but we need it :( */
24#endif
21 25
22#define CD_CORO 0 26#define CD_CORO 0
23#define CD_TYPE 1 27#define CD_TYPE 1
24#define CD_OK 2 28#define CD_OK 2
25#define CD_PRIO 3 /* hardcoded in Coro::Event */ 29#define CD_PRIO 3 /* hardcoded in Coro::Event */
26#define CD_HITS 4 /* hardcoded in Coro::Event */ 30#define CD_HITS 4 /* hardcoded in Coro::Event */
27#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */ 31#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
28#define CD_MAX 5 32#define CD_MAX 5
29 33
30#define EV_CLASS "Coro::Event" 34#define EV_CLASS "Coro::Event"
31
32static pe_idle *scheduler;
33static int do_schedule;
34
35#define NEED_SCHEDULE if (!do_schedule) \
36 { \
37 do_schedule = 1; \
38 GEventAPI->now ((pe_watcher *)scheduler); \
39 }
40 35
41static void 36static void
42coro_std_cb(pe_event *pe) 37coro_std_cb(pe_event *pe)
43{ 38{
44 AV *priv = (AV *)pe->ext_data; 39 AV *priv = (AV *)pe->ext_data;
54 if (*cd_coro != &PL_sv_undef) 49 if (*cd_coro != &PL_sv_undef)
55 { 50 {
56 CORO_READY (*cd_coro); 51 CORO_READY (*cd_coro);
57 SvREFCNT_dec (*cd_coro); 52 SvREFCNT_dec (*cd_coro);
58 *cd_coro = &PL_sv_undef; 53 *cd_coro = &PL_sv_undef;
59 NEED_SCHEDULE;
60 } 54 }
61 else 55 else
62 { 56 {
63 AvARRAY(priv)[CD_OK] = &PL_sv_yes; 57 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
64 GEventAPI->stop (pe->up, 0); 58 GEventAPI->stop (pe->up, 0);
65 } 59 }
66} 60}
67 61
68static void 62static double
69scheduler_cb(pe_event *pe) 63prepare_hook (void *data)
70{ 64{
71 while (CORO_NREADY) 65 while (CORO_NREADY)
72 CORO_CEDE; 66 CORO_CEDE;
73 67
74 do_schedule = 0; 68 return 1e20;
75} 69}
76 70
77MODULE = Coro::Event PACKAGE = Coro::Event 71MODULE = Coro::Event PACKAGE = Coro::Event
78 72
79PROTOTYPES: ENABLE 73PROTOTYPES: ENABLE
80 74
81BOOT: 75BOOT:
82{ 76{
83 I_EVENT_API("Coro::Event"); 77 I_EVENT_API ("Coro::Event");
84 I_CORO_API ("Coro::Event"); 78 I_CORO_API ("Coro::Event");
85 79
86 /* create a fake idle handler (we only ever call now) */ 80 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
87 scheduler = GEventAPI->new_idle (0, 0);
88 scheduler->base.callback = scheduler_cb;
89 scheduler->base.prio = PE_PRIO_NORMAL; /* StarvePrio */
90 scheduler->min_interval = newSVnv (0);
91 scheduler->max_interval = newSVnv (0);
92 GEventAPI->stop ((pe_watcher *)scheduler, 0);
93} 81}
94 82
95void 83void
96_install_std_cb(self,type) 84_install_std_cb(self,type)
97 SV * self 85 SV * self
98 int type 86 int type
99 CODE: 87 CODE:
100 pe_watcher *w = GEventAPI->sv_2watcher (self); 88 pe_watcher *w = GEventAPI->sv_2watcher (self);
101 AV *priv = newAV ();
102 SV *rv = newRV_noinc ((SV *)priv);
103 89
104 av_extend (priv, CD_MAX); 90 if (WaFLAGS (w) & PE_PERLCB)
105 av_store (priv, CD_CORO, &PL_sv_undef); 91 croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught");
106 av_store (priv, CD_TYPE, newSViv (type)); 92 {
107 av_store (priv, CD_OK , &PL_sv_no); 93 AV *priv = newAV ();
108 av_store (priv, CD_PRIO, newSViv (0)); 94 SV *rv = newRV_noinc ((SV *)priv);
109 av_store (priv, CD_HITS, newSViv (0));
110 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
111 SvREADONLY_on (priv);
112 95
113 w->callback = coro_std_cb; 96 av_extend (priv, CD_MAX);
114 w->ext_data = priv; 97 av_store (priv, CD_CORO, &PL_sv_undef);
98 av_store (priv, CD_TYPE, newSViv (type));
99 av_store (priv, CD_OK , &PL_sv_no);
100 av_store (priv, CD_PRIO, newSViv (0));
101 av_store (priv, CD_HITS, newSViv (0));
102 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
103 SvREADONLY_on (priv);
115 104
105 w->callback = coro_std_cb;
106 w->ext_data = priv;
107
116 hv_store ((HV *)SvRV (self), 108 hv_store ((HV *)SvRV (self),
117 EV_CLASS, strlen (EV_CLASS), 109 EV_CLASS, strlen (EV_CLASS),
118 rv, 0); 110 rv, 0);
111
112 GEventAPI->start (w, 0);
113 }
119 114
120void 115void
121_next(self) 116_next(self)
122 SV * self 117 SV * self
123 CODE: 118 CODE:
139 134
140 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT); 135 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
141 XSRETURN_YES; 136 XSRETURN_YES;
142 } 137 }
143 138
144MODULE = Coro::Event PACKAGE = Coro
145
146# overwrite the ready function
147void
148ready(self)
149 SV * self
150 PROTOTYPE: $
151 CODE:
152 NEED_SCHEDULE;
153 CORO_READY (self);
154
155

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines