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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines