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.12 by root, Sun Jul 15 15:58:16 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);
34@EXPORT_OK = qw($current); 34@EXPORT_OK = qw($current);
35 35
36{ 36{
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...
90my @ready = (); # the ready queue. hehe, rather broken ;) 99my @ready = (); # the ready queue. hehe, rather broken ;)
91 100
92# static methods. not really. 101# static methods. not really.
104terminated. 113terminated.
105 114
106=cut 115=cut
107 116
108sub async(&) { 117sub async(&) {
109 (new Coro $_[0])->ready; 118 my $pid = new Coro $_[0];
119 $pid->ready;
120 $pid;
110} 121}
111 122
112=item schedule 123=item schedule
113 124
114Calls the scheduler. Please note that the current process will not be put 125Calls the scheduler. Please note that the current process will not be put
118=cut 129=cut
119 130
120my $prev; 131my $prev;
121 132
122sub schedule { 133sub schedule {
134 local @_;
135 # should be done using priorities :(
123 ($prev, $current) = ($current, shift @ready); 136 ($prev, $current) = ($current, shift @ready || $idle);
124 Coro::State::transfer($prev, $current); 137 Coro::State::transfer($prev, $current);
125} 138}
126 139
127=item yield 140=item yield
128 141
186 199
187=cut 200=cut
188 201
1891; 2021;
190 203
204=head1 SEE ALSO
205
206L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>,
207L<Coro::Signal>, L<Coro::State>, L<Coro::Event>.
208
191=head1 AUTHOR 209=head1 AUTHOR
192 210
193 Marc Lehmann <pcg@goof.com> 211 Marc Lehmann <pcg@goof.com>
194 http://www.goof.com/pcg/marc/ 212 http://www.goof.com/pcg/marc/
195 213

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines