2010-11-20 10:37:14 +00:00
|
|
|
/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
|
|
|
|
/* If you are missing that file, acquire a complete release at teeworlds.com. */
|
2010-05-29 07:25:38 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
#include "jobs.h"
|
|
|
|
|
|
|
|
CJobPool::CJobPool()
|
|
|
|
{
|
|
|
|
// empty the pool
|
2017-07-21 13:52:42 +00:00
|
|
|
m_NumThreads = 0;
|
|
|
|
m_Shutdown = false;
|
2010-05-29 07:25:38 +00:00
|
|
|
m_Lock = lock_create();
|
|
|
|
m_pFirstJob = 0;
|
|
|
|
m_pLastJob = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 13:52:42 +00:00
|
|
|
CJobPool::~CJobPool()
|
|
|
|
{
|
|
|
|
m_Shutdown = true;
|
|
|
|
for(int i = 0; i < m_NumThreads; i++)
|
|
|
|
thread_wait(m_apThreads[i]);
|
|
|
|
lock_destroy(m_Lock);
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
void CJobPool::WorkerThread(void *pUser)
|
|
|
|
{
|
|
|
|
CJobPool *pPool = (CJobPool *)pUser;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-07-21 13:52:42 +00:00
|
|
|
while(!pPool->m_Shutdown)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
CJob *pJob = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// fetch job from queue
|
|
|
|
lock_wait(pPool->m_Lock);
|
|
|
|
if(pPool->m_pFirstJob)
|
|
|
|
{
|
|
|
|
pJob = pPool->m_pFirstJob;
|
|
|
|
pPool->m_pFirstJob = pPool->m_pFirstJob->m_pNext;
|
|
|
|
if(pPool->m_pFirstJob)
|
|
|
|
pPool->m_pFirstJob->m_pPrev = 0;
|
|
|
|
else
|
|
|
|
pPool->m_pLastJob = 0;
|
|
|
|
}
|
2015-04-07 17:07:38 +00:00
|
|
|
lock_unlock(pPool->m_Lock);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// do the job if we have one
|
|
|
|
if(pJob)
|
|
|
|
{
|
|
|
|
pJob->m_Status = CJob::STATE_RUNNING;
|
|
|
|
pJob->m_Result = pJob->m_pfnFunc(pJob->m_pFuncData);
|
|
|
|
pJob->m_Status = CJob::STATE_DONE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
thread_sleep(10);
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int CJobPool::Init(int NumThreads)
|
|
|
|
{
|
|
|
|
// start threads
|
2017-07-21 13:52:42 +00:00
|
|
|
m_NumThreads = NumThreads > MAX_THREADS ? MAX_THREADS : NumThreads;
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < NumThreads; i++)
|
2017-07-21 13:52:42 +00:00
|
|
|
m_apThreads[i] = thread_init(WorkerThread, this);
|
2010-05-29 07:25:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int CJobPool::Add(CJob *pJob, JOBFUNC pfnFunc, void *pData)
|
|
|
|
{
|
|
|
|
mem_zero(pJob, sizeof(CJob));
|
|
|
|
pJob->m_pfnFunc = pfnFunc;
|
|
|
|
pJob->m_pFuncData = pData;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
lock_wait(m_Lock);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// add job to queue
|
|
|
|
pJob->m_pPrev = m_pLastJob;
|
|
|
|
if(m_pLastJob)
|
|
|
|
m_pLastJob->m_pNext = pJob;
|
|
|
|
m_pLastJob = pJob;
|
|
|
|
if(!m_pFirstJob)
|
|
|
|
m_pFirstJob = pJob;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2015-04-07 17:07:38 +00:00
|
|
|
lock_unlock(m_Lock);
|
2010-05-29 07:25:38 +00:00
|
|
|
return 0;
|
|
|
|
}
|