mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-04-26 05:58:48 -05:00
Pr/core threadpool (#401)
Move GlobeBrowsing thread pool to OpenSpace core
This commit is contained in:
@@ -159,6 +159,7 @@ set(OPENSPACE_SOURCE
|
||||
${OPENSPACE_BASE_DIR}/src/util/histogram.cpp
|
||||
${OPENSPACE_BASE_DIR}/src/util/task.cpp
|
||||
${OPENSPACE_BASE_DIR}/src/util/taskloader.cpp
|
||||
${OPENSPACE_BASE_DIR}/src/util/threadpool.cpp
|
||||
${OPENSPACE_BASE_DIR}/src/util/time.cpp
|
||||
${OPENSPACE_BASE_DIR}/src/util/timeline.cpp
|
||||
${OPENSPACE_BASE_DIR}/src/util/timemanager.cpp
|
||||
@@ -296,6 +297,10 @@ set(OPENSPACE_HEADER
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/blockplaneintersectiongeometry.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/boxgeometry.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/camera.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/concurrentjobmanager.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/concurrentjobmanager.inl
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/concurrentqueue.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/concurrentqueue.inl
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/factorymanager.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/factorymanager.inl
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/keys.h
|
||||
@@ -318,6 +323,7 @@ set(OPENSPACE_HEADER
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/timerange.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/updatestructures.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/transformationmanager.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/threadpool.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/histogram.h
|
||||
${OPENSPACE_BASE_DIR}/include/openspace/util/gpudata.h
|
||||
)
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* OpenSpace *
|
||||
* *
|
||||
* Copyright (c) 2014-2017 *
|
||||
* *
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this *
|
||||
* software and associated documentation files (the "Software"), to deal in the Software *
|
||||
* without restriction, including without limitation the rights to use, copy, modify, *
|
||||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to *
|
||||
* permit persons to whom the Software is furnished to do so, subject to the following *
|
||||
* conditions: *
|
||||
* *
|
||||
* The above copyright notice and this permission notice shall be included in all copies *
|
||||
* or substantial portions of the Software. *
|
||||
* *
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
|
||||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A *
|
||||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT *
|
||||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF *
|
||||
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE *
|
||||
* OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
|
||||
****************************************************************************************/
|
||||
|
||||
#include <openspace/util/threadpool.h>
|
||||
|
||||
namespace openspace {
|
||||
|
||||
Worker::Worker(ThreadPool& pool) : pool(pool) {}
|
||||
|
||||
void Worker::operator()() {
|
||||
std::function<void()> task;
|
||||
while (true) {
|
||||
// acquire lock
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(pool.queue_mutex);
|
||||
|
||||
// look for a work item
|
||||
while (!pool.stop && pool.tasks.empty()) {
|
||||
// if there are none wait for notification
|
||||
pool.condition.wait(lock);
|
||||
}
|
||||
|
||||
if (pool.stop) { // exit if the pool is stopped
|
||||
return;
|
||||
}
|
||||
|
||||
// get the task from the queue
|
||||
task = pool.tasks.front();
|
||||
pool.tasks.pop_front();
|
||||
|
||||
}// release lock
|
||||
|
||||
// execute the task
|
||||
task();
|
||||
}
|
||||
}
|
||||
|
||||
ThreadPool::ThreadPool(size_t numThreads)
|
||||
: stop(false)
|
||||
{
|
||||
for (size_t i = 0; i < numThreads; ++i) {
|
||||
workers.push_back(std::thread(Worker(*this)));
|
||||
}
|
||||
}
|
||||
|
||||
ThreadPool::ThreadPool(const ThreadPool& toCopy)
|
||||
: ThreadPool(toCopy.workers.size())
|
||||
{ }
|
||||
|
||||
// the destructor joins all threads
|
||||
ThreadPool::~ThreadPool() {
|
||||
// stop all threads
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(queue_mutex);
|
||||
stop = true;
|
||||
}
|
||||
condition.notify_all();
|
||||
|
||||
// join them
|
||||
for (size_t i = 0; i < workers.size(); ++i) {
|
||||
workers[i].join();
|
||||
}
|
||||
}
|
||||
|
||||
// add new work item to the pool
|
||||
void ThreadPool::enqueue(std::function<void()> f) {
|
||||
{ // acquire lock
|
||||
std::unique_lock<std::mutex> lock(queue_mutex);
|
||||
|
||||
// add the task
|
||||
tasks.push_back(f);
|
||||
} // release lock
|
||||
|
||||
// wake up one thread
|
||||
condition.notify_one();
|
||||
}
|
||||
|
||||
void ThreadPool::clearTasks() {
|
||||
{ // acquire lock
|
||||
std::unique_lock<std::mutex> lock(queue_mutex);
|
||||
tasks.clear();
|
||||
} // release lock
|
||||
}
|
||||
|
||||
} // namespace openspace
|
||||
Reference in New Issue
Block a user