shithub: openh264

Download patch

ref: 4db9c329762779189ece5ff46bec402c62792eff
parent: d4f09d9048f5d7388358d3838fe02c5377cb79b9
author: sijchen <sijchen@cisco.com>
date: Wed Mar 2 12:08:09 EST 2016

remove sink in WelsThreadPool and hide the construtor to finish the singleTon

--- a/codec/common/inc/WelsThreadPool.h
+++ b/codec/common/inc/WelsThreadPool.h
@@ -50,13 +50,7 @@
 
 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:
   enum {
@@ -63,13 +57,11 @@
     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 @@
 
 
  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 @@
   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 @@
   CWelsCircleQueue<IWelsTask>* m_cWaitedTasks;
   CWelsCircleQueue<CWelsTaskThread>* m_cIdleThreads;
   CWelsList<CWelsTaskThread>* m_cBusyThreads;
-  IWelsThreadPoolSink*   m_pSink;
 
   CWelsLock   m_cLockPool;
   CWelsLock   m_cLockWaitedTasks;
--- a/codec/common/src/WelsThreadPool.cpp
+++ b/codec/common/src/WelsThreadPool.cpp
@@ -47,8 +47,8 @@
 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 @@
   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,24 +98,18 @@
   -- 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);
   //fprintf(stdout, "CWelsThreadPool::AddThreadToBusyList: Task %x at Thread %x\n", pTask, pThread);
@@ -143,7 +137,7 @@
   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 @@
 
 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();
   }
 }
 
--- a/codec/encoder/core/inc/wels_task_management.h
+++ b/codec/encoder/core/inc/wels_task_management.h
@@ -65,8 +65,7 @@
 };
 
 
-class  CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsThreadPoolSink,
-  public WelsCommon::IWelsTaskSink {
+class  CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsTaskSink {
  public:
   typedef  CWelsCircleQueue<CWelsBaseTask>            TASKLIST_TYPE;
   //typedef  std::pair<int, int>                  SLICE_BOUNDARY_PAIR;
@@ -79,10 +78,6 @@
   virtual void           InitFrame (const int32_t kiCurDid = 0);
 
   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();
--- a/codec/encoder/core/src/slice_multi_threading.cpp
+++ b/codec/encoder/core/src/slice_multi_threading.cpp
@@ -347,7 +347,6 @@
     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);
--- a/codec/encoder/core/src/wels_task_management.cpp
+++ b/codec/encoder/core/src/wels_task_management.cpp
@@ -99,7 +99,7 @@
   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());
@@ -199,16 +199,6 @@
     //fprintf(stdout, "OnTaskMinusOne WelsEventSignal m_iWaitTaskNum=%d\n", m_iWaitTaskNum);
   }
   //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() {
--- a/test/common/WelsThreadPoolTest.cpp
+++ b/test/common/WelsThreadPoolTest.cpp
@@ -39,7 +39,7 @@
 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 @@
   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 @@
 
   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();
--- 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;