Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions Rx/v2/src/rxcpp/subjects/rx-behavior.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class behavior_observer : public detail::multicast_observer<T>

class behavior_observer_state : public std::enable_shared_from_this<behavior_observer_state>
{
mutable std::mutex lock;
mutable std::recursive_mutex lock;
mutable T value;

public:
Expand All @@ -31,13 +31,19 @@ class behavior_observer : public detail::multicast_observer<T>
}

void reset(T v) const {
std::unique_lock<std::mutex> guard(lock);
std::lock_guard<std::recursive_mutex> guard(lock);
value = std::move(v);
}

T get() const {
std::unique_lock<std::mutex> guard(lock);
std::lock_guard<std::recursive_mutex> guard(lock);
return value;
}

std::recursive_mutex& get_lock() const
{
return lock;
}
};

std::shared_ptr<behavior_observer_state> state;
Expand All @@ -62,6 +68,11 @@ class behavior_observer : public detail::multicast_observer<T>
state->reset(v);
base_type::on_next(std::move(v));
}

std::recursive_mutex& get_state_lock() const
{
return state->get_lock();
}
};

}
Expand Down Expand Up @@ -92,10 +103,11 @@ class behavior
observable<T> get_observable() const {
auto keepAlive = s;
return make_observable_dynamic<T>([=](subscriber<T> o){
std::lock_guard<std::recursive_mutex> guard(keepAlive.get_state_lock());
if (keepAlive.get_subscription().is_subscribed()) {
o.on_next(get_value());
o.on_next(keepAlive.get_value());
}
keepAlive.add(s.get_subscriber(), std::move(o));
keepAlive.add(keepAlive.get_subscriber(), std::move(o));
});
}
};
Expand Down