Commit 0606d170 authored by Andrii Grynenko's avatar Andrii Grynenko Committed by Facebook Github Bot

Add update-mutex into Observable

Reviewed By: yfeldblum

Differential Revision: D4013256

fbshipit-source-id: 771f9becfa9e7676439209dfbf4a746c110d629d
parent cef27cd0
...@@ -40,6 +40,14 @@ class ObserverCreator<Observable, Traits>::Context { ...@@ -40,6 +40,14 @@ class ObserverCreator<Observable, Traits>::Context {
} }
void update() { 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<std::mutex> lg(updateMutex_);
{ {
auto newValue = Traits::get(observable_); auto newValue = Traits::get(observable_);
if (!newValue) { if (!newValue) {
...@@ -69,6 +77,8 @@ class ObserverCreator<Observable, Traits>::Context { ...@@ -69,6 +77,8 @@ class ObserverCreator<Observable, Traits>::Context {
observer_detail::Core::WeakPtr coreWeak_; observer_detail::Core::WeakPtr coreWeak_;
Observable observable_; Observable observable_;
std::mutex updateMutex_;
}; };
template <typename Observable, typename Traits> template <typename Observable, typename Traits>
......
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