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