From 871165353021b5c0c59e3e7dfc5542d611f0455f Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 3 Sep 2020 07:41:03 +0000 Subject: [PATCH] ocl: fixes for OpenCL multiple contexts support --- modules/core/src/ocl.cpp | 9 +++++---- modules/core/src/opengl.cpp | 2 +- modules/core/src/system.cpp | 7 ++++++- samples/sycl/CMakeLists.txt | 2 +- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index c5c5b12953..75bec627eb 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -2316,8 +2316,9 @@ struct Context::Impl typedef std::deque container_t; static container_t& getGlobalContainer() { - static container_t g_contexts; - return g_contexts; + // never delete this container (Impl lifetime is greater due to TLS storage) + static container_t* g_contexts = new container_t(); + return *g_contexts; } protected: @@ -2356,7 +2357,7 @@ protected: { cv::AutoLock lock(cv::getInitializationMutex()); auto& container = getGlobalContainer(); - CV_Assert((size_t)contextId < container.size()); + CV_CheckLT((size_t)contextId, container.size(), ""); container[contextId] = NULL; } } @@ -2839,7 +2840,7 @@ bool Context::create() if (!haveOpenCL()) return false; p = Impl::findOrCreateContext(std::string()); - if (p->handle) + if (p && p->handle) return true; release(); return false; diff --git a/modules/core/src/opengl.cpp b/modules/core/src/opengl.cpp index 37ab8623d8..a95191ef0c 100644 --- a/modules/core/src/opengl.cpp +++ b/modules/core/src/opengl.cpp @@ -1692,7 +1692,7 @@ Context& initializeContextFromGL() cl_platform_id platform = platforms[found]; std::string platformName = PlatformInfo(platform).name(); - OpenCLExecutionContext clExecCtx = OpenCLExecutionContext::create(platformName, platform, context, deviceID); + OpenCLExecutionContext clExecCtx = OpenCLExecutionContext::create(platformName, platform, context, device); clReleaseDevice(device); clReleaseContext(context); clExecCtx.bind(); diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index b0c93136ff..3acf77035f 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -1416,7 +1416,10 @@ static TlsAbstraction* getTlsAbstraction() #ifdef WINRT static __declspec( thread ) void* tlsData = NULL; // using C++11 thread attribute for local thread data TlsAbstraction::TlsAbstraction() {} -TlsAbstraction::~TlsAbstraction() {} +TlsAbstraction::~TlsAbstraction() +{ + cv::__termination = true; // DllMain is missing in static builds +} void* TlsAbstraction::getData_() const { return tlsData; @@ -1440,6 +1443,7 @@ TlsAbstraction::TlsAbstraction() } TlsAbstraction::~TlsAbstraction() { + cv::__termination = true; // DllMain is missing in static builds #ifndef CV_USE_FLS TlsFree(tlsKey); #else // CV_USE_FLS @@ -1472,6 +1476,7 @@ TlsAbstraction::TlsAbstraction() } TlsAbstraction::~TlsAbstraction() { + cv::__termination = true; // DllMain is missing in static builds if (pthread_key_delete(tlsKey) != 0) { // Don't use logging here diff --git a/samples/sycl/CMakeLists.txt b/samples/sycl/CMakeLists.txt index 093ed9f3b6..3a3483bd21 100644 --- a/samples/sycl/CMakeLists.txt +++ b/samples/sycl/CMakeLists.txt @@ -2,7 +2,7 @@ if(OPENCV_SKIP_SAMPLES_SYCL) return() endif() -ocv_install_example_src(opencl *.cpp *.hpp CMakeLists.txt) +ocv_install_example_src(sycl *.cpp *.hpp CMakeLists.txt) set(OPENCV_SYCL_SAMPLES_REQUIRED_DEPS opencv_core