ddnet/src/engine/shared/jobs.h

62 lines
1,018 B
C
Raw Normal View History

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
#include <atomic>
#include <memory>
class IJob;
class CJobPool;
2017-10-17 13:39:20 +00:00
class IJob
2010-05-29 07:25:38 +00:00
{
friend CJobPool;
private:
std::shared_ptr<IJob> m_pNext;
std::atomic<int> m_Status;
virtual void Run() = 0;
2010-05-29 07:25:38 +00:00
public:
IJob();
IJob(const IJob &Other);
IJob &operator=(const IJob &Other);
virtual ~IJob();
int Status();
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];
std::atomic<bool> m_Shutdown;
2017-07-21 13:52:42 +00:00
2010-05-29 07:25:38 +00:00
LOCK m_Lock;
SEMAPHORE m_Semaphore;
std::shared_ptr<IJob> m_pFirstJob;
std::shared_ptr<IJob> m_pLastJob;
2010-05-29 07:25:38 +00:00
static void WorkerThread(void *pUser);
2010-05-29 07:25:38 +00:00
public:
CJobPool();
2017-07-21 13:52:42 +00:00
~CJobPool();
2010-05-29 07:25:38 +00:00
int Init(int NumThreads);
int Add(std::shared_ptr<IJob> pJob);
2010-05-29 07:25:38 +00:00
};
#endif