ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.458 by root, Sat Dec 13 20:34:37 2008 UTC vs.
Revision 1.459 by root, Sat Dec 13 21:46:12 2008 UTC

41use Coro::EV; 41use Coro::EV;
42use Coro::AnyEvent; 42use Coro::AnyEvent;
43use Coro::Timer; 43use Coro::Timer;
44use Coro::Signal; 44use Coro::Signal;
45use Coro::Semaphore; 45use Coro::Semaphore;
46use Coro::SemaphoreSet;
46use Coro::AnyEvent; 47use Coro::AnyEvent;
47use Coro::AIO; 48use Coro::AIO;
48use Coro::BDB 1.6; 49use Coro::BDB 1.6;
49use Coro::Storable; 50use Coro::Storable;
50use Coro::Util (); 51use Coro::Util ();
347 348
348Return true if the lock is currently active, i.e. somebody has locked it. 349Return true if the lock is currently active, i.e. somebody has locked it.
349 350
350=cut 351=cut
351 352
352our %LOCK; 353our $LOCKS = new Coro::SemaphoreSet;
353our %LOCKER;#d#
354 354
355sub lock_wait($) { 355sub lock_wait($) {
356 my ($key) = @_; 356 $LOCKS->wait ($_[0]);
357
358 if ($LOCKER{$key} == $Coro::current) {#d#
359 Carp::cluck "lock_wait($key) for already-acquired lock";#d#
360 return;#d#
361 }#d#
362
363 # wait for lock, if any
364 while ($LOCK{$key}) {
365 #local $Coro::current->{desc} = "$Coro::current->{desc} <waiting for lock $key>";
366 push @{ $LOCK{$key} }, $Coro::current;
367 Coro::schedule;
368 }
369} 357}
370 358
371sub lock_acquire($) { 359sub lock_acquire($) {
372 my ($key) = @_; 360 $LOCKS->guard ($_[0])
373
374 # wait, to be sure we are not locked
375 lock_wait $key;
376
377 $LOCK{$key} = [];
378 $LOCKER{$key} = $Coro::current;#d#
379
380 Guard::guard {
381 delete $LOCKER{$key};#d#
382 # wake up all waiters, to be on the safe side
383 $_->ready for @{ delete $LOCK{$key} };
384 }
385} 361}
386 362
387sub lock_active($) { 363sub lock_active($) {
388 my ($key) = @_; 364 $LOCKS->count ($_[0]) < 1
389
390 ! ! $LOCK{$key}
391} 365}
392 366
393sub freeze_mainloop { 367sub freeze_mainloop {
394 tick_inhibit_inc; 368 tick_inhibit_inc;
395 369
1988sub find { 1962sub find {
1989 my ($path, $origin) = @_; 1963 my ($path, $origin) = @_;
1990 1964
1991 $path = normalise $path, $origin && $origin->path; 1965 $path = normalise $path, $origin && $origin->path;
1992 1966
1993 cf::lock_wait "map_data:$path";#d#remove 1967 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
1994 cf::lock_wait "map_find:$path"; 1968 my $guard2 = cf::lock_acquire "map_find:$path";
1995 1969
1996 $cf::MAP{$path} || do { 1970 $cf::MAP{$path} || do {
1997 my $guard1 = cf::lock_acquire "map_data:$path"; # just for the fun of it
1998 my $guard2 = cf::lock_acquire "map_find:$path";
1999
2000 my $map = new_from_path cf::map $path 1971 my $map = new_from_path cf::map $path
2001 or return; 1972 or return;
2002 1973
2003 $map->{last_save} = $cf::RUNTIME; 1974 $map->{last_save} = $cf::RUNTIME;
2004 1975

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines