… | |
… | |
133 | }; |
133 | }; |
134 | |
134 | |
135 | # this coroutine is necessary because a coroutine |
135 | # this coroutine is necessary because a coroutine |
136 | # cannot destroy itself. |
136 | # cannot destroy itself. |
137 | my @destroy; |
137 | my @destroy; |
138 | my $manager; |
|
|
139 | $manager = new Coro sub { |
138 | my $manager; $manager = new Coro sub { |
140 | while () { |
139 | while () { |
141 | # by overwriting the state object with the manager we destroy it |
140 | # by overwriting the state object with the manager we destroy it |
142 | # while still being able to schedule this coroutine (in case it has |
141 | # while still being able to schedule this coroutine (in case it has |
143 | # been readied multiple times. this is harmless since the manager |
142 | # been readied multiple times. this is harmless since the manager |
144 | # can be called as many times as neccessary and will always |
143 | # can be called as many times as neccessary and will always |
… | |
… | |
184 | |
183 | |
185 | =cut |
184 | =cut |
186 | |
185 | |
187 | sub async(&@) { |
186 | sub async(&@) { |
188 | my $pid = new Coro @_; |
187 | my $pid = new Coro @_; |
189 | $manager->ready; # this ensures that the stack is cloned from the manager |
|
|
190 | $pid->ready; |
188 | $pid->ready; |
191 | $pid; |
189 | $pid |
192 | } |
190 | } |
193 | |
191 | |
194 | =item schedule |
192 | =item schedule |
195 | |
193 | |
196 | Calls the scheduler. Please note that the current process will not be put |
194 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
234 | called. To make the process run you must first put it into the ready queue |
232 | called. To make the process run you must first put it into the ready queue |
235 | by calling the ready method. |
233 | by calling the ready method. |
236 | |
234 | |
237 | =cut |
235 | =cut |
238 | |
236 | |
239 | sub _newcoro { |
237 | sub _new_coro { |
240 | terminate &{+shift}; |
238 | terminate &{+shift}; |
241 | } |
239 | } |
242 | |
240 | |
243 | sub new { |
241 | sub new { |
244 | my $class = shift; |
242 | my $class = shift; |
245 | |
243 | |
246 | $class->SUPER::new (\&_newcoro, @_) |
244 | $class->SUPER::new (\&_new_coro, @_) |
247 | } |
245 | } |
248 | |
246 | |
249 | =item $process->ready |
247 | =item $process->ready |
250 | |
248 | |
251 | Put the given process into the ready queue. |
249 | Put the given process into the ready queue. |