43 void push(std::shared_ptr<Task> newTask)
override {
45 std::lock_guard<std::mutex> lock(m_queueLock);
46 m_cost += newTask->cost();
48 std::shared_ptr<std::mutex> mut = newTask->getMutex();
49 m_supermap[mut].emplace(newTask->cost(), newTask);
53 std::shared_ptr<Task>
pop(
size_t threadnum)
override {
56 std::shared_ptr<Task> temp =
nullptr;
58 std::lock_guard<std::mutex> lock(m_queueLock);
61 if (!m_supermap.empty()) {
64 for (
auto &mutexedMap : m_supermap) {
66 std::shared_ptr<std::mutex> mapMutex = mutexedMap.first;
67 if ((!mapMutex) || (m_mutexes.empty()) || (m_mutexes.find(mapMutex) == m_mutexes.end())) {
76 temp = std::move(it2->second);
83 if (temp ==
nullptr) {
86 for (
auto &mutexedMap : m_supermap) {
87 if (!mutexedMap.second.empty()) {
90 temp = std::move(map.begin()->second);
92 map.erase(map.begin());
102 std::shared_ptr<std::mutex> mut = temp->getMutex();
104 m_mutexes.insert(mut);
118 std::shared_ptr<std::mutex> mut = task->
getMutex();
120 std::lock_guard<std::mutex> lock(m_queueLock);
122 m_mutexes.erase(mut);
128 std::lock_guard<std::mutex> lock(m_queueLock);
130 return std::accumulate(
131 m_supermap.cbegin(), m_supermap.cend(),
size_t{0},
132 [](
size_t total,
const std::pair<
const std::shared_ptr<std::mutex> &,
const InnerMap &> &mutexedMap) {
133 return total + mutexedMap.second.size();
140 std::lock_guard<std::mutex> lock(m_queueLock);
142 std::find_if_not(m_supermap.cbegin(), m_supermap.cend(),
143 [](
const std::pair<
const std::shared_ptr<std::mutex>,
const InnerMap &> &mutexedMap) {
144 return mutexedMap.second.empty();
146 return mapWithTasks == m_supermap.cend();
151 std::lock_guard<std::mutex> lock(m_queueLock);
154 for (
auto &it : m_supermap) {
165 using InnerMap = std::multimap<double, std::shared_ptr<Task>>;
#define DLLExport
Definitions of the DLLImport compiler directives for MSVC.
#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.
std::shared_ptr< std::mutex > getMutex()
Get the mutex object for this Task.
ThreadSchedulerMutexes : Version of a ThreadSchedulerLargestCost that also makes sure to not try to s...
void clear() override
Empty out the queue.
SuperMap m_supermap
A super map; first key = a Mutex * Inside it: second key = the cost.
std::set< std::shared_ptr< std::mutex > > m_mutexes
Vector of currently used mutexes.
ThreadSchedulerMutexes()=default
std::map< std::shared_ptr< std::mutex >, InnerMap > SuperMap
Map to maps, sorted by Mutex*.
~ThreadSchedulerMutexes() override
std::shared_ptr< Task > pop(size_t threadnum) override
Retrieves the next Task to execute.
void finished(Task *task, size_t threadnum) override
Signal to the scheduler that a task is complete.
std::multimap< double, std::shared_ptr< Task > > InnerMap
Map to tasks, sorted by cost.
void push(std::shared_ptr< Task > newTask) override
Add a Task to the queue.
size_t size() override
Returns the size of the queue.
The ThreadScheduler object defines how tasks are allocated to threads and in what order.
Helper class which provides the Collimation Length for SANS instruments.