… | |
… | |
18 | |
18 | |
19 | yield; |
19 | yield; |
20 | |
20 | |
21 | =head1 DESCRIPTION |
21 | =head1 DESCRIPTION |
22 | |
22 | |
|
|
23 | This module collection manages coroutines. Coroutines are similar to |
|
|
24 | Threads but don't run in parallel. |
|
|
25 | |
|
|
26 | This module is still experimental, see the BUGS section below. |
|
|
27 | |
23 | =cut |
28 | =cut |
24 | |
29 | |
25 | package Coro; |
30 | package Coro; |
26 | |
31 | |
27 | use Coro::State; |
32 | use Coro::State; |
28 | |
33 | |
29 | use base Exporter; |
34 | use base Exporter; |
30 | |
35 | |
31 | $VERSION = 0.05; |
36 | $VERSION = 0.09; |
32 | |
37 | |
33 | @EXPORT = qw(async yield schedule terminate); |
38 | @EXPORT = qw(async yield schedule terminate current); |
34 | @EXPORT_OK = qw($current); |
39 | @EXPORT_OK = qw($current); |
35 | |
40 | |
36 | { |
41 | { |
37 | use subs 'async'; |
42 | use subs 'async'; |
38 | |
43 | |
… | |
… | |
47 | my @attrs; |
52 | my @attrs; |
48 | for (@_) { |
53 | for (@_) { |
49 | if ($_ eq "Coro") { |
54 | if ($_ eq "Coro") { |
50 | push @async, $ref; |
55 | push @async, $ref; |
51 | } else { |
56 | } else { |
52 | push @attrs, @_; |
57 | push @attrs, $_; |
53 | } |
58 | } |
54 | } |
59 | } |
55 | return $old ? $old->($package, $name, @attrs) : @attrs; |
60 | return $old ? $old->($package, $ref, @attrs) : @attrs; |
56 | }; |
61 | }; |
57 | } |
62 | } |
58 | |
63 | |
59 | sub INIT { |
64 | sub INIT { |
60 | async pop @async while @async; |
65 | async pop @async while @async; |
… | |
… | |
67 | |
72 | |
68 | =cut |
73 | =cut |
69 | |
74 | |
70 | our $main = new Coro; |
75 | our $main = new Coro; |
71 | |
76 | |
72 | =item $current |
77 | =item $current (or as function: current) |
73 | |
78 | |
74 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
79 | The current coroutine (the last coroutine switched to). The initial value is C<$main> (of course). |
75 | |
80 | |
76 | =cut |
81 | =cut |
77 | |
82 | |
… | |
… | |
79 | if ($current) { |
84 | if ($current) { |
80 | $main->{specific} = $current->{specific}; |
85 | $main->{specific} = $current->{specific}; |
81 | } |
86 | } |
82 | |
87 | |
83 | our $current = $main; |
88 | our $current = $main; |
|
|
89 | |
|
|
90 | sub current() { $current } |
84 | |
91 | |
85 | =item $idle |
92 | =item $idle |
86 | |
93 | |
87 | The coroutine to switch to when no other coroutine is running. The default |
94 | The coroutine to switch to when no other coroutine is running. The default |
88 | implementation prints "FATAL: deadlock detected" and exits. |
95 | implementation prints "FATAL: deadlock detected" and exits. |
… | |
… | |
94 | print STDERR "FATAL: deadlock detected\n"; |
101 | print STDERR "FATAL: deadlock detected\n"; |
95 | exit(51); |
102 | exit(51); |
96 | }; |
103 | }; |
97 | |
104 | |
98 | # we really need priorities... |
105 | # we really need priorities... |
99 | ## my @ready; #d# |
|
|
100 | our @ready = (); # the ready queue. hehe, rather broken ;) |
106 | my @ready; # the ready queue. hehe, rather broken ;) |
101 | |
107 | |
102 | # static methods. not really. |
108 | # static methods. not really. |
103 | |
109 | |
104 | =head2 STATIC METHODS |
110 | =head2 STATIC METHODS |
105 | |
111 | |
… | |
… | |
216 | |
222 | |
217 | =cut |
223 | =cut |
218 | |
224 | |
219 | 1; |
225 | 1; |
220 | |
226 | |
|
|
227 | =head1 BUGS/LIMITATIONS |
|
|
228 | |
|
|
229 | - could be faster, especially when the core would introduce special |
|
|
230 | support for coroutines (like it does for threads). |
|
|
231 | - there is still a memleak on coroutine termination that I could not |
|
|
232 | identify. Could be as small as a single SV. |
|
|
233 | - this module is not well-tested. |
|
|
234 | - if variables or arguments "disappear" (become undef) or become |
|
|
235 | corrupted please contact the author so he cen iron out the |
|
|
236 | remaining bugs. |
|
|
237 | - this module is not thread-safe. You must only ever use this module from |
|
|
238 | the same thread (this requirement might be loosened in the future to |
|
|
239 | allow per-thread schedulers, but Coro::Satte does not yet allow this). |
|
|
240 | |
221 | =head1 SEE ALSO |
241 | =head1 SEE ALSO |
222 | |
242 | |
223 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
243 | L<Coro::Channel>, L<Coro::Cont>, L<Coro::Specific>, L<Coro::Semaphore>, |
224 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
244 | L<Coro::Signal>, L<Coro::State>, L<Coro::Event>. |
225 | |
245 | |