From 4db9c329762779189ece5ff46bec402c62792eff Mon Sep 17 00:00:00 2001 From: sijchen Date: Wed, 2 Mar 2016 17:08:09 -0800 Subject: [PATCH] remove sink in WelsThreadPool and hide the construtor to finish the singleTon --- codec/common/inc/WelsThreadPool.h | 19 ++++------- codec/common/src/WelsThreadPool.cpp | 33 ++++++++----------- codec/encoder/core/inc/wels_task_management.h | 7 +--- .../core/src/slice_multi_threading.cpp | 1 - .../encoder/core/src/wels_task_management.cpp | 12 +------ test/common/WelsThreadPoolTest.cpp | 8 +++-- test/common/WelsThreadPoolTest.h | 2 +- 7 files changed, 28 insertions(+), 54 deletions(-) diff --git a/codec/common/inc/WelsThreadPool.h b/codec/common/inc/WelsThreadPool.h index 9bafde19..129f037d 100644 --- a/codec/common/inc/WelsThreadPool.h +++ b/codec/common/inc/WelsThreadPool.h @@ -50,12 +50,6 @@ namespace WelsCommon { -class IWelsThreadPoolSink { - public: - virtual WELS_THREAD_ERROR_CODE OnTaskExecuted (IWelsTask* pTask) = 0; - virtual WELS_THREAD_ERROR_CODE OnTaskCancelled (IWelsTask* pTask) = 0; -}; - class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink { public: @@ -63,13 +57,11 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink { DEFAULT_THREAD_NUM = 4, }; - CWelsThreadPool (IWelsThreadPoolSink* pSink = NULL); - virtual ~CWelsThreadPool(); - static WELS_THREAD_ERROR_CODE SetThreadNum (int32_t iMaxThreadNum); - static CWelsThreadPool& AddReference (IWelsThreadPoolSink* pSink = NULL); + static CWelsThreadPool& AddReference(); void RemoveInstance(); + static bool IsReferenced(); //IWelsTaskThreadSink @@ -86,7 +78,7 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink { protected: - WELS_THREAD_ERROR_CODE Init (IWelsThreadPoolSink* pSink); + WELS_THREAD_ERROR_CODE Init(); WELS_THREAD_ERROR_CODE Uninit(); WELS_THREAD_ERROR_CODE CreateIdleThread(); @@ -103,8 +95,10 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink { void ClearWaitedTasks(); private: + CWelsThreadPool(); + virtual ~CWelsThreadPool(); + WELS_THREAD_ERROR_CODE StopAllRunning(); - void UpdateSink (IWelsThreadPoolSink* pSink); static int32_t m_iRefCount; static CWelsLock m_cInitLock; @@ -113,7 +107,6 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink { CWelsCircleQueue* m_cWaitedTasks; CWelsCircleQueue* m_cIdleThreads; CWelsList* m_cBusyThreads; - IWelsThreadPoolSink* m_pSink; CWelsLock m_cLockPool; CWelsLock m_cLockWaitedTasks; diff --git a/codec/common/src/WelsThreadPool.cpp b/codec/common/src/WelsThreadPool.cpp index 9359e40c..3e86d711 100644 --- a/codec/common/src/WelsThreadPool.cpp +++ b/codec/common/src/WelsThreadPool.cpp @@ -47,8 +47,8 @@ int32_t CWelsThreadPool::m_iRefCount = 0; CWelsLock CWelsThreadPool::m_cInitLock; int32_t CWelsThreadPool::m_iMaxThreadNum = DEFAULT_THREAD_NUM; -CWelsThreadPool::CWelsThreadPool (IWelsThreadPoolSink* pSink) : - m_cWaitedTasks (NULL), m_cIdleThreads (NULL), m_cBusyThreads (NULL), m_pSink (pSink) { +CWelsThreadPool::CWelsThreadPool() : + m_cWaitedTasks (NULL), m_cIdleThreads (NULL), m_cBusyThreads (NULL) { } @@ -74,15 +74,15 @@ WELS_THREAD_ERROR_CODE CWelsThreadPool::SetThreadNum (int32_t iMaxThreadNum) { return WELS_THREAD_ERROR_OK; } -CWelsThreadPool& CWelsThreadPool::AddReference (IWelsThreadPoolSink* pSink) { + +CWelsThreadPool& CWelsThreadPool::AddReference () { CWelsAutoLock cLock (m_cInitLock); - static CWelsThreadPool m_cThreadPoolSelf (pSink); + static CWelsThreadPool m_cThreadPoolSelf; if (m_iRefCount == 0) { //TODO: will remove this afterwards - if (WELS_THREAD_ERROR_OK != m_cThreadPoolSelf.Init(pSink)) { + if (WELS_THREAD_ERROR_OK != m_cThreadPoolSelf.Init()) { m_cThreadPoolSelf.Uninit(); } - m_cThreadPoolSelf.UpdateSink (pSink); } //fprintf(stdout, "m_iRefCount=%d, pSink=%x, iMaxThreadNum=%d\n", m_iRefCount, pSink, iMaxThreadNum); @@ -98,23 +98,17 @@ void CWelsThreadPool::RemoveInstance() { -- m_iRefCount; if (0 == m_iRefCount) { StopAllRunning(); - m_pSink = NULL; Uninit(); //fprintf(stdout, "m_iRefCount=%d, IdleThreadNum=%d, BusyThreadNum=%d, WaitedTask=%d\n", m_iRefCount, GetIdleThreadNum(), GetBusyThreadNum(), GetWaitedTaskNum()); } } + bool CWelsThreadPool::IsReferenced() { CWelsAutoLock cLock (m_cInitLock); return (m_iRefCount>0); } -void CWelsThreadPool::UpdateSink (IWelsThreadPoolSink* pSink) { - m_pSink = pSink; - //fprintf(stdout, "UpdateSink: m_pSink=%x\n", m_pSink); - //fprintf(stdout, "m_iRefCount=%d, IdleThreadNum=%d, BusyThreadNum=%d, WaitedTask=%d\n", m_iRefCount, GetIdleThreadNum(), GetBusyThreadNum(), GetWaitedTaskNum()); -} - WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStart (CWelsTaskThread* pThread, IWelsTask* pTask) { AddThreadToBusyList (pThread); @@ -143,7 +137,7 @@ WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStop (CWelsTaskThread* pThread, IW return WELS_THREAD_ERROR_OK; } -WELS_THREAD_ERROR_CODE CWelsThreadPool::Init (IWelsThreadPoolSink* pSink) { +WELS_THREAD_ERROR_CODE CWelsThreadPool::Init () { //fprintf(stdout, "Enter WelsThreadPool Init\n"); CWelsAutoLock cLock (m_cLockPool); @@ -341,12 +335,13 @@ IWelsTask* CWelsThreadPool::GetWaitedTask() { void CWelsThreadPool::ClearWaitedTasks() { CWelsAutoLock cLock (m_cLockWaitedTasks); - - if (m_pSink) { - while (0 != m_cWaitedTasks->size()) { - m_pSink->OnTaskCancelled (m_cWaitedTasks->begin()); - m_cWaitedTasks->pop_front(); + IWelsTask* pTask = NULL; + while (0 != m_cWaitedTasks->size()) { + pTask = m_cWaitedTasks->begin(); + if (pTask->GetSink()) { + pTask->GetSink()->OnTaskCancelled(); } + m_cWaitedTasks->pop_front(); } } diff --git a/codec/encoder/core/inc/wels_task_management.h b/codec/encoder/core/inc/wels_task_management.h index 53d29943..ff77943e 100644 --- a/codec/encoder/core/inc/wels_task_management.h +++ b/codec/encoder/core/inc/wels_task_management.h @@ -65,8 +65,7 @@ class IWelsTaskManage { }; -class CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsThreadPoolSink, - public WelsCommon::IWelsTaskSink { +class CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsTaskSink { public: typedef CWelsCircleQueue TASKLIST_TYPE; //typedef std::pair SLICE_BOUNDARY_PAIR; @@ -80,10 +79,6 @@ class CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsThr virtual WelsErrorType ExecuteTasks (const CWelsBaseTask::ETaskType iTaskType = CWelsBaseTask::WELS_ENC_TASK_ENCODING); - //IWelsThreadPoolSink - virtual WelsErrorType OnTaskExecuted (WelsCommon::IWelsTask* pTask); - virtual WelsErrorType OnTaskCancelled (WelsCommon::IWelsTask* pTask); - //IWelsTaskSink virtual WelsErrorType OnTaskExecuted(); virtual WelsErrorType OnTaskCancelled(); diff --git a/codec/encoder/core/src/slice_multi_threading.cpp b/codec/encoder/core/src/slice_multi_threading.cpp index 1e4b5262..54f5842a 100644 --- a/codec/encoder/core/src/slice_multi_threading.cpp +++ b/codec/encoder/core/src/slice_multi_threading.cpp @@ -347,7 +347,6 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara pSmt->piSliceIndexInThread[iIdx] = NULL; } - WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace); err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name); MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno); diff --git a/codec/encoder/core/src/wels_task_management.cpp b/codec/encoder/core/src/wels_task_management.cpp index 92b62870..6ac7cad2 100644 --- a/codec/encoder/core/src/wels_task_management.cpp +++ b/codec/encoder/core/src/wels_task_management.cpp @@ -99,7 +99,7 @@ WelsErrorType CWelsTaskManageBase::Init (sWelsEncCtx* pEncCtx) { int32_t iReturn = ENC_RETURN_SUCCESS; //fprintf(stdout, "m_pThreadPool = &(CWelsThreadPool::GetInstance, this=%x\n", this); iReturn = CWelsThreadPool::SetThreadNum (m_iThreadNum); - m_pThreadPool = & (CWelsThreadPool::AddReference (this)); + m_pThreadPool = & (CWelsThreadPool::AddReference ()); if ( (iReturn != ENC_RETURN_SUCCESS) && pEncCtx ) { WelsLog (& (pEncCtx->sLogCtx), WELS_LOG_WARNING, "Set Thread Num to %d did not succeed, current thread num in use: %d", m_iThreadNum, m_pThreadPool->GetThreadNum()); @@ -201,16 +201,6 @@ void CWelsTaskManageBase::OnTaskMinusOne() { //fprintf(stdout, "OnTaskMinusOne m_iWaitTaskNum=%d\n", m_iWaitTaskNum); } -WelsErrorType CWelsTaskManageBase::OnTaskCancelled (WelsCommon::IWelsTask* pTask) { - OnTaskMinusOne(); - return ENC_RETURN_SUCCESS; -} - -WelsErrorType CWelsTaskManageBase::OnTaskExecuted (WelsCommon::IWelsTask* pTask) { - OnTaskMinusOne(); - return ENC_RETURN_SUCCESS; -} - WelsErrorType CWelsTaskManageBase::OnTaskCancelled() { OnTaskMinusOne(); return ENC_RETURN_SUCCESS; diff --git a/test/common/WelsThreadPoolTest.cpp b/test/common/WelsThreadPoolTest.cpp index e46a9a8f..3c37d264 100644 --- a/test/common/WelsThreadPoolTest.cpp +++ b/test/common/WelsThreadPoolTest.cpp @@ -39,7 +39,7 @@ uint32_t CSimpleTask::id = 0; void* OneCallingFunc() { CThreadPoolTest cThreadPoolTest; CSimpleTask* aTasks[TEST_TASK_NUM]; - CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference (&cThreadPoolTest)); + CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference()); int32_t i; for (i = 0; i < TEST_TASK_NUM; i++) { @@ -70,8 +70,9 @@ TEST (CThreadPoolTest, CThreadPoolTest) { EXPECT_EQ (0, iRet); EXPECT_FALSE (CWelsThreadPool::IsReferenced()); - CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference (NULL)); + CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference ()); EXPECT_TRUE(pThreadPool->IsReferenced()); + EXPECT_EQ (8, pThreadPool->GetThreadNum()); iRet = CWelsThreadPool::SetThreadNum (4); @@ -82,7 +83,8 @@ TEST (CThreadPoolTest, CThreadPoolTest) { iRet = CWelsThreadPool::SetThreadNum (4); EXPECT_EQ (0, iRet); - pThreadPool = & (CWelsThreadPool::AddReference (NULL)); + + pThreadPool = & (CWelsThreadPool::AddReference ()); EXPECT_TRUE (pThreadPool->IsReferenced()); EXPECT_EQ (4, pThreadPool->GetThreadNum()); pThreadPool->RemoveInstance(); diff --git a/test/common/WelsThreadPoolTest.h b/test/common/WelsThreadPoolTest.h index cabe4e05..e841d7ba 100644 --- a/test/common/WelsThreadPoolTest.h +++ b/test/common/WelsThreadPoolTest.h @@ -6,7 +6,7 @@ using namespace WelsCommon; -class CThreadPoolTest : public IWelsThreadPoolSink, public IWelsTaskSink { +class CThreadPoolTest : public IWelsTaskSink { public: CThreadPoolTest() { m_iTaskCount = 0;