… | |
… | |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.10; |
41 | $VERSION = 0.45; |
42 | |
42 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
47 | my @async; |
47 | my @async; |
|
|
48 | my $init; |
48 | |
49 | |
49 | # this way of handling attributes simply is NOT scalable ;() |
50 | # this way of handling attributes simply is NOT scalable ;() |
50 | sub import { |
51 | sub import { |
51 | Coro->export_to_level(1, @_); |
52 | Coro->export_to_level(1, @_); |
52 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
53 | my $old = *{(caller)[0]."::MODIFY_CODE_ATTRIBUTES"}{CODE}; |
… | |
… | |
54 | my ($package, $ref) = (shift, shift); |
55 | my ($package, $ref) = (shift, shift); |
55 | my @attrs; |
56 | my @attrs; |
56 | for (@_) { |
57 | for (@_) { |
57 | if ($_ eq "Coro") { |
58 | if ($_ eq "Coro") { |
58 | push @async, $ref; |
59 | push @async, $ref; |
|
|
60 | unless ($init++) { |
|
|
61 | eval q{ |
|
|
62 | sub INIT { |
|
|
63 | &async(pop @async) while @async; |
|
|
64 | } |
|
|
65 | }; |
|
|
66 | } |
59 | } else { |
67 | } else { |
60 | push @attrs, $_; |
68 | push @attrs, $_; |
61 | } |
69 | } |
62 | } |
70 | } |
63 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
71 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
64 | }; |
72 | }; |
65 | } |
73 | } |
66 | |
74 | |
67 | sub INIT { |
|
|
68 | &async(pop @async) while @async; |
|
|
69 | } |
|
|
70 | } |
75 | } |
71 | |
76 | |
72 | =item $main |
77 | =item $main |
73 | |
78 | |
74 | This coroutine represents the main program. |
79 | This coroutine represents the main program. |
… | |
… | |
103 | our $idle = new Coro sub { |
108 | our $idle = new Coro sub { |
104 | print STDERR "FATAL: deadlock detected\n"; |
109 | print STDERR "FATAL: deadlock detected\n"; |
105 | exit(51); |
110 | exit(51); |
106 | }; |
111 | }; |
107 | |
112 | |
108 | # we really need priorities... |
|
|
109 | my @ready; # the ready queue. hehe, rather broken ;) |
|
|
110 | |
|
|
111 | # static methods. not really. |
|
|
112 | |
|
|
113 | =head2 STATIC METHODS |
|
|
114 | |
|
|
115 | Static methods are actually functions that operate on the current process only. |
|
|
116 | |
|
|
117 | =over 4 |
|
|
118 | |
|
|
119 | =item async { ... } [@args...] |
|
|
120 | |
|
|
121 | Create a new asynchronous process and return it's process object |
|
|
122 | (usually unused). When the sub returns the new process is automatically |
|
|
123 | terminated. |
|
|
124 | |
|
|
125 | # create a new coroutine that just prints its arguments |
|
|
126 | async { |
|
|
127 | print "@_\n"; |
|
|
128 | } 1,2,3,4; |
|
|
129 | |
|
|
130 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
131 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
132 | |
|
|
133 | =cut |
|
|
134 | |
|
|
135 | sub async(&@) { |
|
|
136 | my $pid = new Coro @_; |
|
|
137 | $pid->ready; |
|
|
138 | $pid; |
|
|
139 | } |
|
|
140 | |
|
|
141 | =item schedule |
|
|
142 | |
|
|
143 | Calls the scheduler. Please note that the current process will not be put |
|
|
144 | into the ready queue, so calling this function usually means you will |
|
|
145 | never be called again. |
|
|
146 | |
|
|
147 | =cut |
|
|
148 | |
|
|
149 | my $prev; |
|
|
150 | |
|
|
151 | sub schedule { |
|
|
152 | # should be done using priorities :( |
|
|
153 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
154 | Coro::State::transfer($prev, $current); |
|
|
155 | } |
|
|
156 | |
|
|
157 | =item cede |
|
|
158 | |
|
|
159 | "Cede" to other processes. This function puts the current process into the |
|
|
160 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
161 | current "timeslice" to other coroutines of the same or higher priority. |
|
|
162 | |
|
|
163 | =cut |
|
|
164 | |
|
|
165 | sub cede { |
|
|
166 | $current->ready; |
|
|
167 | &schedule; |
|
|
168 | } |
|
|
169 | |
|
|
170 | =item terminate |
|
|
171 | |
|
|
172 | Terminates the current process. |
|
|
173 | |
|
|
174 | Future versions of this function will allow result arguments. |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
|
|
178 | # this coroutine is necessary because a coroutine |
113 | # this coroutine is necessary because a coroutine |
179 | # cannot destroy itself. |
114 | # cannot destroy itself. |
180 | my @destroy; |
115 | my @destroy; |
181 | my $terminate = new Coro sub { |
116 | my $manager = new Coro sub { |
182 | while() { |
117 | while() { |
183 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
118 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
184 | &schedule; |
119 | &schedule; |
185 | } |
120 | } |
186 | }; |
121 | }; |
187 | |
122 | |
|
|
123 | # static methods. not really. |
|
|
124 | |
|
|
125 | =head2 STATIC METHODS |
|
|
126 | |
|
|
127 | Static methods are actually functions that operate on the current process only. |
|
|
128 | |
|
|
129 | =over 4 |
|
|
130 | |
|
|
131 | =item async { ... } [@args...] |
|
|
132 | |
|
|
133 | Create a new asynchronous process and return it's process object |
|
|
134 | (usually unused). When the sub returns the new process is automatically |
|
|
135 | terminated. |
|
|
136 | |
|
|
137 | # create a new coroutine that just prints its arguments |
|
|
138 | async { |
|
|
139 | print "@_\n"; |
|
|
140 | } 1,2,3,4; |
|
|
141 | |
|
|
142 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
143 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
144 | |
|
|
145 | =cut |
|
|
146 | |
|
|
147 | sub async(&@) { |
|
|
148 | my $pid = new Coro @_; |
|
|
149 | $manager->ready; # this ensures that the stack is cloned from the manager |
|
|
150 | $pid->ready; |
|
|
151 | $pid; |
|
|
152 | } |
|
|
153 | |
|
|
154 | =item schedule |
|
|
155 | |
|
|
156 | Calls the scheduler. Please note that the current process will not be put |
|
|
157 | into the ready queue, so calling this function usually means you will |
|
|
158 | never be called again. |
|
|
159 | |
|
|
160 | =cut |
|
|
161 | |
|
|
162 | =item cede |
|
|
163 | |
|
|
164 | "Cede" to other processes. This function puts the current process into the |
|
|
165 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
166 | current "timeslice" to other coroutines of the same or higher priority. |
|
|
167 | |
|
|
168 | =cut |
|
|
169 | |
|
|
170 | =item terminate |
|
|
171 | |
|
|
172 | Terminates the current process. |
|
|
173 | |
|
|
174 | Future versions of this function will allow result arguments. |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
188 | sub terminate { |
178 | sub terminate { |
189 | push @destroy, $current; |
179 | $current->cancel; |
190 | $terminate->ready; |
|
|
191 | &schedule; |
180 | &schedule; |
192 | # NORETURN |
181 | die; # NORETURN |
193 | } |
182 | } |
194 | |
183 | |
195 | =back |
184 | =back |
196 | |
185 | |
197 | # dynamic methods |
186 | # dynamic methods |
… | |
… | |
228 | |
217 | |
229 | Put the current process into the ready queue. |
218 | Put the current process into the ready queue. |
230 | |
219 | |
231 | =cut |
220 | =cut |
232 | |
221 | |
233 | sub ready { |
222 | =item $process->cancel |
|
|
223 | |
|
|
224 | Like C<terminate>, but terminates the specified process instead. |
|
|
225 | |
|
|
226 | =cut |
|
|
227 | |
|
|
228 | sub cancel { |
234 | push @ready, $_[0]; |
229 | push @destroy, $_[0]; |
|
|
230 | $manager->ready; |
235 | } |
231 | } |
236 | |
232 | |
237 | =back |
233 | =back |
238 | |
234 | |
239 | =cut |
235 | =cut |
… | |
… | |
255 | allow per-thread schedulers, but Coro::State does not yet allow this). |
251 | allow per-thread schedulers, but Coro::State does not yet allow this). |
256 | |
252 | |
257 | =head1 SEE ALSO |
253 | =head1 SEE ALSO |
258 | |
254 | |
259 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
255 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
260 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
256 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>, |
|
|
257 | L<Coro::Handle>, L<Coro::Socket>. |
261 | |
258 | |
262 | =head1 AUTHOR |
259 | =head1 AUTHOR |
263 | |
260 | |
264 | Marc Lehmann <pcg@goof.com> |
261 | Marc Lehmann <pcg@goof.com> |
265 | http://www.goof.com/pcg/marc/ |
262 | http://www.goof.com/pcg/marc/ |