Imported existing code

This commit is contained in:
Hazim Gazov
2010-04-02 02:48:44 -03:00
parent 48fbc5ae91
commit 7a86d01598
13996 changed files with 2468699 additions and 0 deletions

View File

@@ -0,0 +1,46 @@
//////////////////////////////////////////////////////////////////////////////
//
// (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>
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 {

View File

@@ -0,0 +1,219 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2008. 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/detail/posix_time_types_wrk.hpp>
#include <boost/interprocess/detail/move.hpp>
namespace boost {
namespace interprocess {
inline interprocess_condition::interprocess_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 interprocess_condition::~interprocess_condition()
{
//Trivial destructor
}
inline void interprocess_condition::notify_one()
{
this->notify(NOTIFY_ONE);
}
inline void interprocess_condition::notify_all()
{
this->notify(NOTIFY_ALL);
}
inline void interprocess_condition::notify(boost::uint32_t command)
{
//This interprocess_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 interprocess_condition before this one ends
m_enter_mut.lock();
//Return if there are no waiters
if(!detail::atomic_read32(&m_num_waiters)) {
m_enter_mut.unlock();
return;
}
//Notify that all threads should execute wait logic
while(SLEEP != detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), command, SLEEP)){
detail::thread_yield();
}
/*
//Wait until the threads are woken
while(SLEEP != detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), 0)){
detail::thread_yield();
}
*/
//The enter interprocess_mutex will rest locked until the last waiting thread unlocks it
}
inline void interprocess_condition::do_wait(interprocess_mutex &mut)
{
this->do_timed_wait(false, boost::posix_time::ptime(), mut);
}
inline bool interprocess_condition::do_timed_wait
(const boost::posix_time::ptime &abs_time, interprocess_mutex &mut)
{
return this->do_timed_wait(true, abs_time, mut);
}
inline bool interprocess_condition::do_timed_wait(bool tout_enabled,
const boost::posix_time::ptime &abs_time,
interprocess_mutex &mut)
{
boost::posix_time::ptime now = microsec_clock::universal_time();
if(tout_enabled){
if(now >= abs_time) return false;
}
typedef boost::interprocess::scoped_lock<interprocess_mutex> InternalLock;
//The enter interprocess_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::interprocess::move(dummy);
}
else{
InternalLock dummy(m_enter_mut);
lock = boost::interprocess::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 interprocess_condition"
detail::atomic_inc32(const_cast<boost::uint32_t*>(&m_num_waiters));
//We unlock the external interprocess_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 interprocess_condition commands a notification
//Notification occurred, we will lock the checking interprocess_mutex so that
while(detail::atomic_read32(&m_command) == SLEEP){
detail::thread_yield();
//Check for timeout
if(tout_enabled){
now = microsec_clock::universal_time();
if(now >= abs_time){
//If we can lock the interprocess_mutex it means that no notification
//is being executed in this interprocess_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 interprocess_mutex is locked.
//We will execute time-out logic, so we will decrement count,
//release the enter interprocess_mutex and return false.
break;
}
}
}
//If a timeout occurred, the interprocess_mutex will not execute checking logic
if(tout_enabled && timed_out){
//Decrement wait count
detail::atomic_dec32(const_cast<boost::uint32_t*>(&m_num_waiters));
unlock_enter_mut = true;
break;
}
else{
//Notification occurred, we will lock the checking interprocess_mutex so that
//if a notify_one notification occurs, only one thread can exit
//---------------------------------------------------------------
InternalLock lock;
if(tout_enabled){
InternalLock dummy(m_check_mut, abs_time);
lock = boost::interprocess::move(dummy);
}
else{
InternalLock dummy(m_check_mut);
lock = boost::interprocess::move(dummy);
}
if(!lock){
timed_out = true;
unlock_enter_mut = true;
break;
}
//---------------------------------------------------------------
boost::uint32_t result = detail::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;
detail::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 == detail::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 interprocess_mutex
if(unlock_enter_mut){
detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_command), SLEEP, NOTIFY_ALL);
}
break;
}
}
}
//Unlock the enter interprocess_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 interprocess
} // namespace boost

View File

@@ -0,0 +1,81 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2008. 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/detail/posix_time_types_wrk.hpp>
namespace boost {
namespace interprocess {
inline interprocess_mutex::interprocess_mutex()
: m_s(0)
{
//Note that this class is initialized to zero.
//So zeroed memory can be interpreted as an
//initialized mutex
}
inline interprocess_mutex::~interprocess_mutex()
{
//Trivial destructor
}
inline void interprocess_mutex::lock(void)
{
do{
boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
if (m_s == 1 && prev_s == 0){
break;
}
// relinquish current timeslice
detail::thread_yield();
}while (true);
}
inline bool interprocess_mutex::try_lock(void)
{
boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
return m_s == 1 && prev_s == 0;
}
inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
{
if(abs_time == boost::posix_time::pos_infin){
this->lock();
return true;
}
//Obtain current count and target time
boost::posix_time::ptime now = microsec_clock::universal_time();
if(now >= abs_time) return false;
do{
if(this->try_lock()){
break;
}
now = microsec_clock::universal_time();
if(now >= abs_time){
return false;
}
// relinquish current time slice
detail::thread_yield();
}while (true);
return true;
}
inline void interprocess_mutex::unlock(void)
{ detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1); }
} //namespace interprocess {
} //namespace boost {

View File

