summaryrefslogtreecommitdiffstats
path: root/lib/base/bulker.hpp
blob: 2c30dc38b60640a18c999e1737a727450128c406 (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
111
112
113
114
115
116
117
118
119
/* Icinga 2 | (c) 2022 Icinga GmbH | GPLv2+ */

#ifndef BULKER_H
#define BULKER_H

#include <boost/config.hpp>
#include <chrono>
#include <condition_variable>
#include <mutex>
#include <queue>
#include <utility>
#include <vector>

namespace icinga
{

/**
 * A queue which outputs the input as bulks of a defined size
 * or after a defined time, whichever is reached first
 *
 * @ingroup base
 */
template<class T>
class Bulker
{
private:
	typedef std::chrono::steady_clock Clock;

public:
	typedef std::vector<T> Container;
	typedef typename Container::size_type SizeType;
	typedef typename Clock::duration Duration;

	Bulker(SizeType bulkSize, Duration threshold)
		: m_BulkSize(bulkSize), m_Threshold(threshold), m_NextConsumption(NullTimePoint()) { }

	void ProduceOne(T needle);
	Container ConsumeMany();
	SizeType Size();

	inline SizeType GetBulkSize() const noexcept
	{
		return m_BulkSize;
	}

private:
	typedef std::chrono::time_point<Clock> TimePoint;

	static inline
	TimePoint NullTimePoint()
	{
		return TimePoint::min();
	}

	inline void UpdateNextConsumption()
	{
		m_NextConsumption = Clock::now() + m_Threshold;
	}

	const SizeType m_BulkSize;
	const Duration m_Threshold;

	std::mutex m_Mutex;
	std::condition_variable m_CV;
	std::queue<Container> m_Bulks;
	TimePoint m_NextConsumption;
};

template<class T>
void Bulker<T>::ProduceOne(T needle)
{
	std::unique_lock<std::mutex> lock (m_Mutex);

	if (m_Bulks.empty() || m_Bulks.back().size() == m_BulkSize) {
		m_Bulks.emplace();
	}

	m_Bulks.back().emplace_back(std::move(needle));

	if (m_Bulks.size() == 1u && m_Bulks.back().size() == m_BulkSize) {
		m_CV.notify_one();
	}
}

template<class T>
typename Bulker<T>::Container Bulker<T>::ConsumeMany()
{
	std::unique_lock<std::mutex> lock (m_Mutex);

	if (BOOST_UNLIKELY(m_NextConsumption == NullTimePoint())) {
		UpdateNextConsumption();
	}

	auto deadline (m_NextConsumption);

	m_CV.wait_until(lock, deadline, [this]() { return !m_Bulks.empty() && m_Bulks.front().size() == m_BulkSize; });
	UpdateNextConsumption();

	if (m_Bulks.empty()) {
		return Container();
	}

	auto haystack (std::move(m_Bulks.front()));

	m_Bulks.pop();
	return haystack;
}

template<class T>
typename Bulker<T>::SizeType Bulker<T>::Size()
{
	std::unique_lock<std::mutex> lock (m_Mutex);

	return m_Bulks.empty() ? 0 : (m_Bulks.size() - 1u) * m_BulkSize + m_Bulks.back().size();
}

}

#endif /* BULKER_H */