blob: b2f90934e8483a017ee059c4bb4fa2022f8e302d (
plain) (
blame)
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once
#include "generationhandler.h"
#include <atomic>
#include <deque>
#include <vector>
namespace vespalib {
/**
* Class used to hold data elements until they can be safely reclaimed when they are no longer accessed by readers.
*
* This class must be used in accordance with a GenerationHandler.
*/
template <typename T, bool track_bytes_held, bool use_deque>
class GenerationHoldList {
private:
using generation_t = vespalib::GenerationHandler::generation_t;
struct ElemWithGen {
T elem;
generation_t gen;
ElemWithGen(T elem_in, generation_t gen_in)
: elem(std::move(elem_in)),
gen(gen_in)
{}
size_t byte_size() const { return elem->byte_size(); }
};
using ElemList = std::vector<T>;
using ElemWithGenList = std::conditional_t<use_deque,
std::deque<ElemWithGen>,
std::vector<ElemWithGen>>;
ElemList _phase_1_list;
ElemWithGenList _phase_2_list;
std::atomic<size_t> _held_bytes;
/**
* Transfer elements from phase 1 to phase 2 list, assigning the current generation.
*/
void assign_generation_internal(generation_t current_gen);
void reclaim_internal(generation_t oldest_used_gen);
public:
GenerationHoldList();
~GenerationHoldList();
/**
* Insert the given data element on this hold list.
*/
void insert(T data);
/**
* Assign the current generation to all data elements inserted on the hold list
* since the last time this function was called.
*/
void assign_generation(generation_t current_gen) {
if (!_phase_1_list.empty()) {
assign_generation_internal(current_gen);
}
}
/**
* Reclaim all data elements where the assigned generation < oldest used generation.
**/
void reclaim(generation_t oldest_used_gen) {
if (!_phase_2_list.empty() && (_phase_2_list.front().gen < oldest_used_gen)) {
reclaim_internal(oldest_used_gen);
}
}
/**
* Reclaim all data elements from this hold list.
*/
void reclaim_all();
size_t get_held_bytes() const { return _held_bytes.load(std::memory_order_relaxed); }
};
}
|