-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathtbb_thread.cpp
More file actions
195 lines (177 loc) · 6.33 KB
/
tbb_thread.cpp
File metadata and controls
195 lines (177 loc) · 6.33 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
Copyright 2005-2014 Intel Corporation. All Rights Reserved.
This file is part of Threading Building Blocks. Threading Building Blocks is free software;
you can redistribute it and/or modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation. Threading Building Blocks is
distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details. You should have received a copy of
the GNU General Public License along with Threading Building Blocks; if not, write to the
Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
As a special exception, you may use this file as part of a free software library without
restriction. Specifically, if other files instantiate templates or use macros or inline
functions from this file, or you compile this file and link it with other files to produce
an executable, this file does not by itself cause the resulting executable to be covered
by the GNU General Public License. This exception does not however invalidate any other
reasons why the executable file might be covered by the GNU General Public License.
*/
#if _WIN32||_WIN64
#include <process.h> // _beginthreadex()
#endif
#include <errno.h>
#include "tbb_misc.h" // handle_win_error(), ThreadStackSize
#include "tbb/tbb_stddef.h"
#include "tbb/tbb_thread.h"
#include "tbb/tbb_allocator.h"
#include "governor.h" // default_num_threads()
#if __TBB_WIN8UI_SUPPORT
#include <thread>
#endif
namespace tbb {
namespace internal {
//! Allocate a closure
void* allocate_closure_v3( size_t size )
{
return allocate_via_handler_v3( size );
}
//! Free a closure allocated by allocate_closure_v3
void free_closure_v3( void *ptr )
{
deallocate_via_handler_v3( ptr );
}
void tbb_thread_v3::join()
{
if (!joinable())
handle_perror( EINVAL, "tbb_thread::join" ); // Invalid argument
if (this_tbb_thread::get_id() == get_id())
handle_perror( EDEADLK, "tbb_thread::join" ); // Resource deadlock avoided
#if _WIN32||_WIN64
#if __TBB_WIN8UI_SUPPORT
std::thread* thread_tmp=(std::thread*)my_thread_id;
thread_tmp->join();
delete thread_tmp;
#else // __TBB_WIN8UI_SUPPORT
DWORD status = WaitForSingleObjectEx( my_handle, INFINITE, FALSE );
if ( status == WAIT_FAILED )
handle_win_error( GetLastError() );
BOOL close_stat = CloseHandle( my_handle );
if ( close_stat == 0 )
handle_win_error( GetLastError() );
my_thread_id = 0;
#endif // __TBB_WIN8UI_SUPPORT
#else
int status = pthread_join( my_handle, NULL );
if( status )
handle_perror( status, "pthread_join" );
#endif // _WIN32||_WIN64
my_handle = 0;
}
void tbb_thread_v3::detach() {
if (!joinable())
handle_perror( EINVAL, "tbb_thread::detach" ); // Invalid argument
#if _WIN32||_WIN64
BOOL status = CloseHandle( my_handle );
if ( status == 0 )
handle_win_error( GetLastError() );
my_thread_id = 0;
#else
int status = pthread_detach( my_handle );
if( status )
handle_perror( status, "pthread_detach" );
#endif // _WIN32||_WIN64
my_handle = 0;
}
void tbb_thread_v3::internal_start( __TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine),
void* closure ) {
#if _WIN32||_WIN64
#if __TBB_WIN8UI_SUPPORT
std::thread* thread_tmp=new std::thread(start_routine, closure);
my_handle = thread_tmp->native_handle();
// TODO: to find out the way to find thread_id without GetThreadId and other
// desktop functions.
// Now tbb_thread does have its own thread_id that stores std::thread object
my_thread_id = (size_t)thread_tmp;
#else
unsigned thread_id;
// The return type of _beginthreadex is "uintptr_t" on new MS compilers,
// and 'unsigned long' on old MS compilers. uintptr_t works for both.
uintptr_t status = _beginthreadex( NULL, ThreadStackSize, start_routine,
closure, 0, &thread_id );
if( status==0 )
handle_perror(errno,"__beginthreadex");
else {
my_handle = (HANDLE)status;
my_thread_id = thread_id;
}
#endif
#else
pthread_t thread_handle;
int status;
pthread_attr_t stack_size;
status = pthread_attr_init( &stack_size );
if( status )
handle_perror( status, "pthread_attr_init" );
status = pthread_attr_setstacksize( &stack_size, ThreadStackSize );
if( status )
handle_perror( status, "pthread_attr_setstacksize" );
status = pthread_create( &thread_handle, &stack_size, start_routine, closure );
if( status )
handle_perror( status, "pthread_create" );
status = pthread_attr_destroy( &stack_size );
if( status )
handle_perror( status, "pthread_attr_destroy" );
my_handle = thread_handle;
#endif // _WIN32||_WIN64
}
unsigned tbb_thread_v3::hardware_concurrency() __TBB_NOEXCEPT(true) {
return governor::default_num_threads();
}
tbb_thread_v3::id thread_get_id_v3() {
#if _WIN32||_WIN64
return tbb_thread_v3::id( GetCurrentThreadId() );
#else
return tbb_thread_v3::id( pthread_self() );
#endif // _WIN32||_WIN64
}
void move_v3( tbb_thread_v3& t1, tbb_thread_v3& t2 )
{
if (t1.joinable())
t1.detach();
t1.my_handle = t2.my_handle;
t2.my_handle = 0;
#if _WIN32||_WIN64
t1.my_thread_id = t2.my_thread_id;
t2.my_thread_id = 0;
#endif // _WIN32||_WIN64
}
void thread_yield_v3()
{
__TBB_Yield();
}
void thread_sleep_v3(const tick_count::interval_t &i)
{
#if _WIN32||_WIN64
tick_count t0 = tick_count::now();
tick_count t1 = t0;
for(;;) {
double remainder = (i-(t1-t0)).seconds()*1e3; // milliseconds remaining to sleep
if( remainder<=0 ) break;
DWORD t = remainder>=INFINITE ? INFINITE-1 : DWORD(remainder);
#if !__TBB_WIN8UI_SUPPORT
Sleep( t );
#else
std::chrono::milliseconds sleep_time( t );
std::this_thread::sleep_for( sleep_time );
#endif
t1 = tick_count::now();
}
#else
struct timespec req;
double sec = i.seconds();
req.tv_sec = static_cast<long>(sec);
req.tv_nsec = static_cast<long>( (sec - req.tv_sec)*1e9 );
nanosleep(&req, NULL);
#endif // _WIN32||_WIN64
}
} // internal
} // tbb