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

Comparing Coro/Coro.pm (file contents):
Revision 1.10 by root, Sun Jul 15 02:36:54 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 (new Coro $_[0])->ready; 127 my $pid = new Coro @_;
128 $pid->ready;
129 $pid;
119} 130}
120 131
121=item schedule 132=item schedule
122 133
123Calls the scheduler. Please note that the current process will not be put 134Calls the scheduler. Please note that the current process will not be put
148 159
149=item terminate 160=item terminate
150 161
151Terminates the current process. 162Terminates the current process.
152 163
164Future versions of this function will allow result arguments.
165
153=cut 166=cut
154 167
155sub terminate { 168sub terminate {
169 $current->{_results} = [@_];
156 &schedule; 170 &schedule;
157} 171}
158 172
159=back 173=back
160 174
164 178
165These are the methods you can call on process objects. 179These are the methods you can call on process objects.
166 180
167=over 4 181=over 4
168 182
169=item new Coro \⊂ 183=item new Coro \&sub [, @args...]
170 184
171Create 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
172automatically terminates. To start the process you must first put it into 186automatically terminates. To start the process you must first put it into
173the ready queue by calling the ready method. 187the ready queue by calling the ready method.
174 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
175=cut 192=cut
193
194sub _newcoro {
195 terminate &{+shift};
196}
176 197
177sub new { 198sub new {
178 my $class = shift; 199 my $class = shift;
179 my $proc = $_[0];
180 bless { 200 bless {
181 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 201 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
182 }, $class; 202 }, $class;
183} 203}
184 204
185=item $process->ready 205=item $process->ready
186 206

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines