remove sink in WelsThreadPool and hide the construtor to finish the singleTon
This commit is contained in:
parent
d4f09d9048
commit
4db9c32976
@ -50,12 +50,6 @@
|
|||||||
|
|
||||||
namespace WelsCommon {
|
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 {
|
class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink {
|
||||||
public:
|
public:
|
||||||
@ -63,13 +57,11 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink {
|
|||||||
DEFAULT_THREAD_NUM = 4,
|
DEFAULT_THREAD_NUM = 4,
|
||||||
};
|
};
|
||||||
|
|
||||||
CWelsThreadPool (IWelsThreadPoolSink* pSink = NULL);
|
|
||||||
virtual ~CWelsThreadPool();
|
|
||||||
|
|
||||||
static WELS_THREAD_ERROR_CODE SetThreadNum (int32_t iMaxThreadNum);
|
static WELS_THREAD_ERROR_CODE SetThreadNum (int32_t iMaxThreadNum);
|
||||||
|
|
||||||
static CWelsThreadPool& AddReference (IWelsThreadPoolSink* pSink = NULL);
|
static CWelsThreadPool& AddReference();
|
||||||
void RemoveInstance();
|
void RemoveInstance();
|
||||||
|
|
||||||
static bool IsReferenced();
|
static bool IsReferenced();
|
||||||
|
|
||||||
//IWelsTaskThreadSink
|
//IWelsTaskThreadSink
|
||||||
@ -86,7 +78,7 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink {
|
|||||||
|
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
WELS_THREAD_ERROR_CODE Init (IWelsThreadPoolSink* pSink);
|
WELS_THREAD_ERROR_CODE Init();
|
||||||
WELS_THREAD_ERROR_CODE Uninit();
|
WELS_THREAD_ERROR_CODE Uninit();
|
||||||
|
|
||||||
WELS_THREAD_ERROR_CODE CreateIdleThread();
|
WELS_THREAD_ERROR_CODE CreateIdleThread();
|
||||||
@ -103,8 +95,10 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink {
|
|||||||
void ClearWaitedTasks();
|
void ClearWaitedTasks();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
CWelsThreadPool();
|
||||||
|
virtual ~CWelsThreadPool();
|
||||||
|
|
||||||
WELS_THREAD_ERROR_CODE StopAllRunning();
|
WELS_THREAD_ERROR_CODE StopAllRunning();
|
||||||
void UpdateSink (IWelsThreadPoolSink* pSink);
|
|
||||||
|
|
||||||
static int32_t m_iRefCount;
|
static int32_t m_iRefCount;
|
||||||
static CWelsLock m_cInitLock;
|
static CWelsLock m_cInitLock;
|
||||||
@ -113,7 +107,6 @@ class CWelsThreadPool : public CWelsThread, public IWelsTaskThreadSink {
|
|||||||
CWelsCircleQueue<IWelsTask>* m_cWaitedTasks;
|
CWelsCircleQueue<IWelsTask>* m_cWaitedTasks;
|
||||||
CWelsCircleQueue<CWelsTaskThread>* m_cIdleThreads;
|
CWelsCircleQueue<CWelsTaskThread>* m_cIdleThreads;
|
||||||
CWelsList<CWelsTaskThread>* m_cBusyThreads;
|
CWelsList<CWelsTaskThread>* m_cBusyThreads;
|
||||||
IWelsThreadPoolSink* m_pSink;
|
|
||||||
|
|
||||||
CWelsLock m_cLockPool;
|
CWelsLock m_cLockPool;
|
||||||
CWelsLock m_cLockWaitedTasks;
|
CWelsLock m_cLockWaitedTasks;
|
||||||
|
@ -47,8 +47,8 @@ int32_t CWelsThreadPool::m_iRefCount = 0;
|
|||||||
CWelsLock CWelsThreadPool::m_cInitLock;
|
CWelsLock CWelsThreadPool::m_cInitLock;
|
||||||
int32_t CWelsThreadPool::m_iMaxThreadNum = DEFAULT_THREAD_NUM;
|
int32_t CWelsThreadPool::m_iMaxThreadNum = DEFAULT_THREAD_NUM;
|
||||||
|
|
||||||
CWelsThreadPool::CWelsThreadPool (IWelsThreadPoolSink* pSink) :
|
CWelsThreadPool::CWelsThreadPool() :
|
||||||
m_cWaitedTasks (NULL), m_cIdleThreads (NULL), m_cBusyThreads (NULL), m_pSink (pSink) {
|
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;
|
return WELS_THREAD_ERROR_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
CWelsThreadPool& CWelsThreadPool::AddReference (IWelsThreadPoolSink* pSink) {
|
|
||||||
|
CWelsThreadPool& CWelsThreadPool::AddReference () {
|
||||||
CWelsAutoLock cLock (m_cInitLock);
|
CWelsAutoLock cLock (m_cInitLock);
|
||||||
static CWelsThreadPool m_cThreadPoolSelf (pSink);
|
static CWelsThreadPool m_cThreadPoolSelf;
|
||||||
if (m_iRefCount == 0) {
|
if (m_iRefCount == 0) {
|
||||||
//TODO: will remove this afterwards
|
//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.Uninit();
|
||||||
}
|
}
|
||||||
m_cThreadPoolSelf.UpdateSink (pSink);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//fprintf(stdout, "m_iRefCount=%d, pSink=%x, iMaxThreadNum=%d\n", m_iRefCount, pSink, iMaxThreadNum);
|
//fprintf(stdout, "m_iRefCount=%d, pSink=%x, iMaxThreadNum=%d\n", m_iRefCount, pSink, iMaxThreadNum);
|
||||||
@ -98,23 +98,17 @@ void CWelsThreadPool::RemoveInstance() {
|
|||||||
-- m_iRefCount;
|
-- m_iRefCount;
|
||||||
if (0 == m_iRefCount) {
|
if (0 == m_iRefCount) {
|
||||||
StopAllRunning();
|
StopAllRunning();
|
||||||
m_pSink = NULL;
|
|
||||||
Uninit();
|
Uninit();
|
||||||
//fprintf(stdout, "m_iRefCount=%d, IdleThreadNum=%d, BusyThreadNum=%d, WaitedTask=%d\n", m_iRefCount, GetIdleThreadNum(), GetBusyThreadNum(), GetWaitedTaskNum());
|
//fprintf(stdout, "m_iRefCount=%d, IdleThreadNum=%d, BusyThreadNum=%d, WaitedTask=%d\n", m_iRefCount, GetIdleThreadNum(), GetBusyThreadNum(), GetWaitedTaskNum());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
bool CWelsThreadPool::IsReferenced() {
|
bool CWelsThreadPool::IsReferenced() {
|
||||||
CWelsAutoLock cLock (m_cInitLock);
|
CWelsAutoLock cLock (m_cInitLock);
|
||||||
return (m_iRefCount>0);
|
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) {
|
WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStart (CWelsTaskThread* pThread, IWelsTask* pTask) {
|
||||||
AddThreadToBusyList (pThread);
|
AddThreadToBusyList (pThread);
|
||||||
@ -143,7 +137,7 @@ WELS_THREAD_ERROR_CODE CWelsThreadPool::OnTaskStop (CWelsTaskThread* pThread, IW
|
|||||||
return WELS_THREAD_ERROR_OK;
|
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");
|
//fprintf(stdout, "Enter WelsThreadPool Init\n");
|
||||||
|
|
||||||
CWelsAutoLock cLock (m_cLockPool);
|
CWelsAutoLock cLock (m_cLockPool);
|
||||||
@ -341,12 +335,13 @@ IWelsTask* CWelsThreadPool::GetWaitedTask() {
|
|||||||
|
|
||||||
void CWelsThreadPool::ClearWaitedTasks() {
|
void CWelsThreadPool::ClearWaitedTasks() {
|
||||||
CWelsAutoLock cLock (m_cLockWaitedTasks);
|
CWelsAutoLock cLock (m_cLockWaitedTasks);
|
||||||
|
IWelsTask* pTask = NULL;
|
||||||
if (m_pSink) {
|
while (0 != m_cWaitedTasks->size()) {
|
||||||
while (0 != m_cWaitedTasks->size()) {
|
pTask = m_cWaitedTasks->begin();
|
||||||
m_pSink->OnTaskCancelled (m_cWaitedTasks->begin());
|
if (pTask->GetSink()) {
|
||||||
m_cWaitedTasks->pop_front();
|
pTask->GetSink()->OnTaskCancelled();
|
||||||
}
|
}
|
||||||
|
m_cWaitedTasks->pop_front();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,8 +65,7 @@ class IWelsTaskManage {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
class CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsThreadPoolSink,
|
class CWelsTaskManageBase : public IWelsTaskManage, public WelsCommon::IWelsTaskSink {
|
||||||
public WelsCommon::IWelsTaskSink {
|
|
||||||
public:
|
public:
|
||||||
typedef CWelsCircleQueue<CWelsBaseTask> TASKLIST_TYPE;
|
typedef CWelsCircleQueue<CWelsBaseTask> TASKLIST_TYPE;
|
||||||
//typedef std::pair<int, int> SLICE_BOUNDARY_PAIR;
|
//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);
|
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
|
//IWelsTaskSink
|
||||||
virtual WelsErrorType OnTaskExecuted();
|
virtual WelsErrorType OnTaskExecuted();
|
||||||
virtual WelsErrorType OnTaskCancelled();
|
virtual WelsErrorType OnTaskCancelled();
|
||||||
|
@ -347,7 +347,6 @@ int32_t RequestMtResource (sWelsEncCtx** ppCtx, SWelsSvcCodingParam* pCodingPara
|
|||||||
pSmt->piSliceIndexInThread[iIdx] = NULL;
|
pSmt->piSliceIndexInThread[iIdx] = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace);
|
WelsSnprintf (name, SEM_NAME_MAX, "scm%s", pSmt->eventNamespace);
|
||||||
err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name);
|
err = WelsEventOpen (&pSmt->pSliceCodedMasterEvent, name);
|
||||||
MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno);
|
MT_TRACE_LOG (pLogCtx, WELS_LOG_INFO, "[MT] Open pSliceCodedMasterEvent named(%s) ret%d err%d", name, err, errno);
|
||||||
|
@ -99,7 +99,7 @@ WelsErrorType CWelsTaskManageBase::Init (sWelsEncCtx* pEncCtx) {
|
|||||||
int32_t iReturn = ENC_RETURN_SUCCESS;
|
int32_t iReturn = ENC_RETURN_SUCCESS;
|
||||||
//fprintf(stdout, "m_pThreadPool = &(CWelsThreadPool::GetInstance, this=%x\n", this);
|
//fprintf(stdout, "m_pThreadPool = &(CWelsThreadPool::GetInstance, this=%x\n", this);
|
||||||
iReturn = CWelsThreadPool::SetThreadNum (m_iThreadNum);
|
iReturn = CWelsThreadPool::SetThreadNum (m_iThreadNum);
|
||||||
m_pThreadPool = & (CWelsThreadPool::AddReference (this));
|
m_pThreadPool = & (CWelsThreadPool::AddReference ());
|
||||||
if ( (iReturn != ENC_RETURN_SUCCESS) && pEncCtx ) {
|
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",
|
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());
|
m_iThreadNum, m_pThreadPool->GetThreadNum());
|
||||||
@ -201,16 +201,6 @@ void CWelsTaskManageBase::OnTaskMinusOne() {
|
|||||||
//fprintf(stdout, "OnTaskMinusOne 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() {
|
WelsErrorType CWelsTaskManageBase::OnTaskCancelled() {
|
||||||
OnTaskMinusOne();
|
OnTaskMinusOne();
|
||||||
return ENC_RETURN_SUCCESS;
|
return ENC_RETURN_SUCCESS;
|
||||||
|
@ -39,7 +39,7 @@ uint32_t CSimpleTask::id = 0;
|
|||||||
void* OneCallingFunc() {
|
void* OneCallingFunc() {
|
||||||
CThreadPoolTest cThreadPoolTest;
|
CThreadPoolTest cThreadPoolTest;
|
||||||
CSimpleTask* aTasks[TEST_TASK_NUM];
|
CSimpleTask* aTasks[TEST_TASK_NUM];
|
||||||
CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference (&cThreadPoolTest));
|
CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference());
|
||||||
|
|
||||||
int32_t i;
|
int32_t i;
|
||||||
for (i = 0; i < TEST_TASK_NUM; i++) {
|
for (i = 0; i < TEST_TASK_NUM; i++) {
|
||||||
@ -70,8 +70,9 @@ TEST (CThreadPoolTest, CThreadPoolTest) {
|
|||||||
EXPECT_EQ (0, iRet);
|
EXPECT_EQ (0, iRet);
|
||||||
EXPECT_FALSE (CWelsThreadPool::IsReferenced());
|
EXPECT_FALSE (CWelsThreadPool::IsReferenced());
|
||||||
|
|
||||||
CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference (NULL));
|
CWelsThreadPool* pThreadPool = & (CWelsThreadPool::AddReference ());
|
||||||
EXPECT_TRUE(pThreadPool->IsReferenced());
|
EXPECT_TRUE(pThreadPool->IsReferenced());
|
||||||
|
|
||||||
EXPECT_EQ (8, pThreadPool->GetThreadNum());
|
EXPECT_EQ (8, pThreadPool->GetThreadNum());
|
||||||
|
|
||||||
iRet = CWelsThreadPool::SetThreadNum (4);
|
iRet = CWelsThreadPool::SetThreadNum (4);
|
||||||
@ -82,7 +83,8 @@ TEST (CThreadPoolTest, CThreadPoolTest) {
|
|||||||
|
|
||||||
iRet = CWelsThreadPool::SetThreadNum (4);
|
iRet = CWelsThreadPool::SetThreadNum (4);
|
||||||
EXPECT_EQ (0, iRet);
|
EXPECT_EQ (0, iRet);
|
||||||
pThreadPool = & (CWelsThreadPool::AddReference (NULL));
|
|
||||||
|
pThreadPool = & (CWelsThreadPool::AddReference ());
|
||||||
EXPECT_TRUE (pThreadPool->IsReferenced());
|
EXPECT_TRUE (pThreadPool->IsReferenced());
|
||||||
EXPECT_EQ (4, pThreadPool->GetThreadNum());
|
EXPECT_EQ (4, pThreadPool->GetThreadNum());
|
||||||
pThreadPool->RemoveInstance();
|
pThreadPool->RemoveInstance();
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
using namespace WelsCommon;
|
using namespace WelsCommon;
|
||||||
|
|
||||||
class CThreadPoolTest : public IWelsThreadPoolSink, public IWelsTaskSink {
|
class CThreadPoolTest : public IWelsTaskSink {
|
||||||
public:
|
public:
|
||||||
CThreadPoolTest() {
|
CThreadPoolTest() {
|
||||||
m_iTaskCount = 0;
|
m_iTaskCount = 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user