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.541 by root, Tue May 4 23:33:16 2010 UTC vs.
Revision 1.547 by root, Sun May 9 22:51:13 2010 UTC

124our $BDB_DEADLOCK_WATCHER; 124our $BDB_DEADLOCK_WATCHER;
125our $BDB_CHECKPOINT_WATCHER; 125our $BDB_CHECKPOINT_WATCHER;
126our $BDB_TRICKLE_WATCHER; 126our $BDB_TRICKLE_WATCHER;
127our $DB_ENV; 127our $DB_ENV;
128 128
129our @EXTRA_MODULES = qw(pod match mapscript); 129our @EXTRA_MODULES = qw(pod match mapscript incloader);
130 130
131our %CFG; 131our %CFG;
132 132
133our $UPTIME; $UPTIME ||= time; 133our $UPTIME; $UPTIME ||= time;
134our $RUNTIME; 134our $RUNTIME;
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
587Coro::Storable. May, of course, block. Note that the executed sub may 592Coro::Storable. May, of course, block. Note that the executed sub may
588never block itself or use any form of event handling. 593never block itself or use any form of event handling.
589 594
590=cut 595=cut
591 596
597sub post_fork {
598 reset_signals;
599}
600
592sub fork_call(&@) { 601sub fork_call(&@) {
593 my ($cb, @args) = @_; 602 my ($cb, @args) = @_;
594 603
595 # we seemingly have to make a local copy of the whole thing, 604 # we seemingly have to make a local copy of the whole thing,
596 # otherwise perl prematurely frees the stuff :/ 605 # otherwise perl prematurely frees the stuff :/
597 # TODO: investigate and fix (likely this will be rather laborious) 606 # TODO: investigate and fix (likely this will be rather laborious)
598 607
599 my @res = Coro::Util::fork_eval { 608 my @res = Coro::Util::fork_eval {
600 reset_signals; 609 cf::post_fork;
601 &$cb 610 &$cb
602 }, @args; 611 }, @args;
603 612
604 wantarray ? @res : $res[-1] 613 wantarray ? @res : $res[-1]
605} 614}
1451 1460
1452 my $active = eval $v->{source}; 1461 my $active = eval $v->{source};
1453 1462
1454 if (length $@) { 1463 if (length $@) {
1455 error "$v->{path}: $@\n"; 1464 error "$v->{path}: $@\n";
1456 undef $@; # work around perl 5.10.0 utf-8 caching bug
1457 1465
1458 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1466 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1459 if exists $v->{meta}{mandatory}; 1467 if exists $v->{meta}{mandatory};
1460 1468
1461 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1469 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1644 $pl->{deny_save} = 1; 1652 $pl->{deny_save} = 1;
1645 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1653 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1646 1654
1647 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1655 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1648 $pl->deactivate; 1656 $pl->deactivate;
1657
1649 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1658 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1659 ext::highscore::check ($pl->ob);
1660
1650 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1661 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1651 $pl->ns->destroy if $pl->ns; 1662 $pl->ns->destroy if $pl->ns;
1652 1663
1653 my $path = playerdir $pl; 1664 my $path = playerdir $pl;
1654 my $temp = "$path~$cf::RUNTIME~deleting~"; 1665 my $temp = "$path~$cf::RUNTIME~deleting~";
1870} 1881}
1871 1882
1872sub normalise { 1883sub normalise {
1873 my ($path, $base) = @_; 1884 my ($path, $base) = @_;
1874 1885
1875 $path = "$path"; # make sure its a string 1886 $path = "$path"; # make sure it's a string
1876 1887
1877 $path =~ s/\.map$//; 1888 $path =~ s/\.map$//;
1878 1889
1879 # map plan: 1890 # map plan:
1880 # 1891 #
1919 $self->init; # pass $1 etc. 1930 $self->init; # pass $1 etc.
1920 return $self; 1931 return $self;
1921 } 1932 }
1922 } 1933 }
1923 1934
1924 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1935 Carp::cluck "unable to resolve path '$path' (base '$base')";
1925 () 1936 ()
1926} 1937}
1927 1938
1928sub init { 1939sub init {
1929 my ($self) = @_; 1940 my ($self) = @_;
2051} 2062}
2052 2063
2053sub find; 2064sub find;
2054sub find { 2065sub find {
2055 my ($path, $origin) = @_; 2066 my ($path, $origin) = @_;
2067
2068 cf::cede_to_tick;
2056 2069
2057 $path = normalise $path, $origin && $origin->path; 2070 $path = normalise $path, $origin && $origin->path;
2058 2071
2059 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2072 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2060 my $guard2 = cf::lock_acquire "map_find:$path"; 2073 my $guard2 = cf::lock_acquire "map_find:$path";
3358=cut 3371=cut
3359 3372
3360############################################################################# 3373#############################################################################
3361# the server's init and main functions 3374# the server's init and main functions
3362 3375
3363# async inc loader. yay.
3364sub inc_loader {
3365 my $mod = $_[1];
3366
3367 if (in_main && !tick_inhibit) {
3368 Carp::cluck "ERROR: attempted synchronous perl module load ($mod)";
3369 } else {
3370 debug "loading perl module $mod\n";
3371 }
3372
3373 # 1. find real file
3374 for my $dir (@ORIG_INC) {
3375 ref $dir and next;
3376 0 <= Coro::AIO::aio_load "$dir/$mod", my $data
3377 or next;
3378
3379 $data = "#line 1 $dir/$mod\n$data";
3380
3381 open my $fh, "<", \$data or die;
3382
3383 return $fh;
3384 }
3385
3386 ()
3387}
3388
3389sub init_inc {
3390 # save original @INC
3391 @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC;
3392
3393 # make sure we can do scalar-opens
3394 open my $dummy, "<", \my $dummy2;
3395
3396 # execute some stuff so perl load's some of the core modules
3397 /Ü/ =~ /ü/i;
3398 eval { &Storable::nstore_fd };
3399
3400 @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc.
3401
3402 debug "module loading will be asynchronous from this point on.";
3403}
3404
3405sub load_facedata($) { 3376sub load_facedata($) {
3406 my ($path) = @_; 3377 my ($path) = @_;
3407 3378
3408 # HACK to clear player env face cache, we need some signal framework 3379 # HACK to clear player env face cache, we need some signal framework
3409 # for this (global event?) 3380 # for this (global event?)
3411 3382
3412 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3383 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3413 3384
3414 trace "loading facedata from $path\n"; 3385 trace "loading facedata from $path\n";
3415 3386
3416 my $facedata;
3417 0 < aio_load $path, $facedata 3387 0 < aio_load $path, my $facedata
3418 or die "$path: $!"; 3388 or die "$path: $!";
3419 3389
3420 $facedata = Coro::Storable::thaw $facedata; 3390 $facedata = Coro::Storable::thaw $facedata;
3421 3391
3422 $facedata->{version} == 2 3392 $facedata->{version} == 2
3566} 3536}
3567 3537
3568sub reload_resources { 3538sub reload_resources {
3569 trace "reloading resource files...\n"; 3539 trace "reloading resource files...\n";
3570 3540
3541 reload_exp_table;
3542 reload_materials;
3571 reload_facedata; 3543 reload_facedata;
3572 reload_sound; 3544 reload_sound;
3573 reload_archetypes; 3545 reload_archetypes;
3574 reload_regions; 3546 reload_regions;
3575 reload_treasures; 3547 reload_treasures;
3578} 3550}
3579 3551
3580sub reload_config { 3552sub reload_config {
3581 trace "reloading config file...\n"; 3553 trace "reloading config file...\n";
3582 3554
3583 open my $fh, "<:utf8", "$CONFDIR/config" 3555 0 < aio_load "$CONFDIR/config", my $config
3584 or return; 3556 or die "$CONFDIR/config: $!";
3585 3557
3586 local $/; 3558 utf8::decode $config;
3559
3560 cf::get_slot 0.1, 10, "reload_config"; # yaml might be slow...
3587 *CFG = YAML::XS::Load scalar <$fh>; 3561 *CFG = YAML::XS::Load $config;
3588 3562
3589 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3563 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3590 3564
3591 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3565 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3592 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3566 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3596 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3570 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3597 and die "WARNING: m(un)lockall failed: $!\n"; 3571 and die "WARNING: m(un)lockall failed: $!\n";
3598 }; 3572 };
3599 warn $@ if $@; 3573 warn $@ if $@;
3600 } 3574 }
3601
3602 trace "finished reloading resource files\n";
3603} 3575}
3604 3576
3605sub pidfile() { 3577sub pidfile() {
3606 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3578 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3607 or die "$PIDFILE: $!"; 3579 or die "$PIDFILE: $!";
3650 }; 3622 };
3651 3623
3652 evthread_start IO::AIO::poll_fileno; 3624 evthread_start IO::AIO::poll_fileno;
3653 3625
3654 cf::sync_job { 3626 cf::sync_job {
3655 init_inc; 3627 cf::incloader::init ();
3656 3628
3657 cf::init_experience;
3658 cf::init_anim; 3629 cf::init_anim;
3659 cf::init_attackmess; 3630 cf::init_attackmess;
3660 cf::init_dynamic; 3631 cf::init_dynamic;
3661 3632
3662 cf::load_settings; 3633 cf::load_settings;
3663 cf::load_materials;
3664 3634
3665 reload_resources; 3635 reload_resources;
3666 reload_config; 3636 reload_config;
3667 db_init; 3637 db_init;
3668 3638
3902 } 3872 }
3903} 3873}
3904 3874
3905sub do_reload_perl() { 3875sub do_reload_perl() {
3906 # can/must only be called in main 3876 # can/must only be called in main
3907 if (in_main) { 3877 unless (in_main) {
3908 error "can only reload from main coroutine"; 3878 error "can only reload from main coroutine";
3909 return; 3879 return;
3910 } 3880 }
3911 3881
3912 return if $RELOAD++; 3882 return if $RELOAD++;
3913 3883
3914 my $t1 = AE::time; 3884 my $t1 = AE::time;
3915 3885
3916 while ($RELOAD) { 3886 while ($RELOAD) {
3887 cf::get_slot 0.1, -1, "reload_perl";
3917 info "reloading..."; 3888 info "reloading...";
3918 3889
3919 trace "entering sync_job"; 3890 trace "entering sync_job";
3920 3891
3921 cf::sync_job { 3892 cf::sync_job {
3922 cf::emergency_save; 3893 #cf::emergency_save;
3923 3894
3924 trace "cancelling all extension coros"; 3895 trace "cancelling all extension coros";
3925 $_->cancel for values %EXT_CORO; 3896 $_->cancel for values %EXT_CORO;
3926 %EXT_CORO = (); 3897 %EXT_CORO = ();
3927 3898
4049my $bug_warning = 0; 4020my $bug_warning = 0;
4050 4021
4051our @WAIT_FOR_TICK; 4022our @WAIT_FOR_TICK;
4052our @WAIT_FOR_TICK_BEGIN; 4023our @WAIT_FOR_TICK_BEGIN;
4053 4024
4054sub wait_for_tick { 4025sub wait_for_tick() {
4055 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4026 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4056 4027
4057 my $signal = new Coro::Signal; 4028 my $signal = new Coro::Signal;
4058 push @WAIT_FOR_TICK, $signal; 4029 push @WAIT_FOR_TICK, $signal;
4059 $signal->wait; 4030 $signal->wait;
4060} 4031}
4061 4032
4062sub wait_for_tick_begin { 4033sub wait_for_tick_begin() {
4063 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4034 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4064 4035
4065 my $signal = new Coro::Signal; 4036 my $signal = new Coro::Signal;
4066 push @WAIT_FOR_TICK_BEGIN, $signal; 4037 push @WAIT_FOR_TICK_BEGIN, $signal;
4067 $signal->wait; 4038 $signal->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines