| 1 |
root |
1.1 |
# |
| 2 |
|
|
# This file is part of Deliantra, the Roguelike Realtime MMORPG. |
| 3 |
|
|
# |
| 4 |
|
|
# Copyright (©) 2005,2006,2007,2008,2009,2010 Marc Alexander Lehmann / Robin Redeker / the Deliantra team |
| 5 |
|
|
# |
| 6 |
|
|
# Deliantra is free software: you can redistribute it and/or modify it under |
| 7 |
|
|
# the terms of the Affero GNU General Public License as published by the |
| 8 |
|
|
# Free Software Foundation, either version 3 of the License, or (at your |
| 9 |
|
|
# option) any later version. |
| 10 |
|
|
# |
| 11 |
|
|
# This program is distributed in the hope that it will be useful, |
| 12 |
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
|
|
# GNU General Public License for more details. |
| 15 |
|
|
# |
| 16 |
|
|
# You should have received a copy of the Affero GNU General Public License |
| 17 |
|
|
# and the GNU General Public License along with this program. If not, see |
| 18 |
|
|
# <http://www.gnu.org/licenses/>. |
| 19 |
|
|
# |
| 20 |
|
|
# The authors can be reached via e-mail to <support@deliantra.net> |
| 21 |
|
|
# |
| 22 |
|
|
|
| 23 |
|
|
package cf::incloader; |
| 24 |
|
|
|
| 25 |
|
|
use common::sense; |
| 26 |
|
|
|
| 27 |
root |
1.2 |
our $PID = $$; |
| 28 |
|
|
|
| 29 |
root |
1.1 |
# async inc loader. yay. |
| 30 |
|
|
sub inc_loader { |
| 31 |
|
|
my $mod = $_[1]; |
| 32 |
|
|
|
| 33 |
root |
1.2 |
return if $$ != $PID; # do not do this in child processes |
| 34 |
|
|
|
| 35 |
root |
1.1 |
if (cf::in_main && !cf::tick_inhibit) { |
| 36 |
|
|
Carp::cluck "ERROR: attempted synchronous perl module load ($mod)"; |
| 37 |
|
|
} else { |
| 38 |
|
|
cf::debug "loading perl module $mod\n"; |
| 39 |
|
|
} |
| 40 |
|
|
|
| 41 |
|
|
# find real file |
| 42 |
|
|
for my $dir (@cf::ORIG_INC) { |
| 43 |
|
|
ref $dir and next; |
| 44 |
|
|
|
| 45 |
|
|
my $path = "$dir/$mod"; |
| 46 |
|
|
|
| 47 |
|
|
# avoid upgrade when prepending #line |
| 48 |
|
|
utf8::downgrade $path, 1; |
| 49 |
|
|
|
| 50 |
|
|
0 <= Coro::AIO::aio_load $path, my $data |
| 51 |
|
|
or next; |
| 52 |
|
|
|
| 53 |
|
|
$data = "#line 1 $path\n$data"; |
| 54 |
|
|
|
| 55 |
|
|
# hackish way to pre-cache .so/.bs files |
| 56 |
|
|
if ($path =~ s/\.pm$//) { |
| 57 |
|
|
my @c = split /\//, $path; |
| 58 |
|
|
for (reverse 2 .. $#c) { |
| 59 |
|
|
my $a = join "/", @c[0..$_-1]; |
| 60 |
|
|
my $b = join "/", @c[$_..$#c]; |
| 61 |
|
|
|
| 62 |
|
|
Coro::AIO::aio_stat "$a/auto/$b/." |
| 63 |
|
|
and next; |
| 64 |
|
|
|
| 65 |
|
|
$path = "$a/auto/$b/"; |
| 66 |
|
|
|
| 67 |
|
|
cf::debug "pre-caching $path"; |
| 68 |
|
|
|
| 69 |
|
|
my $files = Coro::AIO::aio_readdir $path; |
| 70 |
|
|
|
| 71 |
|
|
my $grp = IO::AIO::aio_group; |
| 72 |
|
|
|
| 73 |
|
|
for my $file (@$files) { |
| 74 |
|
|
next if $file =~ /(^\.packlist|\.h$|\.xst$)/; |
| 75 |
|
|
|
| 76 |
|
|
add $grp IO::AIO::aio_open "$path/$file", IO::AIO::O_RDONLY, 0, sub { |
| 77 |
|
|
my ($fh) = @_ |
| 78 |
|
|
or return; |
| 79 |
|
|
|
| 80 |
|
|
add $grp IO::AIO::aio_stat $fh, sub { |
| 81 |
|
|
my $size = -s _; |
| 82 |
|
|
|
| 83 |
|
|
add $grp IO::AIO::aio_readahead $fh, 0, $size; |
| 84 |
|
|
|
| 85 |
|
|
cf::debug "pre-caching $path/$file ($size)"; |
| 86 |
|
|
}; |
| 87 |
|
|
}; |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
Coro::AIO::aio_wait $grp; |
| 91 |
|
|
last; |
| 92 |
|
|
} |
| 93 |
|
|
} |
| 94 |
|
|
|
| 95 |
|
|
die if utf8::is_utf8 $data;#d# |
| 96 |
|
|
open my $fh, "<", \$data or die; |
| 97 |
|
|
|
| 98 |
|
|
cf::get_slot 0.1, 10, "\@INC loader"; |
| 99 |
|
|
|
| 100 |
|
|
return $fh; |
| 101 |
|
|
} |
| 102 |
|
|
|
| 103 |
|
|
() |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
sub init { |
| 107 |
|
|
# save original @INC |
| 108 |
|
|
@cf::ORIG_INC = ($cf::LIBDIR, @INC) unless @cf::ORIG_INC; |
| 109 |
|
|
|
| 110 |
|
|
# make sure we can do scalar-opens |
| 111 |
|
|
open my $dummy, "<", \my $dummy2; |
| 112 |
|
|
|
| 113 |
|
|
# execute some stuff so perl load's some of the core modules |
| 114 |
|
|
/Ü/ =~ /ü/i; |
| 115 |
|
|
eval { &Storable::nstore_fd }; |
| 116 |
|
|
|
| 117 |
|
|
@INC = (\&inc_loader, @cf::ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc. |
| 118 |
|
|
|
| 119 |
|
|
cf::debug "module loading will be asynchronous from this point on."; |
| 120 |
|
|
} |
| 121 |
|
|
|
| 122 |
|
|
1 |