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.4 by root, Fri Aug 17 03:33:00 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_OK 2
13#define CD_GOT 3 /* hardcoded in Coro::Event, Coro::Handle */
14#define CD_MAX 3
15/* no support for hits and prio so far. */
16
17#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
28static void
29coro_std_cb(pe_event *pe)
30{
31 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
35 if (type == 1)
36 av_store (priv, CD_GOT, newSViv (((pe_ioevent *)pe)->got));
37
38 if (SvROK (cd_coro))
39 {
40 CORO_READY (cd_coro);
41 av_store (priv, CD_CORO, &PL_sv_undef);
42 NEED_SCHEDULE;
43 }
44 else
45 {
46 av_store (priv, CD_OK, &PL_sv_yes);
47 GEventAPI->stop (pe->up, 0);
48 }
49}
50
51static void
52scheduler_cb(pe_event *pe)
53{
54 while (CORO_NREADY)
55 CORO_CEDE;
56
57 do_schedule = 0;
58}
6 59
7MODULE = Coro::Event PACKAGE = Coro::Event 60MODULE = Coro::Event PACKAGE = Coro::Event
61
62PROTOTYPES: ENABLE
8 63
9BOOT: 64BOOT:
10{ 65{
11 I_EVENT_API("Coro::Event"); 66 I_EVENT_API("Coro::Event");
12 I_CORO_API ("Coro::Event"); 67 I_CORO_API ("Coro::Event");
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 = newRV_noinc ((SV *)priv);
85
86 av_extend (priv, CD_MAX);
87 av_store (priv, CD_TYPE, newSViv (type));
88
89 w->callback = coro_std_cb;
90 w->ext_data = rv;
91
92 hv_store ((HV *)SvRV (self),
93 EV_CLASS, strlen (EV_CLASS),
94 rv, 0);
95
96void
97_next(self)
98 SV * self
99 CODE:
100 pe_watcher *w = GEventAPI->sv_2watcher (self);
101 AV *priv = (AV *)SvRV ((SV *)w->ext_data);
102
103 if (SvOK (*av_fetch (priv, CD_CORO, 1)))
104 croak ("only one coroutine can wait for an event");
105
106 if (!w->running)
107 GEventAPI->start (w, 1);
108
109 if (*av_fetch (priv, CD_OK, 1) == &PL_sv_yes)
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));
117 XSRETURN_YES;
118 }
119

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines