summaryrefslogtreecommitdiffstats
path: root/src/common/QueueRing.h
blob: af5c47be25f9d6d077a82043a0b851fa62e6c13a (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef QUEUE_RING_H
#define QUEUE_RING_H

#include "common/ceph_mutex.h"

#include <list>
#include <atomic>
#include <vector>

template <class T>
class QueueRing {
  struct QueueBucket {
    ceph::mutex lock = ceph::make_mutex("QueueRing::QueueBucket::lock");
    ceph::condition_variable cond;
    typename std::list<T> entries;

    QueueBucket() {}
    QueueBucket(const QueueBucket& rhs) {
      entries = rhs.entries;
    }

    void enqueue(const T& entry) {
      lock.lock();
      if (entries.empty()) {
        cond.notify_all();
      }
      entries.push_back(entry);
      lock.unlock();
    }

    void dequeue(T *entry) {
      std::unique_lock l(lock);
      while (entries.empty()) {
        cond.wait(l);
      };
      ceph_assert(!entries.empty());
      *entry = entries.front();
      entries.pop_front();
    };
  };

  std::vector<QueueBucket> buckets;
  int num_buckets;

  std::atomic<int64_t> cur_read_bucket = { 0 };
  std::atomic<int64_t> cur_write_bucket = { 0 };

public:
  QueueRing(int n) : buckets(n), num_buckets(n) {
  }

  void enqueue(const T& entry) {
    buckets[++cur_write_bucket % num_buckets].enqueue(entry);
  };

  void dequeue(T *entry) {
    buckets[++cur_read_bucket % num_buckets].dequeue(entry);
  }
};

#endif