1
0
forked from Alepha/Alepha

Some thread interruption defensiveness.

This commit is contained in:
2023-12-13 00:10:05 -05:00
parent c04fc7be3c
commit 7225a7759c
2 changed files with 32 additions and 4 deletions

View File

@ -8,6 +8,7 @@ static_assert( __cplusplus > 2020'99 );
#include <Alepha/Utility/StaticValue.h> #include <Alepha/Utility/StaticValue.h>
#include <Alepha/Utility/evaluation_helpers.h> #include <Alepha/Utility/evaluation_helpers.h>
#include <Alepha/Utility/ScopedState.h>
#include "error.h" #include "error.h"
@ -131,6 +132,7 @@ namespace Alepha::Hydrogen::detail::Thread_m
while( impl().state == Opening ) while( impl().state == Opening )
{ {
impl().var.wait( lock ); impl().var.wait( lock );
assert( not impl().threadBlocked );
} }
if( impl().state == Open ) if( impl().state == Open )
@ -167,9 +169,9 @@ namespace Alepha::Hydrogen::detail::Thread_m
{ {
if( impl().state == Waiting ) if( impl().state == Waiting )
{ {
impl().threadBlocked= true;
while( impl().state == Waiting ) while( impl().state == Waiting )
{ {
Utility::ScopedState state{ impl().threadBlocked };
impl().var.wait( lock ); impl().var.wait( lock );
} }
assert( not impl().threadBlocked ); assert( not impl().threadBlocked );
@ -179,9 +181,7 @@ namespace Alepha::Hydrogen::detail::Thread_m
else if( impl().state == Synchronizing ) else if( impl().state == Synchronizing )
{ {
impl().state= Opening; impl().state= Opening;
const bool unblock= impl().threadBlocked; if( impl().threadBlocked )
impl().threadBlocked= false;
if( unblock )
{ {
impl().var.notify_one(); impl().var.notify_one();

28
Utility/ScopedState.h Normal file
View File

@ -0,0 +1,28 @@
static_assert( __cplusplus > 2020'00 );
#pragma once
#include <Alepha/Alepha.h>
#include <boost/noncopyable.hpp>
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;
}