… | |
… | |
38 | |
38 | |
39 | use vars qw($idle $main $current); |
39 | use vars qw($idle $main $current); |
40 | |
40 | |
41 | use base Exporter; |
41 | use base Exporter; |
42 | |
42 | |
43 | $VERSION = 0.8; |
43 | $VERSION = "0.9"; |
44 | |
44 | |
45 | @EXPORT = qw(async cede schedule terminate current); |
45 | @EXPORT = qw(async cede schedule terminate current); |
46 | %EXPORT_TAGS = ( |
46 | %EXPORT_TAGS = ( |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
47 | prio => [qw(PRIO_MAX PRIO_HIGH PRIO_NORMAL PRIO_LOW PRIO_IDLE PRIO_MIN)], |
48 | ); |
48 | ); |
… | |
… | |
120 | # this coroutine is necessary because a coroutine |
120 | # this coroutine is necessary because a coroutine |
121 | # cannot destroy itself. |
121 | # cannot destroy itself. |
122 | my @destroy; |
122 | my @destroy; |
123 | my $manager; |
123 | my $manager; |
124 | $manager = new Coro sub { |
124 | $manager = new Coro sub { |
125 | while() { |
125 | while () { |
126 | # by overwriting the state object with the manager we destroy it |
126 | # by overwriting the state object with the manager we destroy it |
127 | # while still being able to schedule this coroutine (in case it has |
127 | # while still being able to schedule this coroutine (in case it has |
128 | # been readied multiple times. this is harmless since the manager |
128 | # been readied multiple times. this is harmless since the manager |
129 | # can be called as many times as neccessary and will always |
129 | # can be called as many times as neccessary and will always |
130 | # remove itself from the runqueue |
130 | # remove itself from the runqueue |
… | |
… | |
157 | # create a new coroutine that just prints its arguments |
157 | # create a new coroutine that just prints its arguments |
158 | async { |
158 | async { |
159 | print "@_\n"; |
159 | print "@_\n"; |
160 | } 1,2,3,4; |
160 | } 1,2,3,4; |
161 | |
161 | |
162 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
163 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
164 | |
|
|
165 | =cut |
162 | =cut |
166 | |
163 | |
167 | sub async(&@) { |
164 | sub async(&@) { |
168 | my $pid = new Coro @_; |
165 | my $pid = new Coro @_; |
169 | $manager->ready; # this ensures that the stack is cloned from the manager |
166 | $manager->ready; # this ensures that the stack is cloned from the manager |