PageRenderTime 20ms CodeModel.GetById 13ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/Src/Dependencies/Boost/boost/interprocess/sync/posix/interprocess_mutex.hpp

http://hadesmem.googlecode.com/
C++ Header | 111 lines | 68 code | 16 blank | 27 comment | 19 complexity | 5f4201bdcd5b378907347a1d28c03c17 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// Parts of the pthread code come from Boost Threads code:
 12//
 13//////////////////////////////////////////////////////////////////////////////
 14//
 15// Copyright (C) 2001-2003
 16// William E. Kempf
 17//
 18// Permission to use, copy, modify, distribute and sell this software
 19// and its documentation for any purpose is hereby granted without fee,
 20// provided that the above copyright notice appear in all copies and
 21// that both that copyright notice and this permission notice appear
 22// in supporting documentation.  William E. Kempf makes no representations
 23// about the suitability of this software for any purpose.
 24// It is provided "as is" without express or implied warranty.
 25//////////////////////////////////////////////////////////////////////////////
 26
 27#include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
 28#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
 29#include <boost/interprocess/exceptions.hpp>
 30#ifndef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 31#  include <boost/interprocess/detail/os_thread_functions.hpp>
 32#endif
 33#include <boost/assert.hpp>
 34
 35namespace boost {
 36namespace interprocess {
 37
 38inline interprocess_mutex::interprocess_mutex()
 39{
 40   detail::mutexattr_wrapper mut_attr;
 41   detail::mutex_initializer mut(m_mut, mut_attr);
 42   mut.release();
 43}
 44
 45inline interprocess_mutex::~interprocess_mutex() 
 46{
 47   int res = pthread_mutex_destroy(&m_mut);
 48   BOOST_ASSERT(res  == 0);(void)res;
 49}
 50
 51inline void interprocess_mutex::lock()
 52{
 53   if (pthread_mutex_lock(&m_mut) != 0) 
 54      throw lock_exception();
 55}
 56
 57inline bool interprocess_mutex::try_lock()
 58{
 59   int res = pthread_mutex_trylock(&m_mut);
 60   if (!(res == 0 || res == EBUSY))
 61      throw lock_exception();
 62   return res == 0;
 63}
 64
 65inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
 66{
 67   if(abs_time == boost::posix_time::pos_infin){
 68      this->lock();
 69      return true;
 70   }
 71   #ifdef BOOST_INTERPROCESS_POSIX_TIMEOUTS
 72
 73   timespec ts = detail::ptime_to_timespec(abs_time);
 74   int res = pthread_mutex_timedlock(&m_mut, &ts);
 75   if (res != 0 && res != ETIMEDOUT)
 76      throw lock_exception();
 77   return res == 0;
 78
 79   #else //BOOST_INTERPROCESS_POSIX_TIMEOUTS
 80
 81   //Obtain current count and target time
 82   boost::posix_time::ptime now = microsec_clock::universal_time();
 83
 84   if(now >= abs_time) return false;
 85
 86   do{
 87      if(this->try_lock()){
 88         break;
 89      }
 90      now = microsec_clock::universal_time();
 91
 92      if(now >= abs_time){
 93         return false;
 94      }
 95      // relinquish current time slice
 96     detail::thread_yield();
 97   }while (true);
 98   return true;
 99
100   #endif   //BOOST_INTERPROCESS_POSIX_TIMEOUTS
101}
102
103inline void interprocess_mutex::unlock()
104{
105   int res = 0;
106   res = pthread_mutex_unlock(&m_mut);
107   BOOST_ASSERT(res == 0);
108}
109
110}  //namespace interprocess {
111}  //namespace boost {