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.13 by root, Fri Dec 1 19:41:06 2006 UTC vs.
Revision 1.18 by root, Fri Dec 29 11:37:49 2006 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_PRIO 3 /* hardcoded in Coro::Event */ 14
15#define CD_HITS 4 /* hardcoded in Coro::Event */ 15#define CD_HITS 4 /* hardcoded in Coro::Event */
16#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */ 16#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
17#define CD_MAX 5 17#define CD_MAX 5
18 18
19static void 19static void
20coro_std_cb (pe_event *pe) 20coro_std_cb (pe_event *pe)
21{ 21{
22 AV *priv = (AV *)pe->ext_data; 22 AV *priv = (AV *)pe->ext_data;
23 IV type = SvIV (AvARRAY (priv)[CD_TYPE]); 23 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
24 AV *cd_wait;
24 SV **cd_coro; 25 SV *coro;
25 26
26 SvIV_set (AvARRAY (priv)[CD_PRIO], pe->prio);
27 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits); 27 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
28
29 if (type == 1)
30 SvIV_set (AvARRAY (priv)[CD_GOT], ((pe_ioevent *)pe)->got); 28 SvIV_set (AvARRAY (priv)[CD_GOT], type ? ((pe_ioevent *)pe)->got : 0);
31
32 GEventAPI->stop (pe->up, 0);
33 29
34 AvARRAY (priv)[CD_OK] = &PL_sv_yes; 30 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
35 31
36 cd_coro = &AvARRAY(priv)[CD_CORO]; 32 cd_wait = (AV *)AvARRAY(priv)[CD_WAIT];
37 if (*cd_coro != &PL_sv_undef) 33 if ((coro = av_shift (cd_wait)))
38 { 34 {
39 AvARRAY (priv)[CD_OK] = &PL_sv_yes; 35 if (av_len (cd_wait) < 0)
36 GEventAPI->stop (pe->up, 0);
37
40 CORO_READY (*cd_coro); 38 CORO_READY (coro);
41 SvREFCNT_dec (*cd_coro); 39 SvREFCNT_dec (coro);
42 *cd_coro = &PL_sv_undef;
43 } 40 }
44} 41}
45 42
46static void 43static void
47asynccheck_hook (void *data) 44asynccheck_hook (void *data)
48{ 45{
49 /* ceding from C means allocating a stack, but we assume this is a rare case */ 46 /* this loops as long as we have _other_ coros with the same or higher priority */
47 while (CORO_NREADY && CORO_CEDE)
48 ;
49}
50
51static double
52prepare_hook (void *data)
53{
54 /* this yields once to another coro with any priority */
50 while (CORO_NREADY) 55 if (CORO_NREADY)
51 CORO_CEDE; 56 {
57 CORO_CEDE_NOTSELF;
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 */
52} 67}
53 68
54MODULE = Coro::Event PACKAGE = Coro::Event 69MODULE = Coro::Event PACKAGE = Coro::Event
55 70
56PROTOTYPES: ENABLE 71PROTOTYPES: ENABLE
59{ 74{
60 I_EVENT_API ("Coro::Event"); 75 I_EVENT_API ("Coro::Event");
61 I_CORO_API ("Coro::Event"); 76 I_CORO_API ("Coro::Event");
62 77
63 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0); 78 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_hook, 0);
79 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
64} 80}
65 81
66void 82void
67_install_std_cb (SV *self, int type) 83_install_std_cb (SV *self, int type)
68 CODE: 84 CODE:
74 90
75 { 91 {
76 AV *priv = newAV (); 92 AV *priv = newAV ();
77 SV *rv = newRV_noinc ((SV *)priv); 93 SV *rv = newRV_noinc ((SV *)priv);
78 94
79 av_extend (priv, CD_MAX); 95 av_fill (priv, CD_MAX);
80 AvARRAY (priv)[CD_CORO] = &PL_sv_undef; 96 AvARRAY (priv)[CD_WAIT] = (SV *)newAV (); /* badbad */
81 AvARRAY (priv)[CD_TYPE] = newSViv (type); 97 AvARRAY (priv)[CD_TYPE] = newSViv (type);
82 AvARRAY (priv)[CD_OK ] = &PL_sv_no; 98 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
83 AvARRAY (priv)[CD_PRIO] = newSViv (0);
84 AvARRAY (priv)[CD_HITS] = newSViv (0); 99 AvARRAY (priv)[CD_HITS] = newSViv (0);
85 AvARRAY (priv)[CD_GOT ] = newSViv (0); 100 AvARRAY (priv)[CD_GOT ] = newSViv (0);
86 SvREADONLY_on (priv); 101 SvREADONLY_on (priv);
87 102
88 w->callback = coro_std_cb; 103 w->callback = coro_std_cb;
105 { 120 {
106 AvARRAY (priv)[CD_OK] = &PL_sv_no; 121 AvARRAY (priv)[CD_OK] = &PL_sv_no;
107 XSRETURN_NO; /* got an event */ 122 XSRETURN_NO; /* got an event */
108 } 123 }
109 124
125 av_push ((AV *)AvARRAY (priv)[CD_WAIT], SvREFCNT_inc (CORO_CURRENT));
126
110 if (!w->running) 127 if (!w->running)
111 GEventAPI->start (w, 1); 128 GEventAPI->start (w, 1);
112
113 if (AvARRAY (priv)[CD_CORO] == &PL_sv_undef)
114 AvARRAY (priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
115 else if (AvARRAY (priv)[CD_CORO] != CORO_CURRENT)
116 croak ("Coro::Event::next can only be called from a single coroutine at a time, caught");
117 129
118 XSRETURN_YES; /* schedule */ 130 XSRETURN_YES; /* schedule */
119} 131}
120 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