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

Comparing Coro/Coro.pm (file contents):
Revision 1.9 by root, Sun Jul 15 02:35:52 2001 UTC vs.
Revision 1.23 by root, Mon Jul 23 04:23:32 2001 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines