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

Comparing Coro/Coro.pm (file contents):
Revision 1.17 by root, Thu Jul 19 02:45:09 2001 UTC vs.
Revision 1.24 by root, Wed Jul 25 04:14:37 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 23This module collection manages coroutines. Coroutines are similar to
24Threads but don't run in parallel. 24Threads but don't run in parallel.
25 25
26This module is still experimental, see the BUGS section below. 26This module is still experimental, see the BUGS section below.
27 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
28=cut 33=cut
29 34
30package Coro; 35package Coro;
31 36
32use Coro::State; 37use Coro::State;
33 38
34use base Exporter; 39use base Exporter;
35 40
36$VERSION = 0.08; 41$VERSION = 0.12;
37 42
38@EXPORT = qw(async yield schedule terminate); 43@EXPORT = qw(async cede schedule terminate current);
39@EXPORT_OK = qw($current); 44@EXPORT_OK = qw($current);
40 45
41{ 46{
42 use subs 'async';
43
44 my @async; 47 my @async;
45 48
46 # this way of handling attributes simply is NOT scalable ;() 49 # this way of handling attributes simply is NOT scalable ;()
47 sub import { 50 sub import {
48 Coro->export_to_level(1, @_); 51 Coro->export_to_level(1, @_);
60 return $old ? $old->($package, $ref, @attrs) : @attrs; 63 return $old ? $old->($package, $ref, @attrs) : @attrs;
61 }; 64 };
62 } 65 }
63 66
64 sub INIT { 67 sub INIT {
65 async pop @async while @async; 68 &async(pop @async) while @async;
66 } 69 }
67} 70}
68 71
69=item $main 72=item $main
70 73
72 75
73=cut 76=cut
74 77
75our $main = new Coro; 78our $main = new Coro;
76 79
77=item $current 80=item $current (or as function: current)
78 81
79The 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).
80 83
81=cut 84=cut
82 85
84if ($current) { 87if ($current) {
85 $main->{specific} = $current->{specific}; 88 $main->{specific} = $current->{specific};
86} 89}
87 90
88our $current = $main; 91our $current = $main;
92
93sub current() { $current }
89 94
90=item $idle 95=item $idle
91 96
92The 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
93implementation prints "FATAL: deadlock detected" and exits. 98implementation prints "FATAL: deadlock detected" and exits.
98our $idle = new Coro sub { 103our $idle = new Coro sub {
99 print STDERR "FATAL: deadlock detected\n"; 104 print STDERR "FATAL: deadlock detected\n";
100 exit(51); 105 exit(51);
101}; 106};
102 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};
117
103# we really need priorities... 118# we really need priorities...
104my @ready; # the ready queue. hehe, rather broken ;) 119my @ready; # the ready queue. hehe, rather broken ;)
105 120
106# static methods. not really. 121# static methods. not really.
107 122
127 142
128=cut 143=cut
129 144
130sub async(&@) { 145sub async(&@) {
131 my $pid = new Coro @_; 146 my $pid = new Coro @_;
147 $manager->ready; # this ensures that the stack is cloned from the manager
132 $pid->ready; 148 $pid->ready;
133 $pid; 149 $pid;
134} 150}
135 151
136=item schedule 152=item schedule
147 # should be done using priorities :( 163 # should be done using priorities :(
148 ($prev, $current) = ($current, shift @ready || $idle); 164 ($prev, $current) = ($current, shift @ready || $idle);
149 Coro::State::transfer($prev, $current); 165 Coro::State::transfer($prev, $current);
150} 166}
151 167
152=item yield 168=item cede
153 169
154Yield to other processes. This function puts the current process into the 170"Cede" to other processes. This function puts the current process into the
155ready 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.
156 173
157=cut 174=cut
158 175
159sub yield { 176sub cede {
160 $current->ready; 177 $current->ready;
161 &schedule; 178 &schedule;
162} 179}
163 180
164=item terminate 181=item terminate
168Future versions of this function will allow result arguments. 185Future versions of this function will allow result arguments.
169 186
170=cut 187=cut
171 188
172sub terminate { 189sub terminate {
173 $current->{_results} = [@_]; 190 push @destroy, $current;
191 $manager->ready;
174 &schedule; 192 &schedule;
193 # NORETURN
175} 194}
176 195
177=back 196=back
178 197
179# dynamic methods 198# dynamic methods
232 - if variables or arguments "disappear" (become undef) or become 251 - if variables or arguments "disappear" (become undef) or become
233 corrupted please contact the author so he cen iron out the 252 corrupted please contact the author so he cen iron out the
234 remaining bugs. 253 remaining bugs.
235 - this module is not thread-safe. You must only ever use this module from 254 - this module is not thread-safe. You must only ever use this module from
236 the same thread (this requirement might be loosened in the future to 255 the same thread (this requirement might be loosened in the future to
237 allow per-thread schedulers, but Coro::Satte does not yet allow this). 256 allow per-thread schedulers, but Coro::State does not yet allow this).
238 257
239=head1 SEE ALSO 258=head1 SEE ALSO
240 259
241L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, 260L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
242L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. 261L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines