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 "jobs.h"
|
|
|
|
|
2017-11-23 14:47:38 +00:00
|
|
|
IJob::IJob() :
|
|
|
|
m_Status(STATE_PENDING)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-02-14 23:32:04 +00:00
|
|
|
IJob::~IJob() = default;
|
2017-11-23 14:47:38 +00:00
|
|
|
|
|
|
|
int IJob::Status()
|
|
|
|
{
|
|
|
|
return m_Status.load();
|
|
|
|
}
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CJobPool::CJobPool()
|
|
|
|
{
|
|
|
|
// empty the pool
|
2017-07-21 13:52:42 +00:00
|
|
|
m_Shutdown = false;
|
2017-09-02 09:32:10 +00:00
|
|
|
sphore_init(&m_Semaphore);
|
2010-05-29 07:25:38 +00:00
|
|
|
m_pFirstJob = 0;
|
|
|
|
m_pLastJob = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-21 13:52:42 +00:00
|
|
|
CJobPool::~CJobPool()
|
|
|
|
{
|
2022-03-12 12:48:05 +00:00
|
|
|
if(!m_Shutdown)
|
2019-03-19 10:44:16 +00:00
|
|
|
{
|
2022-03-12 12:48:05 +00:00
|
|
|
Destroy();
|
2019-03-19 10:44:16 +00:00
|
|
|
}
|
2017-07-21 13:52:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2017-11-23 14:47:38 +00:00
|
|
|
std::shared_ptr<IJob> pJob = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
// fetch job from queue
|
2017-09-02 09:32:10 +00:00
|
|
|
sphore_wait(&pPool->m_Semaphore);
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2022-06-03 10:10:05 +00:00
|
|
|
CLockScope ls(pPool->m_Lock);
|
|
|
|
if(pPool->m_pFirstJob)
|
|
|
|
{
|
|
|
|
pJob = pPool->m_pFirstJob;
|
|
|
|
pPool->m_pFirstJob = pPool->m_pFirstJob->m_pNext;
|
2023-08-01 08:18:52 +00:00
|
|
|
// allow remaining objects in list to destruct, even when current object stays alive
|
|
|
|
pJob->m_pNext = nullptr;
|
2022-06-03 10:10:05 +00:00
|
|
|
if(!pPool->m_pFirstJob)
|
|
|
|
pPool->m_pLastJob = 0;
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
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)
|
|
|
|
{
|
2021-03-13 17:10:59 +00:00
|
|
|
RunBlocking(pJob.get());
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-24 09:33:42 +00:00
|
|
|
void CJobPool::Init(int NumThreads)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
|
|
|
// start threads
|
2023-10-15 19:45:53 +00:00
|
|
|
char aName[32];
|
2023-10-15 19:27:50 +00:00
|
|
|
m_vpThreads.reserve(NumThreads);
|
2010-05-29 07:25:38 +00:00
|
|
|
for(int i = 0; i < NumThreads; i++)
|
2023-10-15 19:45:53 +00:00
|
|
|
{
|
|
|
|
str_format(aName, sizeof(aName), "CJobPool worker %d", i);
|
|
|
|
m_vpThreads.push_back(thread_init(WorkerThread, this, aName));
|
|
|
|
}
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
|
|
|
|
2022-03-12 12:48:05 +00:00
|
|
|
void CJobPool::Destroy()
|
|
|
|
{
|
|
|
|
m_Shutdown = true;
|
2023-10-15 19:27:50 +00:00
|
|
|
for(size_t i = 0; i < m_vpThreads.size(); i++)
|
2022-03-12 12:48:05 +00:00
|
|
|
sphore_signal(&m_Semaphore);
|
2023-10-15 19:27:50 +00:00
|
|
|
for(void *pThread : m_vpThreads)
|
|
|
|
thread_wait(pThread);
|
|
|
|
m_vpThreads.clear();
|
2022-03-12 12:48:05 +00:00
|
|
|
sphore_destroy(&m_Semaphore);
|
|
|
|
}
|
|
|
|
|
2017-11-24 09:33:42 +00:00
|
|
|
void CJobPool::Add(std::shared_ptr<IJob> pJob)
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2022-06-03 10:10:05 +00:00
|
|
|
{
|
|
|
|
CLockScope ls(m_Lock);
|
|
|
|
// add job to queue
|
|
|
|
if(m_pLastJob)
|
|
|
|
m_pLastJob->m_pNext = pJob;
|
|
|
|
m_pLastJob = std::move(pJob);
|
|
|
|
if(!m_pFirstJob)
|
|
|
|
m_pFirstJob = m_pLastJob;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-09-02 09:32:10 +00:00
|
|
|
sphore_signal(&m_Semaphore);
|
2010-05-29 07:25:38 +00:00
|
|
|
}
|
2021-03-13 17:10:59 +00:00
|
|
|
|
|
|
|
void CJobPool::RunBlocking(IJob *pJob)
|
|
|
|
{
|
|
|
|
pJob->m_Status = IJob::STATE_RUNNING;
|
|
|
|
pJob->Run();
|
|
|
|
pJob->m_Status = IJob::STATE_DONE;
|
|
|
|
}
|