summaryrefslogtreecommitdiff
path: root/Runtime/Threading
diff options
context:
space:
mode:
Diffstat (limited to 'Runtime/Threading')
-rw-r--r--Runtime/Threading/JobSystem.cpp15
-rw-r--r--Runtime/Threading/JobSystem.h3
-rw-r--r--Runtime/Threading/Mutex.cpp4
-rw-r--r--Runtime/Threading/Mutex.h10
-rw-r--r--Runtime/Threading/WorkThread.cpp8
5 files changed, 28 insertions, 12 deletions
diff --git a/Runtime/Threading/JobSystem.cpp b/Runtime/Threading/JobSystem.cpp
index a026300..1c62123 100644
--- a/Runtime/Threading/JobSystem.cpp
+++ b/Runtime/Threading/JobSystem.cpp
@@ -1,6 +1,8 @@
#include "JobSystem.h"
+#include "Runtime/Debug/Log.h"
JobSystem::JobSystem()
+ : m_Initialized(false)
{
}
@@ -12,15 +14,26 @@ 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)
@@ -39,5 +52,5 @@ void JobSystem::AddJobAtEnd(Job* job)
WorkThread* JobSystem::SelectThread()
{
- return m_Threads[0];
+ return m_Threads[(++m_Cur)% m_ThreadCount];
} \ No newline at end of file
diff --git a/Runtime/Threading/JobSystem.h b/Runtime/Threading/JobSystem.h
index c95037c..7f8148a 100644
--- a/Runtime/Threading/JobSystem.h
+++ b/Runtime/Threading/JobSystem.h
@@ -17,6 +17,9 @@ public:
private:
WorkThread* SelectThread();
+ bool m_Initialized;
std::vector<WorkThread*> m_Threads;
+ int m_Cur;
+ int m_ThreadCount;
}; \ No newline at end of file
diff --git a/Runtime/Threading/Mutex.cpp b/Runtime/Threading/Mutex.cpp
index eabe48d..6be162f 100644
--- a/Runtime/Threading/Mutex.cpp
+++ b/Runtime/Threading/Mutex.cpp
@@ -14,12 +14,12 @@ Mutex::~Mutex()
m_Handle = NULL;
}
-void Mutex::Lock()
+void Mutex::LockSelf()
{
::WaitForSingleObject(m_Handle, (~(uint32)0));
}
-void Mutex::Unlock()
+void Mutex::UnlockSelf()
{
::ReleaseMutex(m_Handle);
} \ No newline at end of file
diff --git a/Runtime/Threading/Mutex.h b/Runtime/Threading/Mutex.h
index 44f8cb9..eed69aa 100644
--- a/Runtime/Threading/Mutex.h
+++ b/Runtime/Threading/Mutex.h
@@ -7,8 +7,8 @@ public:
Mutex();
~Mutex();
- void Lock();
- void Unlock();
+ void LockSelf();
+ void UnlockSelf();
private:
HANDLE m_Handle;
@@ -22,11 +22,11 @@ public:
MutexLocker(Mutex& mutex)
: m(mutex)
{
- m.Lock();
+ m.LockSelf();
};
~MutexLocker()
{
- m.Unlock();
+ m.UnlockSelf();
}
operator bool() { return false; };
private:
@@ -34,6 +34,6 @@ private:
Mutex& m;
};
-#define _lock(m) \
+#define Lock(m) \
if(MutexLocker lock_##m = m){} else
diff --git a/Runtime/Threading/WorkThread.cpp b/Runtime/Threading/WorkThread.cpp
index f9bb864..31c3ead 100644
--- a/Runtime/Threading/WorkThread.cpp
+++ b/Runtime/Threading/WorkThread.cpp
@@ -5,7 +5,7 @@ void WorkThread::Run()
{
while (true)
{
- _lock(m_PendingMutex)
+ Lock(m_PendingMutex)
{
for (auto iter = m_PendingJobs.begin(); iter != m_PendingJobs.end();)
{
@@ -13,7 +13,7 @@ void WorkThread::Run()
job->Process();
if (job->IsFinished())
{
- _lock(m_FinishedMutex) {
+ Lock(m_FinishedMutex) {
m_FinishedJobs.push_back(job);
}
iter = m_PendingJobs.erase(iter);
@@ -29,7 +29,7 @@ void WorkThread::Run()
void WorkThread::Dispatch(void* param)
{
- _lock(m_FinishedMutex)
+ Lock(m_FinishedMutex)
{
for (int i = 0; i < m_FinishedJobs.size(); ++i)
{
@@ -41,7 +41,7 @@ void WorkThread::Dispatch(void* param)
void WorkThread::AddJobAtEnd(Job* job)
{
- _lock(m_PendingMutex)
+ Lock(m_PendingMutex)
{
m_PendingJobs.push_back(job);
}