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

[FIX #541] WConditionOneShot hanging

parent bf5c465a
......@@ -28,42 +28,25 @@ WConditionOneShot::WConditionOneShot()
: WCondition()
{
// initialize members
m_lock = std::unique_lock<std::shared_mutex>( m_mutex );
m_isDone = false;
}
WConditionOneShot::~WConditionOneShot()
{
// 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
{
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()
{
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
// because the notify was called multiple times
}
m_isDone = true;
WCondition::notify();
}
......@@ -25,6 +25,7 @@
#ifndef WCONDITIONONESHOT_H
#define WCONDITIONONESHOT_H
#include <atomic>
#include <shared_mutex>
#include <boost/thread.hpp>
......@@ -65,9 +66,9 @@ public:
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:
};
......
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