update boost

This commit is contained in:
2023-11-24 12:56:13 -06:00
parent cfc99971af
commit 19d727037a
9260 changed files with 849256 additions and 299957 deletions

View File

@@ -21,12 +21,14 @@
#include <boost/interprocess/detail/config_begin.hpp>
#include <boost/interprocess/detail/workaround.hpp>
#include <boost/interprocess/sync/cv_status.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/detail/timed_utils.hpp>
#include <boost/interprocess/sync/spin/wait.hpp>
#include <boost/move/utility_core.hpp>
#include <boost/cstdint.hpp>
@@ -39,49 +41,36 @@ 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)
spin_condition()
{
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());
//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;
}
template <typename L, typename Pr>
bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
~spin_condition()
{
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;
//Notify all waiting threads
//to allow POSIX semantics on condition destruction
this->notify_all();
}
void notify_one()
{ this->notify(NOTIFY_ONE); }
void notify_all()
{ this->notify(NOTIFY_ALL); }
template <typename L>
void wait(L& lock)
{
if (!lock)
throw lock_exception();
do_wait(*lock.mutex());
this->do_timed_wait_impl<false>(0, *lock.mutex());
}
template <typename L, typename Pr>
@@ -91,209 +80,214 @@ class spin_condition
throw lock_exception();
while (!pred())
do_wait(*lock.mutex());
this->do_timed_wait_impl<false>(0, *lock.mutex());
}
template<class InterprocessMutex>
void do_wait(InterprocessMutex &mut);
template <typename L, typename TimePoint>
bool timed_wait(L& lock, const TimePoint &abs_time)
{
if (!lock)
throw lock_exception();
//Handle infinity absolute time here to avoid complications in do_timed_wait
if(is_pos_infinity(abs_time)){
this->wait(lock);
return true;
}
return this->do_timed_wait_impl<true>(abs_time, *lock.mutex());
}
template<class InterprocessMutex>
bool do_timed_wait(const boost::posix_time::ptime &abs_time, InterprocessMutex &mut);
template <typename L, typename TimePoint, typename Pr>
bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
{
if (!lock)
throw lock_exception();
//Handle infinity absolute time here to avoid complications in do_timed_wait
if(is_pos_infinity(abs_time)){
this->wait(lock, pred);
return true;
}
while (!pred()){
if (!this->do_timed_wait_impl<true>(abs_time, *lock.mutex()))
return pred();
}
return true;
}
template <typename L, class TimePoint>
cv_status wait_until(L& lock, const TimePoint &abs_time)
{ return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
template <typename L, class TimePoint, typename Pr>
bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
{ return this->timed_wait(lock, abs_time, pred); }
template <typename L, class Duration>
cv_status wait_for(L& lock, const Duration &dur)
{ return this->wait_until(lock, duration_to_ustime(dur)); }
template <typename L, class Duration, typename Pr>
bool wait_for(L& lock, const Duration &dur, Pr pred)
{ return this->wait_until(lock, duration_to_ustime(dur), pred); }
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.
template<bool TimeoutEnabled, class InterprocessMutex, class TimePoint>
bool do_timed_wait_impl(const TimePoint &abs_time, InterprocessMutex &mut)
{
//---------------------------------------------------------------
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);
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;
get_lock(bool_<TimeoutEnabled>(), m_enter_mut, lock, abs_time);
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();
}
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));
//By default, we suppose that no timeout has happened
bool timed_out = false, unlock_enter_mut= false;
//We unlock the external mutex atomically with the increment
mut.unlock();
}
//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();
//By default, we suppose that no timeout has happened
bool timed_out = false, unlock_enter_mut= false;
//Check for timeout
if(TimeoutEnabled){
TimePoint now = get_now<TimePoint>(bool_<TimeoutEnabled>());
//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();
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();
//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;
//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(TimeoutEnabled && 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);
}
//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;
}
//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();
template <class TimePoint>
static TimePoint get_now(bool_<true>)
{ return microsec_clock<TimePoint>::universal_time(); }
template <class TimePoint>
static TimePoint get_now(bool_<false>)
{ return TimePoint(); }
template <class Mutex, class Lock, class TimePoint>
static void get_lock(bool_<true>, Mutex &m, Lock &lck, const TimePoint &abs_time)
{
Lock dummy(m, abs_time);
lck = boost::move(dummy);
}
//Lock external again before returning from the method
mut.lock();
return !timed_out;
}
template <class Mutex, class Lock, class TimePoint>
static void get_lock(bool_<false>, Mutex &m, Lock &lck, const TimePoint &)
{
Lock dummy(m);
lck = boost::move(dummy);
}
void 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
}
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;
};
} //namespace ipcdetail
} //namespace interprocess

