From 94f8330613877b3582d32bd11abd83a97b4399ad Mon Sep 17 00:00:00 2001 From: Li Jin Date: Tue, 15 Nov 2022 17:23:46 +0800 Subject: adding -w option to Yuescript tool. --- src/3rdParty/efsw/platform/posix/ThreadImpl.cpp | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100755 src/3rdParty/efsw/platform/posix/ThreadImpl.cpp (limited to 'src/3rdParty/efsw/platform/posix/ThreadImpl.cpp') diff --git a/src/3rdParty/efsw/platform/posix/ThreadImpl.cpp b/src/3rdParty/efsw/platform/posix/ThreadImpl.cpp new file mode 100755 index 0000000..e0ae84f --- /dev/null +++ b/src/3rdParty/efsw/platform/posix/ThreadImpl.cpp @@ -0,0 +1,60 @@ +#include +#include + +#if defined( EFSW_PLATFORM_POSIX ) + +#include +#include +#include + +namespace efsw { namespace Platform { + +ThreadImpl::ThreadImpl( Thread* owner ) : mIsActive( false ) { + mIsActive = pthread_create( &mThread, NULL, &ThreadImpl::entryPoint, owner ) == 0; + + if ( !mIsActive ) { + efDEBUG( "Failed to create thread\n" ); + } +} + +void ThreadImpl::wait() { + // Wait for the thread to finish, no timeout + if ( mIsActive ) { + assert( pthread_equal( pthread_self(), mThread ) == 0 ); + + pthread_join( mThread, NULL ); + + mIsActive = false; // Reset the thread state + } +} + +void ThreadImpl::terminate() { + if ( mIsActive ) { +#if !defined( __ANDROID__ ) && !defined( ANDROID ) + pthread_cancel( mThread ); +#else + pthread_kill( mThread, SIGUSR1 ); +#endif + + mIsActive = false; + } +} + +void* ThreadImpl::entryPoint( void* userData ) { + // The Thread instance is stored in the user data + Thread* owner = static_cast( userData ); + +// Tell the thread to handle cancel requests immediatly +#ifdef PTHREAD_CANCEL_ASYNCHRONOUS + pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL ); +#endif + + // Forward to the owner + owner->run(); + + return NULL; +} + +}} // namespace efsw::Platform + +#endif -- cgit v1.2.3-55-g6feb