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

Comparing Coro/Coro.pm (file contents):
Revision 1.10 by root, Sun Jul 15 02:36:54 2001 UTC vs.
Revision 1.21 by root, Sun Jul 22 03:24:10 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
28In this module, coroutines are defined as "callchain + lexical variables
29+ @_ + $_ + $@ + $^W), that is, a coroutine has it's own callchain, it's
30own set of lexicals and it's own set of perl's most important global
31variables.
32
33WARNING: When using this module, make sure that, at program end, no
34coroutines are still running OR just call exit before falling off the
35end. The reason for this is that some coroutine of yours might have called
36into a C function, and falling off the end of main:: results in returning
37to that C function instead if to the main C interpreter.
38
23=cut 39=cut
24 40
25package Coro; 41package Coro;
26 42
27use Coro::State; 43use Coro::State;
28 44
29use base Exporter; 45use base Exporter;
30 46
31$VERSION = 0.04; 47$VERSION = 0.10;
32 48
33@EXPORT = qw(async yield schedule); 49@EXPORT = qw(async yield schedule terminate current);
34@EXPORT_OK = qw($current); 50@EXPORT_OK = qw($current);
35 51
36{ 52{
37 use subs 'async';
38
39 my @async; 53 my @async;
40 54
41 # this way of handling attributes simply is NOT scalable ;() 55 # this way of handling attributes simply is NOT scalable ;()
42 sub import { 56 sub import {
43 Coro->export_to_level(1, @_); 57 Coro->export_to_level(1, @_);
47 my @attrs; 61 my @attrs;
48 for (@_) { 62 for (@_) {
49 if ($_ eq "Coro") { 63 if ($_ eq "Coro") {
50 push @async, $ref; 64 push @async, $ref;
51 } else { 65 } else {
52 push @attrs, @_; 66 push @attrs, $_;
53 } 67 }
54 } 68 }
55 return $old ? $old->($package, $name, @attrs) : @attrs; 69 return $old ? $old->($package, $ref, @attrs) : @attrs;
56 }; 70 };
57 } 71 }
58 72
59 sub INIT { 73 sub INIT {
60 async pop @async while @async; 74 &async(pop @async) while @async;
61 } 75 }
62} 76}
63 77
64=item $main 78=item $main
65 79
67 81
68=cut 82=cut
69 83
70our $main = new Coro; 84our $main = new Coro;
71 85
72=item $current 86=item $current (or as function: current)
73 87
74The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). 88The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course).
75 89
76=cut 90=cut
77 91
79if ($current) { 93if ($current) {
80 $main->{specific} = $current->{specific}; 94 $main->{specific} = $current->{specific};
81} 95}
82 96
83our $current = $main; 97our $current = $main;
98
99sub current() { $current }
84 100
85=item $idle 101=item $idle
86 102
87The coroutine to switch to when no other coroutine is running. The default 103The coroutine to switch to when no other coroutine is running. The default
88implementation prints "FATAL: deadlock detected" and exits. 104implementation prints "FATAL: deadlock detected" and exits.
94 print STDERR "FATAL: deadlock detected\n"; 110 print STDERR "FATAL: deadlock detected\n";
95 exit(51); 111 exit(51);
96}; 112};
97 113
98# we really need priorities... 114# we really need priorities...
99my @ready = (); # the ready queue. hehe, rather broken ;) 115my @ready; # the ready queue. hehe, rather broken ;)
100 116
101# static methods. not really. 117# static methods. not really.
102 118
103=head2 STATIC METHODS 119=head2 STATIC METHODS
104 120
105Static methods are actually functions that operate on the current process only. 121Static methods are actually functions that operate on the current process only.
106 122
107=over 4 123=over 4
108 124
109=item async { ... }; 125=item async { ... } [@args...]
110 126
111Create a new asynchronous process and return it's process object 127Create a new asynchronous process and return it's process object
112(usually unused). When the sub returns the new process is automatically 128(usually unused). When the sub returns the new process is automatically
113terminated. 129terminated.
114 130
115=cut 131 # create a new coroutine that just prints its arguments
132 async {
133 print "@_\n";
134 } 1,2,3,4;
116 135
136The coderef you submit MUST NOT be a closure that refers to variables
137in an outer scope. This does NOT work. Pass arguments into it instead.
138
139=cut
140
117sub async(&) { 141sub async(&@) {
118 (new Coro $_[0])->ready; 142 my $pid = new Coro @_;
143 $pid->ready;
144 $pid;
119} 145}
120 146
121=item schedule 147=item schedule
122 148
123Calls the scheduler. Please note that the current process will not be put 149Calls the scheduler. Please note that the current process will not be put
148 174
149=item terminate 175=item terminate
150 176
151Terminates the current process. 177Terminates the current process.
152 178
179Future versions of this function will allow result arguments.
180
153=cut 181=cut
154 182
155sub terminate { 183sub terminate {
184 $current->{_results} = [@_];
185 delete $current->{_coro_state};
156 &schedule; 186 &schedule;
157} 187}
158 188
159=back 189=back
160 190
164 194
165These are the methods you can call on process objects. 195These are the methods you can call on process objects.
166 196
167=over 4 197=over 4
168 198
169=item new Coro \&sub; 199=item new Coro \&sub [, @args...]
170 200
171Create a new process and return it. When the sub returns the process 201Create a new process and return it. When the sub returns the process
172automatically terminates. To start the process you must first put it into 202automatically terminates. To start the process you must first put it into
173the ready queue by calling the ready method. 203the ready queue by calling the ready method.
174 204
205The coderef you submit MUST NOT be a closure that refers to variables
206in an outer scope. This does NOT work. Pass arguments into it instead.
207
175=cut 208=cut
209
210sub _newcoro {
211 terminate &{+shift};
212}
176 213
177sub new { 214sub new {
178 my $class = shift; 215 my $class = shift;
179 my $proc = $_[0];
180 bless { 216 bless {
181 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 217 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
182 }, $class; 218 }, $class;
183} 219}
184 220
185=item $process->ready 221=item $process->ready
186 222
196 232
197=cut 233=cut
198 234
1991; 2351;
200 236
237=head1 BUGS/LIMITATIONS
238
239 - could be faster, especially when the core would introduce special
240 support for coroutines (like it does for threads).
241 - there is still a memleak on coroutine termination that I could not
242 identify. Could be as small as a single SV.
243 - this module is not well-tested.
244 - if variables or arguments "disappear" (become undef) or become
245 corrupted please contact the author so he cen iron out the
246 remaining bugs.
247 - this module is not thread-safe. You must only ever use this module from
248 the same thread (this requirement might be loosened in the future to
249 allow per-thread schedulers, but Coro::State does not yet allow this).
250
201=head1 SEE ALSO 251=head1 SEE ALSO
202 252
203L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 253L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
204L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 254L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
205 255

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines