Skip to content

Commit

Permalink
call _init() in modules at init time. closes #1268
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Feb 25, 2014
1 parent fffd879 commit 2044a41
Show file tree
Hide file tree
Showing 16 changed files with 70 additions and 31 deletions.
17 changes: 0 additions & 17 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,11 +336,6 @@ function init_head_sched()
register_worker(LPROC)
end

function init_profiler()
# Use a max size of 1M profile samples, and fire timer every 1ms
Profile.init(1_000_000, 0.001)
end

function load_juliarc()
# If the user built us with a specific Base.SYSCONFDIR, check that location first for a juliarc.jl file
# If it is not found, then continue on to the relative path based on JULIA_HOME
Expand All @@ -354,25 +349,13 @@ end


function _start()
# set up standard streams
reinit_stdio()
fdwatcher_reinit()
# Initialize RNG
Random.librandom_init()
Sys.init()
global const CPU_CORES = Sys.CPU_CORES
if CPU_CORES > 8 && !("OPENBLAS_NUM_THREADS" in keys(ENV)) && !("OMP_NUM_THREADS" in keys(ENV))
# Prevent openblas from stating to many threads, unless/until specifically requested
ENV["OPENBLAS_NUM_THREADS"] = 8
end
# Check that BLAS is correctly built
check_blas()
LinAlg.init()
GMP.gmp_init()
init_profiler()
start_gc_msgs_task()

#atexit(()->flush(STDOUT))
try
any(a->(a=="--worker"), ARGS) || init_head_sched()
init_load_path()
Expand Down
2 changes: 1 addition & 1 deletion base/gmp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ end
_gmp_clear_func = C_NULL
_mpfr_clear_func = C_NULL

function gmp_init()
function _init()
global _gmp_clear_func = cglobal((:__gmpz_clear, :libgmp))
global _mpfr_clear_func = cglobal((:mpfr_clear, :libmpfr))
ccall((:__gmp_set_memory_functions, :libgmp), Void,
Expand Down
4 changes: 2 additions & 2 deletions base/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export
axpy!,
bkfact,
bkfact!,
check_blas,
chol,
cholfact,
cholfact!,
Expand Down Expand Up @@ -214,7 +213,8 @@ include("linalg/cholmod.jl")
include("linalg/arpack.jl")
include("linalg/arnoldi.jl")

function init()
function _init()
Base.check_blas()
if Base.blas_vendor() == :mkl
ccall((:MKL_Set_Interface_Layer, Base.libblas_name), Void, (Cint,), USE_BLAS64 ? 1 : 0)
end
Expand Down
2 changes: 1 addition & 1 deletion base/multimedia.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Multimedia

export Display, display, pushdisplay, popdisplay, displayable, redisplay,
MIME, @MIME, @MIME_str, writemime, reprmime, stringmime, istext,
mimewritable, TextDisplay, reinit_displays
mimewritable, TextDisplay

###########################################################################
# We define a singleton type MIME{mime symbol} for each MIME type, so
Expand Down
14 changes: 9 additions & 5 deletions base/poll.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,10 @@ end
# on unix

let
global fdwatcher_reinit
global fdwatcher_init
@unix_only begin
local fdwatcher_array = Array(FDWatcher,0)
function fdwatcher_reinit()
local fdwatcher_array
function fdwatcher_init()
fdwatcher_array = Array(FDWatcher,0)
end

Expand All @@ -190,8 +190,8 @@ let
end
end
@windows_only begin
local fdwatcher_array = Dict{WindowsRawSocket,FDWatcher}()
function fdwatcher_reinit()
local fdwatcher_array
function fdwatcher_init()
fdwatcher_array = Dict{WindowsRawSocket,FDWatcher}()
end

Expand All @@ -208,6 +208,10 @@ let
end
end

module _FDWatcherInitializer
_init() = Base.fdwatcher_init()
end

function pfw_wait_cb(pfw::PollingFileWatcher, prev, cur, status)
if status < 0
notify_error(pfw.notify,UVError("PollingFileWatcher",status))
Expand Down
2 changes: 1 addition & 1 deletion base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ precompile(cmp, (Int32, Int32))
precompile(min, (Int32, Int32))
precompile(==, (ASCIIString, ASCIIString))
precompile(arg_gen, (ASCIIString,))
precompile(Random.librandom_init, ())
precompile(Random._init, ())
precompile(Random.srand, (ASCIIString, Int))
precompile(Random.srand, (Uint64,))
precompile(open, (ASCIIString, Bool, Bool, Bool, Bool))
Expand Down
4 changes: 4 additions & 0 deletions base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ function init(n::Integer, delay::Float64)
end
end

# init with default values
# Use a max size of 1M profile samples, and fire timer every 1ms
_init() = init(1_000_000, 0.001)

clear() = ccall(:jl_profile_clear_data, Void, ())

function print{T<:Unsigned}(io::IO = STDOUT, data::Vector{T} = fetch(); format = :tree, C = false, combine = true, cols = Base.tty_cols())
Expand Down
2 changes: 1 addition & 1 deletion base/random.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ end

## initialization

function librandom_init()
function _init()

@unix_only begin
try
Expand Down
1 change: 0 additions & 1 deletion base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,6 @@ function reinit_stdio()
global STDIN = init_stdio(ccall(:jl_stdin_stream ,Ptr{Void},()))
global STDOUT = init_stdio(ccall(:jl_stdout_stream,Ptr{Void},()))
global STDERR = init_stdio(ccall(:jl_stderr_stream,Ptr{Void},()))
reinit_displays() # since Multimedia.displays uses STDOUT as fallback
end

flush(::AsyncStream) = nothing
Expand Down
8 changes: 7 additions & 1 deletion base/sysimg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,13 @@ importall .FS
include("process.jl")
include("multimedia.jl")
importall .Multimedia
reinit_stdio()
module _IOInitializer
function _init()
Base.reinit_stdio()
Base.Multimedia.reinit_displays() # since Multimedia.displays uses STDOUT as fallback
end
end
# TODO: should put this in _init, but need to handle its boolean argument correctly
ccall(:jl_get_uv_hooks, Void, (Cint,), 0)
include("grisu.jl")
import .Grisu.print_shortest
Expand Down
2 changes: 1 addition & 1 deletion base/sysinfo.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export CPU_CORES,
import ..Base: WORD_SIZE, OS_NAME, ARCH, MACHINE
import ..Base: show, uv_error

function init()
function _init()
# set CPU core count
global const CPU_CORES = int(
haskey(ENV,"JULIA_CPU_CORES") ?
Expand Down
14 changes: 14 additions & 0 deletions src/dump.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ static jl_value_t *jl_idtable_type=NULL;
// queue of types to cache
static jl_array_t *datatype_list=NULL;

// queue of modules to initialize
static arraylist_t modules_to_init;

#define write_uint8(s, n) ios_putc((n), (s))
#define read_uint8(s) ((uint8_t)ios_getc(s))
#define write_int8(s, n) write_uint8(s, n)
Expand Down Expand Up @@ -833,6 +836,8 @@ static jl_value_t *jl_deserialize_value_internal(ios_t *s)
arraylist_push(&m->usings, jl_deserialize_value(s));
}
m->constant_table = (jl_array_t*)jl_deserialize_value(s);
if (jl_module_has_initializer(m))
arraylist_push(&modules_to_init, m);
return (jl_value_t*)m;
}
else if (vtag == (jl_value_t*)SmallInt64_tag) {
Expand Down Expand Up @@ -1073,6 +1078,14 @@ void jl_restore_system_image(char *fname)
jl_update_all_fptrs();
}

