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

Comparing cvsroot/Coro/Coro.pm (file contents):
Revision 1.12 by root, Sun Jul 15 15:58:16 2001 UTC vs.
Revision 1.19 by root, Sat Jul 21 03:44:06 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.09;
32 37
33@EXPORT = qw(async yield schedule); 38@EXPORT = qw(async yield schedule terminate current);
34@EXPORT_OK = qw($current); 39@EXPORT_OK = qw($current);
35 40
36{ 41{
37 use subs 'async'; 42 use subs 'async';
38 43
47 my @attrs; 52 my @attrs;
48 for (@_) { 53 for (@_) {
49 if ($_ eq "Coro") { 54 if ($_ eq "Coro") {
50 push @async, $ref; 55 push @async, $ref;
51 } else { 56 } else {
52 push @attrs, @_; 57 push @attrs, $_;
53 } 58 }
54 } 59 }
55 return $old ? $old->($package, $name, @attrs) : @attrs; 60 return $old ? $old->($package, $ref, @attrs) : @attrs;
56 }; 61 };
57 } 62 }
58 63
59 sub INIT { 64 sub INIT {
60 async pop @async while @async; 65 async pop @async while @async;
67 72
68=cut 73=cut
69 74
70our $main = new Coro; 75our $main = new Coro;
71 76
72=item $current 77=item $current (or as function: current)
73 78
74The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). 79The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course).
75 80
76=cut 81=cut
77 82
79if ($current) { 84if ($current) {
80 $main->{specific} = $current->{specific}; 85 $main->{specific} = $current->{specific};
81} 86}
82 87
83our $current = $main; 88our $current = $main;
89
90sub current() { $current }
84 91
85=item $idle 92=item $idle
86 93
87The coroutine to switch to when no other coroutine is running. The default 94The coroutine to switch to when no other coroutine is running. The default
88implementation prints "FATAL: deadlock detected" and exits. 95implementation prints "FATAL: deadlock detected" and exits.
94 print STDERR "FATAL: deadlock detected\n"; 101 print STDERR "FATAL: deadlock detected\n";
95 exit(51); 102 exit(51);
96}; 103};
97 104
98# we really need priorities... 105# we really need priorities...
99my @ready = (); # the ready queue. hehe, rather broken ;) 106my @ready; # the ready queue. hehe, rather broken ;)
100 107
101# static methods. not really. 108# static methods. not really.
102 109
103=head2 STATIC METHODS 110=head2 STATIC METHODS
104 111
105Static methods are actually functions that operate on the current process only. 112Static methods are actually functions that operate on the current process only.
106 113
107=over 4 114=over 4
108 115
109=item async { ... }; 116=item async { ... } [@args...]
110 117
111Create a new asynchronous process and return it's process object 118Create a new asynchronous process and return it's process object
112(usually unused). When the sub returns the new process is automatically 119(usually unused). When the sub returns the new process is automatically
113terminated. 120terminated.
114 121
115=cut 122 # create a new coroutine that just prints its arguments
123 async {
124 print "@_\n";
125 } 1,2,3,4;
116 126
127The coderef you submit MUST NOT be a closure that refers to variables
128in an outer scope. This does NOT work. Pass arguments into it instead.
129
130=cut
131
117sub async(&) { 132sub async(&@) {
118 my $pid = new Coro $_[0]; 133 my $pid = new Coro @_;
119 $pid->ready; 134 $pid->ready;
120 $pid; 135 $pid;
121} 136}
122 137
123=item schedule 138=item schedule
129=cut 144=cut
130 145
131my $prev; 146my $prev;
132 147
133sub schedule { 148sub schedule {
134 local @_;
135 # should be done using priorities :( 149 # should be done using priorities :(
136 ($prev, $current) = ($current, shift @ready || $idle); 150 ($prev, $current) = ($current, shift @ready || $idle);
137 Coro::State::transfer($prev, $current); 151 Coro::State::transfer($prev, $current);
138} 152}
139 153
151 165
152=item terminate 166=item terminate
153 167
154Terminates the current process. 168Terminates the current process.
155 169
170Future versions of this function will allow result arguments.
171
156=cut 172=cut
157 173
158sub terminate { 174sub terminate {
175 $current->{_results} = [@_];
159 &schedule; 176 &schedule;
160} 177}
161 178
162=back 179=back
163 180
167 184
168These are the methods you can call on process objects. 185These are the methods you can call on process objects.
169 186
170=over 4 187=over 4
171 188
172=item new Coro \&sub; 189=item new Coro \&sub [, @args...]
173 190
174Create a new process and return it. When the sub returns the process 191Create a new process and return it. When the sub returns the process
175automatically terminates. To start the process you must first put it into 192automatically terminates. To start the process you must first put it into
176the ready queue by calling the ready method. 193the ready queue by calling the ready method.
177 194
195The coderef you submit MUST NOT be a closure that refers to variables
196in an outer scope. This does NOT work. Pass arguments into it instead.
197
178=cut 198=cut
199
200sub _newcoro {
201 terminate &{+shift};
202}
179 203
180sub new { 204sub new {
181 my $class = shift; 205 my $class = shift;
182 my $proc = $_[0];
183 bless { 206 bless {
184 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 207 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
185 }, $class; 208 }, $class;
186} 209}
187 210
188=item $process->ready 211=item $process->ready
189 212
199 222
200=cut 223=cut
201 224
2021; 2251;
203 226
227=head1 BUGS/LIMITATIONS
228
229 - could be faster, especially when the core would introduce special
230 support for coroutines (like it does for threads).
231 - there is still a memleak on coroutine termination that I could not
232 identify. Could be as small as a single SV.
233 - this module is not well-tested.
234 - if variables or arguments "disappear" (become undef) or become
235 corrupted please contact the author so he cen iron out the
236 remaining bugs.
237 - this module is not thread-safe. You must only ever use this module from
238 the same thread (this requirement might be loosened in the future to
239 allow per-thread schedulers, but Coro::Satte does not yet allow this).
240
204=head1 SEE ALSO 241=head1 SEE ALSO
205 242
206L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 243L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
207L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 244L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
208 245

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines