summaryrefslogtreecommitdiff
path: root/Client/Source/Threading/JobSystem.cpp
blob: 378d74f4503e2be1e8d60f7b23d2fe962a5d05d2 (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
55
56
#include "JobSystem.h"
#include "../Debug/Log.h"

JobSystem::JobSystem()
	: m_Initialized(false)
{

}

JobSystem::~JobSystem()
{

}

void JobSystem::Initilize(int workThreadCount)
{
	if (m_Initialized)
	{
		log_error("JobSystem has already initialized.");
		return;
	}

	if (workThreadCount <= 0)
		return;

	m_ThreadCount = workThreadCount;
	m_Cur = 0;

	for (int i = 0; i < workThreadCount; ++i)
	{
		WorkThread* thread = new WorkThread();
		thread->Resume();
		m_Threads.push_back(thread);
	}

	m_Initialized = true;
}

void JobSystem::Dispatch(void* param)
{
	for (int i = 0; i < m_Threads.size(); ++i)
	{
		m_Threads[i]->Dispatch(param);
	}
}

void JobSystem::AddJobAtEnd(Job* job)
{
	WorkThread* thread = SelectThread();
	thread->AddJobAtEnd(job);
}

WorkThread* JobSystem::SelectThread()
{
	return m_Threads[(++m_Cur)% m_ThreadCount];
}