1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parallel/concurrentqueue.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#ifndef DUCKDB_NO_THREADS
12#include "concurrentqueue.h"
13#else
14
15#include <cstddef>
16#include <deque>
17#include <queue>
18
19namespace duckdb_moodycamel {
20
21template <typename T>
22class ConcurrentQueue;
23template <typename T>
24class BlockingConcurrentQueue;
25
26struct ProducerToken {
27 //! Constructor
28 template <typename T, typename Traits>
29 explicit ProducerToken(ConcurrentQueue<T> &);
30 //! Constructor
31 template <typename T, typename Traits>
32 explicit ProducerToken(BlockingConcurrentQueue<T> &);
33 //! Constructor
34 ProducerToken(ProducerToken &&) {
35 }
36 //! Is valid token?
37 inline bool valid() const {
38 return true;
39 }
40};
41
42template <typename T>
43class ConcurrentQueue {
44private:
45 //! The queue
46 std::queue<T, std::deque<T>> q;
47
48public:
49 //! Constructor
50 ConcurrentQueue() = default;
51 //! Constructor
52 explicit ConcurrentQueue(size_t capacity) {
53 q.reserve(capacity);
54 }
55
56 //! Enqueue item
57 template <typename U>
58 bool enqueue(U &&item) {
59 q.push(std::forward<U>(item));
60 return true;
61 }
62 //! Try to dequeue an item
63 bool try_dequeue(T &item) {
64 if (q.empty()) {
65 return false;
66 }
67 item = std::move(q.front());
68 q.pop();
69 return true;
70 }
71};
72
73} // namespace duckdb_moodycamel
74
75#endif
76