… | |
… | |
41 | |
41 | |
42 | our $idle; # idle handler |
42 | our $idle; # idle handler |
43 | our $main; # main coroutine |
43 | our $main; # main coroutine |
44 | our $current; # current coroutine |
44 | our $current; # current coroutine |
45 | |
45 | |
46 | our $VERSION = '2.5'; |
46 | our $VERSION = '3.0'; |
47 | |
47 | |
48 | our @EXPORT = qw(async cede schedule terminate current); |
48 | our @EXPORT = qw(async cede schedule terminate current); |
49 | our %EXPORT_TAGS = ( |
49 | our %EXPORT_TAGS = ( |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
50 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
51 | ); |
51 | ); |
… | |
… | |
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 |
… | |
… | |
172 | |
171 | |
173 | Create a new asynchronous process and return it's process object |
172 | Create a new asynchronous process and return it's process object |
174 | (usually unused). When the sub returns the new process is automatically |
173 | (usually unused). When the sub returns the new process is automatically |
175 | terminated. |
174 | terminated. |
176 | |
175 | |
|
|
176 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
|
|
177 | |
177 | When the coroutine dies, the program will exit, just as in the main |
178 | When the coroutine dies, the program will exit, just as in the main |
178 | program. |
179 | program. |
179 | |
180 | |
180 | # create a new coroutine that just prints its arguments |
181 | # create a new coroutine that just prints its arguments |
181 | async { |
182 | async { |
… | |
… | |
184 | |
185 | |
185 | =cut |
186 | =cut |
186 | |
187 | |
187 | sub async(&@) { |
188 | sub async(&@) { |
188 | my $pid = new Coro @_; |
189 | my $pid = new Coro @_; |
189 | $manager->ready; # this ensures that the stack is cloned from the manager |
|
|
190 | $pid->ready; |
190 | $pid->ready; |
191 | $pid; |
191 | $pid |
192 | } |
192 | } |
193 | |
193 | |
194 | =item schedule |
194 | =item schedule |
195 | |
195 | |
196 | Calls the scheduler. Please note that the current process will not be put |
196 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
232 | Create a new process and return it. When the sub returns the process |
232 | Create a new process and return it. When the sub returns the process |
233 | automatically terminates as if C<terminate> with the returned values were |
233 | automatically terminates as if C<terminate> with the returned values were |
234 | called. To make the process run you must first put it into the ready queue |
234 | called. To make the process run you must first put it into the ready queue |
235 | by calling the ready method. |
235 | by calling the ready method. |
236 | |
236 | |
|
|
237 | Calling C<exit> in a coroutine will not work correctly, so do not do that. |
|
|
238 | |
237 | =cut |
239 | =cut |
238 | |
240 | |
239 | sub _new_coro { |
241 | sub _new_coro { |
240 | # $current->_clear_idle_sp; # set the idle sp on the following cede |
|
|
241 | _set_cede_self; # ensures that cede cede's us first |
|
|
242 | cede; |
|
|
243 | terminate &{+shift}; |
242 | terminate &{+shift}; |
244 | } |
243 | } |
245 | |
244 | |
246 | sub new { |
245 | sub new { |
247 | my $class = shift; |
246 | my $class = shift; |