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.544 by root, Thu May 6 21:58:07 2010 UTC vs.
Revision 1.553 by root, Tue May 18 21:30:16 2010 UTC

233from wherever your confdir points to. 233from wherever your confdir points to.
234 234
235=item cf::wait_for_tick, cf::wait_for_tick_begin 235=item cf::wait_for_tick, cf::wait_for_tick_begin
236 236
237These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only 237These are functions that inhibit the current coroutine one tick. cf::wait_for_tick_begin only
238returns directly I<after> the tick processing (and consequently, can only wake one process 238returns directly I<after> the tick processing (and consequently, can only wake one thread
239per tick), while cf::wait_for_tick wakes up all waiters after tick processing. 239per tick), while cf::wait_for_tick wakes up all waiters after tick processing.
240
241=cut
242
243sub wait_for_tick();
244sub wait_for_tick_begin();
240 245
241=item @cf::INVOKE_RESULTS 246=item @cf::INVOKE_RESULTS
242 247
243This array contains the results of the last C<invoke ()> call. When 248This array contains the results of the last C<invoke ()> call. When
244C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of 249C<cf::override> is called C<@cf::INVOKE_RESULTS> is set to the parameters of
352our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max 357our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
353 358
354sub encode_json($) { $json_coder->encode ($_[0]) } 359sub encode_json($) { $json_coder->encode ($_[0]) }
355sub decode_json($) { $json_coder->decode ($_[0]) } 360sub decode_json($) { $json_coder->decode ($_[0]) }
356 361
362=item $ref = cf::yaml_load $scalar
363
364Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks).
365
366=cut
367
368sub fork_call(&@);
369
370sub yaml_load($) {
371 fork_call { YAML::XS::Load $_[0] } @_
372}
373
357=item cf::post_init { BLOCK } 374=item cf::post_init { BLOCK }
358 375
359Execute the given codeblock, I<after> all extensions have been (re-)loaded, 376Execute the given codeblock, I<after> all extensions have been (re-)loaded,
360but I<before> the server starts ticking again. 377but I<before> the server starts ticking again.
361 378
362The cdoeblock will have a single boolean argument to indicate whether this 379The codeblock will have a single boolean argument to indicate whether this
363is a reload or not. 380is a reload or not.
364 381
365=cut 382=cut
366 383
367sub post_init(&) { 384sub post_init(&) {
368 push @POST_INIT, shift; 385 push @POST_INIT, shift;
386}
387
388sub _post_init {
389 trace "running post_init jobs";
390
391 # run them in parallel...
392
393 my @join;
394
395 while () {
396 push @join, map &Coro::async ($_, 0), @POST_INIT;
397 @POST_INIT = ();
398
399 @join or last;
400
401 (pop @join)->join;
402 }
369} 403}
370 404
371=item cf::lock_wait $string 405=item cf::lock_wait $string
372 406
373Wait until the given lock is available. See cf::lock_acquire. 407Wait until the given lock is available. See cf::lock_acquire.
578 $EXT_CORO{$coro+0} = $coro; 612 $EXT_CORO{$coro+0} = $coro;
579 613
580 $coro 614 $coro
581} 615}
582 616
583=item fork_call { }, $args 617=item fork_call { }, @args
584 618
585Executes the given code block with the given arguments in a seperate 619Executes the given code block with the given arguments in a seperate
586process, returning the results. Everything must be serialisable with 620process, returning the results. Everything must be serialisable with
587Coro::Storable. May, of course, block. Note that the executed sub may 621Coro::Storable. May, of course, block. Note that the executed sub may
588never block itself or use any form of event handling. 622never block itself or use any form of event handling.
589 623
590=cut 624=cut
591 625
626sub post_fork {
627 reset_signals;
628}
629
592sub fork_call(&@) { 630sub fork_call(&@) {
593 my ($cb, @args) = @_; 631 my ($cb, @args) = @_;
594 632
595 # we seemingly have to make a local copy of the whole thing, 633 # we seemingly have to make a local copy of the whole thing,
596 # otherwise perl prematurely frees the stuff :/ 634 # otherwise perl prematurely frees the stuff :/
597 # TODO: investigate and fix (likely this will be rather laborious) 635 # TODO: investigate and fix (likely this will be rather laborious)
598 636
599 my @res = Coro::Util::fork_eval { 637 my @res = Coro::Util::fork_eval {
600 reset_signals; 638 cf::post_fork;
601 &$cb 639 &$cb
602 }, @args; 640 } @args;
603 641
604 wantarray ? @res : $res[-1] 642 wantarray ? @res : $res[-1]
605} 643}
606 644
607sub objinfo { 645sub objinfo {
1641 my $name = $pl->ob->name; 1679 my $name = $pl->ob->name;
1642 1680
1643 $pl->{deny_save} = 1; 1681 $pl->{deny_save} = 1;
1644 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1682 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1645 1683
1646 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1684 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns;
1647 $pl->deactivate; 1685 $pl->deactivate;
1686
1648 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1687 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1649 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1688 $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns;
1689 ext::highscore::check ($pl->ob);
1690
1650 $pl->ns->destroy if $pl->ns; 1691 $pl->ns->destroy if $pl->ns;
1651 1692
1652 my $path = playerdir $pl; 1693 my $path = playerdir $pl;
1653 my $temp = "$path~$cf::RUNTIME~deleting~"; 1694 my $temp = "$path~$cf::RUNTIME~deleting~";
1654 aio_rename $path, $temp; 1695 aio_rename $path, $temp;
1894 $base =~ s{[^/]+/?$}{}; 1935 $base =~ s{[^/]+/?$}{};
1895 $path = "$base/$path"; 1936 $path = "$base/$path";
1896 } 1937 }
1897 1938
1898 for ($path) { 1939 for ($path) {
1899 redo if s{//}{/};
1900 redo if s{/\.?/}{/}; 1940 redo if s{/\.?/}{/};
1901 redo if s{/[^/]+/\.\./}{/}; 1941 redo if s{/[^/]+/\.\./}{/};
1902 } 1942 }
1903 1943
1904 $path 1944 $path
2146 2186
2147 $self->in_memory (cf::MAP_ACTIVE); 2187 $self->in_memory (cf::MAP_ACTIVE);
2148 } 2188 }
2149 2189
2150 $self->post_load; 2190 $self->post_load;
2191
2192 1
2151} 2193}
2152 2194
2153# customize the map for a given player, i.e. 2195# customize the map for a given player, i.e.
2154# return the _real_ map. used by e.g. per-player 2196# return the _real_ map. used by e.g. per-player
2155# maps to change the path to ~playername/mappath 2197# maps to change the path to ~playername/mappath
3370 3412
3371 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3413 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3372 3414
3373 trace "loading facedata from $path\n"; 3415 trace "loading facedata from $path\n";
3374 3416
3375 my $facedata;
3376 0 < aio_load $path, $facedata 3417 0 < aio_load $path, my $facedata
3377 or die "$path: $!"; 3418 or die "$path: $!";
3378 3419
3379 $facedata = Coro::Storable::thaw $facedata; 3420 $facedata = Coro::Storable::thaw $facedata;
3380 3421
3381 $facedata->{version} == 2 3422 $facedata->{version} == 2
3499sub reload_treasures { 3540sub reload_treasures {
3500 load_resource_file "$DATADIR/treasures" 3541 load_resource_file "$DATADIR/treasures"
3501 or die "unable to load treasurelists\n"; 3542 or die "unable to load treasurelists\n";
3502} 3543}
3503 3544
3504sub reload_exp {
3505 cf::init_experience;
3506}
3507
3508sub reload_sound { 3545sub reload_sound {
3509 trace "loading sound config from $DATADIR/sound\n"; 3546 trace "loading sound config from $DATADIR/sound\n";
3510 3547
3511 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data 3548 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data
3512 or die "$DATADIR/sound $!"; 3549 or die "$DATADIR/sound $!";
3529} 3566}
3530 3567
3531sub reload_resources { 3568sub reload_resources {
3532 trace "reloading resource files...\n"; 3569 trace "reloading resource files...\n";
3533 3570
3534 reload_exp; 3571 reload_exp_table;
3572 reload_materials;
3535 reload_facedata; 3573 reload_facedata;
3536 reload_sound; 3574 reload_sound;
3537 reload_archetypes; 3575 reload_archetypes;
3538 reload_regions; 3576 reload_regions;
3539 reload_treasures; 3577 reload_treasures;
3542} 3580}
3543 3581
3544sub reload_config { 3582sub reload_config {
3545 trace "reloading config file...\n"; 3583 trace "reloading config file...\n";
3546 3584
3547 open my $fh, "<:utf8", "$CONFDIR/config" 3585 0 < aio_load "$CONFDIR/config", my $config
3548 or return; 3586 or die "$CONFDIR/config: $!";
3549 3587
3550 local $/; 3588 utf8::decode $config;
3551 *CFG = YAML::XS::Load scalar <$fh>; 3589 *CFG = yaml_load $config;
3552 3590
3553 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3591 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3554 3592
3555 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3593 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3556 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3594 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3560 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3598 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3561 and die "WARNING: m(un)lockall failed: $!\n"; 3599 and die "WARNING: m(un)lockall failed: $!\n";
3562 }; 3600 };
3563 warn $@ if $@; 3601 warn $@ if $@;
3564 } 3602 }
3565
3566 trace "finished reloading resource files\n";
3567} 3603}
3568 3604
3569sub pidfile() { 3605sub pidfile() {
3570 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3606 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3571 or die "$PIDFILE: $!"; 3607 or die "$PIDFILE: $!";
3621 cf::init_anim; 3657 cf::init_anim;
3622 cf::init_attackmess; 3658 cf::init_attackmess;
3623 cf::init_dynamic; 3659 cf::init_dynamic;
3624 3660
3625 cf::load_settings; 3661 cf::load_settings;
3626 cf::load_materials;
3627 3662
3628 reload_resources; 3663 reload_resources;
3629 reload_config; 3664 reload_config;
3630 db_init; 3665 db_init;
3631 3666
3643 3678
3644 # no (long-running) fork's whatsoever before this point(!) 3679 # no (long-running) fork's whatsoever before this point(!)
3645 use POSIX (); 3680 use POSIX ();
3646 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3681 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3647 3682
3648 (pop @POST_INIT)->(0) while @POST_INIT; 3683 cf::_post_init 0;
3649 }; 3684 };
3650 3685
3651 cf::object::thawer::errors_are_fatal 0; 3686 cf::object::thawer::errors_are_fatal 0;
3652 info "parse errors in files are no longer fatal from this point on.\n"; 3687 info "parse errors in files are no longer fatal from this point on.\n";
3653 3688
3876 3911
3877 my $t1 = AE::time; 3912 my $t1 = AE::time;
3878 3913
3879 while ($RELOAD) { 3914 while ($RELOAD) {
3880 cf::get_slot 0.1, -1, "reload_perl"; 3915 cf::get_slot 0.1, -1, "reload_perl";
3881 info "reloading..."; 3916 info "perl_reload: reloading...";
3882 3917
3883 trace "entering sync_job"; 3918 trace "perl_reload: entering sync_job";
3884 3919
3885 cf::sync_job { 3920 cf::sync_job {
3886 #cf::emergency_save; 3921 #cf::emergency_save;
3887 3922
3888 trace "cancelling all extension coros"; 3923 trace "perl_reload: cancelling all extension coros";
3889 $_->cancel for values %EXT_CORO; 3924 $_->cancel for values %EXT_CORO;
3890 %EXT_CORO = (); 3925 %EXT_CORO = ();
3891 3926
3892 trace "removing commands"; 3927 trace "perl_reload: removing commands";
3893 %COMMAND = (); 3928 %COMMAND = ();
3894 3929
3895 trace "removing ext/exti commands"; 3930 trace "perl_reload: removing ext/exti commands";
3896 %EXTCMD = (); 3931 %EXTCMD = ();
3897 %EXTICMD = (); 3932 %EXTICMD = ();
3898 3933
3899 trace "unloading/nuking all extensions"; 3934 trace "perl_reload: unloading/nuking all extensions";
3900 for my $pkg (@EXTS) { 3935 for my $pkg (@EXTS) {
3901 trace "... unloading $pkg"; 3936 trace "... unloading $pkg";
3902 3937
3903 if (my $cb = $pkg->can ("unload")) { 3938 if (my $cb = $pkg->can ("unload")) {
3904 eval { 3939 eval {
3909 3944
3910 trace "... clearing $pkg"; 3945 trace "... clearing $pkg";
3911 clear_package $pkg; 3946 clear_package $pkg;
3912 } 3947 }
3913 3948
3914 trace "unloading all perl modules loaded from $LIBDIR"; 3949 trace "perl_reload: unloading all perl modules loaded from $LIBDIR";
3915 while (my ($k, $v) = each %INC) { 3950 while (my ($k, $v) = each %INC) {
3916 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3951 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3917 3952
3918 trace "... unloading $k"; 3953 trace "... unloading $k";
3919 delete $INC{$k}; 3954 delete $INC{$k};
3926 } 3961 }
3927 3962
3928 clear_package $k; 3963 clear_package $k;
3929 } 3964 }
3930 3965
3931 trace "getting rid of safe::, as good as possible"; 3966 trace "perl_reload: getting rid of safe::, as good as possible";
3932 clear_package "safe::$_" 3967 clear_package "safe::$_"
3933 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3968 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3934 3969
3935 trace "unloading cf.pm \"a bit\""; 3970 trace "perl_reload: unloading cf.pm \"a bit\"";
3936 delete $INC{"cf.pm"}; 3971 delete $INC{"cf.pm"};
3937 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3972 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3938 3973
3939 # don't, removes xs symbols, too, 3974 # don't, removes xs symbols, too,
3940 # and global variables created in xs 3975 # and global variables created in xs
3941 #clear_package __PACKAGE__; 3976 #clear_package __PACKAGE__;
3942 3977
3943 info "unload completed, starting to reload now"; 3978 info "perl_reload: unload completed, starting to reload now";
3944 3979
3945 trace "reloading cf.pm"; 3980 trace "perl_reload: reloading cf.pm";
3946 require cf; 3981 require cf;
3947 cf::_connect_to_perl_1; 3982 cf::_connect_to_perl_1;
3948 3983
3949 trace "loading config and database again"; 3984 trace "perl_reload: loading config and database again";
3950 cf::reload_config; 3985 cf::reload_config;
3951 3986
3952 trace "loading extensions"; 3987 trace "perl_reload: loading extensions";
3953 cf::load_extensions; 3988 cf::load_extensions;
3954 3989
3955 if ($REATTACH_ON_RELOAD) { 3990 if ($REATTACH_ON_RELOAD) {
3956 trace "reattaching attachments to objects/players"; 3991 trace "perl_reload: reattaching attachments to objects/players";
3957 _global_reattach; # objects, sockets 3992 _global_reattach; # objects, sockets
3958 trace "reattaching attachments to maps"; 3993 trace "perl_reload: reattaching attachments to maps";
3959 reattach $_ for values %MAP; 3994 reattach $_ for values %MAP;
3960 trace "reattaching attachments to players"; 3995 trace "perl_reload: reattaching attachments to players";
3961 reattach $_ for values %PLAYER; 3996 reattach $_ for values %PLAYER;
3962 } 3997 }
3963 3998
3964 trace "running post_init jobs"; 3999 cf::_post_init 1;
3965 (pop @POST_INIT)->(1) while @POST_INIT;
3966 4000
3967 trace "leaving sync_job"; 4001 trace "perl_reload: leaving sync_job";
3968 4002
3969 1 4003 1
3970 } or do { 4004 } or do {
3971 error $@; 4005 error $@;
3972 cf::cleanup "error while reloading, exiting."; 4006 cf::cleanup "perl_reload: error, exiting.";
3973 }; 4007 };
3974 4008
3975 info "reloaded";
3976 --$RELOAD; 4009 --$RELOAD;
3977 } 4010 }
3978 4011
3979 $t1 = AE::time - $t1; 4012 $t1 = AE::time - $t1;
3980 info "reload completed in ${t1}s\n"; 4013 info "perl_reload: completed in ${t1}s\n";
3981}; 4014};
3982 4015
3983our $RELOAD_WATCHER; # used only during reload 4016our $RELOAD_WATCHER; # used only during reload
3984 4017
3985sub reload_perl() { 4018sub reload_perl() {
4013my $bug_warning = 0; 4046my $bug_warning = 0;
4014 4047
4015our @WAIT_FOR_TICK; 4048our @WAIT_FOR_TICK;
4016our @WAIT_FOR_TICK_BEGIN; 4049our @WAIT_FOR_TICK_BEGIN;
4017 4050
4018sub wait_for_tick { 4051sub wait_for_tick() {
4019 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4052 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4020 4053
4021 my $signal = new Coro::Signal; 4054 my $signal = new Coro::Signal;
4022 push @WAIT_FOR_TICK, $signal; 4055 push @WAIT_FOR_TICK, $signal;
4023 $signal->wait; 4056 $signal->wait;
4024} 4057}
4025 4058
4026sub wait_for_tick_begin { 4059sub wait_for_tick_begin() {
4027 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4060 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4028 4061
4029 my $signal = new Coro::Signal; 4062 my $signal = new Coro::Signal;
4030 push @WAIT_FOR_TICK_BEGIN, $signal; 4063 push @WAIT_FOR_TICK_BEGIN, $signal;
4031 $signal->wait; 4064 $signal->wait;
4118 IO::AIO::min_parallel 8; 4151 IO::AIO::min_parallel 8;
4119 IO::AIO::max_poll_time $TICK * 0.1; 4152 IO::AIO::max_poll_time $TICK * 0.1;
4120 undef $AnyEvent::AIO::WATCHER; 4153 undef $AnyEvent::AIO::WATCHER;
4121} 4154}
4122 4155
4123my $_log_backtrace; 4156our $_log_backtrace;
4157our $_log_backtrace_last;
4124 4158
4125sub _log_backtrace { 4159sub _log_backtrace {
4126 my ($msg, @addr) = @_; 4160 my ($msg, @addr) = @_;
4127 4161
4128 $msg =~ s/\n//; 4162 $msg =~ s/\n$//;
4129 4163
4164 if ($_log_backtrace_last eq $msg) {
4165 LOG llevInfo, "[ABT] $msg\n";
4166 LOG llevInfo, "[ABT] [duplicate, suppressed]\n";
4130 # limit the # of concurrent backtraces 4167 # limit the # of concurrent backtraces
4131 if ($_log_backtrace < 2) { 4168 } elsif ($_log_backtrace < 2) {
4169 $_log_backtrace_last = $msg;
4132 ++$_log_backtrace; 4170 ++$_log_backtrace;
4133 my $perl_bt = Carp::longmess $msg; 4171 my $perl_bt = Carp::longmess $msg;
4134 async { 4172 async {
4135 $Coro::current->{desc} = "abt $msg"; 4173 $Coro::current->{desc} = "abt $msg";
4136 4174
4156 LOG llevInfo, "[ABT] $_\n" for @bt; 4194 LOG llevInfo, "[ABT] $_\n" for @bt;
4157 --$_log_backtrace; 4195 --$_log_backtrace;
4158 }; 4196 };
4159 } else { 4197 } else {
4160 LOG llevInfo, "[ABT] $msg\n"; 4198 LOG llevInfo, "[ABT] $msg\n";
4161 LOG llevInfo, "[ABT] [suppressed]\n"; 4199 LOG llevInfo, "[ABT] [overload, suppressed]\n";
4162 } 4200 }
4163} 4201}
4164 4202
4165# load additional modules 4203# load additional modules
4166require "cf/$_.pm" for @EXTRA_MODULES; 4204require "cf/$_.pm" for @EXTRA_MODULES;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines