2
0
mirror of https://github.com/boostorg/thread.git synced 2026-01-28 07:42:12 +00:00
Files
thread/src/pthread/thread.cpp
Eric Niebler 98c3fb606e Merged revisions 41072-41140 via svnmerge from
https://svn.boost.org/svn/boost/trunk

........
  r41077 | troyer | 2007-11-13 23:00:02 -0800 (Tue, 13 Nov 2007) | 1 line
  
  Added support for std::complex to serialization
........
  r41078 | joaquin | 2007-11-13 23:09:29 -0800 (Tue, 13 Nov 2007) | 1 line
  
  added missing include
........
  r41079 | djowel | 2007-11-14 02:13:51 -0800 (Wed, 14 Nov 2007) | 1 line
  
  adjust to compensate for lack of <small> tag.
........
  r41080 | djowel | 2007-11-14 02:17:09 -0800 (Wed, 14 Nov 2007) | 1 line
  
  refresh docs
........
  r41081 | djowel | 2007-11-14 02:23:23 -0800 (Wed, 14 Nov 2007) | 1 line
  
  refresh docs
........
  r41082 | djowel | 2007-11-14 02:24:21 -0800 (Wed, 14 Nov 2007) | 1 line
  
  refresh docs
........
  r41083 | anthonyw | 2007-11-14 03:08:09 -0800 (Wed, 14 Nov 2007) | 1 line
  
  ignore and join all threads in group on exception
........
  r41084 | anthonyw | 2007-11-14 03:56:53 -0800 (Wed, 14 Nov 2007) | 1 line
  
  interrupt and join all threads in a group if an exception is thrown during a test
........
  r41086 | johnmaddock | 2007-11-14 04:14:19 -0800 (Wed, 14 Nov 2007) | 1 line
  
  Merged changes from the Sandbox to Trunk.
........
  r41087 | anthonyw | 2007-11-14 04:17:41 -0800 (Wed, 14 Nov 2007) | 1 line
  
  interrupt and join all threads in a group if an exception is thrown during a test
........
  r41088 | johnmaddock | 2007-11-14 04:41:32 -0800 (Wed, 14 Nov 2007) | 1 line
  
  Added WinCE workaround.
........
  r41089 | johnmaddock | 2007-11-14 05:03:15 -0800 (Wed, 14 Nov 2007) | 1 line
  
  Catch exceptions thrown from thread creation.
........
  r41090 | anthonyw | 2007-11-14 06:49:58 -0800 (Wed, 14 Nov 2007) | 1 line
  
  fixes for pthread implementation
........
  r41091 | bemandawes | 2007-11-14 07:50:43 -0800 (Wed, 14 Nov 2007) | 1 line
  
  Replace old docs with excerpts from http://article.gmane.org/gmane.comp.lib.boost.testing/5020
........
  r41093 | eric_niebler | 2007-11-14 08:36:15 -0800 (Wed, 14 Nov 2007) | 1 line
  
  fix support for segmented iteration
........
  r41094 | dave | 2007-11-14 09:37:55 -0800 (Wed, 14 Nov 2007) | 3 lines
  
  Update command-prompt instructions per
  http://news.gmane.org/find-root.php?message_id=%3cfhcrem%24c4%241%40ger.gmane.org%3e
........
  r41095 | bemandawes | 2007-11-14 12:05:30 -0800 (Wed, 14 Nov 2007) | 1 line
  
  Bring docs more in line with reality:-)
........
  r41097 | hljin | 2007-11-14 18:07:53 -0800 (Wed, 14 Nov 2007) | 1 line
  
  GIL: removed the example directory from the documentation as it is a duplication and re-directed the Doxygen documentation to a local copy (to be submitted next)
........
  r41098 | hljin | 2007-11-14 18:30:13 -0800 (Wed, 14 Nov 2007) | 1 line
  
  GIL: added a local copy of the Doxygen documentation
........
  r41099 | hljin | 2007-11-14 18:32:04 -0800 (Wed, 14 Nov 2007) | 1 line
  
  GIL: added a local copy of the Doxygen documentation (second batch)
........
  r41100 | chris_kohlhoff | 2007-11-14 22:19:04 -0800 (Wed, 14 Nov 2007) | 6 lines
  
  Try to fix an order-of-initialisation problem with error_category references.
  The symptom, which only occurs in some applications, is a crash due to a
  dereference of a null pointer. The exact conditions under which the problem
  occurs are not fully understood, so this fix is probably more paranoid than
  necessary.
........
  r41102 | jhunold | 2007-11-14 23:04:24 -0800 (Wed, 14 Nov 2007) | 1 line
  
  Suppress msvc "deprecated" warnings.
