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.4 by root, Fri Aug 17 03:33:00 2001 UTC vs.
Revision 1.12 by root, Fri Dec 1 02:17:37 2006 UTC

2#include "perl.h" 2#include "perl.h"
3#include "XSUB.h" 3#include "XSUB.h"
4 4
5#include <string.h> 5#include <string.h>
6 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#include "EventAPI.h" 19#include "EventAPI.h"
8#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
9 25
10#define CD_CORO 0 26#define CD_CORO 0
11#define CD_TYPE 1 27#define CD_TYPE 1
12#define CD_OK 2 28#define CD_OK 2
29#define CD_PRIO 3 /* hardcoded in Coro::Event */
30#define CD_HITS 4 /* hardcoded in Coro::Event */
13#define CD_GOT 3 /* hardcoded in Coro::Event, Coro::Handle */ 31#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
14#define CD_MAX 3 32#define CD_MAX 5
15/* no support for hits and prio so far. */
16 33
17#define EV_CLASS "Coro::Event" 34#define EV_CLASS "Coro::Event"
18
19static pe_idle *scheduler;
20static int do_schedule;
21
22#define NEED_SCHEDULE if (!do_schedule) \
23 { \
24 do_schedule = 1; \
25 GEventAPI->now ((pe_watcher *)scheduler); \
26 }
27 35
28static void 36static void
29coro_std_cb(pe_event *pe) 37coro_std_cb(pe_event *pe)
30{ 38{
31 AV *priv = (AV *)SvRV ((SV *)pe->ext_data); 39 AV *priv = (AV *)pe->ext_data;
32 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1)); 40 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1));
33 SV *cd_coro = *av_fetch (priv, CD_CORO, 1); 41 SV **cd_coro;
42
43 sv_setiv (AvARRAY(priv)[CD_PRIO], pe->prio);
44 sv_setiv (AvARRAY(priv)[CD_HITS], pe->hits);
34 45
35 if (type == 1) 46 if (type == 1)
36 av_store (priv, CD_GOT, newSViv (((pe_ioevent *)pe)->got)); 47 sv_setiv (AvARRAY(priv)[CD_GOT], ((pe_ioevent *)pe)->got);
37 48
38 if (SvROK (cd_coro)) 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)
39 { 55 {
56 AvARRAY(priv)[CD_OK] = &PL_sv_yes;
40 CORO_READY (cd_coro); 57 CORO_READY (*cd_coro);
41 av_store (priv, CD_CORO, &PL_sv_undef); 58 SvREFCNT_dec (*cd_coro);
42 NEED_SCHEDULE; 59 *cd_coro = &PL_sv_undef;
43 }
44 else
45 {
46 av_store (priv, CD_OK, &PL_sv_yes);
47 GEventAPI->stop (pe->up, 0);
48 } 60 }
49} 61}
50 62
51static void 63static double
52scheduler_cb(pe_event *pe) 64prepare_hook (void *data)
53{ 65{
54 while (CORO_NREADY) 66 while (CORO_NREADY)
55 CORO_CEDE; 67 CORO_CEDE;
56 68
57 do_schedule = 0; 69 return 1e10;
58} 70}
59 71
60MODULE = Coro::Event PACKAGE = Coro::Event 72MODULE = Coro::Event PACKAGE = Coro::Event
61 73
62PROTOTYPES: ENABLE 74PROTOTYPES: ENABLE
63 75
64BOOT: 76BOOT:
65{ 77{
66 I_EVENT_API("Coro::Event"); 78 I_EVENT_API ("Coro::Event");
67 I_CORO_API ("Coro::Event"); 79 I_CORO_API ("Coro::Event");
68 80
69 /* create a fake idle handler (we only ever call now) */ 81 GEventAPI->add_hook ("prepare", (void *)prepare_hook, 0);
70 scheduler = GEventAPI->new_idle (0, 0);
71 scheduler->base.callback = scheduler_cb;
72 scheduler->min_interval = newSVnv (0);
73 scheduler->max_interval = newSVnv (0);
74 GEventAPI->stop ((pe_watcher *)scheduler, 0);
75} 82}
76 83
77void 84void
78_install_std_cb(self,type) 85_install_std_cb(self,type)
79 SV * self 86 SV * self
80 int type 87 int type
81 CODE: 88 CODE:
89{
82 pe_watcher *w = GEventAPI->sv_2watcher (self); 90 pe_watcher *w = GEventAPI->sv_2watcher (self);
83 AV *priv = newAV ();
84 SV *rv = newRV_noinc ((SV *)priv);
85 91
86 av_extend (priv, CD_MAX); 92 if (WaFLAGS (w) & PE_PERLCB)
87 av_store (priv, CD_TYPE, newSViv (type)); 93 croak ("Coro::Event watchers must not have a perl callback (see Coro::Event), caught");
94 {
95 AV *priv = newAV ();
96 SV *rv = newRV_noinc ((SV *)priv);
88 97
89 w->callback = coro_std_cb; 98 av_extend (priv, CD_MAX);
90 w->ext_data = rv; 99 av_store (priv, CD_CORO, &PL_sv_undef);
100 av_store (priv, CD_TYPE, newSViv (type));
101 av_store (priv, CD_OK , &PL_sv_no);
102 av_store (priv, CD_PRIO, newSViv (0));
103 av_store (priv, CD_HITS, newSViv (0));
104 av_store (priv, CD_GOT , type ? newSViv (0) : &PL_sv_undef);
105 SvREADONLY_on (priv);
91 106
107 w->callback = coro_std_cb;
108 w->ext_data = priv;
109
92 hv_store ((HV *)SvRV (self), 110 hv_store ((HV *)SvRV (self),
93 EV_CLASS, strlen (EV_CLASS), 111 EV_CLASS, strlen (EV_CLASS),
94 rv, 0); 112 rv, 0);
113
114 GEventAPI->start (w, 0);
115 }
116}
95 117
96void 118void
97_next(self) 119_next(self)
98 SV * self 120 SV * self
99 CODE: 121 CODE:
122{
100 pe_watcher *w = GEventAPI->sv_2watcher (self); 123 pe_watcher *w = GEventAPI->sv_2watcher (self);
101 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 124 AV *priv = (AV *)w->ext_data;
102 125
103 if (SvOK (*av_fetch (priv, CD_CORO, 1))) 126 if (AvARRAY(priv)[CD_OK] == &PL_sv_yes)
104 croak ("only one coroutine can wait for an event"); 127 {
128 AvARRAY(priv)[CD_OK] = &PL_sv_no;
129 XSRETURN_NO;
130 }
105 131
106 if (!w->running) 132 if (!w->running)
107 GEventAPI->start (w, 1); 133 GEventAPI->start (w, 1);
108 134
109 if (*av_fetch (priv, CD_OK, 1) == &PL_sv_yes) 135 if (AvARRAY(priv)[CD_CORO] == &PL_sv_undef)
110 {
111 av_store (priv, CD_OK, &PL_sv_no);
112 XSRETURN_NO;
113 }
114 else
115 {
116 av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT)); 136 AvARRAY(priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
117 XSRETURN_YES;
118 }
119 137
138 XSRETURN_YES;
139}
140

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines