From 0606d170400a29b4b1b7e4417f63de0b03c17488 Mon Sep 17 00:00:00 2001 From: Andrii Grynenko Date: Wed, 12 Oct 2016 18:30:12 -0700 Subject: [PATCH] Add update-mutex into Observable Reviewed By: yfeldblum Differential Revision: D4013256 fbshipit-source-id: 771f9becfa9e7676439209dfbf4a746c110d629d --- folly/experimental/observer/Observable-inl.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/folly/experimental/observer/Observable-inl.h b/folly/experimental/observer/Observable-inl.h index 9bd3c83d..95dfb484 100644 --- a/folly/experimental/observer/Observable-inl.h +++ b/folly/experimental/observer/Observable-inl.h @@ -40,6 +40,14 @@ class ObserverCreator::Context { } void update() { + // This mutex ensures there's no race condition between initial update() + // call and update() calls from the subsciption callback. + // + // Additionally it helps avoid races between two different subscription + // callbacks (getting new value from observable and storing it into value_ + // is not atomic). + std::lock_guard lg(updateMutex_); + { auto newValue = Traits::get(observable_); if (!newValue) { @@ -69,6 +77,8 @@ class ObserverCreator::Context { observer_detail::Core::WeakPtr coreWeak_; Observable observable_; + + std::mutex updateMutex_; }; template -- 2.34.1