Merge pull request #46057 from k-morozov/refactoring/multiversion-atomic-functions

MultiVersion: change mutex to lock-free
This commit is contained in:
Robert Schulze 2023-02-06 11:55:27 +01:00 committed by GitHub
commit fa131edba5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,6 @@
#pragma once #pragma once
#include <atomic>
#include <mutex> #include <mutex>
#include <memory> #include <memory>
#include <base/defines.h> #include <base/defines.h>
@ -39,19 +40,15 @@ public:
/// Obtain current version for read-only usage. Returns shared_ptr, that manages lifetime of version. /// Obtain current version for read-only usage. Returns shared_ptr, that manages lifetime of version.
Version get() const Version get() const
{ {
/// NOTE: is it possible to lock-free replace of shared_ptr? return std::atomic_load(&current_version);
std::lock_guard lock(mutex);
return current_version;
} }
/// Update an object with new version. /// Update an object with new version.
void set(std::unique_ptr<const T> && value) void set(std::unique_ptr<const T> && value)
{ {
std::lock_guard lock(mutex); std::atomic_store(&current_version, Version{std::move(value)});
current_version = std::move(value);
} }
private: private:
Version current_version TSA_GUARDED_BY(mutex); Version current_version;
mutable std::mutex mutex;
}; };