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.12 by root, Fri Dec 1 02:17:37 2006 UTC vs.
Revision 1.18 by root, Fri Dec 29 11:37:49 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines