Coverage Report

Created: 2024-11-21 15:53

/root/doris/be/src/common/multi_version.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/MultiVersion.h
19
// and modified by Doris
20
21
#pragma once
22
23
#include <atomic>
24
#include <memory>
25
26
/** Allow to store and read-only usage of an object in several threads,
27
  *  and to atomically replace an object in another thread.
28
  * The replacement is atomic and reading threads can work with different versions of an object.
29
  *
30
  * Usage:
31
  *  MultiVersion<T> x;
32
  * - on data update:
33
  *  x.set(new value);
34
  * - on read-only usage:
35
  * {
36
  *     MultiVersion<T>::Version current_version = x.get();
37
  *     // use *current_version
38
  * }   // now we finish own current version; if the version is outdated and no one else is using it - it will be destroyed.
39
  *
40
  * All methods are thread-safe.
41
  */
42
template <typename T>
43
class MultiVersion {
44
public:
45
    /// Version of object for usage. shared_ptr manage lifetime of version.
46
    using Version = std::shared_ptr<const T>;
47
48
    /// Default initialization - by nullptr.
49
5
    MultiVersion() = default;
50
51
1
    explicit MultiVersion(std::unique_ptr<const T>&& value) : current_version(std::move(value)) {}
52
53
    /// Obtain current version for read-only usage. Returns shared_ptr, that manages lifetime of version.
54
19
    Version get() const { return std::atomic_load(&current_version); }
Unexecuted instantiation: _ZNK12MultiVersionIN5doris14RuntimeProfileEE3getEv
_ZNK12MultiVersionIN5doris11SymbolIndexEE3getEv
Line
Count
Source
54
19
    Version get() const { return std::atomic_load(&current_version); }
55
56
    /// TODO: replace atomic_load/store() on shared_ptr (which is deprecated as of C++20) by C++20 std::atomic<std::shared_ptr>.
57
    /// Clang 15 currently does not support it.
58
59
    /// Update an object with new version.
60
4
    void set(std::unique_ptr<const T>&& value) {
61
4
        std::atomic_store(&current_version, Version {std::move(value)});
62
4
    }
_ZN12MultiVersionIN5doris14RuntimeProfileEE3setEOSt10unique_ptrIKS1_St14default_deleteIS4_EE
Line
Count
Source
60
4
    void set(std::unique_ptr<const T>&& value) {
61
4
        std::atomic_store(&current_version, Version {std::move(value)});
62
4
    }
Unexecuted instantiation: _ZN12MultiVersionIN5doris11SymbolIndexEE3setEOSt10unique_ptrIKS1_St14default_deleteIS4_EE
63
64
private:
65
    Version current_version;
66
};