ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/Coro.pm
(Generate patch)

Comparing Coro/Coro.pm (file contents):
Revision 1.11 by root, Sun Jul 15 03:24:18 2001 UTC vs.
Revision 1.14 by root, Tue Jul 17 02:21:56 2001 UTC

18 18
19 yield; 19 yield;
20 20
21=head1 DESCRIPTION 21=head1 DESCRIPTION
22 22
23This module collection manages coroutines. Coroutines are similar to
24Threads but don't run in parallel.
25
26This module is still experimental, see the BUGS section below.
27
23=cut 28=cut
24 29
25package Coro; 30package Coro;
26 31
27use Coro::State; 32use Coro::State;
28 33
29use base Exporter; 34use base Exporter;
30 35
31$VERSION = 0.04; 36$VERSION = 0.05;
32 37
33@EXPORT = qw(async yield schedule); 38@EXPORT = qw(async yield schedule terminate);
34@EXPORT_OK = qw($current); 39@EXPORT_OK = qw($current);
35 40
36{ 41{
37 use subs 'async'; 42 use subs 'async';
38 43
94 print STDERR "FATAL: deadlock detected\n"; 99 print STDERR "FATAL: deadlock detected\n";
95 exit(51); 100 exit(51);
96}; 101};
97 102
98# we really need priorities... 103# we really need priorities...
104## my @ready; #d#
99my @ready = (); # the ready queue. hehe, rather broken ;) 105our @ready = (); # the ready queue. hehe, rather broken ;)
100 106
101# static methods. not really. 107# static methods. not really.
102 108
103=head2 STATIC METHODS 109=head2 STATIC METHODS
104 110
105Static methods are actually functions that operate on the current process only. 111Static methods are actually functions that operate on the current process only.
106 112
107=over 4 113=over 4
108 114
109=item async { ... }; 115=item async { ... } [@args...]
110 116
111Create a new asynchronous process and return it's process object 117Create a new asynchronous process and return it's process object
112(usually unused). When the sub returns the new process is automatically 118(usually unused). When the sub returns the new process is automatically
113terminated. 119terminated.
114 120
115=cut 121 # create a new coroutine that just prints its arguments
122 async {
123 print "@_\n";
124 } 1,2,3,4;
116 125
126The coderef you submit MUST NOT be a closure that refers to variables
127in an outer scope. This does NOT work. Pass arguments into it instead.
128
129=cut
130
117sub async(&) { 131sub async(&@) {
118 my $pid = new Coro $_[0]; 132 my $pid = new Coro @_;
119 $pid->ready; 133 $pid->ready;
120 $pid; 134 $pid;
121} 135}
122 136
123=item schedule 137=item schedule
150 164
151=item terminate 165=item terminate
152 166
153Terminates the current process. 167Terminates the current process.
154 168
169Future versions of this function will allow result arguments.
170
155=cut 171=cut
156 172
157sub terminate { 173sub terminate {
174 $current->{_results} = [@_];
158 &schedule; 175 &schedule;
159} 176}
160 177
161=back 178=back
162 179
166 183
167These are the methods you can call on process objects. 184These are the methods you can call on process objects.
168 185
169=over 4 186=over 4
170 187
171=item new Coro \⊂ 188=item new Coro \&sub [, @args...]
172 189
173Create a new process and return it. When the sub returns the process 190Create a new process and return it. When the sub returns the process
174automatically terminates. To start the process you must first put it into 191automatically terminates. To start the process you must first put it into
175the ready queue by calling the ready method. 192the ready queue by calling the ready method.
176 193
194The coderef you submit MUST NOT be a closure that refers to variables
195in an outer scope. This does NOT work. Pass arguments into it instead.
196
177=cut 197=cut
198
199sub _newcoro {
200 terminate &{+shift};
201}
178 202
179sub new { 203sub new {
180 my $class = shift; 204 my $class = shift;
181 my $proc = $_[0];
182 bless { 205 bless {
183 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 206 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
184 }, $class; 207 }, $class;
185} 208}
186 209
187=item $process->ready 210=item $process->ready
188 211
198 221
199=cut 222=cut
200 223
2011; 2241;
202 225
226=head1 BUGS
227
228 - could be faster, especially when the core would introduce special
229 support for coroutines (like it does for threads).
230 - there is still a memleak on coroutine termination that I could not
231 identify. Could be as small as a single SV.
232 - this module is not well-tested.
233
203=head1 SEE ALSO 234=head1 SEE ALSO
204 235
205L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 236L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
206L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 237L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
207 238

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines