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 | | #ifdef USE_LIBCPP |
27 | | #include "common/atomic_shared_ptr.h" |
28 | | #endif |
29 | | |
30 | | /** Allow to store and read-only usage of an object in several threads, |
31 | | * and to atomically replace an object in another thread. |
32 | | * The replacement is atomic and reading threads can work with different versions of an object. |
33 | | * |
34 | | * Usage: |
35 | | * MultiVersion<T> x; |
36 | | * - on data update: |
37 | | * x.set(new value); |
38 | | * - on read-only usage: |
39 | | * { |
40 | | * MultiVersion<T>::Version current_version = x.get(); |
41 | | * // use *current_version |
42 | | * } // now we finish own current version; if the version is outdated and no one else is using it - it will be destroyed. |
43 | | * |
44 | | * All methods are thread-safe. |
45 | | */ |
46 | | template <typename T> |
47 | | class MultiVersion { |
48 | | public: |
49 | | /// Version of object for usage. shared_ptr manage lifetime of version. |
50 | | using Version = std::shared_ptr<const T>; |
51 | | |
52 | | /// Default initialization - by nullptr. |
53 | 1.30M | MultiVersion() = default; |
54 | | |
55 | 7 | explicit MultiVersion(std::unique_ptr<const T>&& value) : current_version(std::move(value)) {} |
56 | | |
57 | | /// Obtain current version for read-only usage. Returns shared_ptr, that manages lifetime of version. |
58 | 172k | Version get() const { return current_version.load(); }_ZNK12MultiVersionIN5doris14RuntimeProfileEE3getEv Line | Count | Source | 58 | 16 | Version get() const { return current_version.load(); } |
_ZNK12MultiVersionIN5doris11SymbolIndexEE3getEv Line | Count | Source | 58 | 171k | Version get() const { return current_version.load(); } |
|
59 | | |
60 | | /// Update an object with new version. |
61 | 2.69k | void set(std::unique_ptr<const T>&& value) { |
62 | 2.69k | current_version.store(Version {std::move(value)}); |
63 | 2.69k | } _ZN12MultiVersionIN5doris14RuntimeProfileEE3setEOSt10unique_ptrIKS1_St14default_deleteIS4_EE Line | Count | Source | 61 | 2.69k | void set(std::unique_ptr<const T>&& value) { | 62 | 2.69k | current_version.store(Version {std::move(value)}); | 63 | 2.69k | } |
Unexecuted instantiation: _ZN12MultiVersionIN5doris11SymbolIndexEE3setEOSt10unique_ptrIKS1_St14default_deleteIS4_EE |
64 | | |
65 | | private: |
66 | | #ifdef USE_LIBCPP |
67 | | doris::atomic_shared_ptr<const T> current_version; |
68 | | #else |
69 | | std::atomic<Version> current_version; |
70 | | #endif |
71 | | }; |