PageRenderTime 27ms CodeModel.GetById 12ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://hadesmem.googlecode.com/
C++ Header | 235 lines | 136 code | 49 blank | 50 comment | 5 complexity | 8a7720fde65a5c2940986535ca9f0aa5 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_NAMED_MUTEX_HPP
 12#define BOOST_INTERPROCESS_NAMED_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/creation_tags.hpp>
 21#include <boost/interprocess/exceptions.hpp>
 22#include <boost/interprocess/sync/emulation/named_creation_functor.hpp>
 23#include <boost/interprocess/detail/interprocess_tester.hpp>
 24#include <boost/interprocess/permissions.hpp>
 25
 26#if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
 27   #include <boost/interprocess/sync/posix/semaphore_wrapper.hpp>
 28#else
 29   #include <boost/interprocess/shared_memory_object.hpp>
 30   #include <boost/interprocess/sync/interprocess_mutex.hpp>
 31   #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
 32   #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
 33#endif
 34
 35//!\file
 36//!Describes a named mutex class for inter-process synchronization
 37
 38namespace boost {
 39namespace interprocess {
 40
 41class named_condition;
 42
 43//!A mutex with a global name, so it can be found from different 
 44//!processes. This mutex can't be placed in shared memory, and
 45//!each process should have it's own named_mutex.
 46class named_mutex
 47{
 48   /// @cond
 49
 50   //Non-copyable
 51   named_mutex();
 52   named_mutex(const named_mutex &);
 53   named_mutex &operator=(const named_mutex &);
 54   friend class named_condition;
 55   /// @endcond
 56
 57   public:
 58   //!Creates a global interprocess_mutex with a name.
 59   //!Throws interprocess_exception on error.
 60   named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
 61
 62   //!Opens or creates a global mutex with a name. 
 63   //!If the mutex is created, this call is equivalent to
 64   //!named_mutex(create_only_t, ... )
 65   //!If the mutex is already created, this call is equivalent
 66   //!named_mutex(open_only_t, ... )
 67   //!Does not throw
 68   named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
 69
 70   //!Opens a global mutex with a name if that mutex is previously
 71   //!created. If it is not previously created this function throws
 72   //!interprocess_exception.
 73   named_mutex(open_only_t open_only, const char *name);
 74
 75   //!Destroys *this and indicates that the calling process is finished using
 76   //!the resource. The destructor function will deallocate
 77   //!any system resources allocated by the system for use by this process for
 78   //!this resource. The resource can still be opened again calling
 79   //!the open constructor overload. To erase the resource from the system
 80   //!use remove().
 81   ~named_mutex();
 82
 83   //!Unlocks a previously locked
 84   //!interprocess_mutex.
 85   void unlock();
 86
 87   //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
 88   //!Throws interprocess_exception if a severe error is found
 89   void lock();
 90
 91   //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex 
 92   //!is already locked, returns true when success.
 93   //!Throws interprocess_exception if a severe error is found
 94   bool try_lock();
 95
 96   //!Tries to lock the interprocess_mutex until time abs_time,
 97   //!Returns false when timeout expires, returns true when locks.
 98   //!Throws interprocess_exception if a severe error is found
 99   bool timed_lock(const boost::posix_time::ptime &abs_time);
100
101   //!Erases a named mutex from the system.
102   //!Returns false on error. Never throws.
103   static bool remove(const char *name);
104
105   /// @cond
106   private:
107   friend class detail::interprocess_tester;
108   void dont_close_on_destruction();
109
110   #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
111   detail::named_semaphore_wrapper m_sem;
112   #else
113   interprocess_mutex *mutex() const
114   {  return static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
115
116   detail::managed_open_or_create_impl<shared_memory_object> m_shmem;
117   typedef detail::named_creation_functor<interprocess_mutex> construct_func_t;
118   #endif
119   /// @endcond
120};
121
122/// @cond
123
124#if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
125
126inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
127   :  m_sem(detail::DoCreate, name, 1, perm)
128{}
129
130inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
131   :  m_sem(detail::DoOpenOrCreate, name, 1, perm)
132{}
133
134inline named_mutex::named_mutex(open_only_t, const char *name)
135   :  m_sem(detail::DoOpen, name, 1, permissions())
136{}
137
138inline void named_mutex::dont_close_on_destruction()
139{  detail::interprocess_tester::dont_close_on_destruction(m_sem);  }
140
141inline named_mutex::~named_mutex()
142{}
143
144inline void named_mutex::lock()
145{  m_sem.wait();  }
146
147inline void named_mutex::unlock()
148{  m_sem.post();  }
149
150inline bool named_mutex::try_lock()
151{  return m_sem.try_wait();  }
152
153inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
154{
155   if(abs_time == boost::posix_time::pos_infin){
156      this->lock();
157      return true;
158   }
159   return m_sem.timed_wait(abs_time);
160}
161
162inline bool named_mutex::remove(const char *name)
163{  return detail::named_semaphore_wrapper::remove(name);   }
164
165#else
166
167inline void named_mutex::dont_close_on_destruction()
168{  detail::interprocess_tester::dont_close_on_destruction(m_shmem);  }
169
170inline named_mutex::~named_mutex()
171{}
172
173inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
174   :  m_shmem  (create_only
175               ,name
176               ,sizeof(interprocess_mutex) +
177                  detail::managed_open_or_create_impl<shared_memory_object>::
178                     ManagedOpenOrCreateUserOffset
179               ,read_write
180               ,0
181               ,construct_func_t(detail::DoCreate)
182               ,perm)
183{}
184
185inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
186   :  m_shmem  (open_or_create
187               ,name
188               ,sizeof(interprocess_mutex) +
189                  detail::managed_open_or_create_impl<shared_memory_object>::
190                     ManagedOpenOrCreateUserOffset
191               ,read_write
192               ,0
193               ,construct_func_t(detail::DoOpenOrCreate)
194               ,perm)
195{}
196
197inline named_mutex::named_mutex(open_only_t, const char *name)
198   :  m_shmem  (open_only
199               ,name
200               ,read_write
201               ,0
202               ,construct_func_t(detail::DoOpen))
203{}
204
205inline void named_mutex::lock()
206{  this->mutex()->lock();  }
207
208inline void named_mutex::unlock()
209{  this->mutex()->unlock();  }
210
211inline bool named_mutex::try_lock()
212{  return this->mutex()->try_lock();  }
213
214inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
215{
216   if(abs_time == boost::posix_time::pos_infin){
217      this->lock();
218      return true;
219   }
220   return this->mutex()->timed_lock(abs_time);
221}
222
223inline bool named_mutex::remove(const char *name)
224{  return shared_memory_object::remove(name); }
225
226#endif
227
228/// @endcond
229
230}  //namespace interprocess {
231}  //namespace boost {
232
233#include <boost/interprocess/detail/config_end.hpp>
234
235#endif   //BOOST_INTERPROCESS_NAMED_MUTEX_HPP