-
-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
cb29816
commit 594c480
Showing
15 changed files
with
180 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
#include "stdafx.h" | ||
#include "Threading/ThreadPool.hpp" | ||
#ifdef _GPA_ENABLED | ||
#include <tal.h> | ||
#endif | ||
|
||
/* | ||
* Basic C++11 based thread pool with per-thread job queues | ||
* | ||
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de | ||
* | ||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
Thread::Thread() | ||
{ | ||
worker = std::thread(&Thread::queueLoop, this); | ||
} | ||
|
||
Thread::~Thread() | ||
{ | ||
if (worker.joinable()) | ||
{ | ||
wait(); | ||
queueMutex.lock(); | ||
destroying = true; | ||
condition.notify_one(); | ||
queueMutex.unlock(); | ||
worker.join(); | ||
} | ||
} | ||
|
||
void Thread::addJob(std::function<void()> function) | ||
{ | ||
std::lock_guard<std::mutex> lock(queueMutex); | ||
jobQueue.push(std::move(function)); | ||
condition.notify_one(); | ||
} | ||
|
||
void Thread::wait() | ||
{ | ||
std::unique_lock<std::mutex> lock(queueMutex); | ||
condition.wait(lock, [this]() { return jobQueue.empty(); }); | ||
} | ||
|
||
void Thread::queueLoop() | ||
{ | ||
while (true) | ||
{ | ||
std::function<void()> job; | ||
{ | ||
std::unique_lock<std::mutex> lock(queueMutex); | ||
condition.wait(lock, [this] { return !jobQueue.empty() || destroying; }); | ||
if (destroying) | ||
{ | ||
break; | ||
} | ||
job = jobQueue.front(); | ||
} | ||
|
||
job(); | ||
|
||
{ | ||
std::lock_guard<std::mutex> lock(queueMutex); | ||
jobQueue.pop(); | ||
condition.notify_one(); | ||
} | ||
} | ||
} | ||
|
||
XRCORE_API ThreadPool ttapi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
#pragma once | ||
#include "xrCore/xrCore.h" | ||
|
||
/* | ||
* Basic C++11 based thread pool with per-thread job queues | ||
* | ||
* Copyright (C) 2016 by Sascha Willems - www.saschawillems.de | ||
* | ||
* This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) | ||
*/ | ||
|
||
#include <vector> | ||
#include <thread> | ||
#include <queue> | ||
#include <mutex> | ||
#include <condition_variable> | ||
#include <functional> | ||
#include <memory> | ||
|
||
class XRCORE_API Thread | ||
{ | ||
bool destroying = false; | ||
std::thread worker; | ||
std::queue<std::function<void()>> jobQueue; | ||
std::mutex queueMutex; | ||
std::condition_variable condition; | ||
|
||
// Loop through all remaining jobs | ||
void queueLoop(); | ||
|
||
public: | ||
Thread(); | ||
~Thread(); | ||
|
||
// Add a new job to the thread's queue | ||
void addJob(std::function<void()> function); | ||
|
||
// Wait until all work items have been finished | ||
void wait(); | ||
}; | ||
|
||
class ThreadPool | ||
{ | ||
public: | ||
std::vector<std::unique_ptr<Thread>> threads; | ||
|
||
void initialize() | ||
{ | ||
const int num_threads = std::thread::hardware_concurrency(); | ||
R_ASSERT(num_threads > 0); | ||
setThreadCount(num_threads); | ||
} | ||
|
||
// Sets the number of threads to be allocated in this pool | ||
void setThreadCount(const uint32_t count) | ||
{ | ||
threads.clear(); | ||
for (auto i = 0; i < count; i++) | ||
threads.push_back(std::make_unique<Thread>()); | ||
} | ||
|
||
// Wait until all threads have finished their work items | ||
void wait() | ||
{ | ||
for (auto &thread : threads) | ||
thread->wait(); | ||
} | ||
}; | ||
|
||
extern XRCORE_API ThreadPool ttapi; |
Oops, something went wrong.