mirror of
https://github.com/opencv/opencv.git
synced 2026-11-29 04:47:04 -06:00
Merge pull request #30072 from tanveer-arch:investigate-30067-tbb-lifetime
core: avoid TBB static destruction during Windows teardown - #30072 ## Summary Avoid destruction of the TBB task arena/scheduler during C++ static teardown. On Windows with the UCRT and TBB backend, process teardown can involve interaction between TBB cleanup and the Windows loader/CRT shutdown path. Keeping the OpenCV-owned TBB arena/scheduler alive until process termination avoids invoking its destructor during static destruction. ## Validation - Reproduced the reported teardown deadlock with the MSYS2 packaged OpenCV 5.0.0 environment. - Built OpenCV 5.x locally with TBB enabled. - The patched local build completed 2,001 stress-test iterations without reproducing the deadlock. - `git diff --check` passes. The local 5.x baseline did not reproduce the issue during the available baseline stress run, so the stress result is considered supporting evidence rather than a definitive reproduction/fix confirmation. Related issue: #30067
This commit is contained in:
@@ -465,9 +465,17 @@ namespace {
|
||||
|
||||
#if defined HAVE_TBB
|
||||
#if TBB_INTERFACE_VERSION >= 8000
|
||||
static tbb::task_arena tbbArena(tbb::task_arena::automatic);
|
||||
static tbb::task_arena& getTbbArena() {
|
||||
// Keep the TBB object alive until process termination to avoid static
|
||||
// destruction during process teardown on Windows.
|
||||
CV_SINGLETON_LAZY_INIT_REF(tbb::task_arena, new tbb::task_arena(tbb::task_arena::automatic));
|
||||
}
|
||||
#else
|
||||
static tbb::task_scheduler_init tbbScheduler(tbb::task_scheduler_init::deferred);
|
||||
static tbb::task_scheduler_init& getTbbScheduler() {
|
||||
// Keep the TBB object alive until process termination to avoid static
|
||||
// destruction during process teardown on Windows.
|
||||
CV_SINGLETON_LAZY_INIT_REF(tbb::task_scheduler_init, new tbb::task_scheduler_init(tbb::task_scheduler_init::deferred));
|
||||
}
|
||||
#endif
|
||||
#elif defined HAVE_HPX
|
||||
// nothing for HPX
|
||||
@@ -582,7 +590,7 @@ static void parallel_for_impl(const cv::Range& range, const cv::ParallelLoopBody
|
||||
#if defined HAVE_TBB
|
||||
|
||||
#if TBB_INTERFACE_VERSION >= 8000
|
||||
tbbArena.execute(pbody);
|
||||
getTbbArena().execute(pbody);
|
||||
#else
|
||||
pbody();
|
||||
#endif
|
||||
@@ -651,13 +659,13 @@ int getNumThreads(void)
|
||||
#if defined HAVE_TBB
|
||||
|
||||
#if TBB_INTERFACE_VERSION >= 9100
|
||||
return tbbArena.max_concurrency();
|
||||
return getTbbArena().max_concurrency();
|
||||
#elif TBB_INTERFACE_VERSION >= 8000
|
||||
return numThreads > 0
|
||||
? numThreads
|
||||
: tbb::task_scheduler_init::default_num_threads();
|
||||
#else
|
||||
return tbbScheduler.is_active()
|
||||
return getTbbScheduler().is_active()
|
||||
? numThreads
|
||||
: tbb::task_scheduler_init::default_num_threads();
|
||||
#endif
|
||||
@@ -735,11 +743,11 @@ void setNumThreads( int threads_ )
|
||||
#ifdef HAVE_TBB
|
||||
|
||||
#if TBB_INTERFACE_VERSION >= 8000
|
||||
if(tbbArena.is_active()) tbbArena.terminate();
|
||||
if(threads > 0) tbbArena.initialize(threads);
|
||||
if(getTbbArena().is_active()) getTbbArena().terminate();
|
||||
if(threads > 0) getTbbArena().initialize(threads);
|
||||
#else
|
||||
if(tbbScheduler.is_active()) tbbScheduler.terminate();
|
||||
if(threads > 0) tbbScheduler.initialize(threads);
|
||||
if(getTbbScheduler().is_active()) getTbbScheduler().terminate();
|
||||
if(threads > 0) getTbbScheduler().initialize(threads);
|
||||
#endif
|
||||
|
||||
#elif defined HAVE_HPX
|
||||
|
||||
Reference in New Issue
Block a user