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.13 by root, Fri Dec 1 19:41:06 2006 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 <assert.h>
5#include <string.h> 6#include <string.h>
6 7
7#include "EventAPI.h" 8#include "EventAPI.h"
8#include "../Coro/CoroAPI.h" 9#include "../Coro/CoroAPI.h"
9 10
10#define CD_CORO 0 11#define CD_CORO 0
11#define CD_TYPE 1 12#define CD_TYPE 1
12#define CD_OK 2 13#define CD_OK 2
14#define CD_PRIO 3 /* hardcoded in Coro::Event */
15#define CD_HITS 4 /* hardcoded in Coro::Event */
13#define CD_GOT 3 /* hardcoded in Coro::Event, Coro::Handle */ 16#define CD_GOT 5 /* hardcoded in Coro::Event, Coro::Handle */
14#define CD_MAX 3 17#define CD_MAX 5
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 18
28static void 19static void
29coro_std_cb(pe_event *pe) 20coro_std_cb (pe_event *pe)
30{ 21{
31 AV *priv = (AV *)SvRV ((SV *)pe->ext_data); 22 AV *priv = (AV *)pe->ext_data;
32 IV type = SvIV (*av_fetch (priv, CD_TYPE, 1)); 23 IV type = SvIV (AvARRAY (priv)[CD_TYPE]);
33 SV *cd_coro = *av_fetch (priv, CD_CORO, 1); 24 SV **cd_coro;
25
26 SvIV_set (AvARRAY (priv)[CD_PRIO], pe->prio);
27 SvIV_set (AvARRAY (priv)[CD_HITS], pe->hits);
34 28
35 if (type == 1) 29 if (type == 1)
36 av_store (priv, CD_GOT, newSViv (((pe_ioevent *)pe)->got)); 30 SvIV_set (AvARRAY (priv)[CD_GOT], ((pe_ioevent *)pe)->got);
37 31
38 if (SvROK (cd_coro)) 32 GEventAPI->stop (pe->up, 0);
33
34 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
35
36 cd_coro = &AvARRAY(priv)[CD_CORO];
37 if (*cd_coro != &PL_sv_undef)
39 { 38 {
39 AvARRAY (priv)[CD_OK] = &PL_sv_yes;
40 CORO_READY (cd_coro); 40 CORO_READY (*cd_coro);
41 av_store (priv, CD_CORO, &PL_sv_undef); 41 SvREFCNT_dec (*cd_coro);
42 NEED_SCHEDULE; 42 *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 } 43 }
49} 44}
50 45
51static void 46static void
52scheduler_cb(pe_event *pe) 47asynccheck_hook (void *data)
53{ 48{
49 /* ceding from C means allocating a stack, but we assume this is a rare case */
54 while (CORO_NREADY) 50 while (CORO_NREADY)
55 CORO_CEDE; 51 CORO_CEDE;
56
57 do_schedule = 0;
58} 52}
59 53
60MODULE = Coro::Event PACKAGE = Coro::Event 54MODULE = Coro::Event PACKAGE = Coro::Event
61 55
62PROTOTYPES: ENABLE 56PROTOTYPES: ENABLE
63 57
64BOOT: 58BOOT:
65{ 59{
66 I_EVENT_API("Coro::Event"); 60 I_EVENT_API ("Coro::Event");
67 I_CORO_API ("Coro::Event"); 61 I_CORO_API ("Coro::Event");
68 62
69 /* create a fake idle handler (we only ever call now) */ 63 GEventAPI->add_hook ("asynccheck", (void *)asynccheck_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} 64}
76 65
77void 66void
78_install_std_cb(self,type) 67_install_std_cb (SV *self, int type)
79 SV * self
80 int type
81 CODE: 68 CODE:
69{
82 pe_watcher *w = GEventAPI->sv_2watcher (self); 70 pe_watcher *w = GEventAPI->sv_2watcher (self);
83 AV *priv = newAV ();
84 SV *rv = newRV_noinc ((SV *)priv);
85 71
86 av_extend (priv, CD_MAX); 72 if (w->callback)
87 av_store (priv, CD_TYPE, newSViv (type)); 73 croak ("Coro::Event watchers must not have a callback (see Coro::Event), caught");
88 74
89 w->callback = coro_std_cb; 75 {
90 w->ext_data = rv; 76 AV *priv = newAV ();
77 SV *rv = newRV_noinc ((SV *)priv);
91 78
92 hv_store ((HV *)SvRV (self), 79 av_extend (priv, CD_MAX);
93 EV_CLASS, strlen (EV_CLASS), 80 AvARRAY (priv)[CD_CORO] = &PL_sv_undef;
94 rv, 0); 81 AvARRAY (priv)[CD_TYPE] = newSViv (type);
82 AvARRAY (priv)[CD_OK ] = &PL_sv_no;
83 AvARRAY (priv)[CD_PRIO] = newSViv (0);
84 AvARRAY (priv)[CD_HITS] = newSViv (0);
85 AvARRAY (priv)[CD_GOT ] = newSViv (0);
86 SvREADONLY_on (priv);
87
88 w->callback = coro_std_cb;
89 w->ext_data = priv;
90
91 /* make sure Event does not use PERL_MAGIC_uvar, which */
92 /* we abuse for non-uvar purposes. */
93 sv_magicext (SvRV (self), rv, PERL_MAGIC_uvar, 0, 0, 0);
94 }
95}
95 96
96void 97void
97_next(self) 98_next (SV *self)
98 SV * self
99 CODE: 99 CODE:
100{
100 pe_watcher *w = GEventAPI->sv_2watcher (self); 101 pe_watcher *w = GEventAPI->sv_2watcher (self);
101 AV *priv = (AV *)SvRV ((SV *)w->ext_data); 102 AV *priv = (AV *)w->ext_data;
102 103
103 if (SvOK (*av_fetch (priv, CD_CORO, 1))) 104 if (AvARRAY (priv)[CD_OK] == &PL_sv_yes)
104 croak ("only one coroutine can wait for an event"); 105 {
106 AvARRAY (priv)[CD_OK] = &PL_sv_no;
107 XSRETURN_NO; /* got an event */
108 }
105 109
106 if (!w->running) 110 if (!w->running)
107 GEventAPI->start (w, 1); 111 GEventAPI->start (w, 1);
108 112
109 if (*av_fetch (priv, CD_OK, 1) == &PL_sv_yes) 113 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)); 114 AvARRAY (priv)[CD_CORO] = SvREFCNT_inc (CORO_CURRENT);
117 XSRETURN_YES; 115 else if (AvARRAY (priv)[CD_CORO] != CORO_CURRENT)
118 } 116 croak ("Coro::Event::next can only be called from a single coroutine at a time, caught");
119 117
118 XSRETURN_YES; /* schedule */
119}
120

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines