about summary refs log tree commit diff
path: root/src/engine/shared/jobs.h
blob: 9e15228ae74adb2e943a20498bfc1dfe8314901b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/* (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.                */
#ifndef ENGINE_SHARED_JOBS_H
#define ENGINE_SHARED_JOBS_H
typedef int (*JOBFUNC)(void *pData);

class CJobPool;

class CJob
{
	friend class CJobPool;

	CJobPool *m_pPool;
	CJob *m_pPrev;
	CJob *m_pNext;

	volatile int m_Status;
	volatile int m_Result;

	JOBFUNC m_pfnFunc;
	void *m_pFuncData;
public:
	CJob()
	{
		m_Status = STATE_DONE;
		m_pFuncData = 0;
	}

	enum
	{
		STATE_PENDING=0,
		STATE_RUNNING,
		STATE_DONE
	};

	int Status() const { return m_Status; }
	int Result() const {return m_Result; }
};

class CJobPool
{
	LOCK m_Lock;
	CJob *m_pFirstJob;
	CJob *m_pLastJob;

	static void WorkerThread(void *pUser);

public:
	CJobPool();

	int Init(int NumThreads);
	int Add(CJob *pJob, JOBFUNC pfnFunc, void *pData);
};
#endif