ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Event/Event.xs
Revision: 1.5
Committed: Fri Aug 17 04:08:58 2001 UTC (22 years, 10 months ago) by root
Branch: MAIN
Changes since 1.4: +1 -0 lines
Log Message:
*** empty log message ***

File Contents

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