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.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 {
1451 1472
1452 my $active = eval $v->{source}; 1473 my $active = eval $v->{source};
1453 1474
1454 if (length $@) { 1475 if (length $@) {
1455 error "$v->{path}: $@\n"; 1476 error "$v->{path}: $@\n";
1456 undef $@; # work around perl 5.10.0 utf-8 caching bug
1457 1477
1458 cf::cleanup "mandatory extension '$k' failed to load, exiting." 1478 cf::cleanup "mandatory extension '$k' failed to load, exiting."
1459 if exists $v->{meta}{mandatory}; 1479 if exists $v->{meta}{mandatory};
1460 1480
1461 warn "$v->{base}: optional extension cannot be loaded, skipping.\n"; 1481 warn "$v->{base}: optional extension cannot be loaded, skipping.\n";
1644 $pl->{deny_save} = 1; 1664 $pl->{deny_save} = 1;
1645 $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
1646 1666
1647 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active; 1667 $pl->invoke (cf::EVENT_PLAYER_LOGOUT, 1) if $pl->active;
1648 $pl->deactivate; 1668 $pl->deactivate;
1669
1649 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
1650 $pl->invoke (cf::EVENT_PLAYER_QUIT); 1673 $pl->invoke (cf::EVENT_PLAYER_QUIT);
1651 $pl->ns->destroy if $pl->ns; 1674 $pl->ns->destroy if $pl->ns;
1652 1675
1653 my $path = playerdir $pl; 1676 my $path = playerdir $pl;
1654 my $temp = "$path~$cf::RUNTIME~deleting~"; 1677 my $temp = "$path~$cf::RUNTIME~deleting~";
1870} 1893}
1871 1894
1872sub normalise { 1895sub normalise {
1873 my ($path, $base) = @_; 1896 my ($path, $base) = @_;
1874 1897
1875 $path = "$path"; # make sure its a string 1898 $path = "$path"; # make sure it's a string
1876 1899
1877 $path =~ s/\.map$//; 1900 $path =~ s/\.map$//;
1878 1901
1879 # map plan: 1902 # map plan:
1880 # 1903 #
1919 $self->init; # pass $1 etc. 1942 $self->init; # pass $1 etc.
1920 return $self; 1943 return $self;
1921 } 1944 }
1922 } 1945 }
1923 1946
1924 Carp::cluck "unable to resolve path '$path' (base '$base')."; 1947 Carp::cluck "unable to resolve path '$path' (base '$base')";
1925 () 1948 ()
1926} 1949}
1927 1950
1928sub init { 1951sub init {
1929 my ($self) = @_; 1952 my ($self) = @_;
2051} 2074}
2052 2075
2053sub find; 2076sub find;
2054sub find { 2077sub find {
2055 my ($path, $origin) = @_; 2078 my ($path, $origin) = @_;
2079
2080 cf::cede_to_tick;
2056 2081
2057 $path = normalise $path, $origin && $origin->path; 2082 $path = normalise $path, $origin && $origin->path;
2058 2083
2059 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove 2084 my $guard1 = cf::lock_acquire "map_data:$path";#d#remove
2060 my $guard2 = cf::lock_acquire "map_find:$path"; 2085 my $guard2 = cf::lock_acquire "map_find:$path";
3358=cut 3383=cut
3359 3384
3360############################################################################# 3385#############################################################################
3361# the server's init and main functions 3386# the server's init and main functions
3362 3387
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($) { 3388sub load_facedata($) {
3407 my ($path) = @_; 3389 my ($path) = @_;
3408 3390
3409 # 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
3410 # for this (global event?) 3392 # for this (global event?)
3412 3394
3413 my $enc = JSON::XS->new->utf8->canonical->relaxed; 3395 my $enc = JSON::XS->new->utf8->canonical->relaxed;
3414 3396
3415 trace "loading facedata from $path\n"; 3397 trace "loading facedata from $path\n";
3416 3398
3417 my $facedata;
3418 0 < aio_load $path, $facedata 3399 0 < aio_load $path, my $facedata
3419 or die "$path: $!"; 3400 or die "$path: $!";
3420 3401
3421 $facedata = Coro::Storable::thaw $facedata; 3402 $facedata = Coro::Storable::thaw $facedata;
3422 3403
3423 $facedata->{version} == 2 3404 $facedata->{version} == 2
3567} 3548}
3568 3549
3569sub reload_resources { 3550sub reload_resources {
3570 trace "reloading resource files...\n"; 3551 trace "reloading resource files...\n";
3571 3552
3553 reload_exp_table;
3554 reload_materials;
3572 reload_facedata; 3555 reload_facedata;
3573 reload_sound; 3556 reload_sound;
3574 reload_archetypes; 3557 reload_archetypes;
3575 reload_regions; 3558 reload_regions;
3576 reload_treasures; 3559 reload_treasures;
3579} 3562}
3580 3563
3581sub reload_config { 3564sub reload_config {
3582 trace "reloading config file...\n"; 3565 trace "reloading config file...\n";
3583 3566
3584 open my $fh, "<:utf8", "$CONFDIR/config" 3567 0 < aio_load "$CONFDIR/config", my $config
3585 or return; 3568 or die "$CONFDIR/config: $!";
3586 3569
3587 local $/; 3570 utf8::decode $config;
3588 *CFG = YAML::XS::Load scalar <$fh>; 3571 *CFG = yaml_load $config;
3589 3572
3590 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38]; 3573 $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];
3591 3574
3592 $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};
3593 $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};
3597 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()" 3580 $CFG{mlockall} ? eval "mlockall()" : eval "munlockall()"
3598 and die "WARNING: m(un)lockall failed: $!\n"; 3581 and die "WARNING: m(un)lockall failed: $!\n";
3599 }; 3582 };
3600 warn $@ if $@; 3583 warn $@ if $@;
3601 } 3584 }
3602
3603 trace "finished reloading resource files\n";
3604} 3585}
3605 3586
3606sub pidfile() { 3587sub pidfile() {
3607 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT 3588 sysopen my $fh, $PIDFILE, O_RDWR | O_CREAT
3608 or die "$PIDFILE: $!"; 3589 or die "$PIDFILE: $!";
3651 }; 3632 };
3652 3633
3653 evthread_start IO::AIO::poll_fileno; 3634 evthread_start IO::AIO::poll_fileno;
3654 3635
3655 cf::sync_job { 3636 cf::sync_job {
3656 init_inc; 3637 cf::incloader::init ();
3657 3638
3658 cf::init_experience;
3659 cf::init_anim; 3639 cf::init_anim;
3660 cf::init_attackmess; 3640 cf::init_attackmess;
3661 cf::init_dynamic; 3641 cf::init_dynamic;
3662 3642
3663 cf::load_settings; 3643 cf::load_settings;
3664 cf::load_materials;
3665 3644
3666 reload_resources; 3645 reload_resources;
3667 reload_config; 3646 reload_config;
3668 db_init; 3647 db_init;
3669 3648
3903 } 3882 }
3904} 3883}
3905 3884
3906sub do_reload_perl() { 3885sub do_reload_perl() {
3907 # can/must only be called in main 3886 # can/must only be called in main
3908 if (in_main) { 3887 unless (in_main) {
3909 error "can only reload from main coroutine"; 3888 error "can only reload from main coroutine";
3910 return; 3889 return;
3911 } 3890 }
3912 3891
3913 return if $RELOAD++; 3892 return if $RELOAD++;
3914 3893
3915 my $t1 = AE::time; 3894 my $t1 = AE::time;
3916 3895
3917 while ($RELOAD) { 3896 while ($RELOAD) {
3897 cf::get_slot 0.1, -1, "reload_perl";
3918 info "reloading..."; 3898 info "reloading...";
3919 3899
3920 trace "entering sync_job"; 3900 trace "entering sync_job";
3921 3901
3922 cf::sync_job { 3902 cf::sync_job {
3923 cf::emergency_save; 3903 #cf::emergency_save;
3924 3904
3925 trace "cancelling all extension coros"; 3905 trace "cancelling all extension coros";
3926 $_->cancel for values %EXT_CORO; 3906 $_->cancel for values %EXT_CORO;
3927 %EXT_CORO = (); 3907 %EXT_CORO = ();
3928 3908
4050my $bug_warning = 0; 4030my $bug_warning = 0;
4051 4031
4052our @WAIT_FOR_TICK; 4032our @WAIT_FOR_TICK;
4053our @WAIT_FOR_TICK_BEGIN; 4033our @WAIT_FOR_TICK_BEGIN;
4054 4034
4055sub wait_for_tick { 4035sub wait_for_tick() {
4056 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4036 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4057 4037
4058 my $signal = new Coro::Signal; 4038 my $signal = new Coro::Signal;
4059 push @WAIT_FOR_TICK, $signal; 4039 push @WAIT_FOR_TICK, $signal;
4060 $signal->wait; 4040 $signal->wait;
4061} 4041}
4062 4042
4063sub wait_for_tick_begin { 4043sub wait_for_tick_begin() {
4064 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main; 4044 return Coro::cede if tick_inhibit || $Coro::current == $Coro::main;
4065 4045
4066 my $signal = new Coro::Signal; 4046 my $signal = new Coro::Signal;
4067 push @WAIT_FOR_TICK_BEGIN, $signal; 4047 push @WAIT_FOR_TICK_BEGIN, $signal;
4068 $signal->wait; 4048 $signal->wait;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines