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

Comparing Coro/Coro.pm (file contents):
Revision 1.8 by root, Sat Jul 14 22:14:21 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.03; 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
59 sub INIT { 59 sub INIT {
60 async pop @async while @async; 60 async pop @async while @async;
61 } 61 }
62} 62}
63 63
64my $idle = new Coro sub {
65 &yield while 1;
66};
67
68=item $main 64=item $main
69 65
70This coroutine represents the main program. 66This coroutine represents the main program.
71 67
72=cut 68=cut
73 69
74$main = new Coro; 70our $main = new Coro;
75 71
76=item $current 72=item $current
77 73
78The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). 74The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course).
79 75
82# maybe some other module used Coro::Specific before... 78# maybe some other module used Coro::Specific before...
83if ($current) { 79if ($current) {
84 $main->{specific} = $current->{specific}; 80 $main->{specific} = $current->{specific};
85} 81}
86 82
87$current = $main; 83our $current = $main;
84
85=item $idle
86
87The coroutine to switch to when no other coroutine is running. The default
88implementation prints "FATAL: deadlock detected" and exits.
89
90=cut
91
92# should be done using priorities :(
93our $idle = new Coro sub {
94 print STDERR "FATAL: deadlock detected\n";
95 exit(51);
96};
88 97
89# we really need priorities... 98# we really need priorities...
99## my @ready; #d#
90my @ready = (); # the ready queue. hehe, rather broken ;) 100our @ready = (); # the ready queue. hehe, rather broken ;)
91 101
92# static methods. not really. 102# static methods. not really.
93 103
94=head2 STATIC METHODS 104=head2 STATIC METHODS
95 105
96Static methods are actually functions that operate on the current process only. 106Static methods are actually functions that operate on the current process only.
97 107
98=over 4 108=over 4
99 109
100=item async { ... }; 110=item async { ... } [@args...]
101 111
102Create a new asynchronous process and return it's process object 112Create a new asynchronous process and return it's process object
103(usually unused). When the sub returns the new process is automatically 113(usually unused). When the sub returns the new process is automatically
104terminated. 114terminated.
105 115
106=cut 116 # create a new coroutine that just prints its arguments
117 async {
118 print "@_\n";
119 } 1,2,3,4;
107 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
108sub async(&) { 126sub async(&@) {
109 (new Coro $_[0])->ready; 127 my $pid = new Coro @_;
128 $pid->ready;
129 $pid;
110} 130}
111 131
112=item schedule 132=item schedule
113 133
114Calls 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
118=cut 138=cut
119 139
120my $prev; 140my $prev;
121 141
122sub schedule { 142sub schedule {
143 # should be done using priorities :(
123 ($prev, $current) = ($current, shift @ready); 144 ($prev, $current) = ($current, shift @ready || $idle);
124 Coro::State::transfer($prev, $current); 145 Coro::State::transfer($prev, $current);
125} 146}
126 147
127=item yield 148=item yield
128 149
138 159
139=item terminate 160=item terminate
140 161
141Terminates the current process. 162Terminates the current process.
142 163
164Future versions of this function will allow result arguments.
165
143=cut 166=cut
144 167
145sub terminate { 168sub terminate {
169 $current->{_results} = [@_];
146 &schedule; 170 &schedule;
147} 171}
148 172
149=back 173=back
150 174
154 178
155These are the methods you can call on process objects. 179These are the methods you can call on process objects.
156 180
157=over 4 181=over 4
158 182
159=item new Coro \&sub; 183=item new Coro \&sub [, @args...]
160 184
161Create 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
162automatically terminates. To start the process you must first put it into 186automatically terminates. To start the process you must first put it into
163the ready queue by calling the ready method. 187the ready queue by calling the ready method.
164 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
165=cut 192=cut
193
194sub _newcoro {
195 terminate &{+shift};
196}
166 197
167sub new { 198sub new {
168 my $class = shift; 199 my $class = shift;
169 my $proc = $_[0];
170 bless { 200 bless {
171 _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), 201 _coro_state => (new Coro::State $_[0] && \&_newcoro, @_),
172 }, $class; 202 }, $class;
173} 203}
174 204
175=item $process->ready 205=item $process->ready
176 206
186 216
187=cut 217=cut
188 218
1891; 2191;
190 220
221=head1 SEE ALSO
222
223L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
224L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
225
191=head1 AUTHOR 226=head1 AUTHOR
192 227
193 Marc Lehmann <pcg@goof.com> 228 Marc Lehmann <pcg@goof.com>
194 http://www.goof.com/pcg/marc/ 229 http://www.goof.com/pcg/marc/
195 230

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines