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

This commit is contained in:
sijchen 2016-03-02 17:08:09 -08:00
parent d4f09d9048
commit 4db9c32976
7 changed files with 28 additions and 54 deletions

View File

@ -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<IWelsTask>* m_cWaitedTasks;
CWelsCircleQueue<CWelsTaskThread>* m_cIdleThreads;
CWelsList<CWelsTaskThread>* m_cBusyThreads;
IWelsThreadPoolSink* m_pSink;
CWelsLock m_cLockPool;
CWelsLock m_cLockWaitedTasks;

View File

@ -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,14 +335,15 @@ IWelsTask* CWelsThreadPool::GetWaitedTask() {
void CWelsThreadPool::ClearWaitedTasks() {
CWelsAutoLock cLock (m_cLockWaitedTasks);
if (m_pSink) {
IWelsTask* pTask = NULL;
while (0 != m_cWaitedTasks->size()) {
m_pSink->OnTaskCancelled (m_cWaitedTasks->begin());
pTask = m_cWaitedTasks->begin();
if (pTask->GetSink()) {
pTask->GetSink()->OnTaskCancelled();
}
m_cWaitedTasks->pop_front();
}
}
}
}

View File

@ -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<CWelsBaseTask> TASKLIST_TYPE;
//typedef std::pair<int, int> 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();

View File

@ -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);

View File

@ -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;

View File

@ -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();

View File

@ -6,7 +6,7 @@
using namespace WelsCommon;
class CThreadPoolTest : public IWelsThreadPoolSink, public IWelsTaskSink {
class CThreadPoolTest : public IWelsTaskSink {
public:
CThreadPoolTest() {
m_iTaskCount = 0;