-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnut_platform_thread.cpp
More file actions
76 lines (59 loc) · 1.71 KB
/
nut_platform_thread.cpp
File metadata and controls
76 lines (59 loc) · 1.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "nut_platform_threads.hpp"
#ifdef NUT_CONFIG_WANT_THREAD_NAMING
#include "nut_platform_debugging.hpp"
#endif
namespace nut {
namespace platform {
Thread::Thread( const string& name, Callback callback, void* argument ):
thread_( nullptr ), id_( 0 ), name_( name ), argument_( argument ), callback_( callback )
{
}
DWORD Thread::threadProc( void* argument )
{
// TODO: catch exceptions
auto self = (Thread*)argument;
auto ret = self->callback_( self->run_, self->stop_, self->argument_ );
return ( ret ? EXIT_SUCCESS : EXIT_FAILURE );
}
bool Thread::start()
{
thread_ = CreateThread( nullptr, 0, threadProc, this, CREATE_SUSPENDED, &id_ );
if ( !thread_ )
return false;
#ifdef NUT_CONFIG_WANT_THREAD_NAMING
setDebuggerThreadName( thread_, name_ );
#endif
if ( ResumeThread( thread_ ) == -1 )
NUT_RUNTIME_EXCEPT( "Thread resume failed" );
HANDLE events[2] = { run_.get(), thread_ };
auto wait = WaitForMultipleObjects( 2, events, FALSE, INFINITE );
if ( wait == WAIT_OBJECT_0 )
return true;
if ( wait == WAIT_OBJECT_0 + 1 )
return false;
NUT_RUNTIME_EXCEPT( "Thread wait failed" );
return false;
}
void Thread::stop()
{
if ( thread_ )
{
stop_.set();
WaitForSingleObject( thread_, INFINITE );
}
stop_.reset();
run_.reset();
thread_ = nullptr;
id_ = 0;
}
bool Thread::waitFor( uint32_t milliseconds ) const
{
auto retval = WaitForSingleObject( thread_, milliseconds );
return ( retval == WAIT_TIMEOUT );
}
Thread::~Thread()
{
stop();
}
}
}