From: Andrii Grynenko Date: Thu, 13 Oct 2016 01:30:12 +0000 (-0700) Subject: Add update-mutex into Observable X-Git-Tag: v2016.10.17.00~8 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=0606d170400a29b4b1b7e4417f63de0b03c17488;p=folly.git Add update-mutex into Observable Reviewed By: yfeldblum Differential Revision: D4013256 fbshipit-source-id: 771f9becfa9e7676439209dfbf4a746c110d629d --- 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