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 IJob;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
STATE_PENDING=0,
|
|
|
|
STATE_RUNNING,
|
|
|
|
STATE_DONE
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
class CJobPool
|
|
|
|
{
|
2017-07-21 13:52:42 +00:00
|
|
|
enum
|
|
|
|
{
|
2017-07-28 12:49:09 +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;
|
2017-11-23 14:47:38 +00:00
|
|
|
std::shared_ptr<IJob> m_pFirstJob;
|
|
|
|
std::shared_ptr<IJob> m_pLastJob;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
static void WorkerThread(void *pUser);
|
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);
|
|
|
|
void Add(std::shared_ptr<IJob> pJob);
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
#endif
|