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

Comparing Coro/Coro.pm (file contents):
Revision 1.12 by root, Sun Jul 15 15:58:16 2001 UTC vs.
Revision 1.16 by root, Tue Jul 17 15:42:28 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.05; 36$VERSION = 0.07;
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
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
129=cut 142=cut
130 143
131my $prev; 144my $prev;
132 145
133sub schedule { 146sub schedule {
134 local @_;
135 # should be done using priorities :( 147 # should be done using priorities :(
136 ($prev, $current) = ($current, shift @ready || $idle); 148 ($prev, $current) = ($current, shift @ready || $idle);
137 Coro::State::transfer($prev, $current); 149 Coro::State::transfer($prev, $current);
138} 150}
139 151
151 163
152=item terminate 164=item terminate
153 165
154Terminates the current process. 166Terminates the current process.
155 167
168Future versions of this function will allow result arguments.
169
156=cut 170=cut
157 171
158sub terminate { 172sub terminate {
173 $current->{_results} = [@_];
159 &schedule; 174 &schedule;
160} 175}
161 176
162=back 177=back
163 178
167 182
168These are the methods you can call on process objects. 183These are the methods you can call on process objects.
169 184
170=over 4 185=over 4
171 186
172=item new Coro \⊂ 187=item new Coro \&sub [, @args...]
173 188
174Create 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
175automatically terminates. To start the process you must first put it into 190automatically terminates. To start the process you must first put it into
176the ready queue by calling the ready method. 191the ready queue by calling the ready method.
177 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
178=cut 196=cut
197
198sub _newcoro {
199 terminate &{+shift};
200}
179 201
180sub new { 202sub new {
181 my $class = shift; 203 my $class = shift;
182 my $proc = $_[0];
183 bless { 204 bless {
184 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 205 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
185 }, $class; 206 }, $class;
186} 207}
187 208
188=item $process->ready 209=item $process->ready
189 210
199 220
200=cut 221=cut
201 222
2021; 2231;
203 224
225=head1 BUGS
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
204=head1 SEE ALSO 233=head1 SEE ALSO
205 234
206L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 235L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
207L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 236L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
208 237

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines