summaryrefslogtreecommitdiffstats
path: root/src/jaegertracing/opentelemetry-cpp/sdk/test/common/baseline_circular_buffer.h
blob: 398a4d0385680868da55f536710f915104685c3e (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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <cstdint>
#include <memory>
#include <mutex>
#include <vector>

#include "opentelemetry/version.h"

OPENTELEMETRY_BEGIN_NAMESPACE
namespace testing
{
/**
 * A locking circular buffer.
 *
 * Used as a baseline in benchmarking.
 */
template <class T>
class BaselineCircularBuffer
{
public:
  explicit BaselineCircularBuffer(size_t max_size) : data_{max_size} {}

  /**
   * Add an element to the circular buffer.
   * @param element the element to add
   * @return true if the element was added successfully
   */
  bool Add(std::unique_ptr<T> &element) noexcept { return this->Add(std::move(element)); }

  bool Add(std::unique_ptr<T> &&element) noexcept
  {
    std::lock_guard<std::mutex> lock_guard{mutex_};
    if (tail_ + data_.size() == head_)
    {
      return false;
    }
    data_[head_ % data_.size()] = std::move(element);
    head_ += 1;
    return true;
  }

  /**
   * Consume elements in the circular buffer.
   * @param f the callback to call for each element
   */
  template <class F>
  void Consume(F f) noexcept
  {
    std::lock_guard<std::mutex> lock_guard{mutex_};
    if (head_ == tail_)
    {
      return;
    }
    auto tail_index = tail_ % data_.size();
    auto head_index = head_ % data_.size();
    if (tail_index < head_index)
    {
      for (auto i = tail_index; i < head_index; ++i)
      {
        f(std::move(data_[i]));
      }
    }
    else
    {
      for (auto i = tail_index; i < data_.size(); ++i)
      {
        f(std::move(data_[i]));
      }
      for (auto i = 0ull; i < head_index; ++i)
      {
        f(std::move(data_[i]));
      }
    }
    tail_ = head_;
  }

private:
  std::mutex mutex_;
  uint64_t head_{0};
  uint64_t tail_{0};
  std::vector<std::unique_ptr<T>> data_;
};
}  // namespace testing
OPENTELEMETRY_END_NAMESPACE