… | |
… | |
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 |
… | |
… | |
235 | by calling the ready method. |
233 | by calling the ready method. |
236 | |
234 | |
237 | =cut |
235 | =cut |
238 | |
236 | |
239 | sub _new_coro { |
237 | sub _new_coro { |
240 | # $current->_clear_idle_sp; # set the idle sp on the following cede |
238 | $current->_clear_idle_sp; # (re-)set the idle sp on the following cede |
241 | _set_cede_self; # ensures that cede cede's us first |
239 | _set_cede_self; # ensures that cede cede's us first |
242 | cede; |
240 | cede; |
243 | terminate &{+shift}; |
241 | terminate &{+shift}; |
244 | } |
242 | } |
245 | |
243 | |