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.17 by root, Thu Jul 19 02:45:09 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.08;
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
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;
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...
99my @ready = (); # the ready queue. hehe, rather broken ;) 104my @ready; # the ready queue. hehe, rather broken ;)
100 105
101# static methods. not really. 106# static methods. not really.
102 107
103=head2 STATIC METHODS 108=head2 STATIC METHODS
104 109
105Static methods are actually functions that operate on the current process only. 110Static methods are actually functions that operate on the current process only.
106 111
107=over 4 112=over 4
108 113
109=item async { ... }; 114=item async { ... } [@args...]
110 115
111Create a new asynchronous process and return it's process object 116Create a new asynchronous process and return it's process object
112(usually unused). When the sub returns the new process is automatically 117(usually unused). When the sub returns the new process is automatically
113terminated. 118terminated.
114 119
115=cut 120 # create a new coroutine that just prints its arguments
121 async {
122 print "@_\n";
123 } 1,2,3,4;
116 124
125The coderef you submit MUST NOT be a closure that refers to variables
126in an outer scope. This does NOT work. Pass arguments into it instead.
127
128=cut
129
117sub async(&) { 130sub async(&@) {
118 my $pid = new Coro $_[0]; 131 my $pid = new Coro @_;
119 $pid->ready; 132 $pid->ready;
120 $pid; 133 $pid;
121} 134}
122 135
123=item schedule 136=item schedule
150 163
151=item terminate 164=item terminate
152 165
153Terminates the current process. 166Terminates the current process.
154 167
168Future versions of this function will allow result arguments.
169
155=cut 170=cut
156 171
157sub terminate { 172sub terminate {
173 $current->{_results} = [@_];
158 &schedule; 174 &schedule;
159} 175}
160 176
161=back 177=back
162 178
166 182
167These are the methods you can call on process objects. 183These are the methods you can call on process objects.
168 184
169=over 4 185=over 4
170 186
171=item new Coro \⊂ 187=item new Coro \&sub [, @args...]
172 188
173Create a new process and return it. When the sub returns the process 189Create a new process and return it. When the sub returns the process
174automatically terminates. To start the process you must first put it into 190automatically terminates. To start the process you must first put it into
175the ready queue by calling the ready method. 191the ready queue by calling the ready method.
176 192
193The coderef you submit MUST NOT be a closure that refers to variables
194in an outer scope. This does NOT work. Pass arguments into it instead.
195
177=cut 196=cut
197
198sub _newcoro {
199 terminate &{+shift};
200}
178 201
179sub new { 202sub new {
180 my $class = shift; 203 my $class = shift;
181 my $proc = $_[0];
182 bless { 204 bless {
183 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 205 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
184 }, $class; 206 }, $class;
185} 207}
186 208
187=item $process->ready 209=item $process->ready
188 210
198 220
199=cut 221=cut
200 222
2011; 2231;
202 224
225=head1 BUGS/LIMITATIONS
226
227 - could be faster, especially when the core would introduce special
228 support for coroutines (like it does for threads).
229 - there is still a memleak on coroutine termination that I could not
230 identify. Could be as small as a single SV.
231 - this module is not well-tested.
232 - if variables or arguments "disappear" (become undef) or become
233 corrupted please contact the author so he cen iron out the
234 remaining bugs.
235 - this module is not thread-safe. You must only ever use this module from
236 the same thread (this requirement might be loosened in the future to
237 allow per-thread schedulers, but Coro::Satte does not yet allow this).
238
203=head1 SEE ALSO 239=head1 SEE ALSO
204 240
205L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 241L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
206L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 242L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
207 243

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines