… | |
… | |
28 | |
28 | |
29 | use base Exporter; |
29 | use base Exporter; |
30 | |
30 | |
31 | $VERSION = 0.05; |
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 | |
… | |
… | |
94 | print STDERR "FATAL: deadlock detected\n"; |
94 | print STDERR "FATAL: deadlock detected\n"; |
95 | exit(51); |
95 | exit(51); |
96 | }; |
96 | }; |
97 | |
97 | |
98 | # we really need priorities... |
98 | # we really need priorities... |
|
|
99 | ## my @ready; #d# |
99 | my @ready = (); # the ready queue. hehe, rather broken ;) |
100 | our @ready = (); # the ready queue. hehe, rather broken ;) |
100 | |
101 | |
101 | # static methods. not really. |
102 | # static methods. not really. |
102 | |
103 | |
103 | =head2 STATIC METHODS |
104 | =head2 STATIC METHODS |
104 | |
105 | |
105 | Static methods are actually functions that operate on the current process only. |
106 | Static methods are actually functions that operate on the current process only. |
106 | |
107 | |
107 | =over 4 |
108 | =over 4 |
108 | |
109 | |
109 | =item async { ... }; |
110 | =item async { ... } [@args...] |
110 | |
111 | |
111 | Create a new asynchronous process and return it's process object |
112 | Create a new asynchronous process and return it's process object |
112 | (usually unused). When the sub returns the new process is automatically |
113 | (usually unused). When the sub returns the new process is automatically |
113 | terminated. |
114 | terminated. |
114 | |
115 | |
115 | =cut |
116 | # create a new coroutine that just prints its arguments |
|
|
117 | async { |
|
|
118 | print "@_\n"; |
|
|
119 | } 1,2,3,4; |
116 | |
120 | |
|
|
121 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
122 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
123 | |
|
|
124 | =cut |
|
|
125 | |
117 | sub async(&) { |
126 | sub async(&@) { |
118 | my $pid = new Coro $_[0]; |
127 | my $pid = new Coro @_; |
119 | $pid->ready; |
128 | $pid->ready; |
120 | $pid; |
129 | $pid; |
121 | } |
130 | } |
122 | |
131 | |
123 | =item schedule |
132 | =item schedule |
… | |
… | |
129 | =cut |
138 | =cut |
130 | |
139 | |
131 | my $prev; |
140 | my $prev; |
132 | |
141 | |
133 | sub schedule { |
142 | sub schedule { |
134 | local @_; |
|
|
135 | # should be done using priorities :( |
143 | # should be done using priorities :( |
136 | ($prev, $current) = ($current, shift @ready || $idle); |
144 | ($prev, $current) = ($current, shift @ready || $idle); |
137 | Coro::State::transfer($prev, $current); |
145 | Coro::State::transfer($prev, $current); |
138 | } |
146 | } |
139 | |
147 | |
… | |
… | |
151 | |
159 | |
152 | =item terminate |
160 | =item terminate |
153 | |
161 | |
154 | Terminates the current process. |
162 | Terminates the current process. |
155 | |
163 | |
|
|
164 | Future versions of this function will allow result arguments. |
|
|
165 | |
156 | =cut |
166 | =cut |
157 | |
167 | |
158 | sub terminate { |
168 | sub terminate { |
|
|
169 | $current->{_results} = [@_]; |
159 | &schedule; |
170 | &schedule; |
160 | } |
171 | } |
161 | |
172 | |
162 | =back |
173 | =back |
163 | |
174 | |
… | |
… | |
167 | |
178 | |
168 | These are the methods you can call on process objects. |
179 | These are the methods you can call on process objects. |
169 | |
180 | |
170 | =over 4 |
181 | =over 4 |
171 | |
182 | |
172 | =item new Coro \⊂ |
183 | =item new Coro \&sub [, @args...] |
173 | |
184 | |
174 | Create a new process and return it. When the sub returns the process |
185 | Create a new process and return it. When the sub returns the process |
175 | automatically terminates. To start the process you must first put it into |
186 | automatically terminates. To start the process you must first put it into |
176 | the ready queue by calling the ready method. |
187 | the ready queue by calling the ready method. |
177 | |
188 | |
|
|
189 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
190 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
191 | |
178 | =cut |
192 | =cut |
|
|
193 | |
|
|
194 | sub _newcoro { |
|
|
195 | terminate &{+shift}; |
|
|
196 | } |
179 | |
197 | |
180 | sub new { |
198 | sub new { |
181 | my $class = shift; |
199 | my $class = shift; |
182 | my $proc = $_[0]; |
|
|
183 | bless { |
200 | bless { |
184 | _coro_state => new Coro::State ($proc ? sub { &$proc; &terminate } : $proc), |
201 | _coro_state => (new Coro::State $_[0] && \&_newcoro, @_), |
185 | }, $class; |
202 | }, $class; |
186 | } |
203 | } |
187 | |
204 | |
188 | =item $process->ready |
205 | =item $process->ready |
189 | |
206 | |