… | |
… | |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.12; |
41 | $VERSION = 0.45; |
42 | |
42 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
… | |
… | |
118 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
118 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
119 | &schedule; |
119 | &schedule; |
120 | } |
120 | } |
121 | }; |
121 | }; |
122 | |
122 | |
123 | # we really need priorities... |
|
|
124 | my @ready; # the ready queue. hehe, rather broken ;) |
|
|
125 | |
|
|
126 | # static methods. not really. |
123 | # static methods. not really. |
127 | |
124 | |
128 | =head2 STATIC METHODS |
125 | =head2 STATIC METHODS |
129 | |
126 | |
130 | Static methods are actually functions that operate on the current process only. |
127 | Static methods are actually functions that operate on the current process only. |
… | |
… | |
160 | into the ready queue, so calling this function usually means you will |
157 | into the ready queue, so calling this function usually means you will |
161 | never be called again. |
158 | never be called again. |
162 | |
159 | |
163 | =cut |
160 | =cut |
164 | |
161 | |
165 | my $prev; |
|
|
166 | |
|
|
167 | sub schedule { |
|
|
168 | # should be done using priorities :( |
|
|
169 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
170 | Coro::State::transfer($prev, $current); |
|
|
171 | } |
|
|
172 | |
|
|
173 | =item cede |
162 | =item cede |
174 | |
163 | |
175 | "Cede" to other processes. This function puts the current process into the |
164 | "Cede" to other processes. This function puts the current process into the |
176 | ready queue and calls C<schedule>, which has the effect of giving up the |
165 | ready queue and calls C<schedule>, which has the effect of giving up the |
177 | current "timeslice" to other coroutines of the same or higher priority. |
166 | current "timeslice" to other coroutines of the same or higher priority. |
178 | |
167 | |
179 | =cut |
168 | =cut |
180 | |
169 | |
181 | sub cede { |
170 | =item terminate |
|
|
171 | |
|
|
172 | Terminates the current process. |
|
|
173 | |
|
|
174 | Future versions of this function will allow result arguments. |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
|
|
178 | sub terminate { |
182 | $current->ready; |
179 | $current->cancel; |
183 | &schedule; |
180 | &schedule; |
184 | } |
|
|
185 | |
|
|
186 | =item terminate |
|
|
187 | |
|
|
188 | Terminates the current process. |
|
|
189 | |
|
|
190 | Future versions of this function will allow result arguments. |
|
|
191 | |
|
|
192 | =cut |
|
|
193 | |
|
|
194 | sub terminate { |
|
|
195 | push @destroy, $current; |
|
|
196 | $manager->ready; |
|
|
197 | &schedule; |
|
|
198 | # NORETURN |
181 | die; # NORETURN |
199 | } |
182 | } |
200 | |
183 | |
201 | =back |
184 | =back |
202 | |
185 | |
203 | # dynamic methods |
186 | # dynamic methods |
… | |
… | |
234 | |
217 | |
235 | Put the current process into the ready queue. |
218 | Put the current process into the ready queue. |
236 | |
219 | |
237 | =cut |
220 | =cut |
238 | |
221 | |
239 | sub ready { |
222 | =item $process->cancel |
|
|
223 | |
|
|
224 | Like C<terminate>, but terminates the specified process instead. |
|
|
225 | |
|
|
226 | =cut |
|
|
227 | |
|
|
228 | sub cancel { |
240 | push @ready, $_[0]; |
229 | push @destroy, $_[0]; |
|
|
230 | $manager->ready; |
241 | } |
231 | } |
242 | |
232 | |
243 | =back |
233 | =back |
244 | |
234 | |
245 | =cut |
235 | =cut |