void jl_init_restored_modules()
{
while (modules_to_init.len > 0) {
jl_module_t *m = arraylist_pop(&modules_to_init);
jl_module_run_initializer(m);
}
}

DLLEXPORT
jl_value_t *jl_ast_rettype(jl_lambda_info_t *li, jl_value_t *ast)
{
Expand Down Expand Up @@ -1153,6 +1166,7 @@ void jl_init_serializer(void)
htable_new(&fptr_to_id, 0);
htable_new(&id_to_fptr, 0);
htable_new(&backref_table, 50000);
arraylist_new(&modules_to_init, 0);

void *tags[] = { jl_symbol_type, jl_datatype_type,
jl_function_type, jl_tuple_type, jl_array_type,
Expand Down
3 changes: 3 additions & 0 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -816,6 +816,9 @@ void julia_init(char *imageFile)
#ifdef JL_GC_MARKSWEEP
jl_gc_enable();
#endif

if (imageFile)
jl_init_restored_modules();
}

DLLEXPORT void jl_install_sigint_handler()
Expand Down
3 changes: 3 additions & 0 deletions src/julia.h
Original file line number Diff line number Diff line change
Expand Up @@ -797,6 +797,8 @@ STATIC_INLINE jl_function_t *jl_get_function(jl_module_t *m, const char *name)
{
return (jl_function_t*) jl_get_global(m, jl_symbol(name));
}
DLLEXPORT int jl_module_has_initializer(jl_module_t *m);
DLLEXPORT void jl_module_run_initializer(jl_module_t *m);

// eq hash tables
DLLEXPORT jl_array_t *jl_eqtable_put(jl_array_t *h, void *key, void *val);
Expand Down Expand Up @@ -860,6 +862,7 @@ DLLEXPORT extern char *julia_home;

DLLEXPORT void jl_save_system_image(char *fname);
DLLEXPORT void jl_restore_system_image(char *fname);
void jl_init_restored_modules();

// front end interface
DLLEXPORT jl_value_t *jl_parse_input_line(const char *str);
Expand Down
20 changes: 20 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,23 @@ DLLEXPORT jl_value_t *jl_module_names(jl_module_t *m, int all, int imported)

DLLEXPORT jl_sym_t *jl_module_name(jl_module_t *m) { return m->name; }
DLLEXPORT jl_module_t *jl_module_parent(jl_module_t *m) { return m->parent; }

int jl_module_has_initializer(jl_module_t *m)
{
return jl_get_global(m, jl_symbol("_init")) != NULL;
}

void jl_module_run_initializer(jl_module_t *m)
{
jl_value_t *f = jl_get_global(m, jl_symbol("_init"));
if (f == NULL || !jl_is_function(f))
return;
JL_TRY {
jl_apply((jl_function_t*)f, NULL, 0);
}
JL_CATCH {
JL_PRINTF(JL_STDERR, "Warning: error initializing module %s:\n", m->name->name);
jl_static_show(JL_STDERR, jl_exception_in_transit);
JL_PRINTF(JL_STDERR, "\n");
}
}
3 changes: 3 additions & 0 deletions src/toplevel.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ jl_value_t *jl_eval_module_expr(jl_expr_t *ex)
}
}
#endif

jl_module_run_initializer(newm);

return jl_nothing;
}

Expand Down

0 comments on commit 2044a41

Please sign in to comment.