diff options
Diffstat (limited to 'src/3rdParty/efsw/platform/posix/ThreadImpl.cpp')
-rwxr-xr-x | src/3rdParty/efsw/platform/posix/ThreadImpl.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
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 @@ | |||
1 | #include <efsw/Thread.hpp> | ||
2 | #include <efsw/platform/posix/ThreadImpl.hpp> | ||
3 | |||
4 | #if defined( EFSW_PLATFORM_POSIX ) | ||
5 | |||
6 | #include <cassert> | ||
7 | #include <efsw/Debug.hpp> | ||
8 | #include <iostream> | ||
9 | |||
10 | namespace efsw { namespace Platform { | ||
11 | |||
12 | ThreadImpl::ThreadImpl( Thread* owner ) : mIsActive( false ) { | ||
13 | mIsActive = pthread_create( &mThread, NULL, &ThreadImpl::entryPoint, owner ) == 0; | ||
14 | |||
15 | if ( !mIsActive ) { | ||
16 | efDEBUG( "Failed to create thread\n" ); | ||
17 | } | ||
18 | } | ||
19 | |||
20 | void ThreadImpl::wait() { | ||
21 | // Wait for the thread to finish, no timeout | ||
22 | if ( mIsActive ) { | ||
23 | assert( pthread_equal( pthread_self(), mThread ) == 0 ); | ||
24 | |||
25 | pthread_join( mThread, NULL ); | ||
26 | |||
27 | mIsActive = false; // Reset the thread state | ||
28 | } | ||
29 | } | ||
30 | |||
31 | void ThreadImpl::terminate() { | ||
32 | if ( mIsActive ) { | ||
33 | #if !defined( __ANDROID__ ) && !defined( ANDROID ) | ||
34 | pthread_cancel( mThread ); | ||
35 | #else | ||
36 | pthread_kill( mThread, SIGUSR1 ); | ||
37 | #endif | ||
38 | |||
39 | mIsActive = false; | ||
40 | } | ||
41 | } | ||
42 | |||
43 | void* ThreadImpl::entryPoint( void* userData ) { | ||
44 | // The Thread instance is stored in the user data | ||
45 | Thread* owner = static_cast<Thread*>( userData ); | ||
46 | |||
47 | // Tell the thread to handle cancel requests immediatly | ||
48 | #ifdef PTHREAD_CANCEL_ASYNCHRONOUS | ||
49 | pthread_setcanceltype( PTHREAD_CANCEL_ASYNCHRONOUS, NULL ); | ||
50 | #endif | ||
51 | |||
52 | // Forward to the owner | ||
53 | owner->run(); | ||
54 | |||
55 | return NULL; | ||
56 | } | ||
57 | |||
58 | }} // namespace efsw::Platform | ||
59 | |||
60 | #endif | ||