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.14 by root, Fri Dec 1 20:49:54 2006 UTC vs.
Revision 1.22 by root, Sun Sep 23 21:52:04 2007 UTC

6#include <string.h> 6#include <string.h>
7 7
8#include "EventAPI.h" 8#include "EventAPI.h"
9#include "../Coro/CoroAPI.h" 9#include "../Coro/CoroAPI.h"
10 10
11#define CD_CORO 0 11#define CD_WAIT 0 /* wait queue */
12#define CD_TYPE 1 12#define CD_TYPE 1
13#define CD_OK 2 13#define CD_OK 2
14#define CD_HITS 3 /* hardcoded in Coro::Event */
15#define CD_GOT 4 /* hardcoded in Coro::Event, Coro::Handle */
16#define CD_MAX 4
14 17
15#define CD_HITS 4 /* hardcoded in Coro::Event */ 18static HV *coro_event_event_stash;
16#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */ 19
17#define CD_MAX 5 20#define PERL_MAGIC_coro_event 0x18 /* to avoid clashes with e.g. event */
18 21
19static void 22static void
20coro_std_cb (pe_event *pe) 23coro_std_cb (pe_event *pe)
21{ 24{
22 AV *priv = (AV *)pe->ext_data; 25 AV *priv = (AV *)pe->ext_data;
23 IV type = SvIV (AvARRAY (priv)[CD_TYPE]); 26 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
27 AV *cd_wait;
24 SV **cd_coro; 28 SV *coro;
25 29
26 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits); 30 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
27
28 if (type == 1)
29 SvIV_set (AvARRAY (priv)[CD_GOT], ((pe_ioevent *)pe)->got); 31 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0);
30
31 GEventAPI->stop (pe->up, 0);
32 32
33 AvARRAY (priv)[CD_OK] = &PL_sv_yes; 33 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
34 34
35 cd_coro = &AvARRAY(priv)[CD_CORO]; 35 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
36
37 coro = av_shift (cd_wait);
36 if (*cd_coro != &PL_sv_undef) 38 if (coro != &PL_sv_undef)
37 { 39 {
38 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
39 CORO_READY (*cd_coro); 40 CORO_READY (coro);
40 SvREFCNT_dec (*cd_coro); 41 SvREFCNT_dec (coro);
41 *cd_coro = &PL_sv_undef;
42 } 42 }
43
44 if (av_len (cd_wait) < 0)
45 GEventAPI->stop (pe->up, 0);
43} 46}
44 47
45static void 48static void
46asynccheck_hook (void *data) 49asynccheck_hook (void *data)
47{ 50{
48 /* ceding from C means allocating a stack, but we assume this is a rare case */ 51 /* this loops as long as we have _other_ coros with the same or higher priority */
52 while (CORO_NREADY && CORO_CEDE)
53 ;
54}
55
56static double
57prepare_hook (void *data)
58{
59 /* this yields once to another coro with any priority */
49 while (CORO_NREADY) 60 if (CORO_NREADY)
50 CORO_CEDE; 61 {
62 CORO_CEDE_NOTSELF;
63 /*
64 * timers might have changed, and Event fails to notice this
65 * so we have to assume the worst. If Event didn't have that bug,
66 * we would only need to do this if CORO_NREADY is != 0 now.
67 */
68 return 0.;
69 }
70 else
71 return 85197.73; /* this is as good as any value, but it factors badly with common values */
51} 72}
52 73
53MODULE = Coro::Event PACKAGE = Coro::Event 74MODULE = Coro::Event PACKAGE = Coro::Event
54 75
55PROTOTYPES: ENABLE 76PROTOTYPES: ENABLE
56 77
57BOOT: 78BOOT:
58{ 79{
80 coro_event_event_stash = gv_stashpv ("Coro::Event::Event", TRUE);
81
59 I_EVENT_API ("Coro::Event"); 82 I_EVENT_API ("Coro::Event");
60 I_CORO_API ("Coro::Event"); 83 I_CORO_API ("Coro::Event");
61 84
62 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0); 85 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
86 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
63} 87}
64 88
65void 89void
66_install_std_cb (SV *self, int type) 90_install_std_cb (SV *self, int type)
67 CODE: 91 CODE:
71 if (w->callback) 95 if (w->callback)
72 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught"); 96 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
73 97
74 { 98 {
75 AV *priv = newAV (); 99 AV *priv = newAV ();
76 SV *rv = newRV_noinc ((SV *)priv);
77 100
78 av_extend (priv, CD_MAX); 101 av_fill (priv, CD_MAX);
79 AvARRAY (priv)[CD_CORO] = &PL_sv_undef; 102 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* AV in AV _should_ not be exposed to perl */
80 AvARRAY (priv)[CD_TYPE] = newSViv (type); 103 AvARRAY (priv)[CD_TYPE] = newSViv (type);
81 AvARRAY (priv)[CD_OK ] = &PL_sv_no; 104 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
82 AvARRAY (priv)[CD_HITS] = newSViv (0); 105 AvARRAY (priv)[CD_HITS] = newSViv (0);
83 AvARRAY (priv)[CD_GOT ] = newSViv (0); 106 AvARRAY (priv)[CD_GOT ] = newSViv (0);
84 SvREADONLY_on (priv); 107 SvREADONLY_on (priv);
85 108
86 w->callback = coro_std_cb; 109 w->callback = coro_std_cb;
87 w->ext_data = priv; 110 w->ext_data = priv;
88 111
89 /* make sure Event does not use PERL_MAGIC_uvar, which */ 112 sv_magicext (SvRV (self), newRV_noinc ((SV *)priv), PERL_MAGIC_coro_event, 0, (char *)w, 0);
90 /* we abuse for non-uvar purposes. */
91 sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0);
92 } 113 }
93} 114}
94 115
95void 116void
96_next (SV *self) 117_next (SV *self)
103 { 124 {
104 AvARRAY (priv)[CD_OK] = &PL_sv_no; 125 AvARRAY (priv)[CD_OK] = &PL_sv_no;
105 XSRETURN_NO; /* got an event */ 126 XSRETURN_NO; /* got an event */
106 } 127 }
107 128
129 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
130
108 if (!w->running) 131 if (!w->running)
109 {
110 SvIV_set (AvARRAY (priv)[CD_GOT], 0);
111 SvIV_set (AvARRAY (priv)[CD_HITS], 0);
112
113 GEventAPI->start (w, 1); 132 GEventAPI->start (w, 1);
114 }
115
116 if (AvARRAY (priv)[CD_CORO] == &PL_sv_undef)
117 AvARRAY (priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
118 else if (AvARRAY (priv)[CD_CORO] != CORO_CURRENT)
119 croak ("Coro::Event::next can only be called from a single coroutine at a time, caught");
120 133
121 XSRETURN_YES; /* schedule */ 134 XSRETURN_YES; /* schedule */
122} 135}
123 136
124SV * 137SV *
131 { 144 {
132 pe_watcher *w = GEventAPI->sv_2watcher (self); 145 pe_watcher *w = GEventAPI->sv_2watcher (self);
133 AV *priv = (AV *)w->ext_data; 146 AV *priv = (AV *)w->ext_data;
134 147
135 RETVAL = newRV_inc ((SV *)priv); 148 RETVAL = newRV_inc ((SV *)priv);
149
150 /* may need to bless it now */
151 if (!SvOBJECT (priv))
152 {
153 SvREADONLY_off ((SV *)priv);
154 sv_bless (RETVAL, coro_event_event_stash);
155 SvREADONLY_on ((SV *)priv);
156 }
136 } 157 }
137} 158}
138 OUTPUT: 159 OUTPUT:
139 RETVAL 160 RETVAL
140 161

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines