mirror of
https://github.com/OpenSpace/OpenSpace.git
synced 2026-01-04 10:40:09 -06:00
Added templated class ConcurrentJobManager skeleton
This commit is contained in:
113
modules/globebrowsing/other/concurrentjobmanager.h
Normal file
113
modules/globebrowsing/other/concurrentjobmanager.h
Normal file
@@ -0,0 +1,113 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* 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 __CONCURRENT_JOB_MANAGER_H__
|
||||
#define __CONCURRENT_JOB_MANAGER_H__
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <memory>
|
||||
#include <ostream>
|
||||
#include <thread>
|
||||
#include <queue>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
namespace openspace {
|
||||
|
||||
|
||||
// Templated abstract base class representing a job to be done.
|
||||
// Client code derive from this class and implement the virtual execute() method
|
||||
template<typename P>
|
||||
struct Job {
|
||||
|
||||
Job() { }
|
||||
virtual ~Job() { }
|
||||
|
||||
virtual void execute() = 0;
|
||||
virtual P product() = 0;
|
||||
|
||||
/*
|
||||
enum Status {
|
||||
UNKNOWN,
|
||||
QUEUED,
|
||||
IN_PROGRESS,
|
||||
FINISHED
|
||||
};
|
||||
|
||||
Status status;
|
||||
*/
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
// Templated
|
||||
template<typename P>
|
||||
class ConcurrentJobManager{
|
||||
public:
|
||||
ConcurrentJobManager() {
|
||||
|
||||
}
|
||||
|
||||
~ConcurrentJobManager() {
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
void enqueueFutureJob(std::unique_ptr<Job<P>> job) {
|
||||
dummy = std::move(job);
|
||||
}
|
||||
|
||||
void startInSeparateThread() {
|
||||
// create new thread
|
||||
dummy->execute();
|
||||
}
|
||||
|
||||
std::unique_ptr<Job<P>> popFinishedJob() {
|
||||
return std::move(dummy);
|
||||
}
|
||||
|
||||
|
||||
|
||||
private:
|
||||
|
||||
void workerThreadMainTask() {
|
||||
|
||||
}
|
||||
|
||||
std::unique_ptr<Job<P>> dummy;
|
||||
|
||||
};
|
||||
|
||||
|
||||
} // namespace openspace
|
||||
|
||||
|
||||
#include <modules/globebrowsing/other/concurrentjobmanager.inl>
|
||||
|
||||
#endif // __CONCURRENT_JOB_MANAGER_H__
|
||||
@@ -42,7 +42,9 @@
|
||||
//#include <test_angle.inl>
|
||||
//#include <test_latlonpatch.inl>
|
||||
//#include <test_texturetileset.inl>
|
||||
#include <test_gdalwms.inl>
|
||||
//#include <test_gdalwms.inl>
|
||||
|
||||
#include <test_concurrentjobmanager.inl>
|
||||
|
||||
#include <openspace/engine/openspaceengine.h>
|
||||
#include <openspace/engine/wrapper/windowwrapper.h>
|
||||
|
||||
70
tests/test_concurrentjobmanager.inl
Normal file
70
tests/test_concurrentjobmanager.inl
Normal file
@@ -0,0 +1,70 @@
|
||||
/*****************************************************************************************
|
||||
* *
|
||||
* 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 "gtest/gtest.h"
|
||||
|
||||
#include <modules/globebrowsing/other/concurrentjobmanager.h>
|
||||
|
||||
#define _USE_MATH_DEFINES
|
||||
#include <math.h>
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
|
||||
|
||||
|
||||
class ConcurrentJobManagerTest : public testing::Test {};
|
||||
|
||||
|
||||
using namespace openspace;
|
||||
|
||||
|
||||
struct TestJob : public Job<int> {
|
||||
virtual void execute() {
|
||||
std::cout << "executing l33t job" << std::endl;
|
||||
prod = 1337;
|
||||
}
|
||||
|
||||
virtual int product() {
|
||||
return prod;
|
||||
}
|
||||
|
||||
int prod;
|
||||
};
|
||||
|
||||
|
||||
TEST_F(ConcurrentJobManagerTest, Basic) {
|
||||
ConcurrentJobManager<int> jobManager;
|
||||
std::unique_ptr<TestJob> testJob = std::unique_ptr<TestJob>(new TestJob());
|
||||
|
||||
jobManager.enqueueFutureJob(std::move(testJob));
|
||||
jobManager.startInSeparateThread();
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
std::this_thread::sleep_for(2s);
|
||||
|
||||
auto finishedJob = jobManager.popFinishedJob();
|
||||
|
||||
int product = finishedJob->product();
|
||||
std::cout << "product is " << product << std::endl;
|
||||
}
|
||||
Reference in New Issue
Block a user