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
|
|
|
#ifndef ENGINE_SHARED_JOBS_H
|
|
|
|
#define ENGINE_SHARED_JOBS_H
|
|
|
|
|
2018-06-19 12:45:53 +00:00
|
|
|
#include <base/system.h>
|
|
|
|
|
2017-11-23 14:47:38 +00:00
|
|
|
#include <atomic>
|
|
|
|
#include <memory>
|
|
|
|
|
|
|
|
class CJobPool;
|
2017-10-17 13:39:20 +00:00
|
|
|
|
2017-11-23 14:47:38 +00:00
|
|
|
class IJob
|
2010-05-29 07:25:38 +00:00
|
|
|
{
|
2019-04-11 10:21:42 +00:00
|
|
|
friend class CJobPool;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-11-23 14:47:38 +00:00
|
|
|
private:
|
|
|
|
std::shared_ptr<IJob> m_pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-11-23 14:47:38 +00:00
|
|
|
std::atomic<int> m_Status;
|
|
|
|
virtual void Run() = 0;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
2017-11-23 14:47:38 +00:00
|
|
|
IJob();
|
|
|
|
IJob(const IJob &Other);
|
|
|
|
IJob &operator=(const IJob &Other);
|
|
|
|
virtual ~IJob();
|
|
|
|
int Status();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
enum
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
STATE_PENDING = 0,
|
2010-05-29 07:25:38 +00:00
|
|
|
STATE_RUNNING,
|
|
|
|
STATE_DONE
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
class CJobPool
|
|
|
|
{
|
2017-07-21 13:52:42 +00:00
|
|
|
enum
|
|
|
|
{
|
2020-09-26 19:41:58 +00:00
|
|
|
MAX_THREADS = 32
|
2017-07-21 13:52:42 +00:00
|
|
|
};
|
|
|
|
int m_NumThreads;
|
|
|
|
void *m_apThreads[MAX_THREADS];
|
2017-11-24 08:23:23 +00:00
|
|
|
std::atomic<bool> m_Shutdown;
|
2017-07-21 13:52:42 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
LOCK m_Lock;
|
2017-09-02 09:32:10 +00:00
|
|
|
SEMAPHORE m_Semaphore;
|
2020-12-02 18:11:19 +00:00
|
|
|
std::shared_ptr<IJob> m_pFirstJob GUARDED_BY(m_Lock);
|
|
|
|
std::shared_ptr<IJob> m_pLastJob GUARDED_BY(m_Lock);
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2022-05-28 00:14:46 +00:00
|
|
|
static void WorkerThread(void *pUser) NO_THREAD_SAFETY_ANALYSIS;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
public:
|
|
|
|
CJobPool();
|
2017-07-21 13:52:42 +00:00
|
|
|
~CJobPool();
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2017-11-24 09:33:42 +00:00
|
|
|
void Init(int NumThreads);
|
2022-03-12 12:48:05 +00:00
|
|
|
void Destroy();
|
2022-05-28 00:14:46 +00:00
|
|
|
void Add(std::shared_ptr<IJob> pJob) REQUIRES(!m_Lock);
|
2021-03-13 17:10:59 +00:00
|
|
|
static void RunBlocking(IJob *pJob);
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
#endif
|