........
  r41103 | johnmaddock | 2007-11-15 01:52:26 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Fix some compiler warnings when building with the Intel and aCC compilers.
........
  r41104 | garcia | 2007-11-15 04:50:48 -0800 (Thu, 15 Nov 2007) | 2 lines
  
  John Torjo is reviewing the X-files.
........
  r41105 | hkaiser | 2007-11-15 05:27:24 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Suppressed more warnings during build of Wave.
........
  r41106 | bemandawes | 2007-11-15 06:36:40 -0800 (Thu, 15 Nov 2007) | 1 line
  
  GetUserDefaultUILanguage isn't present for Cygwin, so disable use
........
  r41107 | bemandawes | 2007-11-15 07:20:27 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Get rid of .cvsignore files
........
  r41108 | eric_niebler | 2007-11-15 08:06:22 -0800 (Thu, 15 Nov 2007) | 1 line
  
  fix typo
........
  r41110 | grafik | 2007-11-15 08:58:21 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Fix building of multiple regression tools at once.
........
  r41111 | gmelquio | 2007-11-15 09:52:31 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Fixed documentation typo.
........
  r41113 | grafik | 2007-11-15 10:56:43 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Switch intel-win32 to use static multi thread runtime since the single thread static runtime is no longer available. (fixes #1287)
........
  r41114 | andreas_huber69 | 2007-11-15 11:22:12 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Added new FAQ item suggested by Chris Paulse.
........
  r41116 | guwi17 | 2007-11-15 12:52:37 -0800 (Thu, 15 Nov 2007) | 4 lines
  
  This patch now lets the Sun compiler work for most of uBLAS. 'test4' is now failing.
  Too avoid other compilers complaining about missing return values in some functions where exceptions are thrown, code was added so an arbitatry were chosen.
........
  r41117 | danmarsden | 2007-11-15 13:15:47 -0800 (Thu, 15 Nov 2007) | 1 line
  
  removing failing test on EDG compilers
........
  r41118 | grafik | 2007-11-15 14:05:15 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Remove redundant reference to system library. It causes double linking when used indirectly through filesystem. Which can lead to double linking incompatible variants when a specific filesystem variant is requested.
........
  r41119 | grafik | 2007-11-15 14:05:22 -0800 (Thu, 15 Nov 2007) | 1 line
  
  Oops, forgot we need to use the root boost project settings when building.
........


[SVN r41141]
2007-11-16 08:03:15 +00:00

513 lines
15 KiB
C++

// Copyright (C) 2001-2003
// William E. Kempf
// Copyright (C) 2007 Anthony Williams
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#include <boost/thread/detail/config.hpp>
#include <boost/thread/thread.hpp>
#include <boost/thread/xtime.hpp>
#include <boost/thread/condition.hpp>
#include <boost/thread/locks.hpp>
#include <boost/thread/once.hpp>
#include "timeconv.inl"
namespace boost
{
namespace detail
{
struct thread_exit_callback_node
{
boost::detail::thread_exit_function_base* func;
thread_exit_callback_node* next;
};
namespace
{
boost::once_flag current_thread_tls_init_flag=BOOST_ONCE_INIT;
pthread_key_t current_thread_tls_key;
extern "C"
{
void tls_destructor(void* data)
{
boost::detail::thread_data_base* thread_info=static_cast<boost::detail::thread_data_base*>(data);
if(thread_info)
{
while(thread_info->thread_exit_callbacks)
{
boost::detail::thread_exit_callback_node* const current_node=thread_info->thread_exit_callbacks;
thread_info->thread_exit_callbacks=current_node->next;
if(current_node->func)
{
(*current_node->func)();
delete current_node->func;
}
delete current_node;
}
}
}
}
void create_current_thread_tls_key()
{
BOOST_VERIFY(!pthread_key_create(&current_thread_tls_key,NULL));
}
}
boost::detail::thread_data_base* get_current_thread_data()
{
boost::call_once(current_thread_tls_init_flag,create_current_thread_tls_key);
return (boost::detail::thread_data_base*)pthread_getspecific(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);
BOOST_VERIFY(!pthread_setspecific(current_thread_tls_key,new_data));
}
}
namespace
{
extern "C"
{
void* thread_proxy(void* param)
{
boost::shared_ptr<boost::detail::thread_data_base> thread_info = static_cast<boost::detail::thread_data_base*>(param)->self;
thread_info->self.reset();
detail::set_current_thread_data(thread_info.get());
try
{
thread_info->run();
}
catch(thread_interrupted const&)
{
}
catch(...)
{
std::terminate();
}
detail::tls_destructor(thread_info.get());
detail::set_current_thread_data(0);
boost::lock_guard<boost::mutex> lock(thread_info->data_mutex);
thread_info->done=true;
thread_info->done_condition.notify_all();
return 0;
}
}
}
thread::thread()
{}
void thread::start_thread()
{
thread_info->self=thread_info;
int const res = pthread_create(&thread_info->thread_handle, 0, &thread_proxy, thread_info.get());
if (res != 0)
{
thread_info->self.reset();
throw thread_resource_error();
}
}
thread::~thread()
{
detach();
}
bool thread::operator==(const thread& other) const
{
return get_id()==other.get_id();
}
bool thread::operator!=(const thread& other) const
{
return !operator==(other);
}
boost::shared_ptr<detail::thread_data_base> thread::get_thread_info() const
{
lock_guard<mutex> l(thread_info_mutex);
return thread_info;
}
void thread::join()
{
boost::shared_ptr<detail::thread_data_base> const local_thread_info=get_thread_info();
if(local_thread_info)
{
bool do_join=false;
{
unique_lock<mutex> lock(local_thread_info->data_mutex);
while(!local_thread_info->done)
{
local_thread_info->done_condition.wait(lock);
}
do_join=!local_thread_info->join_started;
if(do_join)
{
local_thread_info->join_started=true;
}
else
{
while(!local_thread_info->joined)
{
local_thread_info->done_condition.wait(lock);
}
}
}
if(do_join)
{
void* result=0;
BOOST_VERIFY(!pthread_join(local_thread_info->thread_handle,&result));
lock_guard<mutex> lock(local_thread_info->data_mutex);
local_thread_info->joined=true;
local_thread_info->done_condition.notify_all();
}
lock_guard<mutex> l1(thread_info_mutex);
if(thread_info==local_thread_info)
{
thread_info.reset();
}
}
}
bool thread::timed_join(system_time const& wait_until)
{
boost::shared_ptr<detail::thread_data_base> const local_thread_info=get_thread_info();
if(local_thread_info)
{
bool do_join=false;
{
unique_lock<mutex> lock(local_thread_info->data_mutex);
while(!local_thread_info->done)
{
if(!local_thread_info->done_condition.timed_wait(lock,wait_until))
{
return false;
}
}
do_join=!local_thread_info->join_started;
if(do_join)
{
local_thread_info->join_started=true;
}
else
{
while(!local_thread_info->joined)
{
local_thread_info->done_condition.wait(lock);
}
}
}
if(do_join)
{
void* result=0;
BOOST_VERIFY(!pthread_join(local_thread_info->thread_handle,&result));
lock_guard<mutex> lock(local_thread_info->data_mutex);
local_thread_info->joined=true;
local_thread_info->done_condition.notify_all();
}
lock_guard<mutex> l1(thread_info_mutex);
if(thread_info==local_thread_info)
{
thread_info.reset();
}
}
return true;
}
bool thread::joinable() const
{
return get_thread_info();
}
void thread::detach()
{
boost::shared_ptr<detail::thread_data_base> local_thread_info;
{
lock_guard<mutex> l1(thread_info_mutex);
thread_info.swap(local_thread_info);
}
if(local_thread_info)
{
lock_guard<mutex> lock(local_thread_info->data_mutex);
if(!local_thread_info->join_started)
{
BOOST_VERIFY(!pthread_detach(local_thread_info->thread_handle));
local_thread_info->join_started=true;
local_thread_info->joined=true;
}
}
}
void thread::sleep(const system_time& st)
{
detail::thread_data_base* const thread_info=detail::get_current_thread_data();
if(thread_info)
{
unique_lock<mutex> lk(thread_info->sleep_mutex);
while(thread_info->sleep_condition.timed_wait(lk,st));
}
else
{
xtime const xt=get_xtime(st);
for (int foo=0; foo < 5; ++foo)
{
# if defined(BOOST_HAS_PTHREAD_DELAY_NP)
timespec ts;
to_timespec_duration(xt, ts);
BOOST_VERIFY(!pthread_delay_np(&ts));
# elif defined(BOOST_HAS_NANOSLEEP)
timespec ts;
to_timespec_duration(xt, ts);
// nanosleep takes a timespec that is an offset, not
// an absolute time.
nanosleep(&ts, 0);
# else
mutex mx;
mutex::scoped_lock lock(mx);
condition cond;
cond.timed_wait(lock, xt);
# endif
xtime cur;
xtime_get(&cur, TIME_UTC);
if (xtime_cmp(xt, cur) <= 0)
return;
}
}
}
void thread::yield()
{
# if defined(BOOST_HAS_SCHED_YIELD)
BOOST_VERIFY(!sched_yield());
# elif defined(BOOST_HAS_PTHREAD_YIELD)
BOOST_VERIFY(!pthread_yield());
# else
xtime xt;
xtime_get(&xt, TIME_UTC);
sleep(xt);
# endif
}
unsigned thread::hardware_concurrency()
{
return 1;
}
thread::id thread::get_id() const
{
boost::shared_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
if(local_thread_info)
{
return id(local_thread_info->thread_handle);
}
else
{
return id();
}
}
void thread::interrupt()
{
boost::shared_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
if(local_thread_info)
{
lock_guard<mutex> lk(local_thread_info->data_mutex);
local_thread_info->interrupt_requested=true;
if(local_thread_info->current_cond)
{
BOOST_VERIFY(!pthread_cond_broadcast(local_thread_info->current_cond));
}
}
}
bool thread::interruption_requested() const
{
boost::shared_ptr<detail::thread_data_base> local_thread_info=get_thread_info();
if(local_thread_info)
{
lock_guard<mutex> lk(local_thread_info->data_mutex);
return local_thread_info->interrupt_requested;
}
else
{
return false;
}
}
namespace this_thread
{
void interruption_point()
{
boost::detail::thread_data_base* const thread_info=detail::get_current_thread_data();
if(thread_info && thread_info->interrupt_enabled)
{
lock_guard<mutex> lg(thread_info->data_mutex);
if(thread_info->interrupt_requested)
{
thread_info->interrupt_requested=false;
throw thread_interrupted();
}
}
}
bool interruption_enabled()
{
boost::detail::thread_data_base* const thread_info=detail::get_current_thread_data();
return thread_info && thread_info->interrupt_enabled;
}
bool interruption_requested()
{
boost::detail::thread_data_base* const thread_info=detail::get_current_thread_data();
if(!thread_info)
{
return false;
}
else
{
lock_guard<mutex> lg(thread_info->data_mutex);
return thread_info->interrupt_requested;
}
}
disable_interruption::disable_interruption():
interruption_was_enabled(interruption_enabled())
{
if(interruption_was_enabled)
{
detail::get_current_thread_data()->interrupt_enabled=false;
}
}
disable_interruption::~disable_interruption()
{
if(detail::get_current_thread_data())
{
detail::get_current_thread_data()->interrupt_enabled=interruption_was_enabled;
}
}
restore_interruption::restore_interruption(disable_interruption& d)
{
if(d.interruption_was_enabled)
{
detail::get_current_thread_data()->interrupt_enabled=true;
}
}
restore_interruption::~restore_interruption()
{
if(detail::get_current_thread_data())
{
detail::get_current_thread_data()->interrupt_enabled=false;
}
}
}
thread_group::thread_group()
{
}
thread_group::~thread_group()
{
// We shouldn't have to scoped_lock here, since referencing this object
// from another thread while we're deleting it in the current thread is
// going to lead to undefined behavior any way.
for (std::list<thread*>::iterator it = m_threads.begin();
it != m_threads.end(); ++it)
{
delete (*it);
}
}
thread* thread_group::create_thread(const function0<void>& threadfunc)
{
// No scoped_lock required here since the only "shared data" that's
// modified here occurs inside add_thread which does scoped_lock.
std::auto_ptr<thread> thrd(new thread(threadfunc));
add_thread(thrd.get());
return thrd.release();
}
void thread_group::add_thread(thread* thrd)
{
mutex::scoped_lock scoped_lock(m_mutex);
// For now we'll simply ignore requests to add a thread object multiple
// times. Should we consider this an error and either throw or return an
// error value?
std::list<thread*>::iterator it = std::find(m_threads.begin(),
m_threads.end(), thrd);
BOOST_ASSERT(it == m_threads.end());
if (it == m_threads.end())
m_threads.push_back(thrd);
}
void thread_group::remove_thread(thread* thrd)
{
mutex::scoped_lock scoped_lock(m_mutex);
// For now we'll simply ignore requests to remove a thread object that's
// not in the group. Should we consider this an error and either throw or
// return an error value?
std::list<thread*>::iterator it = std::find(m_threads.begin(),
m_threads.end(), thrd);
BOOST_ASSERT(it != m_threads.end());
if (it != m_threads.end())
m_threads.erase(it);
}
void thread_group::join_all()
{
mutex::scoped_lock scoped_lock(m_mutex);
for (std::list<thread*>::iterator it = m_threads.begin();
it != m_threads.end(); ++it)
{
(*it)->join();
}
}
void thread_group::interrupt_all()
{
boost::lock_guard<mutex> guard(m_mutex);
for(std::list<thread*>::iterator it=m_threads.begin(),end=m_threads.end();
it!=end;
++it)
{
(*it)->interrupt();
}
}
int thread_group::size() const
{
return m_threads.size();
}
}