named_mutex.hpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-2012. 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. #ifndef BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP
  12. #if (defined _MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif
  15. #include <boost/interprocess/detail/config_begin.hpp>
  16. #include <boost/interprocess/detail/workaround.hpp>
  17. #include <boost/interprocess/creation_tags.hpp>
  18. #include <boost/interprocess/exceptions.hpp>
  19. #include <boost/interprocess/detail/interprocess_tester.hpp>
  20. #include <boost/interprocess/permissions.hpp>
  21. #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
  22. #include <boost/interprocess/shared_memory_object.hpp>
  23. #include <boost/interprocess/sync/interprocess_mutex.hpp>
  24. #include <boost/interprocess/detail/managed_open_or_create_impl.hpp>
  25. #include <boost/interprocess/sync/shm/named_creation_functor.hpp>
  26. //!\file
  27. //!Describes a named mutex class for inter-process synchronization
  28. namespace boost {
  29. namespace interprocess {
  30. namespace ipcdetail {
  31. class named_condition;
  32. //!A mutex with a global name, so it can be found from different
  33. //!processes. This mutex can't be placed in shared memory, and
  34. //!each process should have it's own named mutex.
  35. class shm_named_mutex
  36. {
  37. /// @cond
  38. //Non-copyable
  39. shm_named_mutex();
  40. shm_named_mutex(const shm_named_mutex &);
  41. shm_named_mutex &operator=(const shm_named_mutex &);
  42. friend class named_condition;
  43. /// @endcond
  44. public:
  45. //!Creates a global interprocess_mutex with a name.
  46. //!Throws interprocess_exception on error.
  47. shm_named_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  48. //!Opens or creates a global mutex with a name.
  49. //!If the mutex is created, this call is equivalent to
  50. //!shm_named_mutex(create_only_t, ... )
  51. //!If the mutex is already created, this call is equivalent
  52. //!shm_named_mutex(open_only_t, ... )
  53. //!Does not throw
  54. shm_named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  55. //!Opens a global mutex with a name if that mutex is previously
  56. //!created. If it is not previously created this function throws
  57. //!interprocess_exception.
  58. shm_named_mutex(open_only_t open_only, const char *name);
  59. //!Destroys *this and indicates that the calling process is finished using
  60. //!the resource. The destructor function will deallocate
  61. //!any system resources allocated by the system for use by this process for
  62. //!this resource. The resource can still be opened again calling
  63. //!the open constructor overload. To erase the resource from the system
  64. //!use remove().
  65. ~shm_named_mutex();
  66. //!Unlocks a previously locked
  67. //!interprocess_mutex.
  68. void unlock();
  69. //!Locks interprocess_mutex, sleeps when interprocess_mutex is already locked.
  70. //!Throws interprocess_exception if a severe error is found
  71. void lock();
  72. //!Tries to lock the interprocess_mutex, returns false when interprocess_mutex
  73. //!is already locked, returns true when success.
  74. //!Throws interprocess_exception if a severe error is found
  75. bool try_lock();
  76. //!Tries to lock the interprocess_mutex until time abs_time,
  77. //!Returns false when timeout expires, returns true when locks.
  78. //!Throws interprocess_exception if a severe error is found
  79. bool timed_lock(const boost::posix_time::ptime &abs_time);
  80. //!Erases a named mutex from the system.
  81. //!Returns false on error. Never throws.
  82. static bool remove(const char *name);
  83. /// @cond
  84. typedef interprocess_mutex internal_mutex_type;
  85. interprocess_mutex &internal_mutex()
  86. { return *static_cast<interprocess_mutex*>(m_shmem.get_user_address()); }
  87. private:
  88. friend class ipcdetail::interprocess_tester;
  89. void dont_close_on_destruction();
  90. typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
  91. open_create_impl_t m_shmem;
  92. typedef ipcdetail::named_creation_functor<interprocess_mutex> construct_func_t;
  93. /// @endcond
  94. };
  95. /// @cond
  96. inline void shm_named_mutex::dont_close_on_destruction()
  97. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_shmem); }
  98. inline shm_named_mutex::~shm_named_mutex()
  99. {}
  100. inline shm_named_mutex::shm_named_mutex(create_only_t, const char *name, const permissions &perm)
  101. : m_shmem (create_only
  102. ,name
  103. ,sizeof(interprocess_mutex) +
  104. open_create_impl_t::ManagedOpenOrCreateUserOffset
  105. ,read_write
  106. ,0
  107. ,construct_func_t(ipcdetail::DoCreate)
  108. ,perm)
  109. {}
  110. inline shm_named_mutex::shm_named_mutex(open_or_create_t, const char *name, const permissions &perm)
  111. : m_shmem (open_or_create
  112. ,name
  113. ,sizeof(interprocess_mutex) +
  114. open_create_impl_t::ManagedOpenOrCreateUserOffset
  115. ,read_write
  116. ,0
  117. ,construct_func_t(ipcdetail::DoOpenOrCreate)
  118. ,perm)
  119. {}
  120. inline shm_named_mutex::shm_named_mutex(open_only_t, const char *name)
  121. : m_shmem (open_only
  122. ,name
  123. ,read_write
  124. ,0
  125. ,construct_func_t(ipcdetail::DoOpen))
  126. {}
  127. inline void shm_named_mutex::lock()
  128. { this->internal_mutex().lock(); }
  129. inline void shm_named_mutex::unlock()
  130. { this->internal_mutex().unlock(); }
  131. inline bool shm_named_mutex::try_lock()
  132. { return this->internal_mutex().try_lock(); }
  133. inline bool shm_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  134. {
  135. if(abs_time == boost::posix_time::pos_infin){
  136. this->lock();
  137. return true;
  138. }
  139. return this->internal_mutex().timed_lock(abs_time);
  140. }
  141. inline bool shm_named_mutex::remove(const char *name)
  142. { return shared_memory_object::remove(name); }
  143. /// @endcond
  144. } //namespace ipcdetail {
  145. } //namespace interprocess {
  146. } //namespace boost {
  147. #include <boost/interprocess/detail/config_end.hpp>
  148. #endif //BOOST_INTERPROCESS_SHM_NAMED_MUTEX_HPP