PageRenderTime 30ms CodeModel.GetById 11ms app.highlight 14ms RepoModel.GetById 2ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/sync/emulation/mutex.hpp

http://hadesmem.googlecode.com/
C++ Header | 114 lines | 78 code | 20 blank | 16 comment | 13 complexity | 31337534ff1fe5a0844afef718f46eee MD5 | raw file
  1//////////////////////////////////////////////////////////////////////////////
  2//
  3// (C) Copyright Ion Gaztanaga 2005-2009. Distributed under the Boost
  4// Software License, Version 1.0. (See accompanying file
  5// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6//
  7// See http://www.boost.org/libs/interprocess for documentation.
  8//
  9//////////////////////////////////////////////////////////////////////////////
 10
 11#ifndef BOOST_INTERPROCESS_DETAIL_EMULATION_MUTEX_HPP
 12#define BOOST_INTERPROCESS_DETAIL_EMULATION_MUTEX_HPP
 13
 14#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 15#  pragma once
 16#endif
 17
 18#include <boost/interprocess/detail/config_begin.hpp>
 19#include <boost/interprocess/detail/workaround.hpp>
 20#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
 21#include <boost/assert.hpp>
 22#include <boost/interprocess/detail/atomic.hpp>
 23#include <boost/cstdint.hpp>
 24#include <boost/interprocess/detail/os_thread_functions.hpp>
 25
 26namespace boost {
 27namespace interprocess {
 28namespace detail {
 29
 30class emulation_mutex
 31{
 32   emulation_mutex(const emulation_mutex &);
 33   emulation_mutex &operator=(const emulation_mutex &);
 34   public:
 35
 36   emulation_mutex();
 37   ~emulation_mutex();
 38
 39   void lock();
 40   bool try_lock();
 41   bool timed_lock(const boost::posix_time::ptime &abs_time);
 42   void unlock();
 43   void take_ownership(){};
 44   private:
 45   volatile boost::uint32_t m_s;
 46};
 47
 48inline emulation_mutex::emulation_mutex() 
 49   : m_s(0) 
 50{
 51   //Note that this class is initialized to zero.
 52   //So zeroed memory can be interpreted as an
 53   //initialized mutex
 54}
 55
 56inline emulation_mutex::~emulation_mutex() 
 57{
 58   //Trivial destructor
 59}
 60
 61inline void emulation_mutex::lock(void)
 62{
 63   do{
 64      boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);
 65
 66      if (m_s == 1 && prev_s == 0){
 67            break;
 68      }
 69      // relinquish current timeslice
 70      detail::thread_yield();
 71   }while (true);
 72}
 73
 74inline bool emulation_mutex::try_lock(void)
 75{
 76   boost::uint32_t prev_s = detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 1, 0);   
 77   return m_s == 1 && prev_s == 0;
 78}
 79
 80inline bool emulation_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
 81{
 82   if(abs_time == boost::posix_time::pos_infin){
 83      this->lock();
 84      return true;
 85   }
 86   //Obtain current count and target time
 87   boost::posix_time::ptime now = microsec_clock::universal_time();
 88
 89   if(now >= abs_time) return false;
 90
 91   do{
 92      if(this->try_lock()){
 93         break;
 94      }
 95      now = microsec_clock::universal_time();
 96
 97      if(now >= abs_time){
 98         return false;
 99      }
100      // relinquish current time slice
101     detail::thread_yield();
102   }while (true);
103
104   return true;
105}
106
107inline void emulation_mutex::unlock(void)
108{  detail::atomic_cas32(const_cast<boost::uint32_t*>(&m_s), 0, 1);   }
109
110}  //namespace detail {
111}  //namespace interprocess {
112}  //namespace boost {
113
114#endif   //BOOST_INTERPROCESS_DETAIL_EMULATION_MUTEX_HPP