ocl: multi-threading: fix bug in intialization

This commit is contained in:
Alexander Alekhin 2013-11-22 14:31:54 +04:00
parent 8a4f35704a
commit 96a4a7b339

View File

@ -187,11 +187,8 @@ static bool parseOpenCLDeviceConfiguration(const std::string& configurationStr,
return true; return true;
} }
static bool __deviceSelected = false;
static bool selectOpenCLDevice() static bool selectOpenCLDevice()
{ {
__deviceSelected = true;
std::string platform; std::string platform;
std::vector<std::string> deviceTypes; std::vector<std::string> deviceTypes;
std::string deviceName; std::string deviceName;
@ -526,13 +523,18 @@ private:
static ContextImpl* currentContext = NULL; static ContextImpl* currentContext = NULL;
static bool __deviceSelected = false;
Context* Context::getContext() Context* Context::getContext()
{ {
if (currentContext == NULL) if (currentContext == NULL)
{ {
if (!__initialized || !__deviceSelected) static bool defaultInitiaization = false;
if (!defaultInitiaization)
{ {
cv::AutoLock lock(getInitializationMutex()); cv::AutoLock lock(getInitializationMutex());
try
{
if (!__initialized) if (!__initialized)
{ {
if (initializeOpenCLDevices() == 0) if (initializeOpenCLDevices() == 0)
@ -547,6 +549,13 @@ Context* Context::getContext()
CV_Error(CV_OpenCLInitError, "Can't select OpenCL device"); CV_Error(CV_OpenCLInitError, "Can't select OpenCL device");
} }
} }
defaultInitiaization = true;
}
catch (...)
{
defaultInitiaization = true;
throw;
}
} }
CV_Assert(currentContext != NULL); CV_Assert(currentContext != NULL);
} }
@ -739,10 +748,16 @@ int getOpenCLDevices(std::vector<const DeviceInfo*> &devices, int deviceType, co
void setDevice(const DeviceInfo* info) void setDevice(const DeviceInfo* info)
{ {
if (!__deviceSelected) try
__deviceSelected = true; {
ContextImpl::setContext(info); ContextImpl::setContext(info);
__deviceSelected = true;
}
catch (...)
{
__deviceSelected = true;
throw;
}
} }
bool supportsFeature(FEATURE_TYPE featureType) bool supportsFeature(FEATURE_TYPE featureType)