1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
//
// MIT License
// Copyright (c) 2020 Jonathan R. Madsen
// 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
// 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.
//
// ---------------------------------------------------------------
// Tasking class header
// Class Description:
// ---------------------------------------------------------------
// Author: Jonathan Madsen
// ---------------------------------------------------------------
#include "PTL/AutoLock.hh"
#include "PTL/Globals.hh"
#include "PTL/ThreadPool.hh"
#include "PTL/Threading.hh"
#include "PTL/Types.hh"
#include "PTL/VUserTaskQueue.hh"
#include <atomic>
#include <cassert>
#include <deque>
#include <list>
#include <memory>
#include <queue>
#include <stack>
namespace PTL
{
class VTask;
//======================================================================================//
class TaskSubQueue
{
public:
template <typename Tp>
using container = std::list<Tp>;
typedef std::shared_ptr<VTask> task_pointer;
typedef container<task_pointer> container_type;
typedef container_type::size_type size_type;
public:
TaskSubQueue(std::atomic_uintmax_t* _ntasks);
TaskSubQueue(const TaskSubQueue&);
~TaskSubQueue();
TaskSubQueue& operator=(const TaskSubQueue&) = delete;
public:
int GetId() const;
bool AcquireClaim();
void ReleaseClaim();
void PushTask(task_pointer&&) PTL_NO_SANITIZE_THREAD;
task_pointer PopTask(bool front = true) PTL_NO_SANITIZE_THREAD;
size_type size() const;
bool empty() const;
private:
// mutex
#if defined(PTL_USE_LOCKS)
Mutex m_mutex{};
#endif
// used internally to keep number of tasks
std::atomic<size_type> m_ntasks;
// for checking if being modified
std::atomic_bool m_available;
// used my master queue to keep track of number of tasks
std::atomic_uintmax_t* m_all_tasks;
// queue of tasks
container_type m_task_queue;
};
//======================================================================================//
inline TaskSubQueue::TaskSubQueue(std::atomic_uintmax_t* _ntasks)
: m_ntasks(0)
, m_available(true)
, m_all_tasks(_ntasks)
{}
//======================================================================================//
inline TaskSubQueue::TaskSubQueue(const TaskSubQueue& rhs)
: m_ntasks(0)
, m_available(true)
, m_all_tasks(rhs.m_all_tasks)
{}
//======================================================================================//
inline TaskSubQueue::~TaskSubQueue() {}
//======================================================================================//
inline bool
TaskSubQueue::AcquireClaim()
{
bool is_avail = m_available.load(std::memory_order_relaxed);
if(!is_avail)
return false;
return m_available.compare_exchange_strong(is_avail, false,
std::memory_order_relaxed);
}
//======================================================================================//
inline void
TaskSubQueue::ReleaseClaim()
{
// if(m_available.load(std::memory_order_relaxed))
// return;
m_available.store(true, std::memory_order_release);
}
//======================================================================================//
inline TaskSubQueue::size_type
TaskSubQueue::size() const
{
return m_ntasks.load();
}
//======================================================================================//
inline bool
TaskSubQueue::empty() const
{
return (m_ntasks.load() == 0);
}
//======================================================================================//
inline void
TaskSubQueue::PushTask(task_pointer&& task)
{
// no need to lock these if claim is acquired via atomic
assert(m_available.load(std::memory_order_relaxed) == false);
++m_ntasks;
#if defined(PTL_USE_LOCKS)
AutoLock lk{ m_mutex };
#endif
m_task_queue.emplace_front(std::move(task));
}
//======================================================================================//
inline TaskSubQueue::task_pointer
TaskSubQueue::PopTask(bool front)
{
// no need to lock -- claim is acquired via atomic
assert(m_available.load(std::memory_order_relaxed) == false);
if(m_ntasks.load() == 0)
return nullptr;
task_pointer _task{ nullptr };
if(front)
{
#if defined(PTL_USE_LOCKS)
AutoLock lk{ m_mutex };
#endif
_task = std::move(m_task_queue.front());
m_task_queue.pop_front();
}
else
{
#if defined(PTL_USE_LOCKS)
AutoLock lk{ m_mutex };
#endif
_task = std::move(m_task_queue.back());
m_task_queue.pop_back();
}
--m_ntasks;
return _task;
}
//======================================================================================//
} // namespace PTL
|