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; }
|
2011-03-30 10:08:33 +00:00
|
|
|
int Result() const {return m_Result; }
|
2010-05-29 07:25:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
class CJobPool
|
|
|
|
{
|
|
|
|
LOCK m_Lock;
|
|
|
|
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();
|
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
|