1
0
forked from Alepha/Alepha

Add a thread primitive which has support for notifications.

Now you can send an `Alepha::Notification` into a thread as an
interrupt.  You have to use the Alepha threading primitives.
This commit is contained in:
2022-10-17 22:51:39 -04:00
parent 0a1ecce9ec
commit b1d83a4bf0
3 changed files with 248 additions and 0 deletions

10
Thread.test/Makefile Normal file
View File

@ -0,0 +1,10 @@
CXXFLAGS+= -std=c++2a -I ../
CXXFLAGS+= -g -O0
CXX=clang++-12
CXXFLAGS+= -Wno-inline-namespace-reopened-noninline
CXXFLAGS+= -Wno-unused-comparison
LDLIBS+= -lboost_thread -lpthread
all: thread

70
Thread.test/thread.cc Normal file
View File

@ -0,0 +1,70 @@
static_assert( __cplusplus > 2020'00 );
#include <Alepha/Thread.h>
#include <type_traits>
#include <Alepha/Testing/test.h>
#include <Alepha/Testing/TableTest.h>
#include <Alepha/Utility/evaluation.h>
namespace
{
using Alepha::exports::types::argcnt_t;
using Alepha::exports::types::argvec_t;
}
int
main( const argcnt_t argcnt, const argvec_t argvec )
{
return Alepha::Testing::runAllTests( argcnt, argvec );
}
namespace
{
namespace util= Alepha::Utility;
using namespace Alepha::Testing::exports;
using MyNotification= Alepha::create_exception< struct my_notification, Alepha::Notification >;
auto tests= Alepha::Utility::enroll <=[]
{
"smoke"_test <=[] () -> bool
{
std::cerr << "Smoke started..." << std::endl;
Alepha::Mutex access;
Alepha::ConditionVariable cv;
auto threadMain= [&]
{
try
{
Alepha::unique_lock lock( access );
std::cerr << "Child thread started..." << std::endl;
cv.wait( lock );
std::cerr << "Child thread awoken illegally!" << std::endl;
}
catch( const boost::thread_interrupted & )
{
std::cerr << "SHIT! We didn't get intercepted!" << std::endl;
throw;
}
catch( const MyNotification &n )
{
std::cerr << "I caught it: " << n.message() << "!" << std::endl;
throw;
}
};
access.lock();
std::cerr << "Launching child thread..." << std::endl;
Alepha::Thread thr( threadMain );
std::cerr << "Child thread now launched..." << std::endl;
::sleep( 1 );
access.unlock();
thr.interrupt( Alepha::build_exception< MyNotification >( "My message" ) );
thr.join();
return true;
};
};
}