ddnet/src/engine/shared/jobs.h

63 lines
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
typedef int (*JOBFUNC)(void *pData);
class CJobPool;
class CJob
{
friend class CJobPool;
2010-05-29 07:25:38 +00:00
CJob *m_pPrev;
CJob *m_pNext;
2010-05-29 07:25:38 +00:00
volatile int m_Status;
volatile int m_Result;
2010-05-29 07:25:38 +00:00
JOBFUNC m_pfnFunc;
void *m_pFuncData;
public:
CJob()
{
m_Status = STATE_DONE;
m_pFuncData = 0;
}
2010-05-29 07:25:38 +00:00
enum
{
STATE_PENDING=0,
STATE_RUNNING,
STATE_DONE
};
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
{
MAX_THREADS=32
};
int m_NumThreads;
void *m_apThreads[MAX_THREADS];
volatile bool m_Shutdown;
2010-05-29 07:25:38 +00:00
LOCK m_Lock;
CJob *m_pFirstJob;
CJob *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(CJob *pJob, JOBFUNC pfnFunc, void *pData);
};
#endif