@@ -0,0 +1,127 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2008. 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.
//////////////////////////////////////////////////////////////////////////////
#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
#include <boost/interprocess/detail/os_thread_functions.hpp>
#include <boost/interprocess/exceptions.hpp>
namespace boost {
namespace interprocess {
inline interprocess_recursive_mutex::interprocess_recursive_mutex()
: m_nLockCount(0), m_nOwner(detail::get_invalid_systemwide_thread_id()){}
inline interprocess_recursive_mutex::~interprocess_recursive_mutex(){}
inline void interprocess_recursive_mutex::lock()
{
typedef detail::OS_systemwide_thread_id_t handle_t;
const handle_t thr_id(detail::get_current_systemwide_thread_id());
handle_t old_id;
detail::systemwide_thread_id_copy(m_nOwner, old_id);
if(detail::equal_systemwide_thread_id(thr_id , old_id)){
if((unsigned int)(m_nLockCount+1) == 0){
//Overflow, throw an exception
throw interprocess_exception();
}
++m_nLockCount;
}
else{
m_mutex.lock();
detail::systemwide_thread_id_copy(thr_id, m_nOwner);
m_nLockCount = 1;
}
}
inline bool interprocess_recursive_mutex::try_lock()
{
typedef detail::OS_systemwide_thread_id_t handle_t;
handle_t thr_id(detail::get_current_systemwide_thread_id());
handle_t old_id;
detail::systemwide_thread_id_copy(m_nOwner, old_id);
if(detail::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();
}
++m_nLockCount;
return true;
}
if(m_mutex.try_lock()){
detail::systemwide_thread_id_copy(thr_id, m_nOwner);
m_nLockCount = 1;
return true;
}
return false;
}
inline bool interprocess_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
{
typedef detail::OS_systemwide_thread_id_t handle_t;
if(abs_time == boost::posix_time::pos_infin){
this->lock();
return true;
}
const handle_t thr_id(detail::get_current_systemwide_thread_id());
handle_t old_id;
detail::systemwide_thread_id_copy(m_nOwner, old_id);
if(detail::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();
}
++m_nLockCount;
return true;
}
if(m_mutex.timed_lock(abs_time)){
detail::systemwide_thread_id_copy(thr_id, m_nOwner);
m_nLockCount = 1;
return true;
}
return false;
}
inline void interprocess_recursive_mutex::unlock()
{
typedef detail::OS_systemwide_thread_id_t handle_t;
handle_t old_id;
detail::systemwide_thread_id_copy(m_nOwner, old_id);
const handle_t thr_id(detail::get_current_systemwide_thread_id());
(void)old_id;
assert(detail::equal_systemwide_thread_id(thr_id, old_id));
--m_nLockCount;
if(!m_nLockCount){
const handle_t new_id(detail::get_invalid_systemwide_thread_id());
detail::systemwide_thread_id_copy(new_id, m_nOwner);
m_mutex.unlock();
}
}
} //namespace interprocess {
} //namespace boost {

View File

@@ -0,0 +1,75 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2005-2008. 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>
namespace boost {
namespace interprocess {
inline interprocess_semaphore::~interprocess_semaphore()
{}
inline interprocess_semaphore::interprocess_semaphore(int initialCount)
: m_mut(), m_cond(), m_count(initialCount)
{}
inline void interprocess_semaphore::post()
{
scoped_lock<interprocess_mutex> lock(m_mut);
if(m_count == 0){
m_cond.notify_one();
}
++m_count;
}
inline void interprocess_semaphore::wait()
{
scoped_lock<interprocess_mutex> lock(m_mut);
while(m_count == 0){
m_cond.wait(lock);
}
--m_count;
}
inline bool interprocess_semaphore::try_wait()
{
scoped_lock<interprocess_mutex> lock(m_mut);
if(m_count == 0){
return false;
}
--m_count;
return true;
}
inline bool interprocess_semaphore::timed_wait(const boost::posix_time::ptime &abs_time)
{
if(abs_time == boost::posix_time::pos_infin){
this->wait();
return true;
}
scoped_lock<interprocess_mutex> lock(m_mut);
while(m_count == 0){
if(!m_cond.timed_wait(lock, abs_time))
return m_count != 0;
}
--m_count;
return true;
}
/*
inline int interprocess_semaphore::get_count() const
{
scoped_lock<interprocess_mutex> lock(m_mut);
return count;
}*/
} //namespace interprocess {
} //namespace boost {

View File

@@ -0,0 +1,68 @@
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2007-2008. 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_SYNC_NAMED_CREATION_FUNCTOR_HPP
#define BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP
#include <boost/interprocess/creation_tags.hpp>
#include <boost/interprocess/detail/type_traits.hpp>
#include <boost/interprocess/detail/mpl.hpp>
namespace boost {
namespace interprocess {
namespace detail {
struct named_creation_functor_no_arg{};
template <class T, class Arg = named_creation_functor_no_arg>
class named_creation_functor
{
typedef named_creation_functor_no_arg no_arg_t;
public:
named_creation_functor(detail::create_enum_t type, Arg arg = Arg())
: m_creation_type(type), m_arg(arg){}
template<class ArgType>
void construct(void *address, typename enable_if_c<is_same<ArgType, no_arg_t>::value>::type * = 0) const
{ new(address)T; }
template<class ArgType>
void construct(void *address, typename enable_if_c<!is_same<ArgType, no_arg_t>::value>::type * = 0) const
{ new(address)T(m_arg); }
bool operator()(void *address, std::size_t, bool created) const
{
switch(m_creation_type){
case detail::DoOpen:
return true;
break;
case detail::DoCreate:
case detail::DoOpenOrCreate:
if(created){
construct<Arg>(address);
}
return true;
break;
default:
return false;
break;
}
}
private:
detail::create_enum_t m_creation_type;
Arg m_arg;
};
} //namespace detail {
} //namespace interprocess {
} //namespace boost {
#endif //BOOST_INTERPROCESS_SYNC_NAMED_CREATION_FUNCTOR_HPP