… | |
… | |
18 | |
18 | |
19 | yield; |
19 | yield; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
|
|
23 | This module collection manages coroutines. Coroutines are similar to |
|
|
24 | Threads but don't run in parallel. |
|
|
25 | |
|
|
26 | This module is still experimental, see the BUGS section below. |
|
|
27 | |
23 | =cut |
28 | =cut |
24 | |
29 | |
25 | package Coro; |
30 | package Coro; |
26 | |
31 | |
27 | use Coro::State; |
32 | use Coro::State; |
28 | |
33 | |
29 | use base Exporter; |
34 | use base Exporter; |
30 | |
35 | |
31 | $VERSION = 0.03; |
36 | $VERSION = 0.09; |
32 | |
37 | |
33 | @EXPORT = qw(async yield schedule); |
38 | @EXPORT = qw(async yield schedule terminate current); |
34 | @EXPORT_OK = qw($current); |
39 | @EXPORT_OK = qw($current); |
35 | |
40 | |
36 | { |
41 | { |
37 | use subs 'async'; |
42 | use subs 'async'; |
38 | |
43 | |
… | |
… | |
47 | my @attrs; |
52 | my @attrs; |
48 | for (@_) { |
53 | for (@_) { |
49 | if ($_ eq "Coro") { |
54 | if ($_ eq "Coro") { |
50 | push @async, $ref; |
55 | push @async, $ref; |
51 | } else { |
56 | } else { |
52 | push @attrs, @_; |
57 | push @attrs, $_; |
53 | } |
58 | } |
54 | } |
59 | } |
55 | return $old ? $old->($package, $name, @attrs) : @attrs; |
60 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
56 | }; |
61 | }; |
57 | } |
62 | } |
58 | |
63 | |
59 | sub INIT { |
64 | sub INIT { |
60 | async pop @async while @async; |
65 | async pop @async while @async; |
61 | } |
66 | } |
62 | } |
67 | } |
63 | |
68 | |
64 | my $idle = new Coro sub { |
|
|
65 | &yield while 1; |
|
|
66 | }; |
|
|
67 | |
|
|
68 | =item $main |
69 | =item $main |
69 | |
70 | |
70 | This coroutine represents the main program. |
71 | This coroutine represents the main program. |
71 | |
72 | |
72 | =cut |
73 | =cut |
73 | |
74 | |
74 | $main = new Coro; |
75 | our $main = new Coro; |
75 | |
76 | |
76 | =item $current |
77 | =item $current (or as function: current) |
77 | |
78 | |
78 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
79 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
79 | |
80 | |
80 | =cut |
81 | =cut |
81 | |
82 | |
82 | # maybe some other module used Coro::Specific before... |
83 | # maybe some other module used Coro::Specific before... |
83 | if ($current) { |
84 | if ($current) { |
84 | $main->{specific} = $current->{specific}; |
85 | $main->{specific} = $current->{specific}; |
85 | } |
86 | } |
86 | |
87 | |
87 | $current = $main; |
88 | our $current = $main; |
|
|
89 | |
|
|
90 | sub current() { $current } |
|
|
91 | |
|
|
92 | =item $idle |
|
|
93 | |
|
|
94 | The coroutine to switch to when no other coroutine is running. The default |
|
|
95 | implementation prints "FATAL: deadlock detected" and exits. |
|
|
96 | |
|
|
97 | =cut |
|
|
98 | |
|
|
99 | # should be done using priorities :( |
|
|
100 | our $idle = new Coro sub { |
|
|
101 | print STDERR "FATAL: deadlock detected\n"; |
|
|
102 | exit(51); |
|
|
103 | }; |
88 | |
104 | |
89 | # we really need priorities... |
105 | # we really need priorities... |
90 | my @ready = (); # the ready queue. hehe, rather broken ;) |
106 | my @ready; # the ready queue. hehe, rather broken ;) |
91 | |
107 | |
92 | # static methods. not really. |
108 | # static methods. not really. |
93 | |
109 | |
94 | =head2 STATIC METHODS |
110 | =head2 STATIC METHODS |
95 | |
111 | |
96 | Static methods are actually functions that operate on the current process only. |
112 | Static methods are actually functions that operate on the current process only. |
97 | |
113 | |
98 | =over 4 |
114 | =over 4 |
99 | |
115 | |
100 | =item async { ... }; |
116 | =item async { ... } [@args...] |
101 | |
117 | |
102 | Create a new asynchronous process and return it's process object |
118 | Create a new asynchronous process and return it's process object |
103 | (usually unused). When the sub returns the new process is automatically |
119 | (usually unused). When the sub returns the new process is automatically |
104 | terminated. |
120 | terminated. |
105 | |
121 | |
106 | =cut |
122 | # create a new coroutine that just prints its arguments |
|
|
123 | async { |
|
|
124 | print "@_\n"; |
|
|
125 | } 1,2,3,4; |
107 | |
126 | |
|
|
127 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
128 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
129 | |
|
|
130 | =cut |
|
|
131 | |
108 | sub async(&) { |
132 | sub async(&@) { |
109 | (new Coro $_[0])->ready; |
133 | my $pid = new Coro @_; |
|
|
134 | $pid->ready; |
|
|
135 | $pid; |
110 | } |
136 | } |
111 | |
137 | |
112 | =item schedule |
138 | =item schedule |
113 | |
139 | |
114 | Calls the scheduler. Please note that the current process will not be put |
140 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
118 | =cut |
144 | =cut |
119 | |
145 | |
120 | my $prev; |
146 | my $prev; |
121 | |
147 | |
122 | sub schedule { |
148 | sub schedule { |
|
|
149 | # should be done using priorities :( |
123 | ($prev, $current) = ($current, shift @ready); |
150 | ($prev, $current) = ($current, shift @ready || $idle); |
124 | Coro::State::transfer($prev, $current); |
151 | Coro::State::transfer($prev, $current); |
125 | } |
152 | } |
126 | |
153 | |
127 | =item yield |
154 | =item yield |
128 | |
155 | |
… | |
… | |
138 | |
165 | |
139 | =item terminate |
166 | =item terminate |
140 | |
167 | |
141 | Terminates the current process. |
168 | Terminates the current process. |
142 | |
169 | |
|
|
170 | Future versions of this function will allow result arguments. |
|
|
171 | |
143 | =cut |
172 | =cut |
144 | |
173 | |
145 | sub terminate { |
174 | sub terminate { |
|
|
175 | $current->{_results} = [@_]; |
146 | &schedule; |
176 | &schedule; |
147 | } |
177 | } |
148 | |
178 | |
149 | =back |
179 | =back |
150 | |
180 | |
… | |
… | |
154 | |
184 | |
155 | These are the methods you can call on process objects. |
185 | These are the methods you can call on process objects. |
156 | |
186 | |
157 | =over 4 |
187 | =over 4 |
158 | |
188 | |
159 | =item new Coro \⊂ |
189 | =item new Coro \&sub [, @args...] |
160 | |
190 | |
161 | Create a new process and return it. When the sub returns the process |
191 | Create a new process and return it. When the sub returns the process |
162 | automatically terminates. To start the process you must first put it into |
192 | automatically terminates. To start the process you must first put it into |
163 | the ready queue by calling the ready method. |
193 | the ready queue by calling the ready method. |
164 | |
194 | |
|
|
195 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
196 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
197 | |
165 | =cut |
198 | =cut |
|
|
199 | |
|
|
200 | sub _newcoro { |
|
|
201 | terminate &{+shift}; |
|
|
202 | } |
166 | |
203 | |
167 | sub new { |
204 | sub new { |
168 | my $class = shift; |
205 | my $class = shift; |
169 | my $proc = $_[0]; |
|
|
170 | bless { |
206 | bless { |
171 | _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), |
207 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
172 | }, $class; |
208 | }, $class; |
173 | } |
209 | } |
174 | |
210 | |
175 | =item $process->ready |
211 | =item $process->ready |
176 | |
212 | |
… | |
… | |
186 | |
222 | |
187 | =cut |
223 | =cut |
188 | |
224 | |
189 | 1; |
225 | 1; |
190 | |
226 | |
|
|
227 | =head1 BUGS/LIMITATIONS |
|
|
228 | |
|
|
229 | - could be faster, especially when the core would introduce special |
|
|
230 | support for coroutines (like it does for threads). |
|
|
231 | - there is still a memleak on coroutine termination that I could not |
|
|
232 | identify. Could be as small as a single SV. |
|
|
233 | - this module is not well-tested. |
|
|
234 | - if variables or arguments "disappear" (become undef) or become |
|
|
235 | corrupted please contact the author so he cen iron out the |
|
|
236 | remaining bugs. |
|
|
237 | - this module is not thread-safe. You must only ever use this module from |
|
|
238 | the same thread (this requirement might be loosened in the future to |
|
|
239 | allow per-thread schedulers, but Coro::Satte does not yet allow this). |
|
|
240 | |
|
|
241 | =head1 SEE ALSO |
|
|
242 | |
|
|
243 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
|
|
244 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
|
|
245 | |
191 | =head1 AUTHOR |
246 | =head1 AUTHOR |
192 | |
247 | |
193 | Marc Lehmann <pcg@goof.com> |
248 | Marc Lehmann <pcg@goof.com> |
194 | http://www.goof.com/pcg/marc/ |
249 | http://www.goof.com/pcg/marc/ |
195 | |
250 | |