ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/cf.schmorp.de/server/lib/cf/incloader.pm
Revision: 1.1
Committed: Wed May 5 23:23:29 2010 UTC (16 years, 6 months ago) by root
Branch: MAIN
Log Message:
move incloader to its own module, no onger emergency_save before perl_reload

File Contents

# User Rev Content
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     # async inc loader. yay.
28     sub inc_loader {
29     my $mod = $_[1];
30    
31     if (cf::in_main && !cf::tick_inhibit) {
32     Carp::cluck "ERROR: attempted synchronous perl module load ($mod)";
33     } else {
34     cf::debug "loading perl module $mod\n";
35     }
36    
37     # find real file
38     for my $dir (@cf::ORIG_INC) {
39     ref $dir and next;
40    
41     my $path = "$dir/$mod";
42    
43     # avoid upgrade when prepending #line
44     utf8::downgrade $path, 1;
45    
46     0 <= Coro::AIO::aio_load $path, my $data
47     or next;
48    
49     $data = "#line 1 $path\n$data";
50    
51     # hackish way to pre-cache .so/.bs files
52     if ($path =~ s/\.pm$//) {
53     my @c = split /\//, $path;
54     for (reverse 2 .. $#c) {
55     my $a = join "/", @c[0..$_-1];
56     my $b = join "/", @c[$_..$#c];
57    
58     Coro::AIO::aio_stat "$a/auto/$b/."
59     and next;
60    
61     $path = "$a/auto/$b/";
62    
63     cf::debug "pre-caching $path";
64    
65     my $files = Coro::AIO::aio_readdir $path;
66    
67     my $grp = IO::AIO::aio_group;
68    
69     for my $file (@$files) {
70     next if $file =~ /(^\.packlist|\.h$|\.xst$)/;
71    
72     add $grp IO::AIO::aio_open "$path/$file", IO::AIO::O_RDONLY, 0, sub {
73     my ($fh) = @_
74     or return;
75    
76     add $grp IO::AIO::aio_stat $fh, sub {
77     my $size = -s _;
78    
79     add $grp IO::AIO::aio_readahead $fh, 0, $size;
80    
81     cf::debug "pre-caching $path/$file ($size)";
82     };
83     };
84     }
85    
86     Coro::AIO::aio_wait $grp;
87     last;
88     }
89     }
90    
91     die if utf8::is_utf8 $data;#d#
92     open my $fh, "<", \$data or die;
93    
94     cf::get_slot 0.1, 10, "\@INC loader";
95    
96     return $fh;
97     }
98    
99     ()
100     }
101    
102     sub init {
103     # save original @INC
104     @cf::ORIG_INC = ($cf::LIBDIR, @INC) unless @cf::ORIG_INC;
105    
106     # make sure we can do scalar-opens
107     open my $dummy, "<", \my $dummy2;
108    
109     # execute some stuff so perl load's some of the core modules
110     /Ü/ =~ /ü/i;
111     eval { &Storable::nstore_fd };
112    
113     @INC = (\&inc_loader, @cf::ORIG_INC); # @ORIG_INC is needed for DynaLoader, AutoLoad etc.
114    
115     cf::debug "module loading will be asynchronous from this point on.";
116     }
117    
118     1