Commit 7e472fbf authored by Alexander Alekhin's avatar Alexander Alekhin

ocl: thread-safe OpenCL loading (6056)

parent 6f51dd12
...@@ -58,11 +58,11 @@ static void* AppleCLGetProcAddress(const char* name) ...@@ -58,11 +58,11 @@ static void* AppleCLGetProcAddress(const char* name)
{ {
static bool initialized = false; static bool initialized = false;
static void* handle = NULL; static void* handle = NULL;
if (!handle) if (!handle && !initialized)
{ {
if(!initialized) cv::AutoLock lock(cv::getInitializationMutex());
if (!initialized)
{ {
initialized = true;
const char* path = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL"; const char* path = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL";
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME"); const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
if (envPath) if (envPath)
...@@ -78,10 +78,11 @@ static void* AppleCLGetProcAddress(const char* name) ...@@ -78,10 +78,11 @@ static void* AppleCLGetProcAddress(const char* name)
fprintf(stderr, ERROR_MSG_INVALID_VERSION); fprintf(stderr, ERROR_MSG_INVALID_VERSION);
handle = NULL; handle = NULL;
} }
initialized = true;
}
} }
if (!handle) if (!handle)
return NULL; return NULL;
}
return dlsym(handle, name); return dlsym(handle, name);
} }
#define CV_CL_GET_PROC_ADDRESS(name) AppleCLGetProcAddress(name) #define CV_CL_GET_PROC_ADDRESS(name) AppleCLGetProcAddress(name)
...@@ -94,11 +95,11 @@ static void* WinGetProcAddress(const char* name) ...@@ -94,11 +95,11 @@ static void* WinGetProcAddress(const char* name)
{ {
static bool initialized = false; static bool initialized = false;
static HMODULE handle = NULL; static HMODULE handle = NULL;
if (!handle) if (!handle && !initialized)
{ {
if(!initialized) cv::AutoLock lock(cv::getInitializationMutex());
if (!initialized)
{ {
initialized = true;
handle = GetModuleHandleA("OpenCL.dll"); handle = GetModuleHandleA("OpenCL.dll");
if (!handle) if (!handle)
{ {
...@@ -118,10 +119,11 @@ static void* WinGetProcAddress(const char* name) ...@@ -118,10 +119,11 @@ static void* WinGetProcAddress(const char* name)
handle = NULL; handle = NULL;
} }
} }
initialized = true;
}
} }
if (!handle) if (!handle)
return NULL; return NULL;
}
return (void*)GetProcAddress(handle, name); return (void*)GetProcAddress(handle, name);
} }
#define CV_CL_GET_PROC_ADDRESS(name) WinGetProcAddress(name) #define CV_CL_GET_PROC_ADDRESS(name) WinGetProcAddress(name)
...@@ -135,11 +137,11 @@ static void* GetProcAddress(const char* name) ...@@ -135,11 +137,11 @@ static void* GetProcAddress(const char* name)
{ {
static bool initialized = false; static bool initialized = false;
static void* handle = NULL; static void* handle = NULL;
if (!handle) if (!handle && !initialized)
{ {
if(!initialized) cv::AutoLock lock(cv::getInitializationMutex());
if (!initialized)
{ {
initialized = true;
const char* path = "libOpenCL.so"; const char* path = "libOpenCL.so";
const char* envPath = getenv("OPENCV_OPENCL_RUNTIME"); const char* envPath = getenv("OPENCV_OPENCL_RUNTIME");
if (envPath) if (envPath)
...@@ -155,10 +157,11 @@ static void* GetProcAddress(const char* name) ...@@ -155,10 +157,11 @@ static void* GetProcAddress(const char* name)
fprintf(stderr, ERROR_MSG_INVALID_VERSION); fprintf(stderr, ERROR_MSG_INVALID_VERSION);
handle = NULL; handle = NULL;
} }
initialized = true;
}
} }
if (!handle) if (!handle)
return NULL; return NULL;
}
return dlsym(handle, name); return dlsym(handle, name);
} }
#define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name) #define CV_CL_GET_PROC_ADDRESS(name) GetProcAddress(name)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment