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.540 by root, Tue May 4 23:32:29 2010 UTC vs.
Revision 1.550 by root, Fri May 14 22:56:47 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
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 {
1451 1489
1452 my $active = eval $v->{source}; 1490 my $active = eval $v->{source};
1453 1491
1454 if (length $@) { 1492 if (length $@) {
1455 error "$v->{path}: $@\n"; 1493 error "$v->{path}: $@\n";
1456 undef $@; # work around perl 5.10.0 utf-8 caching bug
1457 1494
1458 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1495 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1459 if exists $v->{meta}{mandatory}; 1496 if exists $v->{meta}{mandatory};
1460 1497
1461 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1498 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1642 my $name = $pl->ob->name; 1679 my $name = $pl->ob->name;
1643 1680
1644 $pl->{deny_save} = 1; 1681 $pl->{deny_save} = 1;
1645 $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
1646 1683
1647 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1684 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->ns;
1648 $pl->deactivate; 1685 $pl->deactivate;
1686
1649 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;
1650 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1688 $pl->invoke (cf::EVENT_PLAYER_QUIT) if $pl->ns;
1689 ext::highscore::check ($pl->ob);
1690
1651 $pl->ns->destroy if $pl->ns; 1691 $pl->ns->destroy if $pl->ns;
1652 1692
1653 my $path = playerdir $pl; 1693 my $path = playerdir $pl;
1654 my $temp = "$path~$cf::RUNTIME~deleting~"; 1694 my $temp = "$path~$cf::RUNTIME~deleting~";
1655 aio_rename $path, $temp; 1695 aio_rename $path, $temp;
1870} 1910}
1871 1911
1872sub normalise { 1912sub normalise {
1873 my ($path, $base) = @_; 1913 my ($path, $base) = @_;
1874 1914
1875 $path = "$path"; # make sure its a string 1915 $path = "$path"; # make sure it's a string
1876 1916
1877 $path =~ s/\.map$//; 1917 $path =~ s/\.map$//;
1878 1918
1879 # map plan: 1919 # map plan:
1880 # 1920 #
1919 $self->init; # pass $1 etc. 1959 $self->init; # pass $1 etc.
1920 return $self; 1960 return $self;
1921 } 1961 }
1922 } 1962 }
1923 1963
1924 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1964 Carp::cluck "unable to resolve path '$path' (base '$base')";
1925 () 1965 ()
1926} 1966}
1927 1967
1928sub init { 1968sub init {
1929 my ($self) = @_; 1969 my ($self) = @_;
2051} 2091}
2052 2092
2053sub find; 2093sub find;
2054sub find { 2094sub find {
2055 my ($path, $origin) = @_; 2095 my ($path, $origin) = @_;
2096
2097 cf::cede_to_tick;
2056 2098
2057 $path = normalise $path, $origin && $origin->path; 2099 $path = normalise $path, $origin && $origin->path;
2058 2100
2059 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2101 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2060 my $guard2 = cf::lock_acquire "map_find:$path"; 2102 my $guard2 = cf::lock_acquire "map_find:$path";
3358=cut 3400=cut
3359 3401
3360############################################################################# 3402#############################################################################
3361# the server's init and main functions 3403# the server's init and main functions
3362 3404
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 warn "$dir/$mod\n";#d#
3376 ref $dir and next;
3377 0 <= Coro::AIO::aio_load "$dir/$mod", my $data
3378 or next;
3379
3380 $data = "#line 1 $dir/$mod\n$data";
3381
3382 open my $fh, "<", \$data or die;
3383
3384 return $fh;
3385 }
3386
3387 ()
3388}
3389
3390sub init_inc {
3391 # save original @INC
3392 @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC;
3393
3394 # make sure we can do scalar-opens
3395 open my $dummy, "<", \my $dummy2;
3396
3397 # execute some stuff so perl load's some of the core modules
3398 /Ü/ =~ /ü/i;
3399 eval { &Storable::nstore_fd };
3400
3401 @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc.
3402
3403 debug "module loading will be asynchronous from this point on.";
3404}
3405
3406sub load_facedata($) { 3405sub load_facedata($) {
3407 my ($path) = @_; 3406 my ($path) = @_;
3408 3407
3409 # HACK to clear player env face cache, we need some signal framework 3408 # HACK to clear player env face cache, we need some signal framework
3410 # for this (global event?) 3409 # for this (global event?)
3412 3411
3413 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3412 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3414 3413
3415 trace "loading facedata from $path\n"; 3414 trace "loading facedata from $path\n";
3416 3415
3417 my $facedata;
3418 0 < aio_load $path, $facedata 3416 0 < aio_load $path, my $facedata
3419 or die "$path: $!"; 3417 or die "$path: $!";
3420 3418
3421 $facedata = Coro::Storable::thaw $facedata; 3419 $facedata = Coro::Storable::thaw $facedata;
3422 3420
3423 $facedata->{version} == 2 3421 $facedata->{version} == 2
3567} 3565}
3568 3566
3569sub reload_resources { 3567sub reload_resources {
3570 trace "reloading resource files...\n"; 3568 trace "reloading resource files...\n";
3571 3569
3570 reload_exp_table;
3571 reload_materials;
3572 reload_facedata; 3572 reload_facedata;
3573 reload_sound; 3573 reload_sound;
3574 reload_archetypes; 3574 reload_archetypes;
3575 reload_regions; 3575 reload_regions;
3576 reload_treasures; 3576 reload_treasures;
3579} 3579}
3580 3580
3581sub reload_config { 3581sub reload_config {
3582 trace "reloading config file...\n"; 3582 trace "reloading config file...\n";
3583 3583
3584 open my $fh, "<:utf8", "$CONFDIR/config" 3584 0 < aio_load "$CONFDIR/config", my $config
3585 or return; 3585 or die "$CONFDIR/config: $!";
3586 3586
3587 local $/; 3587 utf8::decode $config;
3588 *CFG = YAML::XS::Load scalar <$fh>; 3588 *CFG = yaml_load $config;
3589 3589
3590 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3590 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3591 3591
3592 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3592 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3593 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3593 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3597 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3597 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3598 and die "WARNING: m(un)lockall failed: $!\n"; 3598 and die "WARNING: m(un)lockall failed: $!\n";
3599 }; 3599 };
3600 warn $@ if $@; 3600 warn $@ if $@;
3601 } 3601 }
3602
3603 trace "finished reloading resource files\n";
3604} 3602}
3605 3603
3606sub pidfile() { 3604sub pidfile() {
3607 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3605 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3608 or die "$PIDFILE: $!"; 3606 or die "$PIDFILE: $!";
3651 }; 3649 };
3652 3650
3653 evthread_start IO::AIO::poll_fileno; 3651 evthread_start IO::AIO::poll_fileno;
3654 3652
3655 cf::sync_job { 3653 cf::sync_job {
3656 init_inc; 3654 cf::incloader::init ();
3657 3655
3658 cf::init_experience;
3659 cf::init_anim; 3656 cf::init_anim;
3660 cf::init_attackmess; 3657 cf::init_attackmess;
3661 cf::init_dynamic; 3658 cf::init_dynamic;
3662 3659
3663 cf::load_settings; 3660 cf::load_settings;
3664 cf::load_materials;
3665 3661
3666 reload_resources; 3662 reload_resources;
3667 reload_config; 3663 reload_config;
3668 db_init; 3664 db_init;
3669 3665
3681 3677
3682 # no (long-running) fork's whatsoever before this point(!) 3678 # no (long-running) fork's whatsoever before this point(!)
3683 use POSIX (); 3679 use POSIX ();
3684 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD}; 3680 POSIX::close delete $ENV{LOCKUTIL_LOCK_FD} if exists $ENV{LOCKUTIL_LOCK_FD};
3685 3681
3686 (pop @POST_INIT)->(0) while @POST_INIT; 3682 cf::_post_init 0;
3687 }; 3683 };
3688 3684
3689 cf::object::thawer::errors_are_fatal 0; 3685 cf::object::thawer::errors_are_fatal 0;
3690 info "parse errors in files are no longer fatal from this point on.\n"; 3686 info "parse errors in files are no longer fatal from this point on.\n";
3691 3687
3903 } 3899 }
3904} 3900}
3905 3901
3906sub do_reload_perl() { 3902sub do_reload_perl() {
3907 # can/must only be called in main 3903 # can/must only be called in main
3908 if (in_main) { 3904 unless (in_main) {
3909 error "can only reload from main coroutine"; 3905 error "can only reload from main coroutine";
3910 return; 3906 return;
3911 } 3907 }
3912 3908
3913 return if $RELOAD++; 3909 return if $RELOAD++;
3914 3910
3915 my $t1 = AE::time; 3911 my $t1 = AE::time;
3916 3912
3917 while ($RELOAD) { 3913 while ($RELOAD) {
3914 cf::get_slot 0.1, -1, "reload_perl";
3918 info "reloading..."; 3915 info "perl_reload: reloading...";
3919 3916
3920 trace "entering sync_job"; 3917 trace "perl_reload: entering sync_job";
3921 3918
3922 cf::sync_job { 3919 cf::sync_job {
3923 cf::emergency_save; 3920 #cf::emergency_save;
3924 3921
3925 trace "cancelling all extension coros"; 3922 trace "perl_reload: cancelling all extension coros";
3926 $_->cancel for values %EXT_CORO; 3923 $_->cancel for values %EXT_CORO;
3927 %EXT_CORO = (); 3924 %EXT_CORO = ();
3928 3925
3929 trace "removing commands"; 3926 trace "perl_reload: removing commands";
3930 %COMMAND = (); 3927 %COMMAND = ();
3931 3928
3932 trace "removing ext/exti commands"; 3929 trace "perl_reload: removing ext/exti commands";
3933 %EXTCMD = (); 3930 %EXTCMD = ();
3934 %EXTICMD = (); 3931 %EXTICMD = ();
3935 3932
3936 trace "unloading/nuking all extensions"; 3933 trace "perl_reload: unloading/nuking all extensions";
3937 for my $pkg (@EXTS) { 3934 for my $pkg (@EXTS) {
3938 trace "... unloading $pkg"; 3935 trace "... unloading $pkg";
3939 3936
3940 if (my $cb = $pkg->can ("unload")) { 3937 if (my $cb = $pkg->can ("unload")) {
3941 eval { 3938 eval {
3946 3943
3947 trace "... clearing $pkg"; 3944 trace "... clearing $pkg";
3948 clear_package $pkg; 3945 clear_package $pkg;
3949 } 3946 }
3950 3947
3951 trace "unloading all perl modules loaded from $LIBDIR"; 3948 trace "perl_reload: unloading all perl modules loaded from $LIBDIR";
3952 while (my ($k, $v) = each %INC) { 3949 while (my ($k, $v) = each %INC) {
3953 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/; 3950 next unless $v =~ /^\Q$LIBDIR\E\/.*\.pm$/;
3954 3951
3955 trace "... unloading $k"; 3952 trace "... unloading $k";
3956 delete $INC{$k}; 3953 delete $INC{$k};
3963 } 3960 }
3964 3961
3965 clear_package $k; 3962 clear_package $k;
3966 } 3963 }
3967 3964
3968 trace "getting rid of safe::, as good as possible"; 3965 trace "perl_reload: getting rid of safe::, as good as possible";
3969 clear_package "safe::$_" 3966 clear_package "safe::$_"
3970 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region); 3967 for qw(cf::attachable cf::object cf::object::player cf::client cf::player cf::map cf::party cf::region);
3971 3968
3972 trace "unloading cf.pm \"a bit\""; 3969 trace "perl_reload: unloading cf.pm \"a bit\"";
3973 delete $INC{"cf.pm"}; 3970 delete $INC{"cf.pm"};
3974 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES; 3971 delete $INC{"cf/$_.pm"} for @EXTRA_MODULES;
3975 3972
3976 # don't, removes xs symbols, too, 3973 # don't, removes xs symbols, too,
3977 # and global variables created in xs 3974 # and global variables created in xs
3978 #clear_package __PACKAGE__; 3975 #clear_package __PACKAGE__;
3979 3976
3980 info "unload completed, starting to reload now"; 3977 info "perl_reload: unload completed, starting to reload now";
3981 3978
3982 trace "reloading cf.pm"; 3979 trace "perl_reload: reloading cf.pm";
3983 require cf; 3980 require cf;
3984 cf::_connect_to_perl_1; 3981 cf::_connect_to_perl_1;
3985 3982
3986 trace "loading config and database again"; 3983 trace "perl_reload: loading config and database again";
3987 cf::reload_config; 3984 cf::reload_config;
3988 3985
3989 trace "loading extensions"; 3986 trace "perl_reload: loading extensions";
3990 cf::load_extensions; 3987 cf::load_extensions;
3991 3988
3992 if ($REATTACH_ON_RELOAD) { 3989 if ($REATTACH_ON_RELOAD) {
3993 trace "reattaching attachments to objects/players"; 3990 trace "perl_reload: reattaching attachments to objects/players";
3994 _global_reattach; # objects, sockets 3991 _global_reattach; # objects, sockets
3995 trace "reattaching attachments to maps"; 3992 trace "perl_reload: reattaching attachments to maps";
3996 reattach $_ for values %MAP; 3993 reattach $_ for values %MAP;
3997 trace "reattaching attachments to players"; 3994 trace "perl_reload: reattaching attachments to players";
3998 reattach $_ for values %PLAYER; 3995 reattach $_ for values %PLAYER;
3999 } 3996 }
4000 3997
4001 trace "running post_init jobs"; 3998 cf::_post_init 1;
4002 (pop @POST_INIT)->(1) while @POST_INIT;
4003 3999
4004 trace "leaving sync_job"; 4000 trace "perl_reload: leaving sync_job";
4005 4001
4006 1 4002 1
4007 } or do { 4003 } or do {
4008 error $@; 4004 error $@;
4009 cf::cleanup "error while reloading, exiting."; 4005 cf::cleanup "perl_reload: error, exiting.";
4010 }; 4006 };
4011 4007
4012 info "reloaded";
4013 --$RELOAD; 4008 --$RELOAD;
4014 } 4009 }
4015 4010
4016 $t1 = AE::time - $t1; 4011 $t1 = AE::time - $t1;
4017 info "reload completed in ${t1}s\n"; 4012 info "perl_reload: completed in ${t1}s\n";
4018}; 4013};
4019 4014
4020our $RELOAD_WATCHER; # used only during reload 4015our $RELOAD_WATCHER; # used only during reload
4021 4016
4022sub reload_perl() { 4017sub reload_perl() {
4050my $bug_warning = 0; 4045my $bug_warning = 0;
4051 4046
4052our @WAIT_FOR_TICK; 4047our @WAIT_FOR_TICK;
4053our @WAIT_FOR_TICK_BEGIN; 4048our @WAIT_FOR_TICK_BEGIN;
4054 4049
4055sub wait_for_tick { 4050sub wait_for_tick() {
4056 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4051 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4057 4052
4058 my $signal = new Coro::Signal; 4053 my $signal = new Coro::Signal;
4059 push @WAIT_FOR_TICK, $signal; 4054 push @WAIT_FOR_TICK, $signal;
4060 $signal->wait; 4055 $signal->wait;
4061} 4056}
4062 4057
4063sub wait_for_tick_begin { 4058sub wait_for_tick_begin() {
4064 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4059 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4065 4060
4066 my $signal = new Coro::Signal; 4061 my $signal = new Coro::Signal;
4067 push @WAIT_FOR_TICK_BEGIN, $signal; 4062 push @WAIT_FOR_TICK_BEGIN, $signal;
4068 $signal->wait; 4063 $signal->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines