Re-add and revert back to old ThreadPool. See ticket #128

This commit is contained in:
Erik Broberg
2016-08-17 17:05:13 -04:00
parent 194a4e8832
commit 1199acc7b9
8 changed files with 227 additions and 12 deletions

View File

@@ -71,6 +71,7 @@ set(HEADER_FILES
${CMAKE_CURRENT_SOURCE_DIR}/other/concurrentjobmanager.h
${CMAKE_CURRENT_SOURCE_DIR}/other/concurrentqueue.h
${CMAKE_CURRENT_SOURCE_DIR}/other/statscollector.h
${CMAKE_CURRENT_SOURCE_DIR}/other/threadpool.h
)
@@ -122,6 +123,7 @@ set(SOURCE_FILES
${CMAKE_CURRENT_SOURCE_DIR}/other/lrucache.inl
${CMAKE_CURRENT_SOURCE_DIR}/other/concurrentjobmanager.inl
${CMAKE_CURRENT_SOURCE_DIR}/other/statscollector.cpp
${CMAKE_CURRENT_SOURCE_DIR}/other/threadpool.cpp
)
source_group("Source Files" FILES ${SOURCE_FILES})

View File

@@ -32,7 +32,8 @@
#include <queue>
#include <modules/globebrowsing/other/concurrentqueue.h>
#include <ghoul/misc/threadpool.h>
#include <modules/globebrowsing/other/threadpool.h>
//#include <ghoul/misc/threadpool.h>
#include <ghoul/misc/assert.h>
@@ -64,7 +65,7 @@ namespace openspace {
template<typename P>
class ConcurrentJobManager{
public:
ConcurrentJobManager(std::shared_ptr<ghoul::ThreadPool> pool) : threadPool(pool)
ConcurrentJobManager(std::shared_ptr<ThreadPool> pool) : threadPool(pool)
{
}
@@ -75,14 +76,19 @@ namespace openspace {
void enqueueJob(std::shared_ptr<Job<P>> job) {
threadPool->queue([this, job]() {
//threadPool->queue([this, job]() {
// job->execute();
// _finishedJobs.push(job);
//});
threadPool->enqueue([this, job]() {
job->execute();
_finishedJobs.push(job);
});
}
void clearEnqueuedJobs() {
threadPool->clearRemainingTasks();
//threadPool->clearRemainingTasks();
threadPool->clearTasks();
}
std::shared_ptr<Job<P>> popFinishedJob() {
@@ -95,14 +101,15 @@ namespace openspace {
}
void reset() {
threadPool->clearRemainingTasks();
//threadPool->clearRemainingTasks();
threadPool->clearTasks();
}
private:
ConcurrentQueue<std::shared_ptr<Job<P>>> _finishedJobs;
std::shared_ptr<ghoul::ThreadPool> threadPool;
std::shared_ptr<ThreadPool> threadPool;
};

View File

@@ -0,0 +1,123 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2016 *
* *
* 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 <memory>
#include <ostream>
#include <thread>
#include <queue>
#include <modules/globebrowsing/other/concurrentqueue.h>
#include <modules/globebrowsing/other/threadpool.h>
#include <ghoul/misc/assert.h>
#include <iostream>
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)));
}
}
// the destructor joins all threads
ThreadPool::~ThreadPool() {
// stop all threads
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

View File

@@ -0,0 +1,81 @@
/*****************************************************************************************
* *
* OpenSpace *
* *
* Copyright (c) 2014-2016 *
* *
* 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. *
****************************************************************************************/
#ifndef __THREAD_POOL_H__
#define __THREAD_POOL_H__
#include <glm/glm.hpp>
#include <memory>
#include <ostream>
#include <thread>
#include <queue>
#include <modules/globebrowsing/other/concurrentqueue.h>
#include <ghoul/misc/assert.h>
// Implementatin based on http://progsch.net/wordpress/?p=81
namespace openspace {
class ThreadPool;
class Worker {
public:
Worker(ThreadPool& pool);
void operator()();
private:
ThreadPool& pool;
};
class ThreadPool {
public:
ThreadPool(size_t numThreads);
~ThreadPool();
void enqueue(std::function<void()> f);
void clearTasks();
private:
friend class Worker;
std::vector<std::thread> workers;
std::deque<std::function<void()>> tasks;
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
} // namespace openspace
#endif // __THREAD_POOL_H__

View File

@@ -117,7 +117,7 @@ namespace openspace {
AsyncTileDataProvider::AsyncTileDataProvider(
std::shared_ptr<TileDataset> tileDataset,
std::shared_ptr<ghoul::ThreadPool> pool)
std::shared_ptr<ThreadPool> pool)
: _tileDataset(tileDataset)
, _threadPool(pool)
{

View File

@@ -33,7 +33,8 @@
#include <modules/globebrowsing/geometry/geodetic2.h>
#include <modules/globebrowsing/other/concurrentjobmanager.h>
#include <ghoul/misc/threadpool.h>
#include <modules/globebrowsing/other/threadpool.h>
//#include <ghoul/misc/threadpool.h>
#include <modules/globebrowsing/tile/tiledataset.h>
@@ -118,7 +119,7 @@ namespace openspace {
public:
AsyncTileDataProvider(std::shared_ptr<TileDataset> textureDataProvider,
std::shared_ptr<ghoul::ThreadPool> pool);
std::shared_ptr<ThreadPool> pool);
~AsyncTileDataProvider();

View File

@@ -23,6 +23,7 @@
****************************************************************************************/
#include <modules/globebrowsing/tile/tileproviderfactory.h>
#include <modules/globebrowsing/other/threadpool.h>
#include <modules/globebrowsing/tile/tileprovider/singleimageprovider.h>
#include <modules/globebrowsing/tile/tileprovider/cachingtileprovider.h>
@@ -89,7 +90,7 @@ namespace openspace {
config.minimumTilePixelSize = initData.minimumPixelSize;
auto tileDataset = std::make_shared<TileDataset>(desc, config);
auto threadPool = std::make_shared<ghoul::ThreadPool>(1);
auto threadPool = std::make_shared<ThreadPool>(1);
auto tileReader = std::make_shared<AsyncTileDataProvider>(tileDataset, threadPool);
auto tileCache = std::make_shared<TileCache>(initData.cacheSize);
auto tileProvider = std::make_shared<CachingTileProvider>(tileReader, tileCache, initData.framesUntilRequestQueueFlush);

View File

@@ -34,8 +34,8 @@ return {
Light = "${FONTS}/Roboto/Roboto-Regular.ttf"
},
Logging = {
LogLevel = "Info",
ImmediateFlush = false,
LogLevel = "Debug",
ImmediateFlush = true,
Logs = {
{ Type = "HTML", FileName = "${BASE_PATH}/log.html", Append = false }
},