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

Comparing Coro/Coro.pm (file contents):
Revision 1.8 by root, Sat Jul 14 22:14:21 2001 UTC vs.
Revision 1.25 by root, Wed Jul 25 21:12:57 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.03; 41$VERSION = 0.12;
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
64my $idle = new Coro sub {
65 &yield while 1;
66};
67
68=item $main 72=item $main
69 73
70This coroutine represents the main program. 74This coroutine represents the main program.
71 75
72=cut 76=cut
73 77
74$main = new Coro; 78our $main = new Coro;
75 79
76=item $current 80=item $current (or as function: current)
77 81
78The 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).
79 83
80=cut 84=cut
81 85
82# maybe some other module used Coro::Specific before... 86# maybe some other module used Coro::Specific before...
83if ($current) { 87if ($current) {
84 $main->{specific} = $current->{specific}; 88 $main->{specific} = $current->{specific};
85} 89}
86 90
87$current = $main; 91our $current = $main;
92
93sub current() { $current }
94
95=item $idle
96
97The coroutine to switch to when no other coroutine is running. The default
98implementation prints "FATAL: deadlock detected" and exits.
99
100=cut
101
102# should be done using priorities :(
103our $idle = new Coro sub {
104 print STDERR "FATAL: deadlock detected\n";
105 exit(51);
106};
107
108# this coroutine is necessary because a coroutine
109# cannot destroy itself.
110my @destroy;
111my $manager = new Coro sub {
112 while() {
113 delete ((pop @destroy)->{_coro_state}) while @destroy;
114 &schedule;
115 }
116};
88 117
89# we really need priorities... 118# we really need priorities...
90my @ready = (); # the ready queue. hehe, rather broken ;) 119my @ready; # the ready queue. hehe, rather broken ;)
91 120
92# static methods. not really. 121# static methods. not really.
93 122
94=head2 STATIC METHODS 123=head2 STATIC METHODS
95 124
96Static methods are actually functions that operate on the current process only. 125Static methods are actually functions that operate on the current process only.
97 126
98=over 4 127=over 4
99 128
100=item async { ... }; 129=item async { ... } [@args...]
101 130
102Create a new asynchronous process and return it's process object 131Create a new asynchronous process and return it's process object
103(usually unused). When the sub returns the new process is automatically 132(usually unused). When the sub returns the new process is automatically
104terminated. 133terminated.
105 134
106=cut 135 # create a new coroutine that just prints its arguments
136 async {
137 print "@_\n";
138 } 1,2,3,4;
107 139
140The coderef you submit MUST NOT be a closure that refers to variables
141in an outer scope. This does NOT work. Pass arguments into it instead.
142
143=cut
144
108sub async(&) { 145sub async(&@) {
109 (new Coro $_[0])->ready; 146 my $pid = new Coro @_;
147 $manager->ready; # this ensures that the stack is cloned from the manager
148 $pid->ready;
149 $pid;
110} 150}
111 151
112=item schedule 152=item schedule
113 153
114Calls the scheduler. Please note that the current process will not be put 154Calls the scheduler. Please note that the current process will not be put
118=cut 158=cut
119 159
120my $prev; 160my $prev;
121 161
122sub schedule { 162sub schedule {
163 # should be done using priorities :(
123 ($prev, $current) = ($current, shift @ready); 164 ($prev, $current) = ($current, shift @ready || $idle);
124 Coro::State::transfer($prev, $current); 165 Coro::State::transfer($prev, $current);
125} 166}
126 167
127=item yield 168=item cede
128 169
129Yield to other processes. This function puts the current process into the 170"Cede" to other processes. This function puts the current process into the
130ready queue and calls C<schedule>. 171ready queue and calls C<schedule>, which has the effect of giving up the
172current "timeslice" to other coroutines of the same or higher priority.
131 173
132=cut 174=cut
133 175
134sub yield { 176sub cede {
135 $current->ready; 177 $current->ready;
136 &schedule; 178 &schedule;
137} 179}
138 180
139=item terminate 181=item terminate
140 182
141Terminates the current process. 183Terminates the current process.
142 184
185Future versions of this function will allow result arguments.
186
143=cut 187=cut
144 188
145sub terminate { 189sub terminate {
190 push @destroy, $current;
191 $manager->ready;
146 &schedule; 192 &schedule;
193 # NORETURN
147} 194}
148 195
149=back 196=back
150 197
151# dynamic methods 198# dynamic methods
154 201
155These are the methods you can call on process objects. 202These are the methods you can call on process objects.
156 203
157=over 4 204=over 4
158 205
159=item new Coro \&sub; 206=item new Coro \&sub [, @args...]
160 207
161Create a new process and return it. When the sub returns the process 208Create a new process and return it. When the sub returns the process
162automatically terminates. To start the process you must first put it into 209automatically terminates. To start the process you must first put it into
163the ready queue by calling the ready method. 210the ready queue by calling the ready method.
164 211
212The coderef you submit MUST NOT be a closure that refers to variables
213in an outer scope. This does NOT work. Pass arguments into it instead.
214
165=cut 215=cut
216
217sub _newcoro {
218 terminate &{+shift};
219}
166 220
167sub new { 221sub new {
168 my $class = shift; 222 my $class = shift;
169 my $proc = $_[0];
170 bless { 223 bless {
171 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 224 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
172 }, $class; 225 }, $class;
173} 226}
174 227
175=item $process->ready 228=item $process->ready
176 229
186 239
187=cut 240=cut
188 241
1891; 2421;
190 243
244=head1 BUGS/LIMITATIONS
245
246 - could be faster, especially when the core would introduce special
247 support for coroutines (like it does for threads).
248 - there is still a memleak on coroutine termination that I could not
249 identify. Could be as small as a single SV.
250 - this module is not well-tested.
251 - if variables or arguments "disappear" (become undef) or become
252 corrupted please contact the author so he cen iron out the
253 remaining bugs.
254 - this module is not thread-safe. You must only ever use this module from
255 the same thread (this requirement might be loosened in the future to
256 allow per-thread schedulers, but Coro::State does not yet allow this).
257
258=head1 SEE ALSO
259
260L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
261L<Coro::Signal>, L<Coro::State>, L<Coro::Event>, L<Coro::RWLock>,
262L<Coro::L<Coro::Handle>, L<Coro::Socket>.
263
191=head1 AUTHOR 264=head1 AUTHOR
192 265
193 Marc Lehmann <pcg@goof.com> 266 Marc Lehmann <pcg@goof.com>
194 http://www.goof.com/pcg/marc/ 267 http://www.goof.com/pcg/marc/
195 268

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines