… | |
… | |
47 | the current coroutine. |
47 | the current coroutine. |
48 | |
48 | |
49 | While this seems to work superficially, it will eventually cause memory |
49 | While this seems to work superficially, it will eventually cause memory |
50 | corruption. |
50 | corruption. |
51 | |
51 | |
|
|
52 | =head1 SEMANTICS |
|
|
53 | |
|
|
54 | Whenever Event blocks (e.g. in a call to C<one_event>, C<loop> etc.), |
|
|
55 | this module cede's to all other coroutines with the same or higher |
|
|
56 | priority. When any coroutines of lower priority are ready, it will not |
|
|
57 | block but run one of them and then check for events. |
|
|
58 | |
|
|
59 | The effect is that coroutines with the same or higher priority than |
|
|
60 | the blocking coroutine will keep Event from checking for events, while |
|
|
61 | coroutines with lower priority are being run, but Event checks for new |
|
|
62 | events after every cede. |
|
|
63 | |
52 | =head1 FUNCTIONS |
64 | =head1 FUNCTIONS |
53 | |
65 | |
54 | =over 4 |
66 | =over 4 |
55 | |
67 | |
56 | =cut |
68 | =cut |
… | |
… | |
71 | use base Exporter::; |
83 | use base Exporter::; |
72 | |
84 | |
73 | our @EXPORT = qw(loop unloop sweep); |
85 | our @EXPORT = qw(loop unloop sweep); |
74 | |
86 | |
75 | BEGIN { |
87 | BEGIN { |
76 | our $VERSION = 1.9; |
88 | our $VERSION = '2.0'; |
77 | |
89 | |
78 | local $^W = 0; # avoid redefine warning for Coro::ready; |
90 | local $^W = 0; # avoid redefine warning for Coro::ready; |
79 | XSLoader::load __PACKAGE__, $VERSION; |
91 | XSLoader::load __PACKAGE__, $VERSION; |
80 | } |
92 | } |
81 | |
93 | |
… | |
… | |
116 | |
128 | |
117 | shift eq Coro::Event:: |
129 | shift eq Coro::Event:: |
118 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
130 | or croak "event constructor \"Coro::Event->$flavour\" must be called as a static method"; |
119 | |
131 | |
120 | my $w = $new->($class, |
132 | my $w = $new->($class, |
121 | desc => $flavour, |
133 | desc => $flavour, |
122 | @_, |
134 | @_, |
123 | parked => 1, |
135 | parked => 1, |
124 | ); |
136 | ); |
|
|
137 | |
125 | _install_std_cb($w, $type); |
138 | _install_std_cb $w, $type; |
126 | bless $w, $class; # reblessing due to broken Event |
139 | |
|
|
140 | # reblessing due to Event being broken |
|
|
141 | bless $w, $class |
127 | }; |
142 | }; |
128 | *{ $flavour } = $coronew; |
143 | *{ $flavour } = $coronew; |
129 | *{"do_$flavour"} = sub { |
144 | *{"do_$flavour"} = sub { |
130 | unshift @_, Coro::Event::; |
145 | unshift @_, Coro::Event::; |
131 | my $e = (&$coronew)->next; |
146 | @_ = &$coronew; |
132 | $e->cancel; # $e === $e->w |
147 | &Coro::schedule while &_next; |
133 | $e; |
148 | $_[0]->cancel; |
|
|
149 | &_event |
134 | }; |
150 | }; |
135 | } |
151 | } |
136 | |
152 | |
137 | # double calls to avoid stack-cloning ;() |
153 | # do schedule in perl to avoid forcing a stack allocation. |
138 | # is about 10% slower, though. |
154 | # this is about 10% slower, though. |
139 | sub next($) { |
155 | sub next($) { |
140 | &Coro::schedule while &_next; |
156 | &Coro::schedule while &_next; |
141 | |
157 | |
142 | $_[0] |
158 | &_event |
143 | } |
159 | } |
144 | |
160 | |
145 | sub Coro::Event::w { $_[0] } |
161 | sub Coro::Event::w { $_[0] } |
146 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
162 | sub Coro::Event::prio { $_[0]{Coro::Event}[3] } |
147 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |
163 | sub Coro::Event::hits { $_[0]{Coro::Event}[4] } |