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

Comparing Coro/Coro.pm (file contents):
Revision 1.23 by root, Mon Jul 23 04:23:32 2001 UTC vs.
Revision 1.24 by root, Wed Jul 25 04:14:37 2001 UTC

36 36
37use Coro::State; 37use Coro::State;
38 38
39use base Exporter; 39use base Exporter;
40 40
41$VERSION = 0.10; 41$VERSION = 0.12;
42 42
43@EXPORT = qw(async cede schedule terminate current); 43@EXPORT = qw(async cede schedule terminate current);
44@EXPORT_OK = qw($current); 44@EXPORT_OK = qw($current);
45 45
46{ 46{
103our $idle = new Coro sub { 103our $idle = new Coro sub {
104 print STDERR "FATAL: deadlock detected\n"; 104 print STDERR "FATAL: deadlock detected\n";
105 exit(51); 105 exit(51);
106}; 106};
107 107
108# we really need priorities...
109my @ready; # the ready queue. hehe, rather broken ;)
110
111# static methods. not really.
112
113=head2 STATIC METHODS
114
115Static methods are actually functions that operate on the current process only.
116
117=over 4
118
119=item async { ... } [@args...]
120
121Create a new asynchronous process and return it's process object
122(usually unused). When the sub returns the new process is automatically
123terminated.
124
125 # create a new coroutine that just prints its arguments
126 async {
127 print "@_\n";
128 } 1,2,3,4;
129
130The coderef you submit MUST NOT be a closure that refers to variables
131in an outer scope. This does NOT work. Pass arguments into it instead.
132
133=cut
134
135sub async(&@) {
136 my $pid = new Coro @_;
137 $pid->ready;
138 $pid;
139}
140
141=item schedule
142
143Calls the scheduler. Please note that the current process will not be put
144into the ready queue, so calling this function usually means you will
145never be called again.
146
147=cut
148
149my $prev;
150
151sub schedule {
152 # should be done using priorities :(
153 ($prev, $current) = ($current, shift @ready || $idle);
154 Coro::State::transfer($prev, $current);
155}
156
157=item cede
158
159"Cede" to other processes. This function puts the current process into the
160ready queue and calls C<schedule>, which has the effect of giving up the
161current "timeslice" to other coroutines of the same or higher priority.
162
163=cut
164
165sub cede {
166 $current->ready;
167 &schedule;
168}
169
170=item terminate
171
172Terminates the current process.
173
174Future versions of this function will allow result arguments.
175
176=cut
177
178# this coroutine is necessary because a coroutine 108# this coroutine is necessary because a coroutine
179# cannot destroy itself. 109# cannot destroy itself.
180my @destroy; 110my @destroy;
181my $terminate = new Coro sub { 111my $manager = new Coro sub {
182 while() { 112 while() {
183 delete ((pop @destroy)->{_coro_state}) while @destroy; 113 delete ((pop @destroy)->{_coro_state}) while @destroy;
184 &schedule; 114 &schedule;
185 } 115 }
186}; 116};
187 117
118# we really need priorities...
119my @ready; # the ready queue. hehe, rather broken ;)
120
121# static methods. not really.
122
123=head2 STATIC METHODS
124
125Static methods are actually functions that operate on the current process only.
126
127=over 4
128
129=item async { ... } [@args...]
130
131Create a new asynchronous process and return it's process object
132(usually unused). When the sub returns the new process is automatically
133terminated.
134
135 # create a new coroutine that just prints its arguments
136 async {
137 print "@_\n";
138 } 1,2,3,4;
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
145sub async(&@) {
146 my $pid = new Coro @_;
147 $manager->ready; # this ensures that the stack is cloned from the manager
148 $pid->ready;
149 $pid;
150}
151
152=item schedule
153
154Calls the scheduler. Please note that the current process will not be put
155into the ready queue, so calling this function usually means you will
156never be called again.
157
158=cut
159
160my $prev;
161
162sub schedule {
163 # should be done using priorities :(
164 ($prev, $current) = ($current, shift @ready || $idle);
165 Coro::State::transfer($prev, $current);
166}
167
168=item cede
169
170"Cede" to other processes. This function puts the current process into the
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.
173
174=cut
175
176sub cede {
177 $current->ready;
178 &schedule;
179}
180
181=item terminate
182
183Terminates the current process.
184
185Future versions of this function will allow result arguments.
186
187=cut
188
188sub terminate { 189sub terminate {
189 push @destroy, $current; 190 push @destroy, $current;
190 $terminate->ready; 191 $manager->ready;
191 &schedule; 192 &schedule;
192 # NORETURN 193 # NORETURN
193} 194}
194 195
195=back 196=back

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines