# HG changeset patch # User Doug Simon # Date 1397231080 -7200 # Node ID 0e689f20706e47798b3c64b414702a41c86e315c # Parent 2cae21d9f1222926358a2d5c0c3edaac9ffe2d93 HSAIL: avoid loading native Okra library twice Contributed-by: Tom Deneau diff -r 2cae21d9f122 -r 0e689f20706e src/gpu/hsail/vm/gpu_hsail.cpp --- a/src/gpu/hsail/vm/gpu_hsail.cpp Fri Apr 11 17:12:08 2014 +0200 +++ b/src/gpu/hsail/vm/gpu_hsail.cpp Fri Apr 11 17:44:40 2014 +0200 @@ -488,7 +488,7 @@ #define LOOKUP_OKRA_FUNCTION(name, alias) \ _##alias = \ - CAST_TO_FN_PTR(alias##_func_t, os::dll_lookup(handle, STRINGIFY(name))); \ + CAST_TO_FN_PTR(alias##_func_t, os::dll_lookup(okra_lib_handle, STRINGIFY(name))); \ if (_##alias == NULL) { \ tty->print_cr("[HSAIL] ***** Error: Failed to lookup %s in %s, wrong version of OKRA?", STRINGIFY(name), okra_library_name); \ return false; \ @@ -504,22 +504,25 @@ // here we know we have a valid okra_library_name to try to load char ebuf[O_BUFLEN]; + char *okra_lib_name_from_env_var = getenv("_OKRA_SIM_LIB_PATH_"); + if (okra_lib_name_from_env_var != NULL) { + okra_library_name = okra_lib_name_from_env_var; + } if (TraceGPUInteraction) { - tty->print_cr("[HSAIL] library is %s", okra_library_name); + tty->print_cr("[HSAIL] library is %s", okra_library_name); } - - void *handle = os::dll_load(okra_library_name, ebuf, O_BUFLEN); - // try alternate location if env variable set - char *okra_lib_name_from_env_var = getenv("_OKRA_SIM_LIB_PATH_"); - if ((handle == NULL) && (okra_lib_name_from_env_var != NULL)) { - handle = os::dll_load(okra_lib_name_from_env_var, ebuf, O_BUFLEN); - if ((handle != NULL) && TraceGPUInteraction) { - tty->print_cr("[HSAIL] using _OKRA_SIM_LIB_PATH_=%s", getenv("_OKRA_SIM_LIB_PATH_")); - } - } - - if (handle == NULL) { - // Unable to dlopen okra + void *okra_lib_handle = NULL; +#if defined(LINUX) + // Check first if the Okra library is already loaded. + // TODO: Figure out how to do this on other OSes. + okra_lib_handle = ::dlopen(okra_library_name, RTLD_LAZY | RTLD_NOLOAD); +#endif + // If Okra library is not already loaded, load it here + if (okra_lib_handle == NULL) { + okra_lib_handle = os::dll_load(okra_library_name, ebuf, O_BUFLEN); + } + if (okra_lib_handle == NULL) { + // Unable to open Okra library if (TraceGPUInteraction) { tty->print_cr("[HSAIL] library load failed."); } @@ -528,7 +531,8 @@ guarantee(_okra_create_context == NULL, "cannot repeat GPU initialization"); - // at this point we know handle is valid and we can lookup the functions + // at this point we know okra_lib_handle is valid whether we loaded + // here or earlier. In either case, we can lookup the functions LOOKUP_OKRA_FUNCTION(okra_create_context, okra_create_context); LOOKUP_OKRA_FUNCTION(okra_create_kernel, okra_create_kernel); LOOKUP_OKRA_FUNCTION(okra_push_object, okra_push_object);