| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#ifndef DMLC_CONCURRENCY_H_ |
| 8 |
#define DMLC_CONCURRENCY_H_ |
| 9 |
|
| 10 |
#if DMLC_USE_CXX11 |
| 11 |
#include <atomic> |
| 12 |
#include <condition_variable> |
| 13 |
#include <deque> |
| 14 |
#include <mutex> |
| 15 |
#include <queue> |
| 16 |
#include <utility> |
| 17 |
#include <vector> |
| 18 |
|
| 19 |
#include "dmlc/base.h" |
| 20 |
|
| 21 |
namespace dmlc { |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
class Spinlock { |
| 27 |
public: |
| 28 |
#ifdef _MSC_VER |
| 29 |
Spinlock() { |
| 30 |
lock_.clear(); |
| 31 |
} |
| 32 |
#else |
| 33 |
#if defined(__clang__) |
| 34 |
|
| 35 |
|
| 36 |
#endif |
| 37 |
Spinlock() : lock_(ATOMIC_FLAG_INIT) {} |
| 38 |
#if defined(__clang__) |
| 39 |
|
| 40 |
#endif |
| 41 |
#endif |
| 42 |
~Spinlock() = default; |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
inline void lock() noexcept(true); |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
inline void unlock() noexcept(true); |
| 51 |
|
| 52 |
private: |
| 53 |
std::atomic_flag lock_; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
DISALLOW_COPY_AND_ASSIGN(Spinlock); |
| 58 |
}; |
| 59 |
|
| 60 |
|
| 61 |
enum class ConcurrentQueueType { |
| 62 |
|
| 63 |
kFIFO, |
| 64 |
|
| 65 |
kPriority |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
template <typename T, ConcurrentQueueType type = ConcurrentQueueType::kFIFO> |
| 72 |
class ConcurrentBlockingQueue { |
| 73 |
public: |
| 74 |
ConcurrentBlockingQueue(); |
| 75 |
~ConcurrentBlockingQueue() = default; |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
template <typename E> |
| 87 |
void Push(E &&e, int priority = 0); |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
template <typename E> |
| 101 |
void PushFront(E &&e, int priority = 0); |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
bool Pop(T *rv); |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
void SignalForKill(); |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
size_t Size(); |
| 122 |
|
| 123 |
private: |
| 124 |
struct Entry { |
| 125 |
T data; |
| 126 |
int priority; |
| 127 |
inline bool operator<(const Entry &b) const { |
| 128 |
return priority < b.priority; |
| 129 |
} |
| 130 |
}; |
| 131 |
|
| 132 |
std::mutex mutex_; |
| 133 |
std::condition_variable cv_; |
| 134 |
std::atomic<bool> exit_now_; |
| 135 |
int nwait_consumer_; |
| 136 |
|
| 137 |
std::vector<Entry> priority_queue_; |
| 138 |
|
| 139 |
std::deque<T> fifo_queue_; |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
DISALLOW_COPY_AND_ASSIGN(ConcurrentBlockingQueue); |
| 144 |
}; |
| 145 |
|
| 146 |
inline void Spinlock::lock() noexcept(true) { |
| 147 |
while (lock_.test_and_set(std::memory_order_acquire)) {} |
| 148 |
} |
| 149 |
|
| 150 |
inline void Spinlock::unlock() noexcept(true) { |
| 151 |
lock_.clear(std::memory_order_release); |
| 152 |
} |
| 153 |
|
| 154 |
template <typename T, ConcurrentQueueType type> |
| 155 |
ConcurrentBlockingQueue<T, type>::ConcurrentBlockingQueue() |
| 156 |
: exit_now_{false}, nwait_consumer_{0} {} |
| 157 |
|
| 158 |
template <typename T, ConcurrentQueueType type> |
| 159 |
template <typename E> |
| 160 |
void ConcurrentBlockingQueue<T, type>::Push(E &&e, int priority) { |
| 161 |
static_assert(std::is_same<typename std::remove_cv<typename std::remove_reference<E>::type>::type, |
| 162 |
T>::value, |
| 163 |
"Types must match."); |
| 164 |
bool notify; |
| 165 |
{ |
| 166 |
std::lock_guard<std::mutex> lock{mutex_}; |
| 167 |
if (type == ConcurrentQueueType::kFIFO) { |
| 168 |
fifo_queue_.emplace_back(std::forward<E>(e)); |
| 169 |
notify = nwait_consumer_ != 0; |
| 170 |
} else { |
| 171 |
Entry entry; |
| 172 |
entry.data = std::move(e); |
| 173 |
entry.priority = priority; |
| 174 |
priority_queue_.push_back(std::move(entry)); |
| 175 |
std::push_heap(priority_queue_.begin(), priority_queue_.end()); |
| 176 |
notify = nwait_consumer_ != 0; |
| 177 |
} |
| 178 |
} |
| 179 |
if (notify) { |
| 180 |
cv_.notify_one(); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
template <typename T, ConcurrentQueueType type> |
| 185 |
template <typename E> |
| 186 |
void ConcurrentBlockingQueue<T, type>::PushFront(E &&e, int priority) { |
| 187 |
static_assert(std::is_same<typename std::remove_cv<typename std::remove_reference<E>::type>::type, |
| 188 |
T>::value, |
| 189 |
"Types must match."); |
| 190 |
bool notify; |
| 191 |
{ |
| 192 |
std::lock_guard<std::mutex> lock{mutex_}; |
| 193 |
if (type == ConcurrentQueueType::kFIFO) { |
| 194 |
fifo_queue_.emplace_front(std::forward<E>(e)); |
| 195 |
notify = nwait_consumer_ != 0; |
| 196 |
} else { |
| 197 |
Entry entry; |
| 198 |
entry.data = std::move(e); |
| 199 |
entry.priority = priority; |
| 200 |
priority_queue_.push_back(std::move(entry)); |
| 201 |
std::push_heap(priority_queue_.begin(), priority_queue_.end()); |
| 202 |
notify = nwait_consumer_ != 0; |
| 203 |
} |
| 204 |
} |
| 205 |
if (notify) { |
| 206 |
cv_.notify_one(); |
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
template <typename T, ConcurrentQueueType type> |
| 211 |
bool ConcurrentBlockingQueue<T, type>::Pop(T *rv) { |
| 212 |
std::unique_lock<std::mutex> lock{mutex_}; |
| 213 |
if (type == ConcurrentQueueType::kFIFO) { |
| 214 |
++nwait_consumer_; |
| 215 |
cv_.wait(lock, [this] { return !fifo_queue_.empty() || exit_now_.load(); }); |
| 216 |
--nwait_consumer_; |
| 217 |
if (!exit_now_.load()) { |
| 218 |
*rv = std::move(fifo_queue_.front()); |
| 219 |
fifo_queue_.pop_front(); |
| 220 |
return true; |
| 221 |
} else { |
| 222 |
return false; |
| 223 |
} |
| 224 |
} else { |
| 225 |
++nwait_consumer_; |
| 226 |
cv_.wait(lock, [this] { return !priority_queue_.empty() || exit_now_.load(); }); |
| 227 |
--nwait_consumer_; |
| 228 |
if (!exit_now_.load()) { |
| 229 |
std::pop_heap(priority_queue_.begin(), priority_queue_.end()); |
| 230 |
*rv = std::move(priority_queue_.back().data); |
| 231 |
priority_queue_.pop_back(); |
| 232 |
return true; |
| 233 |
} else { |
| 234 |
return false; |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
template <typename T, ConcurrentQueueType type> |
| 240 |
void ConcurrentBlockingQueue<T, type>::SignalForKill() { |
| 241 |
{ |
| 242 |
std::lock_guard<std::mutex> lock{mutex_}; |
| 243 |
exit_now_.store(true); |
| 244 |
} |
| 245 |
cv_.notify_all(); |
| 246 |
} |
| 247 |
|
| 248 |
template <typename T, ConcurrentQueueType type> |
| 249 |
size_t ConcurrentBlockingQueue<T, type>::Size() { |
| 250 |
std::lock_guard<std::mutex> lock{mutex_}; |
| 251 |
if (type == ConcurrentQueueType::kFIFO) { |
| 252 |
return fifo_queue_.size(); |
| 253 |
} else { |
| 254 |
return priority_queue_.size(); |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
#endif |
| 259 |
#endif |