… | |
… | |
26 | |
26 | |
27 | use Coro::State; |
27 | use Coro::State; |
28 | |
28 | |
29 | use base Exporter; |
29 | use base Exporter; |
30 | |
30 | |
31 | $VERSION = 0.03; |
31 | $VERSION = 0.05; |
32 | |
32 | |
33 | @EXPORT = qw(async yield schedule); |
33 | @EXPORT = qw(async yield schedule terminate); |
34 | @EXPORT_OK = qw($current); |
34 | @EXPORT_OK = qw($current); |
35 | |
35 | |
36 | { |
36 | { |
37 | use subs 'async'; |
37 | use subs 'async'; |
38 | |
38 | |
… | |
… | |
59 | sub INIT { |
59 | sub INIT { |
60 | async pop @async while @async; |
60 | async pop @async while @async; |
61 | } |
61 | } |
62 | } |
62 | } |
63 | |
63 | |
64 | my $idle = new Coro sub { |
|
|
65 | &yield while 1; |
|
|
66 | }; |
|
|
67 | |
|
|
68 | =item $main |
64 | =item $main |
69 | |
65 | |
70 | This coroutine represents the main program. |
66 | This coroutine represents the main program. |
71 | |
67 | |
72 | =cut |
68 | =cut |
73 | |
69 | |
74 | $main = new Coro; |
70 | our $main = new Coro; |
75 | |
71 | |
76 | =item $current |
72 | =item $current |
77 | |
73 | |
78 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
74 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
79 | |
75 | |
… | |
… | |
82 | # maybe some other module used Coro::Specific before... |
78 | # maybe some other module used Coro::Specific before... |
83 | if ($current) { |
79 | if ($current) { |
84 | $main->{specific} = $current->{specific}; |
80 | $main->{specific} = $current->{specific}; |
85 | } |
81 | } |
86 | |
82 | |
87 | $current = $main; |
83 | our $current = $main; |
|
|
84 | |
|
|
85 | =item $idle |
|
|
86 | |
|
|
87 | The coroutine to switch to when no other coroutine is running. The default |
|
|
88 | implementation prints "FATAL: deadlock detected" and exits. |
|
|
89 | |
|
|
90 | =cut |
|
|
91 | |
|
|
92 | # should be done using priorities :( |
|
|
93 | our $idle = new Coro sub { |
|
|
94 | print STDERR "FATAL: deadlock detected\n"; |
|
|
95 | exit(51); |
|
|
96 | }; |
88 | |
97 | |
89 | # we really need priorities... |
98 | # we really need priorities... |
|
|
99 | ## my @ready; #d# |
90 | my @ready = (); # the ready queue. hehe, rather broken ;) |
100 | our @ready = (); # the ready queue. hehe, rather broken ;) |
91 | |
101 | |
92 | # static methods. not really. |
102 | # static methods. not really. |
93 | |
103 | |
94 | =head2 STATIC METHODS |
104 | =head2 STATIC METHODS |
95 | |
105 | |
96 | Static methods are actually functions that operate on the current process only. |
106 | Static methods are actually functions that operate on the current process only. |
97 | |
107 | |
98 | =over 4 |
108 | =over 4 |
99 | |
109 | |
100 | =item async { ... }; |
110 | =item async { ... } [@args...] |
101 | |
111 | |
102 | Create a new asynchronous process and return it's process object |
112 | Create a new asynchronous process and return it's process object |
103 | (usually unused). When the sub returns the new process is automatically |
113 | (usually unused). When the sub returns the new process is automatically |
104 | terminated. |
114 | terminated. |
105 | |
115 | |
106 | =cut |
116 | # create a new coroutine that just prints its arguments |
|
|
117 | async { |
|
|
118 | print "@_\n"; |
|
|
119 | } 1,2,3,4; |
107 | |
120 | |
|
|
121 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
122 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
123 | |
|
|
124 | =cut |
|
|
125 | |
108 | sub async(&) { |
126 | sub async(&@) { |
109 | (new Coro $_[0])->ready; |
127 | my $pid = new Coro @_; |
|
|
128 | $pid->ready; |
|
|
129 | $pid; |
110 | } |
130 | } |
111 | |
131 | |
112 | =item schedule |
132 | =item schedule |
113 | |
133 | |
114 | Calls the scheduler. Please note that the current process will not be put |
134 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
118 | =cut |
138 | =cut |
119 | |
139 | |
120 | my $prev; |
140 | my $prev; |
121 | |
141 | |
122 | sub schedule { |
142 | sub schedule { |
|
|
143 | # should be done using priorities :( |
123 | ($prev, $current) = ($current, shift @ready); |
144 | ($prev, $current) = ($current, shift @ready || $idle); |
124 | Coro::State::transfer($prev, $current); |
145 | Coro::State::transfer($prev, $current); |
125 | } |
146 | } |
126 | |
147 | |
127 | =item yield |
148 | =item yield |
128 | |
149 | |
… | |
… | |
138 | |
159 | |
139 | =item terminate |
160 | =item terminate |
140 | |
161 | |
141 | Terminates the current process. |
162 | Terminates the current process. |
142 | |
163 | |
|
|
164 | Future versions of this function will allow result arguments. |
|
|
165 | |
143 | =cut |
166 | =cut |
144 | |
167 | |
145 | sub terminate { |
168 | sub terminate { |
|
|
169 | $current->{_results} = [@_]; |
146 | &schedule; |
170 | &schedule; |
147 | } |
171 | } |
148 | |
172 | |
149 | =back |
173 | =back |
150 | |
174 | |
… | |
… | |
154 | |
178 | |
155 | These are the methods you can call on process objects. |
179 | These are the methods you can call on process objects. |
156 | |
180 | |
157 | =over 4 |
181 | =over 4 |
158 | |
182 | |
159 | =item new Coro \⊂ |
183 | =item new Coro \&sub [, @args...] |
160 | |
184 | |
161 | Create a new process and return it. When the sub returns the process |
185 | 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 |
186 | automatically terminates. To start the process you must first put it into |
163 | the ready queue by calling the ready method. |
187 | the ready queue by calling the ready method. |
164 | |
188 | |
|
|
189 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
190 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
191 | |
165 | =cut |
192 | =cut |
|
|
193 | |
|
|
194 | sub _newcoro { |
|
|
195 | terminate &{+shift}; |
|
|
196 | } |
166 | |
197 | |
167 | sub new { |
198 | sub new { |
168 | my $class = shift; |
199 | my $class = shift; |
169 | my $proc = $_[0]; |
|
|
170 | bless { |
200 | bless { |
171 | _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), |
201 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
172 | }, $class; |
202 | }, $class; |
173 | } |
203 | } |
174 | |
204 | |
175 | =item $process->ready |
205 | =item $process->ready |
176 | |
206 | |
… | |
… | |
186 | |
216 | |
187 | =cut |
217 | =cut |
188 | |
218 | |
189 | 1; |
219 | 1; |
190 | |
220 | |
|
|
221 | =head1 SEE ALSO |
|
|
222 | |
|
|
223 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
|
|
224 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
|
|
225 | |
191 | =head1 AUTHOR |
226 | =head1 AUTHOR |
192 | |
227 | |
193 | Marc Lehmann <pcg@goof.com> |
228 | Marc Lehmann <pcg@goof.com> |
194 | http://www.goof.com/pcg/marc/ |
229 | http://www.goof.com/pcg/marc/ |
195 | |
230 | |