aboutsummaryrefslogtreecommitdiff
path: root/include/cru/common/concurrent/ConcurrentQueue.h
blob: 4f649a41aa476e7ea9bd40e09820f3eb22a81032 (plain)
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
#pragma once
#include <condition_variable>
#include <mutex>
#include <optional>
#include <utility>

namespace cru::concurrent {
namespace details {
template <typename T>
struct ConcurrentQueueNode {
  ConcurrentQueueNode(T&& value, ConcurrentQueueNode* next = nullptr)
      : value(std::move(value)), next(next) {}

  T value;
  ConcurrentQueueNode* next;
};
}  // namespace details

template <typename T>
class ConcurrentQueue {
 public:
  ConcurrentQueue() {}

  ConcurrentQueue(const ConcurrentQueue&) = delete;
  ConcurrentQueue& operator=(const ConcurrentQueue&) = delete;

  ConcurrentQueue(ConcurrentQueue&& other)
      : head_(other.head_),
        tail_(other.tail_),
        mutex_(std::move(other.mutex_)),
        condition_variable_(std::move(other.condition_variable_)) {
    other.head_ = nullptr;
    other.tail_ = nullptr;
  }

  ConcurrentQueue& operator=(ConcurrentQueue&& other) {
    if (this != &other) {
      head_ = other.head_;
      tail_ = other.tail_;
      mutex_ = std::move(other.mutex_);
      condition_variable_ = std::move(other.condition_variable_);
      other.head_ = nullptr;
      other.tail_ = nullptr;
      return *this;
    }
    return *this;
  }

  ~ConcurrentQueue() {
    if (head_) {
      auto node = head_;
      while (node) {
        auto next = node->next;
        delete node;
        node = next;
      }
    }
  }

 public:
  void Push(T&& value) {
    std::unique_lock<std::mutex> lock(mutex_);
    if (head_ == nullptr) {
      head_ = tail_ = new details::ConcurrentQueueNode<T>(std::move(value));
      condition_variable_.notify_one();
    } else {
      tail_->next = new details::ConcurrentQueueNode<T>(std::move(value));
      tail_ = tail_->next;
    }
  }

  T Pull() {
    std::unique_lock<std::mutex> lock(mutex_);
    if (head_ == nullptr) {
      condition_variable_.wait(lock);
    }
    assert(head_ != nullptr);
    auto value = std::move(head_->value);
    auto next = head_->next;
    delete head_;
    head_ = next;
    if (next == nullptr) {
      tail_ = nullptr;
    }
    return value;
  }

  std::optional<T> Poll() {
    std::unique_lock<std::mutex> lock(mutex_);
    if (head_ == nullptr) {
      return std::nullopt;
    }
    auto value = std::move(head_->value);
    auto next = head_->next;
    delete head_;
    head_ = next;
    if (next == nullptr) {
      tail_ = nullptr;
    }
    return value;
  }

 private:
  details::ConcurrentQueueNode<T>* head_ = nullptr;
  details::ConcurrentQueueNode<T>* tail_ = nullptr;

  std::mutex mutex_;
  std::condition_variable condition_variable_;
};
}  // namespace cru::concurrent