9#include "MantidKernel/DllConfig.h"
36 ThreadScheduler() : m_cost(0), m_costExecuted(0), m_abortException(
""), m_aborted(false) {}
45 virtual void push(std::shared_ptr<Task> newTask) = 0;
52 virtual std::shared_ptr<Task>
pop(
size_t threadnum) = 0;
71 virtual void abort(std::runtime_error exception) {
73 m_abortException = exception;
136 std::lock_guard<std::mutex> _lock(m_queueLock);
137 return m_queue.empty();
141 void push(std::shared_ptr<Task> newTask)
override {
144 m_cost += newTask->cost();
145 m_queue.emplace_back(newTask);
146 m_queueLock.unlock();
150 std::shared_ptr<Task>
pop(
size_t threadnum)
override {
152 std::shared_ptr<Task> temp =
nullptr;
156 if (!m_queue.empty()) {
158 temp = m_queue.front();
161 m_queueLock.unlock();
168 size_t temp = m_queue.size();
169 m_queueLock.unlock();
180 m_queueLock.unlock();
200 std::shared_ptr<Task>
pop(
size_t threadnum)
override {
202 std::shared_ptr<Task> temp =
nullptr;
206 if (!m_queue.empty()) {
208 temp = m_queue.back();
211 m_queueLock.unlock();
238 std::lock_guard<std::mutex> _lock(m_queueLock);
239 return m_map.empty();
243 void push(std::shared_ptr<Task> newTask)
override {
246 m_cost += newTask->cost();
247 m_map.emplace(newTask->cost(), newTask);
248 m_queueLock.unlock();
252 std::shared_ptr<Task>
pop(
size_t threadnum)
override {
254 std::shared_ptr<Task> temp =
nullptr;
258 if (!m_map.empty()) {
260 auto it = m_map.end();
265 m_queueLock.unlock();
272 size_t temp = m_map.size();
273 m_queueLock.unlock();
284 m_queueLock.unlock();
289 std::multimap<double, std::shared_ptr<Task>>
m_map;
#define UNUSED_ARG(x)
Function arguments are sometimes unused in certain implmentations but are required for documentation ...
A Task is a unit of work to be scheduled and run by a ThreadPool.
A First-In-First-Out Thread Scheduler.
size_t size() override
Returns the size of the queue.
~ThreadSchedulerFIFO() override
Destructor.
void push(std::shared_ptr< Task > newTask) override
Add a Task to the queue.
std::shared_ptr< Task > pop(size_t threadnum) override
Retrieves the next Task to execute.
void clear() override
Empty out the queue.
std::deque< std::shared_ptr< Task > > m_queue
Queue of tasks.
A Last-In-First-Out Thread Scheduler.
std::shared_ptr< Task > pop(size_t threadnum) override
Retrieves the next Task to execute.
A Largest Cost Thread Scheduler.
~ThreadSchedulerLargestCost() override
Destructor.
void clear() override
Empty out the queue.
std::shared_ptr< Task > pop(size_t threadnum) override
Retrieves the next Task to execute.
size_t size() override
Returns the size of the queue.
ThreadSchedulerLargestCost()
std::multimap< double, std::shared_ptr< Task > > m_map
A multimap keeps tasks sorted by the key (cost)
void push(std::shared_ptr< Task > newTask) override
Add a Task to the queue.
The ThreadScheduler object defines how tasks are allocated to threads and in what order.
virtual void abort(std::runtime_error exception)
Signal to the scheduler that a task is complete.
bool getAborted()
Returns true if the execution was aborted.
bool m_aborted
The run was aborted due to an exception.
std::runtime_error m_abortException
The exception that aborted the run.
virtual ~ThreadScheduler()=default
Destructor.
double m_costExecuted
Accumulated cost of tasks that have been executed (popped)
double totalCostExecuted()
Returns the total cost of all Task's in the queue.
std::mutex m_queueLock
Mutex to prevent simultaneous access to the queue.
virtual size_t size()=0
Returns the size of the queue.
virtual void finished(Task *task, size_t threadnum)
Signal to the scheduler that a task is complete.
virtual std::shared_ptr< Task > pop(size_t threadnum)=0
Retrieves the next Task to execute.
double totalCost()
Returns the total cost of all Task's in the queue.
std::runtime_error getAbortException()
Returns the exception that was caught, if any.
virtual void push(std::shared_ptr< Task > newTask)=0
Add a Task to the queue.
double m_cost
Total cost of all tasks.
ThreadScheduler()
Constructor.
virtual void clear()=0
Empty out the queue.
virtual bool empty()=0
Returns true if the queue is empty.
Helper class which provides the Collimation Length for SANS instruments.