named_mutex.hpp 6.4 KB

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