named_mutex.hpp 5.9 KB

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