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
|
|
|
|
typedef int (*JOBFUNC)(void *pData);
|
|
|
|
|
|
|
|
class CJobPool;
|
|
|
|
|
|
|
|
class CJob
|
|
|
|
{
|
|
|
|
friend class CJobPool;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
CJob *m_pPrev;
|
|
|
|
CJob *m_pNext;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
volatile int m_Status;
|
|
|
|
volatile int m_Result;
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
JOBFUNC m_pfnFunc;
|
|
|
|
void *m_pFuncData;
|
|
|
|
public:
|
|
|
|
CJob()
|
|
|
|
{
|
|
|
|
m_Status = STATE_DONE;
|
|
|
|
m_pFuncData = 0;
|
|
|
|
}
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
STATE_PENDING=0,
|
|
|
|
STATE_RUNNING,
|
|
|
|
STATE_DONE
|
|
|
|
};
|
2011-04-13 18:37:12 +00:00
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Status() const { return m_Status; }
|
2017-07-21 13:52:42 +00:00
|
|
|
int Result() const { return m_Result; }
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
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];
|
|
|
|
volatile bool m_Shutdown;
|
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
LOCK m_Lock;
|
2017-09-02 09:32:10 +00:00
|
|
|
SEMAPHORE m_Semaphore;
|
2010-05-29 07:25:38 +00:00
|
|
|
CJob *m_pFirstJob;
|
|
|
|
CJob *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
|
|
|
|
2010-05-29 07:25:38 +00:00
|
|
|
int Init(int NumThreads);
|
|
|
|
int Add(CJob *pJob, JOBFUNC pfnFunc, void *pData);
|
|
|
|
};
|
|
|
|
#endif
|