--- deliantra/server/lib/cf.pm 2010/05/04 23:32:29 1.540 +++ deliantra/server/lib/cf.pm 2010/05/14 22:56:47 1.550 @@ -126,7 +126,7 @@ our $BDB_TRICKLE_WATCHER; our $DB_ENV; -our @EXTRA_MODULES = qw(pod match mapscript); +our @EXTRA_MODULES = qw(pod match mapscript incloader); our %CFG; @@ -235,9 +235,14 @@ =item cf::wait_for_tick, cf::wait_for_tick_begin These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only -returns directly I the tick processing (and consequently, can only wake one process +returns directly I the tick processing (and consequently, can only wake one thread per tick), while cf::wait_for_tick wakes up all waiters after tick processing. +=cut + +sub wait_for_tick(); +sub wait_for_tick_begin(); + =item @cf::INVOKE_RESULTS This array contains the results of the last C call. When @@ -354,12 +359,24 @@ sub encode_json($) { $json_coder->encode ($_[0]) } sub decode_json($) { $json_coder->decode ($_[0]) } +=item $ref = cf::yaml_load $scalar + +Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). + +=cut + +sub fork_call(&@); + +sub yaml_load($) { + fork_call { YAML::XS::Load $_[0] } @_ +} + =item cf::post_init { BLOCK } Execute the given codeblock, I all extensions have been (re-)loaded, but I the server starts ticking again. -The cdoeblock will have a single boolean argument to indicate whether this +The codeblock will have a single boolean argument to indicate whether this is a reload or not. =cut @@ -368,6 +385,23 @@ push @POST_INIT, shift; } +sub _post_init { + trace "running post_init jobs"; + + # run them in parallel... + + my @join; + + while () { + push @join, map &Coro::async ($_, 0), @POST_INIT; + @POST_INIT = (); + + @join or last; + + (pop @join)->join; + } +} + =item cf::lock_wait $string Wait until the given lock is available. See cf::lock_acquire. @@ -580,7 +614,7 @@ $coro } -=item fork_call { }, $args +=item fork_call { }, @args Executes the given code block with the given arguments in a seperate process, returning the results. Everything must be serialisable with @@ -589,6 +623,10 @@ =cut +sub post_fork { + reset_signals; +} + sub fork_call(&@) { my ($cb, @args) = @_; @@ -597,9 +635,9 @@ # TODO: investigate and fix (likely this will be rather laborious) my @res = Coro::Util::fork_eval { - reset_signals; + cf::post_fork; &$cb - }, @args; + } @args; wantarray ? @res : $res[-1] } @@ -1453,7 +1491,6 @@ if (length $@) { error "$v->{path}: $@\n"; - undef $@; # work around perl 5.10.0 utf-8 caching bug cf::cleanup "mandatory extension '$k' failed to load, exiting." if exists $v->{meta}{mandatory}; @@ -1644,10 +1681,13 @@ $pl->{deny_save} = 1; $pl->password ("*"); # this should lock out the player until we have nuked the dir - $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; + $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns; $pl->deactivate; + my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; - $pl->invoke (cf::EVENT_PLAYER_QUIT); + $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns; + ext::highscore::check ($pl->ob); + $pl->ns->destroy if $pl->ns; my $path = playerdir $pl; @@ -1872,7 +1912,7 @@ sub normalise { my ($path, $base) = @_; - $path = "$path"; # make sure its a string + $path = "$path"; # make sure it's a string $path =~ s/\.map$//; @@ -1921,7 +1961,7 @@ } } - Carp::cluck "unable to resolve path '$path' (base '$base')."; + Carp::cluck "unable to resolve path '$path' (base '$base')"; () } @@ -2054,6 +2094,8 @@ sub find { my ($path, $origin) = @_; + cf::cede_to_tick; + $path = normalise $path, $origin && $origin->path; my $guard1 = cf::lock_acquire "map_data:$path";#d#remove @@ -3360,49 +3402,6 @@ ############################################################################# # the server's init and main functions -# async inc loader. yay. -sub inc_loader { - my $mod = $_[1]; - - if (in_main && !tick_inhibit) { - Carp::cluck "ERROR: attempted synchronous perl module load ($mod)"; - } else { - debug "loading perl module $mod\n"; - } - - # 1. find real file - for my $dir (@ORIG_INC) { - warn "$dir/$mod\n";#d# - ref $dir and next; - 0 <= Coro::AIO::aio_load "$dir/$mod", my $data - or next; - - $data = "#line 1 $dir/$mod\n$data"; - - open my $fh, "<", \$data or die; - - return $fh; - } - - () -} - -sub init_inc { - # save original @INC - @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC; - - # make sure we can do scalar-opens - open my $dummy, "<", \my $dummy2; - - # execute some stuff so perl load's some of the core modules - /Ü/ =~ /ü/i; - eval { &Storable::nstore_fd }; - - @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc. - - debug "module loading will be asynchronous from this point on."; -} - sub load_facedata($) { my ($path) = @_; @@ -3414,8 +3413,7 @@ trace "loading facedata from $path\n"; - my $facedata; - 0 < aio_load $path, $facedata + 0 < aio_load $path, my $facedata or die "$path: $!"; $facedata = Coro::Storable::thaw $facedata; @@ -3569,6 +3567,8 @@ sub reload_resources { trace "reloading resource files...\n"; + reload_exp_table; + reload_materials; reload_facedata; reload_sound; reload_archetypes; @@ -3581,11 +3581,11 @@ sub reload_config { trace "reloading config file...\n"; - open my $fh, "<:utf8", "$CONFDIR/config" - or return; + 0 < aio_load "$CONFDIR/config", my $config + or die "$CONFDIR/config: $!"; - local $/; - *CFG = YAML::XS::Load scalar <$fh>; + utf8::decode $config; + *CFG = yaml_load $config; $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; @@ -3599,8 +3599,6 @@ }; warn $@ if $@; } - - trace "finished reloading resource files\n"; } sub pidfile() { @@ -3653,15 +3651,13 @@ evthread_start IO::AIO::poll_fileno; cf::sync_job { - init_inc; + cf::incloader::init (); - cf::init_experience; cf::init_anim; cf::init_attackmess; cf::init_dynamic; cf::load_settings; - cf::load_materials; reload_resources; reload_config; @@ -3683,7 +3679,7 @@ use POSIX (); POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; - (pop @POST_INIT)->(0) while @POST_INIT; + cf::_post_init 0; }; cf::object::thawer::errors_are_fatal 0; @@ -3905,7 +3901,7 @@ sub do_reload_perl() { # can/must only be called in main - if (in_main) { + unless (in_main) { error "can only reload from main coroutine"; return; } @@ -3915,25 +3911,26 @@ my $t1 = AE::time; while ($RELOAD) { - info "reloading..."; + cf::get_slot 0.1, -1, "reload_perl"; + info "perl_reload: reloading..."; - trace "entering sync_job"; + trace "perl_reload: entering sync_job"; cf::sync_job { - cf::emergency_save; + #cf::emergency_save; - trace "cancelling all extension coros"; + trace "perl_reload: cancelling all extension coros"; $_->cancel for values %EXT_CORO; %EXT_CORO = (); - trace "removing commands"; + trace "perl_reload: removing commands"; %COMMAND = (); - trace "removing ext/exti commands"; + trace "perl_reload: removing ext/exti commands"; %EXTCMD = (); %EXTICMD = (); - trace "unloading/nuking all extensions"; + trace "perl_reload: unloading/nuking all extensions"; for my $pkg (@EXTS) { trace "... unloading $pkg"; @@ -3948,7 +3945,7 @@ clear_package $pkg; } - trace "unloading all perl modules loaded from $LIBDIR"; + trace "perl_reload: unloading all perl modules loaded from $LIBDIR"; while (my ($k, $v) = each %INC) { next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; @@ -3965,11 +3962,11 @@ clear_package $k; } - trace "getting rid of safe::, as good as possible"; + trace "perl_reload: getting rid of safe::, as good as possible"; clear_package "safe::$_" for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); - trace "unloading cf.pm \"a bit\""; + trace "perl_reload: unloading cf.pm \"a bit\""; delete $INC{"cf.pm"}; delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; @@ -3977,44 +3974,42 @@ # and global variables created in xs #clear_package __PACKAGE__; - info "unload completed, starting to reload now"; + info "perl_reload: unload completed, starting to reload now"; - trace "reloading cf.pm"; + trace "perl_reload: reloading cf.pm"; require cf; cf::_connect_to_perl_1; - trace "loading config and database again"; + trace "perl_reload: loading config and database again"; cf::reload_config; - trace "loading extensions"; + trace "perl_reload: loading extensions"; cf::load_extensions; if ($REATTACH_ON_RELOAD) { - trace "reattaching attachments to objects/players"; + trace "perl_reload: reattaching attachments to objects/players"; _global_reattach; # objects, sockets - trace "reattaching attachments to maps"; + trace "perl_reload: reattaching attachments to maps"; reattach $_ for values %MAP; - trace "reattaching attachments to players"; + trace "perl_reload: reattaching attachments to players"; reattach $_ for values %PLAYER; } - trace "running post_init jobs"; - (pop @POST_INIT)->(1) while @POST_INIT; + cf::_post_init 1; - trace "leaving sync_job"; + trace "perl_reload: leaving sync_job"; 1 } or do { error $@; - cf::cleanup "error while reloading, exiting."; + cf::cleanup "perl_reload: error, exiting."; }; - info "reloaded"; --$RELOAD; } $t1 = AE::time - $t1; - info "reload completed in ${t1}s\n"; + info "perl_reload: completed in ${t1}s\n"; }; our $RELOAD_WATCHER; # used only during reload @@ -4052,7 +4047,7 @@ our @WAIT_FOR_TICK; our @WAIT_FOR_TICK_BEGIN; -sub wait_for_tick { +sub wait_for_tick() { return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; my $signal = new Coro::Signal; @@ -4060,7 +4055,7 @@ $signal->wait; } -sub wait_for_tick_begin { +sub wait_for_tick_begin() { return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; my $signal = new Coro::Signal;