… | |
… | |
179 | sub to_json($) { |
179 | sub to_json($) { |
180 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
180 | $JSON::Syck::ImplicitUnicode = 0; # work around JSON::Syck bugs |
181 | JSON::Syck::Dump $_[0] |
181 | JSON::Syck::Dump $_[0] |
182 | } |
182 | } |
183 | |
183 | |
184 | =item my $guard = cf::guard { BLOCK } |
|
|
185 | |
|
|
186 | Run the given callback when the guard object gets destroyed (useful for |
|
|
187 | coroutine cancellations). |
|
|
188 | |
|
|
189 | You can call C<< ->cancel >> on the guard object to stop the block from |
|
|
190 | being executed. |
|
|
191 | |
|
|
192 | =cut |
|
|
193 | |
|
|
194 | sub guard(&) { |
|
|
195 | bless \(my $cb = $_[0]), cf::guard::; |
|
|
196 | } |
|
|
197 | |
|
|
198 | sub cf::guard::cancel { |
|
|
199 | ${$_[0]} = sub { }; |
|
|
200 | } |
|
|
201 | |
|
|
202 | sub cf::guard::DESTROY { |
|
|
203 | ${$_[0]}->(); |
|
|
204 | } |
|
|
205 | |
|
|
206 | =item cf::lock_wait $string |
184 | =item cf::lock_wait $string |
207 | |
185 | |
208 | Wait until the given lock is available. See cf::lock_acquire. |
186 | Wait until the given lock is available. See cf::lock_acquire. |
209 | |
187 | |
210 | =item my $lock = cf::lock_acquire $string |
188 | =item my $lock = cf::lock_acquire $string |
211 | |
189 | |
212 | Wait until the given lock is available and then acquires it and returns |
190 | Wait until the given lock is available and then acquires it and returns |
213 | a guard object. If the guard object gets destroyed (goes out of scope, |
191 | a Coro::guard object. If the guard object gets destroyed (goes out of scope, |
214 | for example when the coroutine gets canceled), the lock is automatically |
192 | for example when the coroutine gets canceled), the lock is automatically |
215 | returned. |
193 | returned. |
216 | |
194 | |
217 | Lock names should begin with a unique identifier (for example, cf::map::find |
195 | Lock names should begin with a unique identifier (for example, cf::map::find |
218 | uses map_find and cf::map::load uses map_load). |
196 | uses map_find and cf::map::load uses map_load). |
… | |
… | |
237 | # wait, to be sure we are not locked |
215 | # wait, to be sure we are not locked |
238 | lock_wait $key; |
216 | lock_wait $key; |
239 | |
217 | |
240 | $LOCK{$key} = []; |
218 | $LOCK{$key} = []; |
241 | |
219 | |
242 | cf::guard { |
220 | Coro::guard { |
243 | # wake up all waiters, to be on the safe side |
221 | # wake up all waiters, to be on the safe side |
244 | $_->ready for @{ delete $LOCK{$key} }; |
222 | $_->ready for @{ delete $LOCK{$key} }; |
245 | } |
223 | } |
246 | } |
224 | } |
247 | |
225 | |
… | |
… | |
262 | } |
240 | } |
263 | |
241 | |
264 | sub freeze_mainloop { |
242 | sub freeze_mainloop { |
265 | return unless $TICK_WATCHER->is_active; |
243 | return unless $TICK_WATCHER->is_active; |
266 | |
244 | |
267 | my $guard = guard { $TICK_WATCHER->start }; |
245 | my $guard = Coro::guard { $TICK_WATCHER->start }; |
268 | $TICK_WATCHER->stop; |
246 | $TICK_WATCHER->stop; |
269 | $guard |
247 | $guard |
270 | } |
248 | } |
271 | |
249 | |
272 | =item cf::sync_job { BLOCK } |
250 | =item cf::sync_job { BLOCK } |
… | |
… | |
1184 | or return; |
1162 | or return; |
1185 | |
1163 | |
1186 | my $map = cf::map::new |
1164 | my $map = cf::map::new |
1187 | or return; |
1165 | or return; |
1188 | |
1166 | |
|
|
1167 | # for better error messages only, will be overwritten |
|
|
1168 | $map->path ($path); |
|
|
1169 | |
1189 | $map->load_header ($path) |
1170 | $map->load_header ($path) |
1190 | or return; |
1171 | or return; |
1191 | |
1172 | |
1192 | $map->{load_path} = $path; |
1173 | $map->{load_path} = $path; |
1193 | |
1174 | |