Merge pull request #1856 from alalek:ocl_fix_mt

This commit is contained in:
Andrey Pavlenko 2013-11-26 09:58:21 +04:00 committed by OpenCV Buildbot
commit d45ce086c1
2 changed files with 41 additions and 19 deletions

View File

@ -189,11 +189,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;
@ -528,13 +525,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)
@ -549,6 +551,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);
} }
@ -741,10 +750,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)

View File

@ -192,6 +192,7 @@ void openCLMallocPitchEx(Context *ctx, void **dev_ptr, size_t *pitch,
clFinish(getClCommandQueue(ctx)); clFinish(getClCommandQueue(ctx));
#endif #endif
CheckBuffers data(mainBuffer, size, widthInBytes, height); CheckBuffers data(mainBuffer, size, widthInBytes, height);
cv::AutoLock lock(getInitializationMutex());
__check_buffers.insert(std::pair<cl_mem, CheckBuffers>((cl_mem)*dev_ptr, data)); __check_buffers.insert(std::pair<cl_mem, CheckBuffers>((cl_mem)*dev_ptr, data));
} }
#endif #endif
@ -253,10 +254,17 @@ void openCLFree(void *devPtr)
bool failBefore = false, failAfter = false; bool failBefore = false, failAfter = false;
#endif #endif
CheckBuffers data; CheckBuffers data;
{
cv::AutoLock lock(getInitializationMutex());
std::map<cl_mem, CheckBuffers>::iterator i = __check_buffers.find((cl_mem)devPtr); std::map<cl_mem, CheckBuffers>::iterator i = __check_buffers.find((cl_mem)devPtr);
if (i != __check_buffers.end()) if (i != __check_buffers.end())
{ {
data = i->second; data = i->second;
__check_buffers.erase(i);
}
}
if (data.mainBuffer != NULL)
{
#ifdef CHECK_MEMORY_CORRUPTION #ifdef CHECK_MEMORY_CORRUPTION
Context* ctx = Context::getContext(); Context* ctx = Context::getContext();
std::vector<uchar> checkBefore(__memory_corruption_guard_bytes); std::vector<uchar> checkBefore(__memory_corruption_guard_bytes);
@ -286,7 +294,6 @@ void openCLFree(void *devPtr)
clFinish(getClCommandQueue(ctx)); clFinish(getClCommandQueue(ctx));
#endif #endif
openCLSafeCall(clReleaseMemObject(data.mainBuffer)); openCLSafeCall(clReleaseMemObject(data.mainBuffer));
__check_buffers.erase(i);
} }
#if defined(CHECK_MEMORY_CORRUPTION) #if defined(CHECK_MEMORY_CORRUPTION)
if (failBefore) if (failBefore)