Skip to content

Commit

Permalink
AutoPacket::Get and AutoPacket::HasSubscribers need to be synchronized
Browse files Browse the repository at this point in the history
  • Loading branch information
codemercenary committed Jul 30, 2014
1 parent c8813e6 commit 69f2300
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
8 changes: 3 additions & 5 deletions autowiring/AutoPacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ class AutoPacket:
/// </summary>
template<class T>
bool Get(const T*& out) const {
std::lock_guard<std::mutex> lk(m_lock);
static_assert(!std::is_same<T, AutoPacket>::value, "Cannot decorate a packet with another packet");

auto q = m_decorations.find(typeid(T));
Expand Down Expand Up @@ -178,6 +179,7 @@ class AutoPacket:
/// </summary>
template<class T>
bool Get(const std::shared_ptr<T>*& out) const {
std::lock_guard<std::mutex> lk(m_lock);
auto q = m_decorations.find(typeid(T));
if(q != m_decorations.end() && q->second.satisfied) {
auto& disposition = q->second;
Expand Down Expand Up @@ -216,13 +218,9 @@ class AutoPacket:
throw std::runtime_error("Cannot decorate this packet with type T, the requested decoration already exists");
if(entry.isCheckedOut)
throw std::runtime_error("Cannot check out this decoration, it's already checked out elsewhere");

entry.isCheckedOut = true;
entry.wasCheckedOut = true;
}

// Have to find the entry _again_ within the context of a lock and satisfy it here:
{
std::lock_guard<std::mutex> lk(m_lock);
m_decorations[typeid(type)].m_decoration = ptr;
}

Expand Down
1 change: 1 addition & 0 deletions src/autowiring/AutoPacket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,6 @@ void AutoPacket::Initialize(void) {
}

bool AutoPacket::HasSubscribers(const std::type_info& ti) const {
std::lock_guard<std::mutex> lk(m_lock);
return m_decorations.count(ti) != 0;
}

0 comments on commit 69f2300

Please sign in to comment.