PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://hadesmem.googlecode.com/
C++ Header | 148 lines | 70 code | 30 blank | 48 comment | 2 complexity | 43423de8ff9d909e84b9aff93bf0a172 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#ifndef BOOST_INTERPROCESS_MUTEX_HPP
 16#define BOOST_INTERPROCESS_MUTEX_HPP
 17
 18/// @cond
 19
 20#if (defined _MSC_VER) && (_MSC_VER >= 1200)
 21#  pragma once
 22#endif
 23
 24#include <boost/interprocess/detail/config_begin.hpp>
 25#include <boost/interprocess/detail/workaround.hpp>
 26#include <boost/interprocess/detail/posix_time_types_wrk.hpp>
 27#include <boost/assert.hpp>
 28
 29#if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
 30   #include <pthread.h>
 31   #include <errno.h>   
 32   #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
 33   #define BOOST_INTERPROCESS_USE_POSIX
 34#else
 35   #include <boost/interprocess/sync/emulation/mutex.hpp>
 36   #define BOOST_INTERPROCESS_USE_GENERIC_EMULATION
 37
 38namespace boost {
 39namespace interprocess {
 40namespace detail{
 41namespace robust_emulation_helpers {
 42
 43template<class T>
 44class mutex_traits;
 45
 46}}}}
 47
 48#endif
 49
 50/// @endcond
 51
 52//!\file
 53//!Describes a mutex class that can be placed in memory shared by
 54//!several processes.
 55
 56namespace boost {
 57namespace interprocess {
 58
 59class interprocess_condition;
 60
 61//!Wraps a interprocess_mutex that can be placed in shared memory and can be 
 62//!shared between processes. Allows timed lock tries
 63class interprocess_mutex
 64{
 65   /// @cond
 66   //Non-copyable
 67   interprocess_mutex(const interprocess_mutex &);
 68   interprocess_mutex &operator=(const interprocess_mutex &);
 69   friend class interprocess_condition;
 70   /// @endcond
 71   public:
 72
 73   //!Constructor.
 74   //!Throws interprocess_exception on error.
 75   interprocess_mutex();
 76
 77   //!Destructor. If any process uses the mutex after the destructor is called
 78   //!the result is undefined. Does not throw.
 79   ~interprocess_mutex();
 80
 81   //!Effects: The calling thread tries to obtain ownership of the mutex, and
 82   //!   if another thread has ownership of the mutex, it waits until it can
 83   //!   obtain the ownership. If a thread takes ownership of the mutex the
 84   //!   mutex must be unlocked by the same mutex.
 85   //!Throws: interprocess_exception on error.
 86   void lock();
 87
 88   //!Effects: The calling thread tries to obtain ownership of the mutex, and
 89   //!   if another thread has ownership of the mutex returns immediately.
 90   //!Returns: If the thread acquires ownership of the mutex, returns true, if
 91   //!   the another thread has ownership of the mutex, returns false.
 92   //!Throws: interprocess_exception on error.
 93   bool try_lock();
 94
 95   //!Effects: The calling thread will try to obtain exclusive ownership of the
 96   //!   mutex if it can do so in until the specified time is reached. If the
 97   //!   mutex supports recursive locking, the mutex must be unlocked the same
 98   //!   number of times it is locked. 
 99   //!Returns: If the thread acquires ownership of the mutex, returns true, if
100   //!   the timeout expires returns false. 
101   //!Throws: interprocess_exception on error.
102   bool timed_lock(const boost::posix_time::ptime &abs_time);
103
104   //!Effects: The calling thread releases the exclusive ownership of the mutex.
105   //!Throws: interprocess_exception on error.
106   void unlock();
107   /// @cond
108   private:
109
110   #if   defined(BOOST_INTERPROCESS_USE_GENERIC_EMULATION)
111   friend class detail::robust_emulation_helpers::mutex_traits<interprocess_mutex>;
112   void take_ownership(){ mutex.take_ownership(); }
113   detail::emulation_mutex mutex;
114   #elif defined(BOOST_INTERPROCESS_USE_POSIX)
115      pthread_mutex_t   m_mut;
116   #endif   //#if (defined BOOST_INTERPROCESS_WINDOWS)
117   /// @endcond
118};
119
120}  //namespace interprocess {
121}  //namespace boost {
122
123#ifdef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
124#  undef BOOST_INTERPROCESS_USE_GENERIC_EMULATION
125
126namespace boost {
127namespace interprocess {
128
129inline interprocess_mutex::interprocess_mutex(){}
130inline interprocess_mutex::~interprocess_mutex(){}
131inline void interprocess_mutex::lock(){ mutex.lock(); }
132inline bool interprocess_mutex::try_lock(){ return mutex.try_lock(); }
133inline bool interprocess_mutex::timed_lock(const boost::posix_time::ptime &abs_time){ return mutex.timed_lock(abs_time); }
134inline void interprocess_mutex::unlock(){ mutex.unlock(); }
135
136}  //namespace interprocess {
137}  //namespace boost {
138
139#endif
140
141#ifdef BOOST_INTERPROCESS_USE_POSIX
142#include <boost/interprocess/sync/posix/interprocess_mutex.hpp>
143#  undef BOOST_INTERPROCESS_USE_POSIX
144#endif
145
146#include <boost/interprocess/detail/config_end.hpp>
147
148#endif   //BOOST_INTERPROCESS_MUTEX_HPP