stabilize build system: depends, installer, boost/bdb fixes, cross targets groundwork
This commit is contained in:
@@ -0,0 +1,304 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2005-2012. 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/interprocess for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
|
||||
#define BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
#
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/interprocess/detail/config_begin.hpp>
|
||||
#include <boost/interprocess/detail/workaround.hpp>
|
||||
#include <boost/interprocess/sync/spin/mutex.hpp>
|
||||
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
||||
#include <boost/interprocess/detail/atomic.hpp>
|
||||
#include <boost/interprocess/sync/scoped_lock.hpp>
|
||||
#include <boost/interprocess/exceptions.hpp>
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
#include <boost/interprocess/sync/spin/wait.hpp>
|
||||
#include <boost/move/utility_core.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace interprocess {
|
||||
namespace ipcdetail {
|
||||
|
||||
class spin_condition
|
||||
{
|
||||
spin_condition(const spin_condition &);
|
||||
spin_condition &operator=(const spin_condition &);
|
||||
public:
|
||||
spin_condition();
|
||||
~spin_condition();
|
||||
|
||||
void notify_one();
|
||||
void notify_all();
|
||||
|
||||
template <typename L>
|
||||
bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_exception();
|
||||
//Handle infinity absolute time here to avoid complications in do_timed_wait
|
||||
if(abs_time == boost::posix_time::pos_infin){
|
||||
this->wait(lock);
|
||||
return true;
|
||||
}
|
||||
return this->do_timed_wait(abs_time, *lock.mutex());
|
||||
}
|
||||
|
||||
template <typename L, typename Pr>
|
||||
bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_exception();
|
||||
//Handle infinity absolute time here to avoid complications in do_timed_wait
|
||||
if(abs_time == boost::posix_time::pos_infin){
|
||||
this->wait(lock, pred);
|
||||
return true;
|
||||
}
|
||||
while (!pred()){
|
||||
if (!this->do_timed_wait(abs_time, *lock.mutex()))
|
||||
return pred();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename L>
|
||||
void wait(L& lock)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_exception();
|
||||
do_wait(*lock.mutex());
|
||||
}
|
||||
|
||||
template <typename L, typename Pr>
|
||||
void wait(L& lock, Pr pred)
|
||||
{
|
||||
if (!lock)
|
||||
throw lock_exception();
|
||||
|
||||
while (!pred())
|
||||
do_wait(*lock.mutex());
|
||||
}
|
||||
|
||||
template<class InterprocessMutex>
|
||||
void do_wait(InterprocessMutex &mut);
|
||||
|
||||
template<class InterprocessMutex>
|
||||
bool do_timed_wait(const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
|
||||
|
||||
private:
|
||||
template<class InterprocessMutex>
|
||||
bool do_timed_wait(bool tout_enabled, const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
|
||||
|
||||
enum { SLEEP = 0, NOTIFY_ONE, NOTIFY_ALL };
|
||||
spin_mutex m_enter_mut;
|
||||
volatile boost::uint32_t m_command;
|
||||
volatile boost::uint32_t m_num_waiters;
|
||||
void notify(boost::uint32_t command);
|
||||
};
|
||||
|
||||
inline spin_condition::spin_condition()
|
||||
{
|
||||
//Note that this class is initialized to zero.
|
||||
//So zeroed memory can be interpreted as an initialized
|
||||
//condition variable
|
||||
m_command = SLEEP;
|
||||
m_num_waiters = 0;
|
||||
}
|
||||
|
||||
inline spin_condition::~spin_condition()
|
||||
{
|
||||
//Notify all waiting threads
|
||||
//to allow POSIX semantics on condition destruction
|
||||
this->notify_all();
|
||||
}
|
||||
|
||||
inline void spin_condition::notify_one()
|
||||
{
|
||||
this->notify(NOTIFY_ONE);
|
||||
}
|
||||
|
||||
inline void spin_condition::notify_all()
|
||||
{
|
||||
this->notify(NOTIFY_ALL);
|
||||
}
|
||||
|
||||
inline void spin_condition::notify(boost::uint32_t command)
|
||||
{
|
||||
//This mutex guarantees that no other thread can enter to the
|
||||
//do_timed_wait method logic, so that thread count will be
|
||||
//constant until the function writes a NOTIFY_ALL command.
|
||||
//It also guarantees that no other notification can be signaled
|
||||
//on this spin_condition before this one ends
|
||||
m_enter_mut.lock();
|
||||
|
||||
//Return if there are no waiters
|
||||
if(!atomic_read32(&m_num_waiters)) {
|
||||
m_enter_mut.unlock();
|
||||
return;
|
||||
}
|
||||
|
||||
//Notify that all threads should execute wait logic
|
||||
spin_wait swait;
|
||||
while(SLEEP != atomic_cas32(const_cast<boost::uint32_t*>(&m_command), command, SLEEP)){
|
||||
swait.yield();
|
||||
}
|
||||
//The enter mutex will rest locked until the last waiting thread unlocks it
|
||||
}
|
||||
|
||||
template<class InterprocessMutex>
|
||||
inline void spin_condition::do_wait(InterprocessMutex &mut)
|
||||
{
|
||||
this->do_timed_wait(false, boost::posix_time::ptime(), mut);
|
||||
}
|
||||
|
||||
template<class InterprocessMutex>
|
||||
inline bool spin_condition::do_timed_wait
|
||||
(const boost::posix_time::ptime &abs_time, InterprocessMutex &mut)
|
||||
{
|
||||
return this->do_timed_wait(true, abs_time, mut);
|
||||
}
|
||||
|
||||
template<class InterprocessMutex>
|
||||
inline bool spin_condition::do_timed_wait(bool tout_enabled,
|
||||
const boost::posix_time::ptime &abs_time,
|
||||
InterprocessMutex &mut)
|
||||
{
|
||||
boost::posix_time::ptime now = microsec_clock::universal_time();
|
||||
|
||||
if(tout_enabled){
|
||||
if(now >= abs_time) return false;
|
||||
}
|
||||
|
||||
typedef boost::interprocess::scoped_lock<spin_mutex> InternalLock;
|
||||
//The enter mutex guarantees that while executing a notification,
|
||||
//no other thread can execute the do_timed_wait method.
|
||||
{
|
||||
//---------------------------------------------------------------
|
||||
InternalLock lock;
|
||||
if(tout_enabled){
|
||||
InternalLock dummy(m_enter_mut, abs_time);
|
||||
lock = boost::move(dummy);
|
||||
}
|
||||
else{
|
||||
InternalLock dummy(m_enter_mut);
|
||||
lock = boost::move(dummy);
|
||||
}
|
||||
|
||||
if(!lock)
|
||||
return false;
|
||||
//---------------------------------------------------------------
|
||||
//We increment the waiting thread count protected so that it will be
|
||||
//always constant when another thread enters the notification logic.
|
||||
//The increment marks this thread as "waiting on spin_condition"
|
||||
atomic_inc32(const_cast<boost::uint32_t*>(&m_num_waiters));
|
||||
|
||||
//We unlock the external mutex atomically with the increment
|
||||
mut.unlock();
|
||||
}
|
||||
|
||||
//By default, we suppose that no timeout has happened
|
||||
bool timed_out = false, unlock_enter_mut= false;
|
||||
|
||||
//Loop until a notification indicates that the thread should
|
||||
//exit or timeout occurs
|
||||
while(1){
|
||||
//The thread sleeps/spins until a spin_condition commands a notification
|
||||
//Notification occurred, we will lock the checking mutex so that
|
||||
spin_wait swait;
|
||||
while(atomic_read32(&m_command) == SLEEP){
|
||||
swait.yield();
|
||||
|
||||
//Check for timeout
|
||||
if(tout_enabled){
|
||||
now = microsec_clock::universal_time();
|
||||
|
||||
if(now >= abs_time){
|
||||
//If we can lock the mutex it means that no notification
|
||||
//is being executed in this spin_condition variable
|
||||
timed_out = m_enter_mut.try_lock();
|
||||
|
||||
//If locking fails, indicates that another thread is executing
|
||||
//notification, so we play the notification game
|
||||
if(!timed_out){
|
||||
//There is an ongoing notification, we will try again later
|
||||
continue;
|
||||
}
|
||||
//No notification in execution, since enter mutex is locked.
|
||||
//We will execute time-out logic, so we will decrement count,
|
||||
//release the enter mutex and return false.
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//If a timeout occurred, the mutex will not execute checking logic
|
||||
if(tout_enabled && timed_out){
|
||||
//Decrement wait count
|
||||
atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
|
||||
unlock_enter_mut = true;
|
||||
break;
|
||||
}
|
||||
else{
|
||||
boost::uint32_t result = atomic_cas32
|
||||
(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ONE);
|
||||
if(result == SLEEP){
|
||||
//Other thread has been notified and since it was a NOTIFY one
|
||||
//command, this thread must sleep again
|
||||
continue;
|
||||
}
|
||||
else if(result == NOTIFY_ONE){
|
||||
//If it was a NOTIFY_ONE command, only this thread should
|
||||
//exit. This thread has atomically marked command as sleep before
|
||||
//so no other thread will exit.
|
||||
//Decrement wait count.
|
||||
unlock_enter_mut = true;
|
||||
atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
|
||||
break;
|
||||
}
|
||||
else{
|
||||
//If it is a NOTIFY_ALL command, all threads should return
|
||||
//from do_timed_wait function. Decrement wait count.
|
||||
unlock_enter_mut = 1 == atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
|
||||
//Check if this is the last thread of notify_all waiters
|
||||
//Only the last thread will release the mutex
|
||||
if(unlock_enter_mut){
|
||||
atomic_cas32(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ALL);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Unlock the enter mutex if it is a single notification, if this is
|
||||
//the last notified thread in a notify_all or a timeout has occurred
|
||||
if(unlock_enter_mut){
|
||||
m_enter_mut.unlock();
|
||||
}
|
||||
|
||||
//Lock external again before returning from the method
|
||||
mut.lock();
|
||||
return !timed_out;
|
||||
}
|
||||
|
||||
} //namespace ipcdetail
|
||||
} //namespace interprocess
|
||||
} //namespace boost
|
||||
|
||||
#include <boost/interprocess/detail/config_end.hpp>
|
||||
|
||||
#endif //BOOST_INTERPROCESS_DETAIL_SPIN_CONDITION_HPP
|
||||
+54
@@ -0,0 +1,54 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2006. 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/interprocess for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#include<boost/interprocess/exceptions.hpp>
|
||||
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
||||
#include <boost/interprocess/sync/scoped_lock.hpp>
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
#
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
namespace boost {
|
||||
namespace interprocess {
|
||||
|
||||
inline barrier::barrier(unsigned int count)
|
||||
: m_threshold(count), m_count(count), m_generation(0)
|
||||
{
|
||||
if (count == 0)
|
||||
throw std::invalid_argument("count cannot be zero.");
|
||||
}
|
||||
|
||||
inline barrier::~barrier(){}
|
||||
|
||||
inline bool barrier::wait()
|
||||
{
|
||||
scoped_lock<interprocess_mutex> lock(m_mutex);
|
||||
unsigned int gen = m_generation;
|
||||
|
||||
if (--m_count == 0){
|
||||
m_generation++;
|
||||
m_count = m_threshold;
|
||||
m_cond.notify_all();
|
||||
return true;
|
||||
}
|
||||
|
||||
while (gen == m_generation){
|
||||
m_cond.wait(lock);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
} //namespace interprocess {
|
||||
} //namespace boost {
|
||||
@@ -0,0 +1,87 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2005-2012. 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/interprocess for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
|
||||
#define BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
#
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/interprocess/detail/config_begin.hpp>
|
||||
#include <boost/interprocess/detail/workaround.hpp>
|
||||
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
#include <boost/interprocess/detail/atomic.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
#include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace interprocess {
|
||||
namespace ipcdetail {
|
||||
|
||||
class spin_mutex
|
||||
{
|
||||
spin_mutex(const spin_mutex &);
|
||||
spin_mutex &operator=(const spin_mutex &);
|
||||
public:
|
||||
|
||||
spin_mutex();
|
||||
~spin_mutex();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
bool timed_lock(const boost::posix_time::ptime &abs_time);
|
||||
void unlock();
|
||||
void take_ownership(){};
|
||||
private:
|
||||
volatile boost::uint32_t m_s;
|
||||
};
|
||||
|
||||
inline spin_mutex::spin_mutex()
|
||||
: m_s(0)
|
||||
{
|
||||
//Note that this class is initialized to zero.
|
||||
//So zeroed memory can be interpreted as an
|
||||
//initialized mutex
|
||||
}
|
||||
|
||||
inline spin_mutex::~spin_mutex()
|
||||
{
|
||||
//Trivial destructor
|
||||
}
|
||||
|
||||
inline void spin_mutex::lock(void)
|
||||
{ return ipcdetail::try_based_lock(*this); }
|
||||
|
||||
inline bool spin_mutex::try_lock(void)
|
||||
{
|
||||
boost::uint32_t prev_s = ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
|
||||
return m_s == 1 && prev_s == 0;
|
||||
}
|
||||
|
||||
inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
||||
{ return ipcdetail::try_based_timed_lock(*this, abs_time); }
|
||||
|
||||
inline void spin_mutex::unlock(void)
|
||||
{ ipcdetail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
|
||||
|
||||
} //namespace ipcdetail {
|
||||
} //namespace interprocess {
|
||||
} //namespace boost {
|
||||
|
||||
#include <boost/interprocess/detail/config_end.hpp>
|
||||
|
||||
#endif //BOOST_INTERPROCESS_DETAIL_SPIN_MUTEX_HPP
|
||||
@@ -0,0 +1,176 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2005-2012. 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/interprocess for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Parts of the pthread code come from Boost Threads code:
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// Copyright (C) 2001-2003
|
||||
// William E. Kempf
|
||||
//
|
||||
// Permission to use, copy, modify, distribute and sell this software
|
||||
// and its documentation for any purpose is hereby granted without fee,
|
||||
// provided that the above copyright notice appear in all copies and
|
||||
// that both that copyright notice and this permission notice appear
|
||||
// in supporting documentation. William E. Kempf makes no representations
|
||||
// about the suitability of this software for any purpose.
|
||||
// It is provided "as is" without express or implied warranty.
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
|
||||
#define BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
#
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/interprocess/detail/config_begin.hpp>
|
||||
#include <boost/interprocess/detail/workaround.hpp>
|
||||
|
||||
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
#include <boost/interprocess/exceptions.hpp>
|
||||
#include <boost/interprocess/detail/atomic.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
#include <boost/interprocess/sync/spin/mutex.hpp>
|
||||
#include <boost/assert.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace interprocess {
|
||||
namespace ipcdetail {
|
||||
|
||||
class spin_recursive_mutex
|
||||
{
|
||||
spin_recursive_mutex(const spin_recursive_mutex &);
|
||||
spin_recursive_mutex &operator=(const spin_recursive_mutex &);
|
||||
public:
|
||||
|
||||
spin_recursive_mutex();
|
||||
~spin_recursive_mutex();
|
||||
|
||||
void lock();
|
||||
bool try_lock();
|
||||
bool timed_lock(const boost::posix_time::ptime &abs_time);
|
||||
void unlock();
|
||||
void take_ownership();
|
||||
private:
|
||||
spin_mutex m_mutex;
|
||||
unsigned int m_nLockCount;
|
||||
volatile ipcdetail::OS_systemwide_thread_id_t m_nOwner;
|
||||
volatile boost::uint32_t m_s;
|
||||
};
|
||||
|
||||
inline spin_recursive_mutex::spin_recursive_mutex()
|
||||
: m_nLockCount(0), m_nOwner(ipcdetail::get_invalid_systemwide_thread_id()){}
|
||||
|
||||
inline spin_recursive_mutex::~spin_recursive_mutex(){}
|
||||
|
||||
inline void spin_recursive_mutex::lock()
|
||||
{
|
||||
typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
||||
const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
||||
handle_t old_id;
|
||||
ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
||||
if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)){
|
||||
if((unsigned int)(m_nLockCount+1) == 0){
|
||||
//Overflow, throw an exception
|
||||
throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
|
||||
}
|
||||
++m_nLockCount;
|
||||
}
|
||||
else{
|
||||
m_mutex.lock();
|
||||
ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
||||
m_nLockCount = 1;
|
||||
}
|
||||
}
|
||||
|
||||
inline bool spin_recursive_mutex::try_lock()
|
||||
{
|
||||
typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
||||
handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
||||
handle_t old_id;
|
||||
ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
||||
if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
|
||||
if((unsigned int)(m_nLockCount+1) == 0){
|
||||
//Overflow, throw an exception
|
||||
throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
|
||||
}
|
||||
++m_nLockCount;
|
||||
return true;
|
||||
}
|
||||
if(m_mutex.try_lock()){
|
||||
ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
||||
m_nLockCount = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
|
||||
{
|
||||
typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
||||
const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
||||
handle_t old_id;
|
||||
ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
||||
if(ipcdetail::equal_systemwide_thread_id(thr_id , old_id)) { // we own it
|
||||
if((unsigned int)(m_nLockCount+1) == 0){
|
||||
//Overflow, throw an exception
|
||||
throw interprocess_exception("boost::interprocess::spin_recursive_mutex recursive lock overflow");
|
||||
}
|
||||
++m_nLockCount;
|
||||
return true;
|
||||
}
|
||||
//m_mutex supports abs_time so no need to check it
|
||||
if(m_mutex.timed_lock(abs_time)){
|
||||
ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
||||
m_nLockCount = 1;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
inline void spin_recursive_mutex::unlock()
|
||||
{
|
||||
typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
||||
handle_t old_id;
|
||||
ipcdetail::systemwide_thread_id_copy(m_nOwner, old_id);
|
||||
const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
||||
(void)old_id;
|
||||
(void)thr_id;
|
||||
BOOST_ASSERT(ipcdetail::equal_systemwide_thread_id(thr_id, old_id));
|
||||
--m_nLockCount;
|
||||
if(!m_nLockCount){
|
||||
const handle_t new_id(ipcdetail::get_invalid_systemwide_thread_id());
|
||||
ipcdetail::systemwide_thread_id_copy(new_id, m_nOwner);
|
||||
m_mutex.unlock();
|
||||
}
|
||||
}
|
||||
|
||||
inline void spin_recursive_mutex::take_ownership()
|
||||
{
|
||||
typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
|
||||
this->m_nLockCount = 1;
|
||||
const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());
|
||||
ipcdetail::systemwide_thread_id_copy(thr_id, m_nOwner);
|
||||
}
|
||||
|
||||
} //namespace ipcdetail {
|
||||
} //namespace interprocess {
|
||||
} //namespace boost {
|
||||
|
||||
#include <boost/interprocess/detail/config_end.hpp>
|
||||
|
||||
#endif //BOOST_INTERPROCESS_DETAIL_SPIN_RECURSIVE_MUTEX_HPP
|
||||
@@ -0,0 +1,94 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Ion Gaztanaga 2005-2012. 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/interprocess for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
|
||||
#define BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
#
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/interprocess/detail/config_begin.hpp>
|
||||
#include <boost/interprocess/detail/workaround.hpp>
|
||||
#include <boost/interprocess/detail/atomic.hpp>
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
|
||||
#include <boost/interprocess/sync/detail/common_algorithms.hpp>
|
||||
#include <boost/interprocess/sync/detail/locks.hpp>
|
||||
#include <boost/cstdint.hpp>
|
||||
|
||||
namespace boost {
|
||||
namespace interprocess {
|
||||
namespace ipcdetail {
|
||||
|
||||
class spin_semaphore
|
||||
{
|
||||
spin_semaphore(const spin_semaphore &);
|
||||
spin_semaphore &operator=(const spin_semaphore &);
|
||||
|
||||
public:
|
||||
spin_semaphore(unsigned int initialCount);
|
||||
~spin_semaphore();
|
||||
|
||||
void post();
|
||||
void wait();
|
||||
bool try_wait();
|
||||
bool timed_wait(const boost::posix_time::ptime &abs_time);
|
||||
|
||||
// int get_count() const;
|
||||
private:
|
||||
volatile boost::uint32_t m_count;
|
||||
};
|
||||
|
||||
|
||||
inline spin_semaphore::~spin_semaphore()
|
||||
{}
|
||||
|
||||
inline spin_semaphore::spin_semaphore(unsigned int initialCount)
|
||||
{ ipcdetail::atomic_write32(&this->m_count, boost::uint32_t(initialCount)); }
|
||||
|
||||
inline void spin_semaphore::post()
|
||||
{
|
||||
ipcdetail::atomic_inc32(&m_count);
|
||||
}
|
||||
|
||||
inline void spin_semaphore::wait()
|
||||
{
|
||||
ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
|
||||
return ipcdetail::try_based_lock(lw);
|
||||
}
|
||||
|
||||
inline bool spin_semaphore::try_wait()
|
||||
{
|
||||
return ipcdetail::atomic_add_unless32(&m_count, boost::uint32_t(-1), boost::uint32_t(0));
|
||||
}
|
||||
|
||||
inline bool spin_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
|
||||
{
|
||||
ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
|
||||
return ipcdetail::try_based_timed_lock(lw, abs_time);
|
||||
}
|
||||
|
||||
//inline int spin_semaphore::get_count() const
|
||||
//{
|
||||
//return (int)ipcdetail::atomic_read32(&m_count);
|
||||
//}
|
||||
|
||||
} //namespace ipcdetail {
|
||||
} //namespace interprocess {
|
||||
} //namespace boost {
|
||||
|
||||
#include <boost/interprocess/detail/config_end.hpp>
|
||||
|
||||
#endif //BOOST_INTERPROCESS_DETAIL_SPIN_SEMAPHORE_HPP
|
||||
@@ -0,0 +1,185 @@
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
//
|
||||
// (C) Copyright Peter Dimov 2008.
|
||||
// (C) Copyright Ion Gaztanaga 2013-2013. 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)
|
||||
//
|
||||
// See http://www.boost.org/libs/interprocess for documentation.
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//Parts of this file come from boost/smart_ptr/detail/yield_k.hpp
|
||||
//Many thanks to Peter Dimov.
|
||||
|
||||
#ifndef BOOST_INTERPROCESS_SYNC_WAIT_HPP_INCLUDED
|
||||
#define BOOST_INTERPROCESS_SYNC_WAIT_HPP_INCLUDED
|
||||
|
||||
#ifndef BOOST_CONFIG_HPP
|
||||
# include <boost/config.hpp>
|
||||
#endif
|
||||
#
|
||||
#if defined(BOOST_HAS_PRAGMA_ONCE)
|
||||
# pragma once
|
||||
#endif
|
||||
|
||||
#include <boost/interprocess/detail/config_begin.hpp>
|
||||
#include <boost/interprocess/detail/workaround.hpp>
|
||||
#include <boost/interprocess/detail/os_thread_functions.hpp>
|
||||
|
||||
//#define BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
|
||||
#ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
|
||||
#include <iostream>
|
||||
#endif
|
||||
|
||||
// BOOST_INTERPROCESS_SMT_PAUSE
|
||||
|
||||
#if defined(_MSC_VER) && ( defined(_M_IX86) || defined(_M_X64) )
|
||||
|
||||
extern "C" void _mm_pause();
|
||||
#pragma intrinsic( _mm_pause )
|
||||
|
||||
#define BOOST_INTERPROCESS_SMT_PAUSE _mm_pause();
|
||||
|
||||
#elif defined(__GNUC__) && ( defined(__i386__) || defined(__x86_64__) ) && !defined(_CRAYC)
|
||||
|
||||
#define BOOST_INTERPROCESS_SMT_PAUSE __asm__ __volatile__( "rep; nop" : : : "memory" );
|
||||
|
||||
#endif
|
||||
|
||||
namespace boost{
|
||||
namespace interprocess{
|
||||
namespace ipcdetail {
|
||||
|
||||
template<int Dummy = 0>
|
||||
class num_core_holder
|
||||
{
|
||||
public:
|
||||
static unsigned int get()
|
||||
{
|
||||
if(!num_cores){
|
||||
return ipcdetail::get_num_cores();
|
||||
}
|
||||
else{
|
||||
return num_cores;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static unsigned int num_cores;
|
||||
};
|
||||
|
||||
template<int Dummy>
|
||||
unsigned int num_core_holder<Dummy>::num_cores = ipcdetail::get_num_cores();
|
||||
|
||||
} //namespace ipcdetail {
|
||||
|
||||
class spin_wait
|
||||
{
|
||||
public:
|
||||
|
||||
static const unsigned int nop_pause_limit = 32u;
|
||||
spin_wait()
|
||||
: m_count_start(), m_ul_yield_only_counts(), m_k()
|
||||
{}
|
||||
|
||||
#ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
|
||||
~spin_wait()
|
||||
{
|
||||
if(m_k){
|
||||
std::cout << "final m_k: " << m_k
|
||||
<< " system tick(us): " << ipcdetail::get_system_tick_us() << std::endl;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
unsigned int count() const
|
||||
{ return m_k; }
|
||||
|
||||
void yield()
|
||||
{
|
||||
//Lazy initialization of limits
|
||||
if( !m_k){
|
||||
this->init_limits();
|
||||
}
|
||||
//Nop tries
|
||||
if( m_k < (nop_pause_limit >> 2) ){
|
||||
|
||||
}
|
||||
//Pause tries if the processor supports it
|
||||
#if defined(BOOST_INTERPROCESS_SMT_PAUSE)
|
||||
else if( m_k < nop_pause_limit ){
|
||||
BOOST_INTERPROCESS_SMT_PAUSE
|
||||
}
|
||||
#endif
|
||||
//Yield/Sleep strategy
|
||||
else{
|
||||
//Lazy initialization of tick information
|
||||
if(m_k == nop_pause_limit){
|
||||
this->init_tick_info();
|
||||
}
|
||||
else if( this->yield_or_sleep() ){
|
||||
ipcdetail::thread_yield();
|
||||
}
|
||||
else{
|
||||
ipcdetail::thread_sleep_tick();
|
||||
}
|
||||
}
|
||||
++m_k;
|
||||
}
|
||||
|
||||
void reset()
|
||||
{
|
||||
m_k = 0u;
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
void init_limits()
|
||||
{
|
||||
unsigned int num_cores = ipcdetail::num_core_holder<0>::get();
|
||||
m_k = num_cores > 1u ? 0u : nop_pause_limit;
|
||||
}
|
||||
|
||||
void init_tick_info()
|
||||
{
|
||||
m_ul_yield_only_counts = ipcdetail::get_system_tick_in_highres_counts();
|
||||
m_count_start = ipcdetail::get_current_system_highres_count();
|
||||
}
|
||||
|
||||
//Returns true if yield must be called, false is sleep must be called
|
||||
bool yield_or_sleep()
|
||||
{
|
||||
if(!m_ul_yield_only_counts){ //If yield-only limit was reached then yield one in every two tries
|
||||
return (m_k & 1u) != 0;
|
||||
}
|
||||
else{ //Try to see if we've reched yield-only time limit
|
||||
const ipcdetail::OS_highres_count_t now = ipcdetail::get_current_system_highres_count();
|
||||
const ipcdetail::OS_highres_count_t elapsed = ipcdetail::system_highres_count_subtract(now, m_count_start);
|
||||
if(!ipcdetail::system_highres_count_less_ul(elapsed, m_ul_yield_only_counts)){
|
||||
#ifdef BOOST_INTERPROCESS_SPIN_WAIT_DEBUG
|
||||
std::cout << "elapsed!\n"
|
||||
<< " m_ul_yield_only_counts: " << m_ul_yield_only_counts
|
||||
<< " system tick(us): " << ipcdetail::get_system_tick_us() << '\n'
|
||||
<< " m_k: " << m_k << " elapsed counts: ";
|
||||
ipcdetail::ostream_highres_count(std::cout, elapsed) << std::endl;
|
||||
#endif
|
||||
//Yield-only time reached, now it's time to sleep
|
||||
m_ul_yield_only_counts = 0ul;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true; //Otherwise yield
|
||||
}
|
||||
|
||||
ipcdetail::OS_highres_count_t m_count_start;
|
||||
unsigned long m_ul_yield_only_counts;
|
||||
unsigned int m_k;
|
||||
};
|
||||
|
||||
} // namespace interprocess
|
||||
} // namespace boost
|
||||
|
||||
#include <boost/interprocess/detail/config_end.hpp>
|
||||
|
||||
#endif // #ifndef BOOST_INTERPROCESS_SYNC_WAIT_HPP_INCLUDED
|
||||
Reference in New Issue
Block a user