View File

@@ -9,7 +9,6 @@
//////////////////////////////////////////////////////////////////////////////
#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

View File

@@ -21,7 +21,6 @@
#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>
@@ -43,11 +42,37 @@ class spin_mutex
void lock();
bool try_lock();
bool timed_lock(const boost::posix_time::ptime &abs_time);
template<class TimePoint>
bool timed_lock(const TimePoint &abs_time);
template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
{ return this->timed_lock(abs_time); }
template<class Duration> bool try_lock_for(const Duration &dur)
{ return this->timed_lock(duration_to_ustime(dur)); }
void unlock();
void take_ownership(){}
private:
volatile boost::uint32_t m_s;
struct common_lock_wrapper
{
common_lock_wrapper(spin_mutex &sp)
: m_sp(sp)
{}
void lock()
{
ipcdetail::try_based_lock(m_sp);
}
template<class TimePoint>
bool timed_lock(const TimePoint &abs_time)
{ return m_sp.timed_lock(abs_time); }
spin_mutex &m_sp;
};
};
inline spin_mutex::spin_mutex()
@@ -65,19 +90,8 @@ inline spin_mutex::~spin_mutex()
inline void spin_mutex::lock(void)
{
#ifdef BOOST_INTERPROCESS_ENABLE_TIMEOUT_WHEN_LOCKING
boost::posix_time::ptime wait_time
= microsec_clock::universal_time()
+ boost::posix_time::milliseconds(BOOST_INTERPROCESS_TIMEOUT_WHEN_LOCKING_DURATION_MS);
if (!timed_lock(wait_time))
{
throw interprocess_exception(timeout_when_locking_error
, "Interprocess mutex timeout when locking. Possible deadlock: "
"owner died without unlocking?");
}
#else
return ipcdetail::try_based_lock(*this);
#endif
common_lock_wrapper clw(*this);
ipcdetail::timeout_when_locking_aware_lock(clw);
}
inline bool spin_mutex::try_lock(void)
@@ -86,7 +100,8 @@ inline bool spin_mutex::try_lock(void)
return m_s == 1 && prev_s == 0;
}
inline bool spin_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
template<class TimePoint>
inline bool spin_mutex::timed_lock(const TimePoint &abs_time)
{ return ipcdetail::try_based_timed_lock(*this, abs_time); }
inline void spin_mutex::unlock(void)

View File

@@ -38,7 +38,6 @@
#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>
@@ -62,7 +61,15 @@ class spin_recursive_mutex
void lock();
bool try_lock();
bool timed_lock(const boost::posix_time::ptime &abs_time);
template<class TimePoint>
bool timed_lock(const TimePoint &abs_time);
template<class TimePoint> bool try_lock_until(const TimePoint &abs_time)
{ return this->timed_lock(abs_time); }
template<class Duration> bool try_lock_for(const Duration &dur)
{ return this->timed_lock(duration_to_ustime(dur)); }
void unlock();
void take_ownership();
private:
@@ -119,7 +126,8 @@ inline bool spin_recursive_mutex::try_lock()
return false;
}
inline bool spin_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
template<class TimePoint>
inline bool spin_recursive_mutex::timed_lock(const TimePoint &abs_time)
{
typedef ipcdetail::OS_systemwide_thread_id_t handle_t;
const handle_t thr_id(ipcdetail::get_current_systemwide_thread_id());

View File

@@ -23,7 +23,6 @@
#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>
@@ -44,7 +43,7 @@ class spin_semaphore
void post();
void wait();
bool try_wait();
bool timed_wait(const boost::posix_time::ptime &abs_time);
template<class TimePoint> bool timed_wait(const TimePoint &abs_time);
// int get_count() const;
private:
@@ -74,7 +73,8 @@ 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)
template<class TimePoint>
inline bool spin_semaphore::timed_wait(const TimePoint &abs_time)
{
ipcdetail::lock_to_wait<spin_semaphore> lw(*this);
return ipcdetail::try_based_timed_lock(lw, abs_time);