fix(Event): Event data race #3629

This commit is contained in:
Alex Fabijanic
2022-06-02 11:40:40 -05:00
parent db0e012f3f
commit fbdb6120aa
3 changed files with 12 additions and 8 deletions

View File

@@ -22,6 +22,7 @@
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include <pthread.h> #include <pthread.h>
#include <errno.h> #include <errno.h>
#include <atomic>
namespace Poco { namespace Poco {
@@ -38,10 +39,10 @@ protected:
void resetImpl(); void resetImpl();
private: private:
bool _auto; bool _auto;
volatile bool _state; std::atomic<bool> _state;
pthread_mutex_t _mutex; pthread_mutex_t _mutex;
pthread_cond_t _cond; pthread_cond_t _cond;
}; };
@@ -50,7 +51,7 @@ private:
// //
inline void EventImpl::setImpl() inline void EventImpl::setImpl()
{ {
if (pthread_mutex_lock(&_mutex)) if (pthread_mutex_lock(&_mutex))
throw SystemException("cannot signal event (lock)"); throw SystemException("cannot signal event (lock)");
_state = true; _state = true;
if (pthread_cond_broadcast(&_cond)) if (pthread_cond_broadcast(&_cond))

View File

@@ -21,7 +21,7 @@
#include "Poco/Foundation.h" #include "Poco/Foundation.h"
#include "Poco/Exception.h" #include "Poco/Exception.h"
#include "Poco/UnWindows.h" #include "Poco/UnWindows.h"
#include <atomic>
namespace Poco { namespace Poco {
@@ -37,7 +37,7 @@ protected:
void resetImpl(); void resetImpl();
private: private:
HANDLE _event; std::atomic<HANDLE> _event;
}; };

View File

@@ -92,7 +92,10 @@ EventImpl::EventImpl(bool autoReset): _auto(autoReset), _state(false)
EventImpl::~EventImpl() EventImpl::~EventImpl()
{ {
pthread_cond_destroy(&_cond); if (0 == pthread_mutex_lock(&_mutex))
pthread_cond_destroy(&_cond);
else poco_unexpected();
pthread_mutex_unlock(&_mutex);
pthread_mutex_destroy(&_mutex); pthread_mutex_destroy(&_mutex);
} }