aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/util/generation_hold_list.h
blob: 5d150c8a0158622343a306bd976bda5162dd601d (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright Vespa.ai. 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 {
            if constexpr (track_bytes_held) {
                return elem->byte_size();
            }
            return 0;
        }
    };

    struct NoopFunc { void operator()(const T&){} };

    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);

    template<typename Func>
    void reclaim_internal(generation_t oldest_used_gen, Func callback);

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.
     * The callback function is called for each data element reclaimed.
     **/
    template<typename Func>
    void reclaim(generation_t oldest_used_gen, Func callback) {
        if (!_phase_2_list.empty() && (_phase_2_list.front().gen < oldest_used_gen)) {
            reclaim_internal(oldest_used_gen, callback);
        }
    }

    void reclaim(generation_t oldest_used_gen) {
        reclaim(oldest_used_gen, NoopFunc());
    }

    /**
     * Reclaim all data elements from this hold list.
     */
    void reclaim_all();

    /**
     * Reclaim all data elements from this hold list.
     * The callback function is called for all data elements reclaimed.
     */
    template<typename Func>
    void reclaim_all(Func callback);

    size_t get_held_bytes() const { return _held_bytes.load(std::memory_order_relaxed); }

    // Static size of _phase_2_list might depend on std::deque implementation
    static constexpr size_t sizeof_phase_2_list = sizeof(ElemWithGenList);
};

}