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.13 by root, Tue Jul 17 00:24:14 2001 UTC

28 28
29use base Exporter; 29use base Exporter;
30 30
31$VERSION = 0.05; 31$VERSION = 0.05;
32 32
33@EXPORT = qw(async yield schedule); 33@EXPORT = qw(async yield schedule terminate);
34@EXPORT_OK = qw($current); 34@EXPORT_OK = qw($current);
35 35
36{ 36{
37 use subs 'async'; 37 use subs 'async';
38 38
94 print STDERR "FATAL: deadlock detected\n"; 94 print STDERR "FATAL: deadlock detected\n";
95 exit(51); 95 exit(51);
96}; 96};
97 97
98# we really need priorities... 98# we really need priorities...
99## my @ready; #d#
99my @ready = (); # the ready queue. hehe, rather broken ;) 100our @ready = (); # the ready queue. hehe, rather broken ;)
100 101
101# static methods. not really. 102# static methods. not really.
102 103
103=head2 STATIC METHODS 104=head2 STATIC METHODS
104 105
105Static methods are actually functions that operate on the current process only. 106Static methods are actually functions that operate on the current process only.
106 107
107=over 4 108=over 4
108 109
109=item async { ... }; 110=item async { ... } [@args...]
110 111
111Create a new asynchronous process and return it's process object 112Create a new asynchronous process and return it's process object
112(usually unused). When the sub returns the new process is automatically 113(usually unused). When the sub returns the new process is automatically
113terminated. 114terminated.
114 115
115=cut 116 # create a new coroutine that just prints its arguments
117 async {
118 print "@_\n";
119 } 1,2,3,4;
116 120
121The coderef you submit MUST NOT be a closure that refers to variables
122in an outer scope. This does NOT work. Pass arguments into it instead.
123
124=cut
125
117sub async(&) { 126sub async(&@) {
118 my $pid = new Coro $_[0]; 127 my $pid = new Coro @_;
119 $pid->ready; 128 $pid->ready;
120 $pid; 129 $pid;
121} 130}
122 131
123=item schedule 132=item schedule
129=cut 138=cut
130 139
131my $prev; 140my $prev;
132 141
133sub schedule { 142sub schedule {
134 local @_;
135 # should be done using priorities :( 143 # should be done using priorities :(
136 ($prev, $current) = ($current, shift @ready || $idle); 144 ($prev, $current) = ($current, shift @ready || $idle);
137 Coro::State::transfer($prev, $current); 145 Coro::State::transfer($prev, $current);
138} 146}
139 147
151 159
152=item terminate 160=item terminate
153 161
154Terminates the current process. 162Terminates the current process.
155 163
164Future versions of this function will allow result arguments.
165
156=cut 166=cut
157 167
158sub terminate { 168sub terminate {
169 $current->{_results} = [@_];
159 &schedule; 170 &schedule;
160} 171}
161 172
162=back 173=back
163 174
167 178
168These are the methods you can call on process objects. 179These are the methods you can call on process objects.
169 180
170=over 4 181=over 4
171 182
172=item new Coro \⊂ 183=item new Coro \&sub [, @args...]
173 184
174Create a new process and return it. When the sub returns the process 185Create a new process and return it. When the sub returns the process
175automatically terminates. To start the process you must first put it into 186automatically terminates. To start the process you must first put it into
176the ready queue by calling the ready method. 187the ready queue by calling the ready method.
177 188
189The coderef you submit MUST NOT be a closure that refers to variables
190in an outer scope. This does NOT work. Pass arguments into it instead.
191
178=cut 192=cut
193
194sub _newcoro {
195 terminate &{+shift};
196}
179 197
180sub new { 198sub new {
181 my $class = shift; 199 my $class = shift;
182 my $proc = $_[0];
183 bless { 200 bless {
184 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 201 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
185 }, $class; 202 }, $class;
186} 203}
187 204
188=item $process->ready 205=item $process->ready
189 206

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines