Commit f1c0c3c1 authored by Pranjal Raihan's avatar Pranjal Raihan Committed by Facebook GitHub Bot

Use SharedMutex instead of MicroLock in AtomicObserver

Summary:
The initialization of `folly::MicroLock` *looks* correct. MSAN instrumentation probably needs to be disabled in its implementation.

Regardless, the main point of using `folly::MicroLock` was its low memory usage (1 byte). I'm now changing it to `folly::SharedMutex` (4 bytes) which will be the same size due to alignment. Although `folly::SharedMutex` is not ideal for write-only locks, we don't care too much about write performance here.

Differential Revision: D25487925

fbshipit-source-id: 4c30650cf22dafbdaa9fea0c2252d29caf477d54
parent 5e2a41e9
......@@ -110,9 +110,7 @@ Observer<T> makeStaticObserver(std::shared_ptr<T> value) {
template <typename T>
AtomicObserver<T>::AtomicObserver(Observer<T> observer)
: observer_(std::move(observer)) {
refreshLock_.init();
}
: observer_(std::move(observer)) {}
template <typename T>
AtomicObserver<T>::AtomicObserver(const AtomicObserver<T>& other)
......@@ -145,7 +143,7 @@ template <typename T>
T AtomicObserver<T>::get() const {
auto version = cachedVersion_.load(std::memory_order_acquire);
if (UNLIKELY(observer_.needRefresh(version))) {
std::lock_guard<folly::MicroLock> guard{refreshLock_};
folly::SharedMutex::WriteHolder guard{refreshLock_};
version = cachedVersion_.load(std::memory_order_acquire);
if (LIKELY(observer_.needRefresh(version))) {
auto snapshot = *observer_;
......
......@@ -16,7 +16,7 @@
#pragma once
#include <folly/MicroLock.h>
#include <folly/SharedMutex.h>
#include <folly/ThreadLocal.h>
#include <folly/experimental/ReadMostlySharedPtr.h>
#include <folly/experimental/observer/Observer-pre.h>
......@@ -271,7 +271,7 @@ class AtomicObserver {
private:
mutable std::atomic<T> cachedValue_{};
mutable std::atomic<size_t> cachedVersion_{};
mutable folly::MicroLock refreshLock_;
mutable folly::SharedMutex refreshLock_;
Observer<T> observer_;
};
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment