ddnet/src/engine/shared/jobs.h

65 lines
1.1 KiB
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 <base/system.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 class 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,
2010-05-29 07:25:38 +00:00
STATE_RUNNING,
STATE_DONE
};
};
class CJobPool
{
2017-07-21 13:52:42 +00:00
enum
{
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 GUARDED_BY(m_Lock);
std::shared_ptr<IJob> m_pLastJob GUARDED_BY(m_Lock);
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();
2017-11-24 09:33:42 +00:00
void Init(int NumThreads);
void Add(std::shared_ptr<IJob> pJob);
static void RunBlocking(IJob *pJob);
2010-05-29 07:25:38 +00:00
};
#endif