| // Licensed to the Apache Software Foundation (ASF) under one |
| // or more contributor license agreements. See the NOTICE file |
| // distributed with this work for additional information |
| // regarding copyright ownership. The ASF licenses this file |
| // to you under the Apache License, Version 2.0 (the |
| // "License"); you may not use this file except in compliance |
| // with the License. You may obtain a copy of the License at |
| // |
| // http://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, |
| // software distributed under the License is distributed on an |
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| // KIND, either express or implied. See the License for the |
| // specific language governing permissions and limitations |
| // under the License. |
| // This file is copied from |
| // https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/MultiVersion.h |
| // and modified by Doris |
| |
| #pragma once |
| |
| #include <atomic> |
| #include <memory> |
| |
| /** Allow to store and read-only usage of an object in several threads, |
| * and to atomically replace an object in another thread. |
| * The replacement is atomic and reading threads can work with different versions of an object. |
| * |
| * Usage: |
| * MultiVersion<T> x; |
| * - on data update: |
| * x.set(new value); |
| * - on read-only usage: |
| * { |
| * MultiVersion<T>::Version current_version = x.get(); |
| * // use *current_version |
| * } // now we finish own current version; if the version is outdated and no one else is using it - it will be destroyed. |
| * |
| * All methods are thread-safe. |
| */ |
| template <typename T> |
| class MultiVersion { |
| public: |
| /// Version of object for usage. shared_ptr manage lifetime of version. |
| using Version = std::shared_ptr<const T>; |
| |
| /// Default initialization - by nullptr. |
| MultiVersion() = default; |
| |
| explicit MultiVersion(std::unique_ptr<const T>&& value) : current_version(std::move(value)) {} |
| |
| /// Obtain current version for read-only usage. Returns shared_ptr, that manages lifetime of version. |
| Version get() const { return std::atomic_load(¤t_version); } |
| |
| /// TODO: replace atomic_load/store() on shared_ptr (which is deprecated as of C++20) by C++20 std::atomic<std::shared_ptr>. |
| /// Clang 15 currently does not support it. |
| |
| /// Update an object with new version. |
| void set(std::unique_ptr<const T>&& value) { |
| std::atomic_store(¤t_version, Version {std::move(value)}); |
| } |
| |
| private: |
| Version current_version; |
| }; |