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.1 by root, Thu Aug 16 21:55:35 2001 UTC vs.
Revision 1.3 by root, Fri Aug 17 01:45:39 2001 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 <string.h>
6
7#include "EventAPI.h"
5#include "CoroAPI.h" 8#include "../Coro/CoroAPI.h"
9
10#define CD_CORO 0
11#define CD_TYPE 1
12#define CD_W 2
13#define CD_GOT 3
14#define CD_MAX 3
15/* no support for hits and prio so far. */
16
17#define EV_CLASS "Coro::Event::Ev"
18
19static HV *ev_stash;
20static pe_idle *scheduler;
21static int do_schedule;
22
23#define NEED_SCHEDULE if (!do_schedule) \
24 { \
25 do_schedule = 1; \
26 GEventAPI->now ((pe_watcher *)scheduler); \
27 }
28static void
29coro_std_cb(pe_event *pe)
30{
31 AV *av = (AV *)SvRV ((SV *)pe->ext_data);
32 IV type = SvIV (*av_fetch (av, CD_TYPE, 1));
33 SV *cd_coro = *av_fetch (av, CD_CORO, 1);
34
35 av_store (av, CD_W, SvREFCNT_inc (pe->up->mysv));
36
37 if (type == 1)
38 av_store (av, CD_GOT, newSViv (((pe_ioevent *)pe)->got));
39
40 GEventAPI->stop (pe->up, 0);
41
42 if (SvROK (cd_coro))
43 {
44 CORO_READY (cd_coro);
45 NEED_SCHEDULE;
46 }
47}
48
49static void
50scheduler_cb(pe_event *pe)
51{
52 while (CORO_NREADY)
53 CORO_CEDE;
54
55 do_schedule = 0;
56}
6 57
7MODULE = Coro::Event PACKAGE = Coro::Event 58MODULE = Coro::Event PACKAGE = Coro::Event
59
60PROTOTYPES: ENABLE
8 61
9BOOT: 62BOOT:
10{ 63{
11 I_EVENT_API("Coro::Event"); 64 I_EVENT_API("Coro::Event");
12 I_CORO_API ("Coro::Event"); 65 I_CORO_API ("Coro::Event");
66
67 ev_stash = gv_stashpv (EV_CLASS, TRUE);
68
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 scheduler->min_interval = newSVnv (0);
73 scheduler->max_interval = newSVnv (0);
74 GEventAPI->stop ((pe_watcher *)scheduler, 0);
13} 75}
76
77void
78_install_std_cb(self,type)
79 SV * self
80 int type
81 CODE:
82 pe_watcher *w = GEventAPI->sv_2watcher (self);
83 AV *priv = newAV ();
84 SV *rv;
85
86 av_extend (priv, CD_MAX);
87 av_store (priv, CD_TYPE, newSViv (type));
88
89 rv = sv_bless (newRV_noinc ((SV *)priv), ev_stash);
90
91 hv_store ((HV *)SvRV (self),
92 EV_CLASS, strlen (EV_CLASS),
93 rv, 0);
94
95 w->ext_data = rv;
96 w->callback = coro_std_cb;
97
98void
99_next0(self)
100 SV * self
101 CODE:
102 pe_watcher *w = GEventAPI->sv_2watcher (self);
103 AV *priv = (AV *)SvRV ((SV *)w->ext_data);
104
105 GEventAPI->start (w, 1);
106
107 if (SvROK (*av_fetch (priv, CD_CORO, 1)))
108 croak ("only one coroutine can wait for an event");
109
110 av_store (priv, CD_CORO, SvREFCNT_inc (CORO_CURRENT));
111
112SV *
113_next1(self)
114 SV * self
115 CODE:
116 pe_watcher *w = GEventAPI->sv_2watcher (self);
117 AV *priv = (AV *)SvRV ((SV *)w->ext_data);
118
119 av_store (priv, CD_CORO, &PL_sv_undef);
120
121 RETVAL = SvREFCNT_inc ((SV *)w->ext_data);
122 OUTPUT:
123 RETVAL
124

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines