From 78376c0f63834642b8133ab5d3b5f658d65a0bb2 Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Tue, 8 Dec 2015 13:55:02 +0300 Subject: [PATCH] pthreads: remove usage of non-POSIX PTHREAD_RECURSIVE_MUTEX_INITIALIZER* defines related issues: #5423 #5476 --- modules/core/src/parallel_pthreads.cpp | 46 +++++--------------------- 1 file changed, 8 insertions(+), 38 deletions(-) diff --git a/modules/core/src/parallel_pthreads.cpp b/modules/core/src/parallel_pthreads.cpp index 8bf131e144..96ddfe7ea6 100644 --- a/modules/core/src/parallel_pthreads.cpp +++ b/modules/core/src/parallel_pthreads.cpp @@ -155,22 +155,9 @@ public: static ThreadManager& instance() { - if(!m_instance.ptr) - { - pthread_mutex_lock(&m_manager_access_mutex); - - if(!m_instance.ptr) - { - m_instance.ptr = new ThreadManager(); - } - - pthread_mutex_unlock(&m_manager_access_mutex); - } - - return *m_instance.ptr; + CV_SINGLETON_LAZY_INIT_REF(ThreadManager, new ThreadManager()) } - static void stop() { ThreadManager& manager = instance(); @@ -194,21 +181,6 @@ public: private: - struct ptr_holder - { - ThreadManager* ptr; - - ptr_holder(): ptr(NULL) { } - - ~ptr_holder() - { - if(ptr) - { - delete ptr; - } - } - }; - ThreadManager(); ~ThreadManager(); @@ -231,8 +203,7 @@ private: unsigned int m_task_position; unsigned int m_num_of_completed_tasks; - static pthread_mutex_t m_manager_access_mutex; - static ptr_holder m_instance; + pthread_mutex_t m_manager_access_mutex; static const char m_env_name[]; static const unsigned int m_default_number_of_threads; @@ -250,13 +221,6 @@ private: ThreadManagerPoolState m_pool_state; }; -#ifndef PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP -#define PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP PTHREAD_RECURSIVE_MUTEX_INITIALIZER -#endif - -pthread_mutex_t ThreadManager::m_manager_access_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP; - -ThreadManager::ptr_holder ThreadManager::m_instance; const char ThreadManager::m_env_name[] = "OPENCV_FOR_THREADS_NUM"; #ifdef ANDROID @@ -383,6 +347,12 @@ ThreadManager::ThreadManager(): m_num_threads(0), m_task_complete(false), m_num_ { int res = 0; + pthread_mutexattr_t attr; + pthread_mutexattr_init(&attr); + pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); + res |= pthread_mutex_init(&m_manager_access_mutex, &attr); + pthread_mutexattr_destroy(&attr); + res |= pthread_mutex_init(&m_manager_task_mutex, NULL); res |= pthread_cond_init(&m_cond_thread_task_complete, NULL);