Commit 85143e83 authored by Robin Eschbach's avatar Robin Eschbach
Browse files

[FIX #541] WConditionOneShot hanging

parent bf5c465a
...@@ -28,42 +28,25 @@ WConditionOneShot::WConditionOneShot() ...@@ -28,42 +28,25 @@ WConditionOneShot::WConditionOneShot()
: WCondition() : WCondition()
{ {
// initialize members // initialize members
m_lock = std::unique_lock<std::shared_mutex>( m_mutex ); m_isDone = false;
} }
WConditionOneShot::~WConditionOneShot() WConditionOneShot::~WConditionOneShot()
{ {
// cleanup // cleanup
try
{
m_lock.unlock();
}
catch( const std::system_error &e )
{
// ignore this particular error since it is thrown when the lock is not locked anymore
}
} }
void WConditionOneShot::wait() const void WConditionOneShot::wait() const
{ {
if( m_lock.owns_lock() ) if( !m_isDone )
{ {
m_condition.wait( m_mutex ); std::unique_lock<std::shared_mutex> lock( m_mutex );
m_condition.wait( m_mutex, [this]{ return m_isDone.load(); } );
} }
} }
void WConditionOneShot::notify() void WConditionOneShot::notify()
{ {
try m_isDone = true;
{
m_lock.unlock();
}
catch( const std::system_error &e )
{
// ignore this particular error since it is thrown when the lock is not locked anymore
// because the notify was called multiple times
}
WCondition::notify(); WCondition::notify();
} }
...@@ -25,6 +25,7 @@ ...@@ -25,6 +25,7 @@
#ifndef WCONDITIONONESHOT_H #ifndef WCONDITIONONESHOT_H
#define WCONDITIONONESHOT_H #define WCONDITIONONESHOT_H
#include <atomic>
#include <shared_mutex> #include <shared_mutex>
#include <boost/thread.hpp> #include <boost/thread.hpp>
...@@ -65,9 +66,9 @@ public: ...@@ -65,9 +66,9 @@ public:
protected: protected:
/** /**
* Locked as long the condition was not fired. * Atomic bool whether this conditon has been done or not.
*/ */
std::unique_lock<std::shared_mutex> m_lock; std::atomic< bool > m_isDone;
private: private:
}; };
......
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