--- deliantra/server/lib/cf.pm 2011/02/26 12:50:27 1.559 +++ deliantra/server/lib/cf.pm 2011/04/22 02:03:12 1.560 @@ -319,6 +319,11 @@ ############################################################################# +sub fork_call(&@); +sub get_slot($;$$); + +############################################################################# + =head2 UTILITY FUNCTIONS =over 4 @@ -346,6 +351,20 @@ } || "[unable to dump $_[0]: '$@']"; } +=item $scalar = load_file $path + +Loads the given file from path and returns its contents. Croaks on error +and can block. + +=cut + +sub load_file($) { + 0 <= aio_load $_[0], my $data + or Carp::croak "$_[0]: $!"; + + $data +} + =item $ref = cf::decode_json $json Converts a JSON string into the corresponding perl data structure. @@ -361,18 +380,37 @@ sub encode_json($) { $json_coder->encode ($_[0]) } sub decode_json($) { $json_coder->decode ($_[0]) } -=item $ref = cf::yaml_load $scalar +=item $ref = cf::decode_storable $scalar -Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). +Same as Coro::Storable::thaw, so blocks. =cut -sub fork_call(&@); +BEGIN { *decode_storable = \&Coro::Storable::thaw } + +=item $ref = cf::decode_yaml $scalar -sub yaml_load($) { +Same as YAML::XS::Load, but doesn't leak, because it forks (and thus blocks). + +=cut + +sub decode_yaml($) { fork_call { YAML::XS::Load $_[0] } @_ } +=item $scalar = cf::unlzf $scalar + +Same as Compress::LZF::compress, but takes server ticks into account, so +blocks. + +=cut + +sub unlzf($) { + # we assume 100mb/s minimum decompression speed (noncompressible data on a ~2ghz machine) + cf::get_slot +(length $_[0]) / 100_000_000, 0, "unlzf"; + Compress::LZF::decompress $_[0] +} + =item cf::post_init { BLOCK } Execute the given codeblock, I all extensions have been (re-)loaded, @@ -464,9 +502,13 @@ =item cf::get_slot $time[, $priority[, $name]] -Allocate $time seconds of blocking CPU time at priority C<$priority>: -This call blocks and returns only when you have at least C<$time> seconds -of cpu time till the next tick. The slot is only valid till the next cede. +Allocate $time seconds of blocking CPU time at priority C<$priority> +(default: 0): This call blocks and returns only when you have at least +C<$time> seconds of cpu time till the next tick. The slot is only valid +till the next cede. + +Background jobs should use a priority les than zero, interactive jobs +should use 100 or more. The optional C<$name> can be used to identify the job to run. It might be used for statistical purposes and should identify the same time-class. @@ -771,8 +813,7 @@ my $md5; for (0 .. $#$src) { - 0 <= aio_load $src->[$_], $data[$_] - or Carp::croak "$src->[$_]: $!"; + $data[$_] = load_file $src->[$_]; } # if processing is expensive, check @@ -3403,10 +3444,7 @@ trace "loading facedata from $path\n"; - 0 < aio_load $path, my $facedata - or die "$path: $!"; - - $facedata = Coro::Storable::thaw $facedata; + my $facedata = decode_storable load_file $path; $facedata->{version} == 2 or cf::cleanup "$path: version mismatch, cannot proceed."; @@ -3535,10 +3573,7 @@ sub reload_sound { trace "loading sound config from $DATADIR/sound\n"; - 0 < Coro::AIO::aio_load "$DATADIR/sound", my $data - or die "$DATADIR/sound $!"; - - my $soundconf = JSON::XS->new->utf8->relaxed->decode ($data); + my $soundconf = JSON::XS->new->utf8->relaxed->decode (load_file "$DATADIR/sound"); for (0 .. SOUND_CAST_SPELL_0 - 1) { my $sound = $soundconf->{compat}[$_] @@ -3572,11 +3607,9 @@ sub reload_config { trace "reloading config file...\n"; - 0 < aio_load "$CONFDIR/config", my $config - or die "$CONFDIR/config: $!"; - + my $config = load_file "$CONFDIR/config"; utf8::decode $config; - *CFG = yaml_load $config; + *CFG = decode_yaml $config; $EMERGENCY_POSITION = $CFG{emergency_position} || ["/world/world_104_115", 49, 38];