--- deliantra/server/lib/cf.pm 2007/01/03 00:05:26 1.127 +++ deliantra/server/lib/cf.pm 2007/01/05 01:06:28 1.136 @@ -51,7 +51,6 @@ our %MAP; # all maps our $LINK_MAP; # the special {link} map -our $FREEZE; our $RANDOM_MAPS = cf::localdir . "/random"; our %EXT_CORO; @@ -73,8 +72,7 @@ # a special map that is always available our $LINK_MAP; - -our $EMERGENCY_POSITION = $cf::CFG{emergency_position} || ["/world/world_105_115", 5, 37]; +our $EMERGENCY_POSITION; ############################################################################# @@ -183,28 +181,6 @@ JSON::Syck::Dump $_[0] } -=item my $guard = cf::guard { BLOCK } - -Run the given callback when the guard object gets destroyed (useful for -coroutine cancellations). - -You can call C<< ->cancel >> on the guard object to stop the block from -being executed. - -=cut - -sub guard(&) { - bless \(my $cb = $_[0]), cf::guard::; -} - -sub cf::guard::cancel { - ${$_[0]} = sub { }; -} - -sub cf::guard::DESTROY { - ${$_[0]}->(); -} - =item cf::lock_wait $string Wait until the given lock is available. See cf::lock_acquire. @@ -212,12 +188,12 @@ =item my $lock = cf::lock_acquire $string Wait until the given lock is available and then acquires it and returns -a guard object. If the guard object gets destroyed (goes out of scope, +a Coro::guard object. If the guard object gets destroyed (goes out of scope, for example when the coroutine gets canceled), the lock is automatically returned. -Lock names should begin with a unique identifier (for example, find_map -uses map_find and load_map uses map_load). +Lock names should begin with a unique identifier (for example, cf::map::find +uses map_find and cf::map::load uses map_load). =cut @@ -241,7 +217,7 @@ $LOCK{$key} = []; - cf::guard { + Coro::guard { # wake up all waiters, to be on the safe side $_->ready for @{ delete $LOCK{$key} }; } @@ -263,6 +239,14 @@ } } +sub freeze_mainloop { + return unless $TICK_WATCHER->is_active; + + my $guard = Coro::guard { $TICK_WATCHER->start }; + $TICK_WATCHER->stop; + $guard +} + =item cf::sync_job { BLOCK } The design of crossfire+ requires that the main coro ($Coro::main) is @@ -285,7 +269,7 @@ # TODO: use suspend/resume instead # (but this is cancel-safe) - local $FREEZE = 1; + my $freeze_guard = freeze_mainloop; my $busy = 1; my @res; @@ -1139,8 +1123,8 @@ use Fcntl; use Coro::AIO; -our $MAX_RESET = 7200; -our $DEFAULT_RESET = 3600; +our $MAX_RESET = 3600; +our $DEFAULT_RESET = 3000; sub generate_random_map { my ($path, $rmp) = @_; @@ -1180,6 +1164,9 @@ my $map = cf::map::new or return; + # for better error messages only, will be overwritten + $map->path ($path); + $map->load_header ($path) or return; @@ -1188,11 +1175,11 @@ $map } -sub find_map; -sub find_map { +sub find; +sub find { my ($path, $origin) = @_; - #warn "find_map<$path,$origin>\n";#d# + #warn "find<$path,$origin>\n";#d# $path = new cf::path $path, $origin && $origin->path; my $key = $path->as_string; @@ -1205,7 +1192,11 @@ # do it the slow way my $map = try_load_header $path->save_path; + Coro::cede; + if ($map) { + $map->last_access ((delete $map->{last_access}) + || $cf::RUNTIME); #d# # safety $map->{instantiate_time} = $cf::RUNTIME if $map->{instantiate_time} > $cf::RUNTIME; @@ -1220,6 +1211,7 @@ $map->{load_original} = 1; $map->{instantiate_time} = $cf::RUNTIME; + $map->last_access ($cf::RUNTIME); $map->instantiate; # per-player maps become, after loading, normal maps @@ -1229,12 +1221,13 @@ $map->path ($key); $map->{path} = $path; $map->{last_save} = $cf::RUNTIME; - $map->last_access ($cf::RUNTIME); + + Coro::cede; if ($map->should_reset) { $map->reset; undef $guard; - $map = find_map $path + $map = find $path or return; } @@ -1267,6 +1260,8 @@ } } + Coro::cede; + # now do the right thing for maps $self->link_multipart_objects; @@ -1283,20 +1278,21 @@ $self->activate; } + Coro::cede; + $self->in_memory (cf::MAP_IN_MEMORY); } -sub load_map_sync { +sub find_sync { my ($path, $origin) = @_; - #warn "load_map_sync<$path, $origin>\n";#d# + cf::sync_job { cf::map::find $path, $origin } +} - cf::sync_job { - my $map = cf::map::find_map $path, $origin - or return; - $map->load; - $map - } +sub do_load_sync { + my ($map) = @_; + + cf::sync_job { $map->load }; } sub save { @@ -1313,6 +1309,8 @@ return if $self->{deny_save}; + local $self->{last_access} = $self->last_access;#d# + if ($uniq) { $self->save_objects ($save, cf::IO_HEADER | cf::IO_OBJECTS); $self->save_objects ($uniq, cf::IO_UNIQUES); @@ -1324,11 +1322,13 @@ sub swap_out { my ($self) = @_; + # save first because save cedes + $self->save; + return if $self->players; return if $self->in_memory != cf::MAP_IN_MEMORY; return if $self->{deny_save}; - $self->save; $self->clear; $self->in_memory (cf::MAP_SWAPPED); } @@ -1404,14 +1404,14 @@ my ($map, $ob) = @_; if ($map->per_player) { - return cf::map::find_map "~" . $ob->name . "/" . $map->{path}{path}; + return cf::map::find "~" . $ob->name . "/" . $map->{path}{path}; } $map } sub emergency_save { - local $cf::FREEZE = 1; + my $freeze_guard = cf::freeze_mainloop; warn "enter emergency map save\n"; @@ -1511,11 +1511,11 @@ unless ($map) { # restore original map position ($map, $x, $y) = @{ $link_pos || [] }; - $map = cf::map::find_map $map; + $map = cf::map::find $map; unless ($map) { ($map, $x, $y) = @$EMERGENCY_POSITION; - $map = cf::map::find_map $map + $map = cf::map::find $map or die "FATAL: cannot load emergency map\n"; } } @@ -1561,11 +1561,11 @@ }, ); -=item $player_object->goto_map ($path, $x, $y) +=item $player_object->goto ($path, $x, $y) =cut -sub cf::object::player::goto_map { +sub cf::object::player::goto { my ($self, $path, $x, $y) = @_; $self->enter_link; @@ -1573,7 +1573,7 @@ (cf::async { $path = new cf::path $path; - my $map = cf::map::find_map $path->as_string; + my $map = cf::map::find $path->as_string; $map = $map->customise_for ($self) if $map; # warn "entering ", $map->path, " at ($x, $y)\n" @@ -1643,12 +1643,13 @@ $self->enter_link; (cf::async { + $self->deactivate_recursive; # just to be sure unless (eval { prepare_random_map $exit if $exit->slaying eq "/!"; my $path = new cf::path $exit->slaying, $exit->map && $exit->map->path; - $self->goto_map ($path, $exit->stats->hp, $exit->stats->sp); + $self->goto ($path, $exit->stats->hp, $exit->stats->sp); 1; }) { @@ -1715,7 +1716,8 @@ # this weird shuffling is so that direct followup queries # get handled first - my $queue = delete $ns->{query_queue}; + my $queue = delete $ns->{query_queue} + or return; # be conservative, not sure how that can happen, but we saw a crash here (shift @$queue)->[1]->($msg); @@ -1987,6 +1989,16 @@ local $/; *CFG = YAML::Syck::Load <$fh>; + + $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_105_115", 5, 37]; + + if (exists $CFG{mlockall}) { + eval { + $CFG{mlockall} ? &mlockall : &munlockall + and die "WARNING: m(un)lockall failed: $!\n"; + }; + warn $@ if $@; + } } sub main { @@ -2016,7 +2028,7 @@ warn "reloading..."; - local $FREEZE = 1; + my $guard = freeze_mainloop; cf::emergency_save; eval { @@ -2162,11 +2174,8 @@ at => $NEXT_TICK || $TICK, data => WF_AUTOCANCEL, cb => sub { - unless ($FREEZE) { - cf::server_tick; # one server iteration - $RUNTIME += $TICK; - } - + cf::server_tick; # one server iteration + $RUNTIME += $TICK; $NEXT_TICK += $TICK; # if we are delayed by four ticks or more, skip them all