… | |
… | |
26 | |
26 | |
27 | use Coro::State; |
27 | use Coro::State; |
28 | |
28 | |
29 | use base Exporter; |
29 | use 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 | |
64 | my $idle = new Coro sub { |
|
|
65 | &yield while 1; |
|
|
66 | }; |
|
|
67 | |
|
|
68 | =item $main |
64 | =item $main |
69 | |
65 | |
70 | This coroutine represents the main program. |
66 | This coroutine represents the main program. |
71 | |
67 | |
72 | =cut |
68 | =cut |
73 | |
69 | |
74 | $main = new Coro; |
70 | our $main = new Coro; |
75 | |
71 | |
76 | =item $current |
72 | =item $current |
77 | |
73 | |
78 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
74 | The 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... |
83 | if ($current) { |
79 | if ($current) { |
84 | $main->{specific} = $current->{specific}; |
80 | $main->{specific} = $current->{specific}; |
85 | } |
81 | } |
86 | |
82 | |
87 | $current = $main; |
83 | our $current = $main; |
|
|
84 | |
|
|
85 | =item $idle |
|
|
86 | |
|
|
87 | The coroutine to switch to when no other coroutine is running. The default |
|
|
88 | implementation prints "FATAL: deadlock detected" and exits. |
|
|
89 | |
|
|
90 | =cut |
|
|
91 | |
|
|
92 | # should be done using priorities :( |
|
|
93 | our $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... |
90 | my @ready = (); # the ready queue. hehe, rather broken ;) |
99 | my @ready = (); # the ready queue. hehe, rather broken ;) |
91 | |
100 | |
92 | # static methods. not really. |
101 | # static methods. not really. |
… | |
… | |
104 | terminated. |
113 | terminated. |
105 | |
114 | |
106 | =cut |
115 | =cut |
107 | |
116 | |
108 | sub async(&) { |
117 | sub 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 | |
114 | Calls the scheduler. Please note that the current process will not be put |
125 | Calls the scheduler. Please note that the current process will not be put |
… | |
… | |
118 | =cut |
129 | =cut |
119 | |
130 | |
120 | my $prev; |
131 | my $prev; |
121 | |
132 | |
122 | sub schedule { |
133 | sub 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 | |
189 | 1; |
202 | 1; |
190 | |
203 | |
|
|
204 | =head1 SEE ALSO |
|
|
205 | |
|
|
206 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
|
|
207 | L<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 | |