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

Comparing Coro/Coro.pm (file contents):
Revision 1.11 by root, Sun Jul 15 03:24:18 2001 UTC vs.
Revision 1.13 by root, Tue Jul 17 00:24:14 2001 UTC

26 26
27use Coro::State; 27use Coro::State;
28 28
29use base Exporter; 29use base Exporter;
30 30
31$VERSION = 0.04; 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
150 159
151=item terminate 160=item terminate
152 161
153Terminates the current process. 162Terminates the current process.
154 163
164Future versions of this function will allow result arguments.
165
155=cut 166=cut
156 167
157sub terminate { 168sub terminate {
169 $current->{_results} = [@_];
158 &schedule; 170 &schedule;
159} 171}
160 172
161=back 173=back
162 174
166 178
167These are the methods you can call on process objects. 179These are the methods you can call on process objects.
168 180
169=over 4 181=over 4
170 182
171=item new Coro \⊂ 183=item new Coro \&sub [, @args...]
172 184
173Create 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
174automatically terminates. To start the process you must first put it into 186automatically terminates. To start the process you must first put it into
175the ready queue by calling the ready method. 187the ready queue by calling the ready method.
176 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
177=cut 192=cut
193
194sub _newcoro {
195 terminate &{+shift};
196}
178 197
179sub new { 198sub new {
180 my $class = shift; 199 my $class = shift;
181 my $proc = $_[0];
182 bless { 200 bless {
183 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 201 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
184 }, $class; 202 }, $class;
185} 203}
186 204
187=item $process->ready 205=item $process->ready
188 206

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines