… | |
… | |
67 | |
67 | |
68 | =cut |
68 | =cut |
69 | |
69 | |
70 | package Coro; |
70 | package Coro; |
71 | |
71 | |
72 | use strict qw(vars subs); |
72 | use common::sense; |
73 | no warnings "uninitialized"; |
73 | |
|
|
74 | use Carp (); |
74 | |
75 | |
75 | use Guard (); |
76 | use Guard (); |
76 | |
77 | |
77 | use Coro::State; |
78 | use Coro::State; |
78 | |
79 | |
… | |
… | |
123 | |
124 | |
124 | This variable is mainly useful to integrate Coro into event loops. It is |
125 | This variable is mainly useful to integrate Coro into event loops. It is |
125 | usually better to rely on L<Coro::AnyEvent> or L<Coro::EV>, as this is |
126 | usually better to rely on L<Coro::AnyEvent> or L<Coro::EV>, as this is |
126 | pretty low-level functionality. |
127 | pretty low-level functionality. |
127 | |
128 | |
128 | This variable stores either a Coro object or a callback. |
129 | This variable stores a Coro object that is put into the ready queue when |
|
|
130 | there are no other ready threads (without invoking any ready hooks). |
129 | |
131 | |
130 | If it is a callback, the it is called whenever the scheduler finds no |
132 | The default implementation dies with "FATAL: deadlock detected.", followed |
131 | ready coros to run. The default implementation prints "FATAL: |
133 | by a thread listing, because the program has no other way to continue. |
132 | deadlock detected" and exits, because the program has no other way to |
|
|
133 | continue. |
|
|
134 | |
|
|
135 | If it is a coro object, then this object will be readied (without |
|
|
136 | invoking any ready hooks, however) when the scheduler finds no other ready |
|
|
137 | coros to run. |
|
|
138 | |
134 | |
139 | This hook is overwritten by modules such as C<Coro::EV> and |
135 | This hook is overwritten by modules such as C<Coro::EV> and |
140 | C<Coro::AnyEvent> to wait on an external event that hopefully wake up a |
136 | C<Coro::AnyEvent> to wait on an external event that hopefully wake up a |
141 | coro so the scheduler can run it. |
137 | coro so the scheduler can run it. |
142 | |
138 | |
143 | Note that the callback I<must not>, under any circumstances, block |
|
|
144 | the current coro. Normally, this is achieved by having an "idle |
|
|
145 | coro" that calls the event loop and then blocks again, and then |
|
|
146 | readying that coro in the idle handler, or by simply placing the idle |
|
|
147 | coro in this variable. |
|
|
148 | |
|
|
149 | See L<Coro::Event> or L<Coro::AnyEvent> for examples of using this |
139 | See L<Coro::EV> or L<Coro::AnyEvent> for examples of using this technique. |
150 | technique. |
|
|
151 | |
140 | |
152 | Please note that if your callback recursively invokes perl (e.g. for event |
|
|
153 | handlers), then it must be prepared to be called recursively itself. |
|
|
154 | |
|
|
155 | =cut |
141 | =cut |
156 | |
142 | |
157 | $idle = sub { |
143 | $idle = new Coro sub { |
158 | require Carp; |
144 | require Coro::Debug; |
159 | Carp::croak ("FATAL: deadlock detected"); |
145 | die "FATAL: deadlock detected.\n" |
|
|
146 | . Coro::Debug::ps_listing (); |
160 | }; |
147 | }; |
161 | |
148 | |
162 | # this coro is necessary because a coro |
149 | # this coro is necessary because a coro |
163 | # cannot destroy itself. |
150 | # cannot destroy itself. |
164 | our @destroy; |
151 | our @destroy; |
… | |
… | |
272 | =item schedule |
259 | =item schedule |
273 | |
260 | |
274 | Calls the scheduler. The scheduler will find the next coro that is |
261 | Calls the scheduler. The scheduler will find the next coro that is |
275 | to be run from the ready queue and switches to it. The next coro |
262 | to be run from the ready queue and switches to it. The next coro |
276 | to be run is simply the one with the highest priority that is longest |
263 | to be run is simply the one with the highest priority that is longest |
277 | in its ready queue. If there is no coro ready, it will clal the |
264 | in its ready queue. If there is no coro ready, it will call the |
278 | C<$Coro::idle> hook. |
265 | C<$Coro::idle> hook. |
279 | |
266 | |
280 | Please note that the current coro will I<not> be put into the ready |
267 | Please note that the current coro will I<not> be put into the ready |
281 | queue, so calling this function usually means you will never be called |
268 | queue, so calling this function usually means you will never be called |
282 | again unless something else (e.g. an event handler) calls C<< ->ready >>, |
269 | again unless something else (e.g. an event handler) calls C<< ->ready >>, |