summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
Diffstat (limited to '3rdParty/Boost/src/libs/thread/src/win32/thread.cpp')
-rw-r--r--3rdParty/Boost/src/libs/thread/src/win32/thread.cpp305
1 files changed, 228 insertions, 77 deletions
diff --git a/3rdParty/Boost/src/libs/thread/src/win32/thread.cpp b/3rdParty/Boost/src/libs/thread/src/win32/thread.cpp
index 5a26f5e..54ebbf3 100644
--- a/3rdParty/Boost/src/libs/thread/src/win32/thread.cpp
+++ b/3rdParty/Boost/src/libs/thread/src/win32/thread.cpp
@@ -3,6 +3,7 @@
// http://www.boost.org/LICENSE_1_0.txt)
// (C) Copyright 2007 Anthony Williams
// (C) Copyright 2007 David Deakins
+// (C) Copyright 2011-2013 Vicente J. Botet Escriba
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x400
@@ -11,17 +12,20 @@
#ifndef WINVER
#define WINVER 0x400
#endif
+//#define BOOST_THREAD_VERSION 3
-#include <boost/thread/thread.hpp>
+#include <boost/thread/thread_only.hpp>
#include <boost/thread/once.hpp>
#include <boost/thread/tss.hpp>
#include <boost/thread/condition_variable.hpp>
#include <boost/thread/detail/tss_hooks.hpp>
+#include <boost/thread/future.hpp>
#include <boost/assert.hpp>
-#include <boost/throw_exception.hpp>
+#include <boost/cstdint.hpp>
+#if defined BOOST_THREAD_USES_DATETIME
#include <boost/date_time/posix_time/conversion.hpp>
-
+#endif
#include <memory>
#include <algorithm>
#ifndef UNDER_CE
@@ -36,16 +40,20 @@ namespace boost
{
thread_data_base::~thread_data_base()
{
- {
for (notify_list_t::iterator i = notify.begin(), e = notify.end();
i != e; ++i)
{
i->second->unlock();
i->first->notify_all();
}
- }
+ for (async_states_t::iterator i = async_states_.begin(), e = async_states_.end();
+ i != e; ++i)
+ {
+ (*i)->make_ready();
+ }
}
}
+
namespace
{
#ifdef BOOST_THREAD_PROVIDES_ONCE_CXX11
@@ -75,24 +83,34 @@ namespace boost
}
}
- detail::thread_data_base* get_current_thread_data()
- {
- if(current_thread_tls_key==TLS_OUT_OF_INDEXES)
- {
- return 0;
- }
- return (detail::thread_data_base*)TlsGetValue(current_thread_tls_key);
- }
-
void set_current_thread_data(detail::thread_data_base* new_data)
{
boost::call_once(current_thread_tls_init_flag,create_current_thread_tls_key);
- if(current_thread_tls_key!=TLS_OUT_OF_INDEXES)
+ if (current_thread_tls_key!=TLS_OUT_OF_INDEXES)
+ {
BOOST_VERIFY(TlsSetValue(current_thread_tls_key,new_data));
+ }
else
- boost::throw_exception(thread_resource_error());
+ {
+ BOOST_VERIFY(false);
+ //boost::throw_exception(thread_resource_error());
+ }
}
+ }
+ namespace detail
+ {
+ thread_data_base* get_current_thread_data()
+ {
+ if(current_thread_tls_key==TLS_OUT_OF_INDEXES)
+ {
+ return 0;
+ }
+ return (detail::thread_data_base*)TlsGetValue(current_thread_tls_key);
+ }
+ }
+ namespace
+ {
#ifndef BOOST_HAS_THREADEX
// Windows CE doesn't define _beginthreadex
@@ -111,7 +129,7 @@ namespace boost
return ret;
}
- typedef void* uintptr_t;
+ //typedef void* uintptr_t;
inline uintptr_t _beginthreadex(void* security, unsigned stack_size, unsigned (__stdcall* start_address)(void*),
void* arglist, unsigned initflag, unsigned* thrdaddr)
@@ -151,7 +169,7 @@ namespace boost
{
void run_thread_exit_callbacks()
{
- detail::thread_data_ptr current_thread_data(get_current_thread_data(),false);
+ detail::thread_data_ptr current_thread_data(detail::get_current_thread_data(),false);
if(current_thread_data)
{
while(! current_thread_data->tss_data.empty() || current_thread_data->thread_exit_callbacks)
@@ -190,23 +208,24 @@ namespace boost
{
detail::thread_data_base* const thread_info(reinterpret_cast<detail::thread_data_base*>(param));
set_current_thread_data(thread_info);
-#ifndef BOOST_NO_EXCEPTIONS
- try // BOOST_NO_EXCEPTIONS protected
-#endif
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
+ BOOST_TRY
{
+#endif
thread_info->run();
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
}
-#ifndef BOOST_NO_EXCEPTIONS
- catch(thread_interrupted const&) // BOOST_NO_EXCEPTIONS protected
+ BOOST_CATCH(thread_interrupted const&)
{
}
-#endif
// Removed as it stops the debugger identifying the cause of the exception
// Unhandled exceptions still cause the application to terminate
-// catch(...) // BOOST_NO_EXCEPTIONS protected
+// BOOST_CATCH(...)
// {
// std::terminate();
// }
+ BOOST_CATCH_END
+#endif
run_thread_exit_callbacks();
return 0;
}
@@ -215,29 +234,33 @@ namespace boost
thread::thread() BOOST_NOEXCEPT
{}
- void thread::start_thread()
+ bool thread::start_thread_noexcept()
{
uintptr_t const new_thread=_beginthreadex(0,0,&thread_start_function,thread_info.get(),CREATE_SUSPENDED,&thread_info->id);
if(!new_thread)
{
- boost::throw_exception(thread_resource_error());
+ return false;
+// boost::throw_exception(thread_resource_error());
}
intrusive_ptr_add_ref(thread_info.get());
thread_info->thread_handle=(detail::win32::handle)(new_thread);
ResumeThread(thread_info->thread_handle);
+ return true;
}
- void thread::start_thread(const attributes& attr)
+ bool thread::start_thread_noexcept(const attributes& attr)
{
//uintptr_t const new_thread=_beginthreadex(attr.get_security(),attr.get_stack_size(),&thread_start_function,thread_info.get(),CREATE_SUSPENDED,&thread_info->id);
uintptr_t const new_thread=_beginthreadex(0,static_cast<unsigned int>(attr.get_stack_size()),&thread_start_function,thread_info.get(),CREATE_SUSPENDED,&thread_info->id);
if(!new_thread)
{
- boost::throw_exception(thread_resource_error());
+ return false;
+// boost::throw_exception(thread_resource_error());
}
intrusive_ptr_add_ref(thread_info.get());
thread_info->thread_handle=(detail::win32::handle)(new_thread);
ResumeThread(thread_info->thread_handle);
+ return true;
}
thread::thread(detail::thread_data_ptr data):
@@ -252,7 +275,15 @@ namespace boost
externally_launched_thread()
{
++count;
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
interruption_enabled=false;
+#endif
+ }
+ ~externally_launched_thread() {
+ BOOST_ASSERT(notify.empty());
+ notify.clear();
+ BOOST_ASSERT(async_states_.empty());
+ async_states_.clear();
}
void run()
@@ -268,28 +299,25 @@ namespace boost
void make_external_thread_data()
{
externally_launched_thread* me=detail::heap_new<externally_launched_thread>();
-#ifndef BOOST_NO_EXCEPTIONS
- try // BOOST_NO_EXCEPTIONS protected
-#endif
+ BOOST_TRY
{
set_current_thread_data(me);
}
-#ifndef BOOST_NO_EXCEPTIONS
- catch(...) // BOOST_NO_EXCEPTIONS protected
+ BOOST_CATCH(...)
{
detail::heap_delete(me);
- throw; // BOOST_NO_EXCEPTIONS protected
+ BOOST_RETHROW
}
-#endif
+ BOOST_CATCH_END
}
detail::thread_data_base* get_or_make_current_thread_data()
{
- detail::thread_data_base* current_thread_data(get_current_thread_data());
+ detail::thread_data_base* current_thread_data(detail::get_current_thread_data());
if(!current_thread_data)
{
make_external_thread_data();
- current_thread_data=get_current_thread_data();
+ current_thread_data=detail::get_current_thread_data();
}
return current_thread_data;
}
@@ -309,54 +337,47 @@ namespace boost
bool thread::joinable() const BOOST_NOEXCEPT
{
- return (get_thread_info)();
+ return (get_thread_info)() ? true : false;
}
- void thread::join()
+ bool thread::join_noexcept()
{
- if (this_thread::get_id() == get_id())
- {
- boost::throw_exception(thread_resource_error(system::errc::resource_deadlock_would_occur, "boost thread: trying joining itself"));
- }
+
detail::thread_data_ptr local_thread_info=(get_thread_info)();
if(local_thread_info)
{
this_thread::interruptible_wait(local_thread_info->thread_handle,detail::timeout::sentinel());
release_handle();
+ return true;
}
else
{
-#ifdef BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
- boost::throw_exception(thread_resource_error(system::errc::invalid_argument, "boost thread: thread not joinable"));
-#endif
+ return false;
}
}
+#if defined BOOST_THREAD_USES_DATETIME
bool thread::timed_join(boost::system_time const& wait_until)
{
return do_try_join_until(get_milliseconds_until(wait_until));
}
-
- bool thread::do_try_join_until(uintmax_t milli)
+#endif
+ bool thread::do_try_join_until_noexcept(uintmax_t milli, bool& res)
{
- if (this_thread::get_id() == get_id())
- {
- boost::throw_exception(thread_resource_error(system::errc::resource_deadlock_would_occur, "boost thread: trying joining itself"));
- }
detail::thread_data_ptr local_thread_info=(get_thread_info)();
if(local_thread_info)
{
if(!this_thread::interruptible_wait(local_thread_info->thread_handle,milli))
{
- return false;
+ res=false;
+ return true;
}
release_handle();
+ res=true;
return true;
}
else
{
-#ifdef BOOST_THREAD_THROW_IF_PRECONDITION_NOT_SATISFIED
- boost::throw_exception(thread_resource_error(system::errc::invalid_argument, "boost thread: thread not joinable"));
-#endif
+ return false;
}
}
@@ -370,6 +391,7 @@ namespace boost
thread_info=0;
}
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
void thread::interrupt()
{
detail::thread_data_ptr local_thread_info=(get_thread_info)();
@@ -385,13 +407,40 @@ namespace boost
return local_thread_info.get() && (detail::win32::WaitForSingleObject(local_thread_info->interruption_handle,0)==0);
}
+#endif
+
unsigned thread::hardware_concurrency() BOOST_NOEXCEPT
{
- SYSTEM_INFO info={{0}};
+ //SYSTEM_INFO info={{0}};
+ SYSTEM_INFO info;
GetSystemInfo(&info);
return info.dwNumberOfProcessors;
}
+ unsigned thread::physical_concurrency() BOOST_NOEXCEPT
+ {
+ unsigned cores = 0;
+#if !(defined(__MINGW32__) || defined (__MINGW64__))
+ DWORD size = 0;
+
+ GetLogicalProcessorInformation(NULL, &size);
+ if (ERROR_INSUFFICIENT_BUFFER != GetLastError())
+ return 0;
+
+ std::vector<SYSTEM_LOGICAL_PROCESSOR_INFORMATION> buffer(size);
+ if (GetLogicalProcessorInformation(&buffer.front(), &size) == FALSE)
+ return 0;
+
+ const size_t Elements = size / sizeof(SYSTEM_LOGICAL_PROCESSOR_INFORMATION);
+
+ for (size_t i = 0; i < Elements; ++i) {
+ if (buffer[i].Relationship == RelationProcessorCore)
+ ++cores;
+ }
+#endif
+ return cores;
+ }
+
thread::native_handle_type thread::native_handle()
{
detail::thread_data_ptr local_thread_info=(get_thread_info)();
@@ -409,10 +458,10 @@ namespace boost
{
LARGE_INTEGER get_due_time(detail::timeout const& target_time)
{
- LARGE_INTEGER due_time={{0}};
+ LARGE_INTEGER due_time={{0,0}};
if(target_time.relative)
{
- unsigned long const elapsed_milliseconds=GetTickCount()-target_time.start;
+ detail::win32::ticks_type const elapsed_milliseconds=detail::win32::GetTickCount64()()-target_time.start;
LONGLONG const remaining_milliseconds=(target_time.milliseconds-elapsed_milliseconds);
LONGLONG const hundred_nanoseconds_in_one_millisecond=10000;
@@ -423,7 +472,7 @@ namespace boost
}
else
{
- SYSTEMTIME target_system_time={0};
+ SYSTEMTIME target_system_time={0,0,0,0,0,0,0,0};
target_system_time.wYear=target_time.abs_time.date().year();
target_system_time.wMonth=target_time.abs_time.date().month();
target_system_time.wDay=target_time.abs_time.date().day();
@@ -467,19 +516,22 @@ namespace boost
detail::win32::handle handles[3]={0};
unsigned handle_count=0;
unsigned wait_handle_index=~0U;
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
unsigned interruption_index=~0U;
+#endif
unsigned timeout_index=~0U;
if(handle_to_wait_for!=detail::win32::invalid_handle_value)
{
wait_handle_index=handle_count;
handles[handle_count++]=handle_to_wait_for;
}
- if(get_current_thread_data() && get_current_thread_data()->interruption_enabled)
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
+ if(detail::get_current_thread_data() && detail::get_current_thread_data()->interruption_enabled)
{
interruption_index=handle_count;
- handles[handle_count++]=get_current_thread_data()->interruption_handle;
+ handles[handle_count++]=detail::get_current_thread_data()->interruption_handle;
}
-
+#endif
detail::win32::handle_manager timer_handle;
#ifndef UNDER_CE
@@ -531,11 +583,13 @@ namespace boost
{
return true;
}
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
else if(notified_index==interruption_index)
{
- detail::win32::ResetEvent(get_current_thread_data()->interruption_handle);
+ detail::win32::ResetEvent(detail::get_current_thread_data()->interruption_handle);
throw thread_interrupted();
}
+#endif
else if(notified_index==timeout_index)
{
return false;
@@ -555,54 +609,140 @@ namespace boost
return false;
}
+ namespace no_interruption_point
+ {
+ bool non_interruptible_wait(detail::win32::handle handle_to_wait_for,detail::timeout target_time)
+ {
+ detail::win32::handle handles[3]={0};
+ unsigned handle_count=0;
+ unsigned wait_handle_index=~0U;
+ unsigned timeout_index=~0U;
+ if(handle_to_wait_for!=detail::win32::invalid_handle_value)
+ {
+ wait_handle_index=handle_count;
+ handles[handle_count++]=handle_to_wait_for;
+ }
+ detail::win32::handle_manager timer_handle;
+
+#ifndef UNDER_CE
+ unsigned const min_timer_wait_period=20;
+
+ if(!target_time.is_sentinel())
+ {
+ detail::timeout::remaining_time const time_left=target_time.remaining_milliseconds();
+ if(time_left.milliseconds > min_timer_wait_period)
+ {
+ // for a long-enough timeout, use a waitable timer (which tracks clock changes)
+ timer_handle=CreateWaitableTimer(NULL,false,NULL);
+ if(timer_handle!=0)
+ {
+ LARGE_INTEGER due_time=get_due_time(target_time);
+
+ bool const set_time_succeeded=SetWaitableTimer(timer_handle,&due_time,0,0,0,false)!=0;
+ if(set_time_succeeded)
+ {
+ timeout_index=handle_count;
+ handles[handle_count++]=timer_handle;
+ }
+ }
+ }
+ else if(!target_time.relative)
+ {
+ // convert short absolute-time timeouts into relative ones, so we don't race against clock changes
+ target_time=detail::timeout(time_left.milliseconds);
+ }
+ }
+#endif
+
+ bool const using_timer=timeout_index!=~0u;
+ detail::timeout::remaining_time time_left(0);
+
+ do
+ {
+ if(!using_timer)
+ {
+ time_left=target_time.remaining_milliseconds();
+ }
+
+ if(handle_count)
+ {
+ unsigned long const notified_index=detail::win32::WaitForMultipleObjects(handle_count,handles,false,using_timer?INFINITE:time_left.milliseconds);
+ if(notified_index<handle_count)
+ {
+ if(notified_index==wait_handle_index)
+ {
+ return true;
+ }
+ else if(notified_index==timeout_index)
+ {
+ return false;
+ }
+ }
+ }
+ else
+ {
+ detail::win32::Sleep(time_left.milliseconds);
+ }
+ if(target_time.relative)
+ {
+ target_time.milliseconds-=detail::timeout::max_non_infinite_wait;
+ }
+ }
+ while(time_left.more);
+ return false;
+ }
+ }
+
thread::id get_id() BOOST_NOEXCEPT
{
#if defined BOOST_THREAD_PROVIDES_BASIC_THREAD_ID
- //return detail::win32::GetCurrentThread();
return detail::win32::GetCurrentThreadId();
#else
return thread::id(get_or_make_current_thread_data());
#endif
}
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
void interruption_point()
{
if(interruption_enabled() && interruption_requested())
{
- detail::win32::ResetEvent(get_current_thread_data()->interruption_handle);
+ detail::win32::ResetEvent(detail::get_current_thread_data()->interruption_handle);
throw thread_interrupted();
}
}
bool interruption_enabled() BOOST_NOEXCEPT
{
- return get_current_thread_data() && get_current_thread_data()->interruption_enabled;
+ return detail::get_current_thread_data() && detail::get_current_thread_data()->interruption_enabled;
}
bool interruption_requested() BOOST_NOEXCEPT
{
- return get_current_thread_data() && (detail::win32::WaitForSingleObject(get_current_thread_data()->interruption_handle,0)==0);
+ return detail::get_current_thread_data() && (detail::win32::WaitForSingleObject(detail::get_current_thread_data()->interruption_handle,0)==0);
}
+#endif
void yield() BOOST_NOEXCEPT
{
detail::win32::Sleep(0);
}
+#if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
disable_interruption::disable_interruption() BOOST_NOEXCEPT:
interruption_was_enabled(interruption_enabled())
{
if(interruption_was_enabled)
{
- get_current_thread_data()->interruption_enabled=false;
+ detail::get_current_thread_data()->interruption_enabled=false;
}
}
disable_interruption::~disable_interruption() BOOST_NOEXCEPT
{
- if(get_current_thread_data())
+ if(detail::get_current_thread_data())
{
- get_current_thread_data()->interruption_enabled=interruption_was_enabled;
+ detail::get_current_thread_data()->interruption_enabled=interruption_was_enabled;
}
}
@@ -610,17 +750,18 @@ namespace boost
{
if(d.interruption_was_enabled)
{
- get_current_thread_data()->interruption_enabled=true;
+ detail::get_current_thread_data()->interruption_enabled=true;
}
}
restore_interruption::~restore_interruption() BOOST_NOEXCEPT
{
- if(get_current_thread_data())
+ if(detail::get_current_thread_data())
{
- get_current_thread_data()->interruption_enabled=false;
+ detail::get_current_thread_data()->interruption_enabled=false;
}
}
+#endif
}
namespace detail
@@ -716,12 +857,22 @@ namespace boost
BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk)
{
- detail::thread_data_base* const current_thread_data(get_current_thread_data());
+ detail::thread_data_base* const current_thread_data(detail::get_current_thread_data());
if(current_thread_data)
{
current_thread_data->notify_all_at_thread_exit(&cond, lk.release());
}
}
+//namespace detail {
+//
+// void BOOST_THREAD_DECL make_ready_at_thread_exit(shared_ptr<shared_state_base> as)
+// {
+// detail::thread_data_base* const current_thread_data(detail::get_current_thread_data());
+// if(current_thread_data)
+// {
+// current_thread_data->make_ready_at_thread_exit(as);
+// }
+// }
+//}
}
-