|
Boost-Commit : |
From: anthony_at_[hidden]
Date: 2007-11-22 17:01:31
Author: anthonyw
Date: 2007-11-22 17:01:30 EST (Thu, 22 Nov 2007)
New Revision: 41311
URL: http://svn.boost.org/trac/boost/changeset/41311
Log:
Removed thread::self in favour of allowing interruption through a thread::id; no longer requires DuplicateHandle
Text files modified:
trunk/boost/thread/win32/thread.hpp | 101 ++++++++++++---------------------------
trunk/libs/thread/src/win32/thread.cpp | 67 +++++++++-----------------
2 files changed, 55 insertions(+), 113 deletions(-)
Modified: trunk/boost/thread/win32/thread.hpp
==============================================================================
--- trunk/boost/thread/win32/thread.hpp (original)
+++ trunk/boost/thread/win32/thread.hpp 2007-11-22 17:01:30 EST (Thu, 22 Nov 2007)
@@ -15,6 +15,7 @@
#include "thread_primitives.hpp"
#include "thread_heap_alloc.hpp"
#include <boost/utility.hpp>
+#include <boost/assert.hpp>
#include <list>
#include <algorithm>
#include <boost/ref.hpp>
@@ -62,8 +63,16 @@
}
}
+ void interrupt()
+ {
+ BOOST_VERIFY(detail::win32::SetEvent(interruption_handle)!=0);
+ }
+
+
virtual void run()=0;
};
+
+ typedef boost::intrusive_ptr<detail::thread_data_base> thread_data_ptr;
}
class BOOST_THREAD_DECL thread
@@ -94,15 +103,15 @@
};
mutable boost::mutex thread_info_mutex;
- boost::intrusive_ptr<detail::thread_data_base> thread_info;
+ detail::thread_data_ptr thread_info;
static unsigned __stdcall thread_start_function(void* param);
void start_thread();
- explicit thread(boost::intrusive_ptr<detail::thread_data_base> data);
+ explicit thread(detail::thread_data_ptr data);
- boost::intrusive_ptr<detail::thread_data_base> get_thread_info() const;
+ detail::thread_data_ptr get_thread_info() const;
public:
thread();
~thread();
@@ -155,12 +164,8 @@
static void sleep(const system_time& xt);
// extensions
- class interruption_handle;
- interruption_handle get_interruption_handle() const;
void interrupt();
bool interruption_requested() const;
-
- static thread self();
};
template<typename F>
@@ -214,7 +219,6 @@
void BOOST_THREAD_DECL interruption_point();
bool BOOST_THREAD_DECL interruption_enabled();
bool BOOST_THREAD_DECL interruption_requested();
- thread::interruption_handle BOOST_THREAD_DECL get_interruption_handle();
void BOOST_THREAD_DECL yield();
template<typename TimeDuration>
@@ -227,54 +231,63 @@
class thread::id
{
private:
- unsigned thread_id;
+ detail::thread_data_ptr thread_data;
- id(unsigned thread_id_):
- thread_id(thread_id_)
+ id(detail::thread_data_ptr thread_data_):
+ thread_data(thread_data_)
{}
friend class thread;
friend id this_thread::get_id();
public:
id():
- thread_id(0)
+ thread_data(0)
{}
bool operator==(const id& y) const
{
- return thread_id==y.thread_id;
+ return thread_data==y.thread_data;
}
bool operator!=(const id& y) const
{
- return thread_id!=y.thread_id;
+ return thread_data!=y.thread_data;
}
bool operator<(const id& y) const
{
- return thread_id<y.thread_id;
+ return thread_data<y.thread_data;
}
bool operator>(const id& y) const
{
- return thread_id>y.thread_id;
+ return y.thread_data<thread_data;
}
bool operator<=(const id& y) const
{
- return thread_id<=y.thread_id;
+ return !(y.thread_data<thread_data);
}
bool operator>=(const id& y) const
{
- return thread_id>=y.thread_id;
+ return !(thread_data<y.thread_data);
}
template<class charT, class traits>
friend std::basic_ostream<charT, traits>&
operator<<(std::basic_ostream<charT, traits>& os, const id& x)
{
- return os<<x.thread_id;
+ return os<<x.thread_data;
}
+
+ void interrupt()
+ {
+ if(thread_data)
+ {
+ thread_data->interrupt();
+ }
+ }
+
};
inline bool thread::operator==(const thread& other) const
@@ -286,56 +299,6 @@
{
return get_id()!=other.get_id();
}
-
- class thread::interruption_handle
- {
- private:
- boost::detail::win32::handle_manager handle;
- friend class thread;
- friend interruption_handle this_thread::get_interruption_handle();
-
- interruption_handle(detail::win32::handle h_):
- handle(h_)
- {}
- public:
- interruption_handle(interruption_handle const& other):
- handle(other.handle.duplicate())
- {}
- interruption_handle():
- handle(0)
- {}
-
- void swap(interruption_handle& other)
- {
- handle.swap(other.handle);
- }
-
- interruption_handle& operator=(interruption_handle const& other)
- {
- interruption_handle temp(other);
- swap(temp);
- return *this;
- }
-
- void reset()
- {
- handle=0;
- }
-
- void interrupt()
- {
- if(handle)
- {
- detail::win32::SetEvent(handle);
- }
- }
-
- typedef void(interruption_handle::*bool_type)();
- operator bool_type() const
- {
- return handle?&interruption_handle::interrupt:0;
- }
- };
namespace detail
{
Modified: trunk/libs/thread/src/win32/thread.cpp
==============================================================================
--- trunk/libs/thread/src/win32/thread.cpp (original)
+++ trunk/libs/thread/src/win32/thread.cpp 2007-11-22 17:01:30 EST (Thu, 22 Nov 2007)
@@ -128,7 +128,7 @@
{
void run_thread_exit_callbacks()
{
- boost::intrusive_ptr<detail::thread_data_base> current_thread_data(get_current_thread_data(),false);
+ detail::thread_data_ptr current_thread_data(get_current_thread_data(),false);
if(current_thread_data)
{
while(current_thread_data->tss_data || current_thread_data->thread_exit_callbacks)
@@ -197,7 +197,7 @@
ResumeThread(thread_info->thread_handle);
}
- thread::thread(boost::intrusive_ptr<detail::thread_data_base> data):
+ thread::thread(detail::thread_data_ptr data):
thread_info(data)
{}
@@ -210,7 +210,6 @@
{
++count;
interruption_enabled=false;
- thread_handle=detail::win32::duplicate_handle(detail::win32::GetCurrentThread());
}
void run()
@@ -222,18 +221,20 @@
externally_launched_thread* me=detail::heap_new<externally_launched_thread>();
set_current_thread_data(me);
}
-
- }
- thread thread::self()
- {
- if(!get_current_thread_data())
+ detail::thread_data_base* get_or_make_current_thread_data()
{
- make_external_thread_data();
+ detail::thread_data_base* current_thread_data(get_current_thread_data());
+ if(!current_thread_data)
+ {
+ make_external_thread_data();
+ current_thread_data=get_current_thread_data();
+ }
+ return current_thread_data;
}
- return thread(boost::intrusive_ptr<detail::thread_data_base>(get_current_thread_data()));
+
}
-
+
thread::~thread()
{
detach();
@@ -273,16 +274,9 @@
thread::id thread::get_id() const
{
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
- return local_thread_info?thread::id(local_thread_info->id):thread::id();
+ return thread::id(get_thread_info());
}
- thread::interruption_handle thread::get_interruption_handle() const
- {
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
- return local_thread_info?thread::interruption_handle(local_thread_info->interruption_handle.duplicate()):thread::interruption_handle();
- }
-
bool thread::joinable() const
{
return get_thread_info();
@@ -290,7 +284,7 @@
void thread::join()
{
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
+ detail::thread_data_ptr local_thread_info=get_thread_info();
if(local_thread_info)
{
this_thread::interruptible_wait(local_thread_info->thread_handle,detail::win32::infinite);
@@ -300,7 +294,7 @@
bool thread::timed_join(boost::system_time const& wait_until)
{
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
+ detail::thread_data_ptr local_thread_info=get_thread_info();
if(local_thread_info)
{
if(!this_thread::interruptible_wait(local_thread_info->thread_handle,get_milliseconds_until(wait_until)))
@@ -325,16 +319,16 @@
void thread::interrupt()
{
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
+ detail::thread_data_ptr local_thread_info=get_thread_info();
if(local_thread_info)
{
- detail::win32::SetEvent(local_thread_info->interruption_handle);
+ local_thread_info->interrupt();
}
}
bool thread::interruption_requested() const
{
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
+ detail::thread_data_ptr local_thread_info=get_thread_info();
return local_thread_info.get() && (detail::win32::WaitForSingleObject(local_thread_info->interruption_handle,0)==0);
}
@@ -347,11 +341,11 @@
thread::native_handle_type thread::native_handle()
{
- boost::intrusive_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
+ detail::thread_data_ptr local_thread_info=get_thread_info();
return local_thread_info?(detail::win32::handle)local_thread_info->thread_handle:detail::win32::invalid_handle_value;
}
- boost::intrusive_ptr<detail::thread_data_base> thread::get_thread_info() const
+ detail::thread_data_ptr thread::get_thread_info() const
{
boost::mutex::scoped_lock l(thread_info_mutex);
return thread_info;
@@ -359,11 +353,6 @@
namespace this_thread
{
- thread::interruption_handle get_interruption_handle()
- {
- return get_current_thread_data()?thread::interruption_handle(get_current_thread_data()->interruption_handle.duplicate()):thread::interruption_handle();
- }
-
bool interruptible_wait(detail::win32::handle handle_to_wait_for,unsigned long milliseconds)
{
detail::win32::handle handles[2]={0};
@@ -401,7 +390,7 @@
thread::id get_id()
{
- return thread::id(detail::win32::GetCurrentThreadId());
+ return thread::id(get_or_make_current_thread_data());
}
void interruption_point()
@@ -466,12 +455,7 @@
{
void add_thread_exit_function(thread_exit_function_base* func)
{
- detail::thread_data_base* current_thread_data(get_current_thread_data());
- if(!current_thread_data)
- {
- make_external_thread_data();
- current_thread_data=get_current_thread_data();
- }
+ detail::thread_data_base* const current_thread_data(get_or_make_current_thread_data());
thread_exit_callback_node* const new_node=
heap_new<thread_exit_callback_node>(func,
current_thread_data->thread_exit_callbacks);
@@ -519,12 +503,7 @@
}
else
{
- detail::thread_data_base* current_thread_data(get_current_thread_data());
- if(!current_thread_data)
- {
- make_external_thread_data();
- current_thread_data=get_current_thread_data();
- }
+ detail::thread_data_base* const current_thread_data(get_or_make_current_thread_data());
tss_data_node* const new_node=heap_new<tss_data_node>(key,func,tss_data,current_thread_data->tss_data);
current_thread_data->tss_data=new_node;
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk