tbb_thread.cpp
Go to the documentation of this file.
/*
Copyright (c) 2005-2020 Intel Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#if _WIN32||_WIN64
#include <process.h> // _beginthreadex()
#endif
#include <errno.h>
#include "tbb_misc.h" // handle_win_error()
#include "tbb/tbb_stddef.h"
#include "tbb/tbb_thread.h"
#include "tbb/tbb_allocator.h"
#include "tbb/global_control.h" // thread_stack_size
#include "governor.h" // default_num_threads()
#if __TBB_WIN8UI_SUPPORT
#include <thread>
#endif
namespace tbb {
namespace internal {
void* allocate_closure_v3( size_t size )
{
return allocate_via_handler_v3( size );
}
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, (unsigned)global_control::active_value(global_control::thread_stack_size),
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, global_control::active_value(global_control::thread_stack_size) );
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
global_control.h
__TBB_Yield
#define __TBB_Yield()
Definition: ibm_aix51.h:44
tbb_allocator.h
tbb_stddef.h
__TBB_NOEXCEPT
#define __TBB_NOEXCEPT(expression)
Definition: tbb_stddef.h:110
tbb_thread.h
__TBB_NATIVE_THREAD_ROUTINE_PTR
#define __TBB_NATIVE_THREAD_ROUTINE_PTR(r)
Definition: tbb_thread.h:49
governor.h
tbb_misc.h
size
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t size
Definition: ittnotify_static.h:109
id
void const char const char int ITT_FORMAT __itt_group_sync x void const char ITT_FORMAT __itt_group_sync s void ITT_FORMAT __itt_group_sync p void ITT_FORMAT p void ITT_FORMAT p no args __itt_suppress_mode_t unsigned int void size_t ITT_FORMAT d void ITT_FORMAT p void ITT_FORMAT p __itt_model_site __itt_model_site_instance ITT_FORMAT p __itt_model_task __itt_model_task_instance ITT_FORMAT p void ITT_FORMAT p void ITT_FORMAT p void size_t ITT_FORMAT d void ITT_FORMAT p const wchar_t ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s const char ITT_FORMAT s no args void ITT_FORMAT p size_t ITT_FORMAT d no args const wchar_t const wchar_t ITT_FORMAT s __itt_heap_function void size_t int ITT_FORMAT d __itt_heap_function void ITT_FORMAT p __itt_heap_function void void size_t int ITT_FORMAT d no args no args unsigned int ITT_FORMAT u const __itt_domain __itt_id id
Definition: ittnotify_static.h:172
tbb
The graph class.
Definition: serial/tbb/parallel_for.h:46
tbb::internal::deallocate_via_handler_v3
void __TBB_EXPORTED_FUNC deallocate_via_handler_v3(void *p)
Deallocates memory using FreeHandler.
Definition: cache_aligned_allocator.cpp:232
tbb::internal::free_closure_v3
void __TBB_EXPORTED_FUNC free_closure_v3(void *)
Free a closure allocated by allocate_closure_v3.
Definition: tbb_thread.cpp:41
tbb::internal::thread_yield_v3
void __TBB_EXPORTED_FUNC thread_yield_v3()
Definition: tbb_thread.cpp:159
tbb::internal::handle_perror
void __TBB_EXPORTED_FUNC handle_perror(int error_code, const char *aux_info)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
Definition: tbb_misc.cpp:87
tbb::internal::thread_get_id_v3
tbb_thread_v3::id __TBB_EXPORTED_FUNC thread_get_id_v3()
Definition: tbb_thread.cpp:139
tbb::internal::allocate_closure_v3
void *__TBB_EXPORTED_FUNC allocate_closure_v3(size_t size)
Allocate a closure.
Definition: tbb_thread.cpp:35
tbb::internal::allocate_via_handler_v3
void *__TBB_EXPORTED_FUNC allocate_via_handler_v3(size_t n)
Allocates memory using MallocHandler.
Definition: cache_aligned_allocator.cpp:224
tbb::internal::handle_win_error
void handle_win_error(int error_code)
Throws std::runtime_error with what() returning error_code description prefixed with aux_info.
tbb::internal::move_v3
void __TBB_EXPORTED_FUNC move_v3(tbb_thread_v3 &t1, tbb_thread_v3 &t2)
Definition: tbb_thread.cpp:147
tbb::internal::thread_sleep_v3
void __TBB_EXPORTED_FUNC thread_sleep_v3(const tick_count::interval_t &i)
Definition: tbb_thread.cpp:164
internal
Definition: _flow_graph_async_msg_impl.h:24
tbb::this_tbb_thread::get_id
__TBB_DEPRECATED_IN_VERBOSE_MODE tbb_thread::id get_id()
Definition: tbb_thread.h:331
tbb::internal::rml::thread_handle
thread_monitor::handle_type thread_handle
Definition: private_server.cpp:31
tbb::interface9::global_control::thread_stack_size
@ thread_stack_size
Definition: global_control.h:29
tbb::interface9::global_control::active_value
static size_t active_value(parameter p)
Definition: global_control.h:59
tbb::internal::tbb_thread_v3
Versioned thread class.
Definition: tbb_thread.h:122
tbb::internal::tbb_thread_v3::my_handle
native_handle_type my_handle
Definition: tbb_thread.h:206
tbb::internal::tbb_thread_v3::joinable
bool joinable() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:185
tbb::internal::tbb_thread_v3::internal_start
void __TBB_EXPORTED_METHOD internal_start(__TBB_NATIVE_THREAD_ROUTINE_PTR(start_routine), void *closure)
Definition: tbb_thread.cpp:90
tbb::internal::tbb_thread_v3::hardware_concurrency
static unsigned __TBB_EXPORTED_FUNC hardware_concurrency() __TBB_NOEXCEPT(true)
The number of hardware thread contexts.
Definition: tbb_thread.cpp:135
tbb::internal::tbb_thread_v3::join
void __TBB_EXPORTED_METHOD join()
The completion of the thread represented by *this happens before join() returns.
Definition: tbb_thread.cpp:46
tbb::internal::tbb_thread_v3::get_id
id get_id() const __TBB_NOEXCEPT(true)
Definition: tbb_thread.h:269
tbb::internal::tbb_thread_v3::detach
void __TBB_EXPORTED_METHOD detach()
When detach() returns, *this no longer represents the possibly continuing thread of execution.
Definition: tbb_thread.cpp:74
tbb::internal::tbb_thread_v3::id
Definition: tbb_thread.h:233
tbb::tick_count
Absolute timestamp.
Definition: tick_count.h:34
tbb::tick_count::now
static tick_count now()
Return current time.
Definition: tick_count.h:101
tbb::tick_count::interval_t
Relative time interval.
Definition: tick_count.h:37
tbb::tick_count::interval_t::seconds
double seconds() const
Return the length of a time interval in seconds.
Definition: tick_count.h:130
tbb::internal::governor::default_num_threads
static unsigned default_num_threads()
Definition: governor.h:84

Copyright © 2005-2020 Intel Corporation. All Rights Reserved.

Intel, Pentium, Intel Xeon, Itanium, Intel XScale and VTune are registered trademarks or trademarks of Intel Corporation or its subsidiaries in the United States and other countries.

* Other names and brands may be claimed as the property of others.