From 7225a7759cf8e6646b2796160bf84e4dfe2764e6 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 13 Dec 2023 00:10:05 -0500 Subject: [PATCH] Some thread interruption defensiveness. --- Thread.cc | 8 ++++---- Utility/ScopedState.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 4 deletions(-) create mode 100644 Utility/ScopedState.h diff --git a/Thread.cc b/Thread.cc index 82fcb1e..bb05a54 100644 --- a/Thread.cc +++ b/Thread.cc @@ -8,6 +8,7 @@ static_assert( __cplusplus > 2020'99 ); #include #include +#include #include "error.h" @@ -131,6 +132,7 @@ namespace Alepha::Hydrogen::detail::Thread_m while( impl().state == Opening ) { impl().var.wait( lock ); + assert( not impl().threadBlocked ); } if( impl().state == Open ) @@ -167,9 +169,9 @@ namespace Alepha::Hydrogen::detail::Thread_m { if( impl().state == Waiting ) { - impl().threadBlocked= true; while( impl().state == Waiting ) { + Utility::ScopedState state{ impl().threadBlocked }; impl().var.wait( lock ); } assert( not impl().threadBlocked ); @@ -179,9 +181,7 @@ namespace Alepha::Hydrogen::detail::Thread_m else if( impl().state == Synchronizing ) { impl().state= Opening; - const bool unblock= impl().threadBlocked; - impl().threadBlocked= false; - if( unblock ) + if( impl().threadBlocked ) { impl().var.notify_one(); diff --git a/Utility/ScopedState.h b/Utility/ScopedState.h new file mode 100644 index 0000000..e356187 --- /dev/null +++ b/Utility/ScopedState.h @@ -0,0 +1,28 @@ +static_assert( __cplusplus > 2020'00 ); + +#pragma once + +#include + +#include + +namespace Alepha::Hydrogen::Utility ::detail:: ScopedState_m +{ + inline namespace exports + { + class ScopedState : boost::noncopyable + { + private: + bool &state; + + public: + ~ScopedState() { state= false; } + explicit ScopedState( bool &state ) : state( state ) { state= true; } + }; + } +} + +namespace Alepha::Hydrogen::Utility::inline exports::inline ScopedState_m +{ + using namespace detail::ScopedState_m::exports; +}