… | |
… | |
36 | |
36 | |
37 | use Coro::State; |
37 | use Coro::State; |
38 | |
38 | |
39 | use base Exporter; |
39 | use base Exporter; |
40 | |
40 | |
41 | $VERSION = 0.10; |
41 | $VERSION = 0.12; |
42 | |
42 | |
43 | @EXPORT = qw(async cede schedule terminate current); |
43 | @EXPORT = qw(async cede schedule terminate current); |
44 | @EXPORT_OK = qw($current); |
44 | @EXPORT_OK = qw($current); |
45 | |
45 | |
46 | { |
46 | { |
… | |
… | |
103 | our $idle = new Coro sub { |
103 | our $idle = new Coro sub { |
104 | print STDERR "FATAL: deadlock detected\n"; |
104 | print STDERR "FATAL: deadlock detected\n"; |
105 | exit(51); |
105 | exit(51); |
106 | }; |
106 | }; |
107 | |
107 | |
108 | # we really need priorities... |
|
|
109 | my @ready; # the ready queue. hehe, rather broken ;) |
|
|
110 | |
|
|
111 | # static methods. not really. |
|
|
112 | |
|
|
113 | =head2 STATIC METHODS |
|
|
114 | |
|
|
115 | Static methods are actually functions that operate on the current process only. |
|
|
116 | |
|
|
117 | =over 4 |
|
|
118 | |
|
|
119 | =item async { ... } [@args...] |
|
|
120 | |
|
|
121 | Create a new asynchronous process and return it's process object |
|
|
122 | (usually unused). When the sub returns the new process is automatically |
|
|
123 | terminated. |
|
|
124 | |
|
|
125 | # create a new coroutine that just prints its arguments |
|
|
126 | async { |
|
|
127 | print "@_\n"; |
|
|
128 | } 1,2,3,4; |
|
|
129 | |
|
|
130 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
131 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
132 | |
|
|
133 | =cut |
|
|
134 | |
|
|
135 | sub async(&@) { |
|
|
136 | my $pid = new Coro @_; |
|
|
137 | $pid->ready; |
|
|
138 | $pid; |
|
|
139 | } |
|
|
140 | |
|
|
141 | =item schedule |
|
|
142 | |
|
|
143 | Calls the scheduler. Please note that the current process will not be put |
|
|
144 | into the ready queue, so calling this function usually means you will |
|
|
145 | never be called again. |
|
|
146 | |
|
|
147 | =cut |
|
|
148 | |
|
|
149 | my $prev; |
|
|
150 | |
|
|
151 | sub schedule { |
|
|
152 | # should be done using priorities :( |
|
|
153 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
154 | Coro::State::transfer($prev, $current); |
|
|
155 | } |
|
|
156 | |
|
|
157 | =item cede |
|
|
158 | |
|
|
159 | "Cede" to other processes. This function puts the current process into the |
|
|
160 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
161 | current "timeslice" to other coroutines of the same or higher priority. |
|
|
162 | |
|
|
163 | =cut |
|
|
164 | |
|
|
165 | sub cede { |
|
|
166 | $current->ready; |
|
|
167 | &schedule; |
|
|
168 | } |
|
|
169 | |
|
|
170 | =item terminate |
|
|
171 | |
|
|
172 | Terminates the current process. |
|
|
173 | |
|
|
174 | Future versions of this function will allow result arguments. |
|
|
175 | |
|
|
176 | =cut |
|
|
177 | |
|
|
178 | # this coroutine is necessary because a coroutine |
108 | # this coroutine is necessary because a coroutine |
179 | # cannot destroy itself. |
109 | # cannot destroy itself. |
180 | my @destroy; |
110 | my @destroy; |
181 | my $terminate = new Coro sub { |
111 | my $manager = new Coro sub { |
182 | while() { |
112 | while() { |
183 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
113 | delete ((pop @destroy)->{_coro_state}) while @destroy; |
184 | &schedule; |
114 | &schedule; |
185 | } |
115 | } |
186 | }; |
116 | }; |
187 | |
117 | |
|
|
118 | # we really need priorities... |
|
|
119 | my @ready; # the ready queue. hehe, rather broken ;) |
|
|
120 | |
|
|
121 | # static methods. not really. |
|
|
122 | |
|
|
123 | =head2 STATIC METHODS |
|
|
124 | |
|
|
125 | Static methods are actually functions that operate on the current process only. |
|
|
126 | |
|
|
127 | =over 4 |
|
|
128 | |
|
|
129 | =item async { ... } [@args...] |
|
|
130 | |
|
|
131 | Create a new asynchronous process and return it's process object |
|
|
132 | (usually unused). When the sub returns the new process is automatically |
|
|
133 | terminated. |
|
|
134 | |
|
|
135 | # create a new coroutine that just prints its arguments |
|
|
136 | async { |
|
|
137 | print "@_\n"; |
|
|
138 | } 1,2,3,4; |
|
|
139 | |
|
|
140 | The coderef you submit MUST NOT be a closure that refers to variables |
|
|
141 | in an outer scope. This does NOT work. Pass arguments into it instead. |
|
|
142 | |
|
|
143 | =cut |
|
|
144 | |
|
|
145 | sub async(&@) { |
|
|
146 | my $pid = new Coro @_; |
|
|
147 | $manager->ready; # this ensures that the stack is cloned from the manager |
|
|
148 | $pid->ready; |
|
|
149 | $pid; |
|
|
150 | } |
|
|
151 | |
|
|
152 | =item schedule |
|
|
153 | |
|
|
154 | Calls the scheduler. Please note that the current process will not be put |
|
|
155 | into the ready queue, so calling this function usually means you will |
|
|
156 | never be called again. |
|
|
157 | |
|
|
158 | =cut |
|
|
159 | |
|
|
160 | my $prev; |
|
|
161 | |
|
|
162 | sub schedule { |
|
|
163 | # should be done using priorities :( |
|
|
164 | ($prev, $current) = ($current, shift @ready || $idle); |
|
|
165 | Coro::State::transfer($prev, $current); |
|
|
166 | } |
|
|
167 | |
|
|
168 | =item cede |
|
|
169 | |
|
|
170 | "Cede" to other processes. This function puts the current process into the |
|
|
171 | ready queue and calls C<schedule>, which has the effect of giving up the |
|
|
172 | current "timeslice" to other coroutines of the same or higher priority. |
|
|
173 | |
|
|
174 | =cut |
|
|
175 | |
|
|
176 | sub cede { |
|
|
177 | $current->ready; |
|
|
178 | &schedule; |
|
|
179 | } |
|
|
180 | |
|
|
181 | =item terminate |
|
|
182 | |
|
|
183 | Terminates the current process. |
|
|
184 | |
|
|
185 | Future versions of this function will allow result arguments. |
|
|
186 | |
|
|
187 | =cut |
|
|
188 | |
188 | sub terminate { |
189 | sub terminate { |
189 | push @destroy, $current; |
190 | push @destroy, $current; |
190 | $terminate->ready; |
191 | $manager->ready; |
191 | &schedule; |
192 | &schedule; |
192 | # NORETURN |
193 | # NORETURN |
193 | } |
194 | } |
194 | |
195 | |
195 | =back |
196 | =back |