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.542 by root, Wed May 5 09:05:03 2010 UTC vs.
Revision 1.548 by root, Mon May 10 21:40:22 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(&) {
578 $EXT_CORO{$coro+0} = $coro; 595 $EXT_CORO{$coro+0} = $coro;
579 596
580 $coro 597 $coro
581} 598}
582 599
583=item fork_call { }, $args 600=item fork_call { }, @args
584 601
585Executes the given code block with the given arguments in a seperate 602Executes the given code block with the given arguments in a seperate
586process, returning the results. Everything must be serialisable with 603process, returning the results. Everything must be serialisable with
587Coro::Storable. May, of course, block. Note that the executed sub may 604Coro::Storable. May, of course, block. Note that the executed sub may
588never block itself or use any form of event handling. 605never block itself or use any form of event handling.
589 606
590=cut 607=cut
591 608
609sub post_fork {
610 reset_signals;
611}
612
592sub fork_call(&@) { 613sub fork_call(&@) {
593 my ($cb, @args) = @_; 614 my ($cb, @args) = @_;
594 615
595 # we seemingly have to make a local copy of the whole thing, 616 # we seemingly have to make a local copy of the whole thing,
596 # otherwise perl prematurely frees the stuff :/ 617 # otherwise perl prematurely frees the stuff :/
597 # TODO: investigate and fix (likely this will be rather laborious) 618 # TODO: investigate and fix (likely this will be rather laborious)
598 619
599 my @res = Coro::Util::fork_eval { 620 my @res = Coro::Util::fork_eval {
600 reset_signals; 621 cf::post_fork;
601 &$cb 622 &$cb
602 }, @args; 623 } @args;
603 624
604 wantarray ? @res : $res[-1] 625 wantarray ? @res : $res[-1]
605} 626}
606 627
607sub objinfo { 628sub objinfo {
1643 $pl->{deny_save} = 1; 1664 $pl->{deny_save} = 1;
1644 $pl->password ("*"); # this should lock out the player until we have nuked the dir 1665 $pl->password ("*"); # this should lock out the player until we have nuked the dir
1645 1666
1646 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1667 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1647 $pl->deactivate; 1668 $pl->deactivate;
1669
1648 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy; 1670 my $killer = cf::arch::get "killer_quit"; $pl->killer ($killer); $killer->destroy;
1671 ext::highscore::check ($pl->ob);
1672
1649 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1673 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1650 $pl->ns->destroy if $pl->ns; 1674 $pl->ns->destroy if $pl->ns;
1651 1675
1652 my $path = playerdir $pl; 1676 my $path = playerdir $pl;
1653 my $temp = "$path~$cf::RUNTIME~deleting~"; 1677 my $temp = "$path~$cf::RUNTIME~deleting~";
1869} 1893}
1870 1894
1871sub normalise { 1895sub normalise {
1872 my ($path, $base) = @_; 1896 my ($path, $base) = @_;
1873 1897
1874 $path = "$path"; # make sure its a string 1898 $path = "$path"; # make sure it's a string
1875 1899
1876 $path =~ s/\.map$//; 1900 $path =~ s/\.map$//;
1877 1901
1878 # map plan: 1902 # map plan:
1879 # 1903 #
1918 $self->init; # pass $1 etc. 1942 $self->init; # pass $1 etc.
1919 return $self; 1943 return $self;
1920 } 1944 }
1921 } 1945 }
1922 1946
1923 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1947 Carp::cluck "unable to resolve path '$path' (base '$base')";
1924 () 1948 ()
1925} 1949}
1926 1950
1927sub init { 1951sub init {
1928 my ($self) = @_; 1952 my ($self) = @_;
2050} 2074}
2051 2075
2052sub find; 2076sub find;
2053sub find { 2077sub find {
2054 my ($path, $origin) = @_; 2078 my ($path, $origin) = @_;
2079
2080 cf::cede_to_tick;
2055 2081
2056 $path = normalise $path, $origin && $origin->path; 2082 $path = normalise $path, $origin && $origin->path;
2057 2083
2058 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2084 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2059 my $guard2 = cf::lock_acquire "map_find:$path"; 2085 my $guard2 = cf::lock_acquire "map_find:$path";
3357=cut 3383=cut
3358 3384
3359############################################################################# 3385#############################################################################
3360# the server's init and main functions 3386# the server's init and main functions
3361 3387
3362# async inc loader. yay.
3363sub inc_loader {
3364 my $mod = $_[1];
3365
3366 if (in_main && !tick_inhibit) {
3367 Carp::cluck "ERROR: attempted synchronous perl module load ($mod)";
3368 } else {
3369 debug "loading perl module $mod\n";
3370 }
3371
3372 # 1. find real file
3373 for my $dir (@ORIG_INC) {
3374 ref $dir and next;
3375 0 <= Coro::AIO::aio_load "$dir/$mod", my $data
3376 or next;
3377
3378 $data = "#line 1 $dir/$mod\n$data";
3379
3380 open my $fh, "<", \$data or die;
3381
3382 return $fh;
3383 }
3384
3385 ()
3386}
3387
3388sub init_inc {
3389 # save original @INC
3390 @ORIG_INC = ($LIBDIR, @INC) unless @ORIG_INC;
3391
3392 # make sure we can do scalar-opens
3393 open my $dummy, "<", \my $dummy2;
3394
3395 # execute some stuff so perl load's some of the core modules
3396 /Ü/ =~ /ü/i;
3397 eval { &Storable::nstore_fd };
3398
3399 @INC = (\&inc_loader, @ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc.
3400
3401 debug "module loading will be asynchronous from this point on.";
3402}
3403
3404sub load_facedata($) { 3388sub load_facedata($) {
3405 my ($path) = @_; 3389 my ($path) = @_;
3406 3390
3407 # HACK to clear player env face cache, we need some signal framework 3391 # HACK to clear player env face cache, we need some signal framework
3408 # for this (global event?) 3392 # for this (global event?)
3410 3394
3411 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3395 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3412 3396
3413 trace "loading facedata from $path\n"; 3397 trace "loading facedata from $path\n";
3414 3398
3415 my $facedata;
3416 0 < aio_load $path, $facedata 3399 0 < aio_load $path, my $facedata
3417 or die "$path: $!"; 3400 or die "$path: $!";
3418 3401
3419 $facedata = Coro::Storable::thaw $facedata; 3402 $facedata = Coro::Storable::thaw $facedata;
3420 3403
3421 $facedata->{version} == 2 3404 $facedata->{version} == 2
3565} 3548}
3566 3549
3567sub reload_resources { 3550sub reload_resources {
3568 trace "reloading resource files...\n"; 3551 trace "reloading resource files...\n";
3569 3552
3553 reload_exp_table;
3554 reload_materials;
3570 reload_facedata; 3555 reload_facedata;
3571 reload_sound; 3556 reload_sound;
3572 reload_archetypes; 3557 reload_archetypes;
3573 reload_regions; 3558 reload_regions;
3574 reload_treasures; 3559 reload_treasures;
3577} 3562}
3578 3563
3579sub reload_config { 3564sub reload_config {
3580 trace "reloading config file...\n"; 3565 trace "reloading config file...\n";
3581 3566
3582 open my $fh, "<:utf8", "$CONFDIR/config" 3567 0 < aio_load "$CONFDIR/config", my $config
3583 or return; 3568 or die "$CONFDIR/config: $!";
3584 3569
3585 local $/; 3570 utf8::decode $config;
3586 *CFG = YAML::XS::Load scalar <$fh>; 3571 *CFG = yaml_load $config;
3587 3572
3588 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3573 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3589 3574
3590 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset}; 3575 $cf::map::MAX_RESET = $CFG{map_max_reset} if exists $CFG{map_max_reset};
3591 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset}; 3576 $cf::map::DEFAULT_RESET = $CFG{map_default_reset} if exists $CFG{map_default_reset};
3595 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3580 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3596 and die "WARNING: m(un)lockall failed: $!\n"; 3581 and die "WARNING: m(un)lockall failed: $!\n";
3597 }; 3582 };
3598 warn $@ if $@; 3583 warn $@ if $@;
3599 } 3584 }
3600
3601 trace "finished reloading resource files\n";
3602} 3585}
3603 3586
3604sub pidfile() { 3587sub pidfile() {
3605 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3588 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3606 or die "$PIDFILE: $!"; 3589 or die "$PIDFILE: $!";
3649 }; 3632 };
3650 3633
3651 evthread_start IO::AIO::poll_fileno; 3634 evthread_start IO::AIO::poll_fileno;
3652 3635
3653 cf::sync_job { 3636 cf::sync_job {
3654 init_inc; 3637 cf::incloader::init ();
3655 3638
3656 cf::init_experience;
3657 cf::init_anim; 3639 cf::init_anim;
3658 cf::init_attackmess; 3640 cf::init_attackmess;
3659 cf::init_dynamic; 3641 cf::init_dynamic;
3660 3642
3661 cf::load_settings; 3643 cf::load_settings;
3662 cf::load_materials;
3663 3644
3664 reload_resources; 3645 reload_resources;
3665 reload_config; 3646 reload_config;
3666 db_init; 3647 db_init;
3667 3648
3901 } 3882 }
3902} 3883}
3903 3884
3904sub do_reload_perl() { 3885sub do_reload_perl() {
3905 # can/must only be called in main 3886 # can/must only be called in main
3906 if (in_main) { 3887 unless (in_main) {
3907 error "can only reload from main coroutine"; 3888 error "can only reload from main coroutine";
3908 return; 3889 return;
3909 } 3890 }
3910 3891
3911 return if $RELOAD++; 3892 return if $RELOAD++;
3912 3893
3913 my $t1 = AE::time; 3894 my $t1 = AE::time;
3914 3895
3915 while ($RELOAD) { 3896 while ($RELOAD) {
3897 cf::get_slot 0.1, -1, "reload_perl";
3916 info "reloading..."; 3898 info "reloading...";
3917 3899
3918 trace "entering sync_job"; 3900 trace "entering sync_job";
3919 3901
3920 cf::sync_job { 3902 cf::sync_job {
3921 cf::emergency_save; 3903 #cf::emergency_save;
3922 3904
3923 trace "cancelling all extension coros"; 3905 trace "cancelling all extension coros";
3924 $_->cancel for values %EXT_CORO; 3906 $_->cancel for values %EXT_CORO;
3925 %EXT_CORO = (); 3907 %EXT_CORO = ();
3926 3908
4048my $bug_warning = 0; 4030my $bug_warning = 0;
4049 4031
4050our @WAIT_FOR_TICK; 4032our @WAIT_FOR_TICK;
4051our @WAIT_FOR_TICK_BEGIN; 4033our @WAIT_FOR_TICK_BEGIN;
4052 4034
4053sub wait_for_tick { 4035sub wait_for_tick() {
4054 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4036 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4055 4037
4056 my $signal = new Coro::Signal; 4038 my $signal = new Coro::Signal;
4057 push @WAIT_FOR_TICK, $signal; 4039 push @WAIT_FOR_TICK, $signal;
4058 $signal->wait; 4040 $signal->wait;
4059} 4041}
4060 4042
4061sub wait_for_tick_begin { 4043sub wait_for_tick_begin() {
4062 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4044 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4063 4045
4064 my $signal = new Coro::Signal; 4046 my $signal = new Coro::Signal;
4065 push @WAIT_FOR_TICK_BEGIN, $signal; 4047 push @WAIT_FOR_TICK_BEGIN, $signal;
4066 $signal->wait; 4048 $signal->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines