Debian Codegraph @

src/dmlc-core/include/dmlc/concurrency.h

raw symbols SCIP
1 /*!
2 * Copyright (c) 2015 by Contributors
3 * \file concurrency.h
4 * \brief thread-safe data structures.
5 * \author Yutian Li
6 */
7 #ifndef DMLC_CONCURRENCY_H_
8 #define DMLC_CONCURRENCY_H_
9 // this code depends on c++11
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 * \brief Simple userspace spinlock implementation.
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 // defined(__clang__)
37 Spinlock() : lock_(ATOMIC_FLAG_INIT) {}
38 #if defined(__clang__)
39
40 #endif // defined(__clang__)
41 #endif
42 ~Spinlock() = default;
43 /*!
44 * \brief Acquire lock.
45 */
46 inline void lock() noexcept(true);
47 /*!
48 * \brief Release lock.
49 */
50 inline void unlock() noexcept(true);
51
52 private:
53 std::atomic_flag lock_;
54 /*!
55 * \brief Disable copy and move.
56 */
57 DISALLOW_COPY_AND_ASSIGN(Spinlock);
58 };
59
60 /*! \brief type of concurrent queue */
61 enum class ConcurrentQueueType {
62 /*! \brief FIFO queue */
63 kFIFO,
64 /*! \brief queue with priority */
65 kPriority
66 };
67
68 /*!
69 * \brief Cocurrent blocking queue.
70 */
71 template <typename T, ConcurrentQueueType type = ConcurrentQueueType::kFIFO>
72 class ConcurrentBlockingQueue {
73 public:
74 ConcurrentBlockingQueue();
75 ~ConcurrentBlockingQueue() = default;
76 /*!
77 * \brief Push element to the end of the queue.
78 * \param e Element to push into.
79 * \param priority the priority of the element, only used for priority queue.
80 * The higher the priority is, the better.
81 * \tparam E the element type
82 *
83 * It will copy or move the element into the queue, depending on the type of
84 * the parameter.
85 */
86 template <typename E>
87 void Push(E &&e, int priority = 0);
88
89 /*!
90 * \brief Push element to the front of the queue. Only works for FIFO queue.
91 * For priority queue it is the same as Push.
92 * \param e Element to push into.
93 * \param priority the priority of the element, only used for priority queue.
94 * The higher the priority is, the better.
95 * \tparam E the element type
96 *
97 * It will copy or move the element into the queue, depending on the type of
98 * the parameter.
99 */
100 template <typename E>
101 void PushFront(E &&e, int priority = 0);
102 /*!
103 * \brief Pop element from the queue.
104 * \param rv Element popped.
105 * \return On false, the queue is exiting.
106 *
107 * The element will be copied or moved into the object passed in.
108 */
109 bool Pop(T *rv);
110 /*!
111 * \brief Signal the queue for destruction.
112 *
113 * After calling this method, all blocking pop call to the queue will return
114 * false.
115 */
116 void SignalForKill();
117 /*!
118 * \brief Get the size of the queue.
119 * \return The size of the queue.
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 // a priority queue
137 std::vector<Entry> priority_queue_;
138 // a FIFO queue
139 std::deque<T> fifo_queue_;
140 /*!
141 * \brief Disable copy and move.
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 } // namespace dmlc
258 #endif // DMLC_USE_CXX11
259 #endif // DMLC_CONCURRENCY_H_