named_mutex.hpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #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/detail/posix_time_types_wrk.hpp>
  21. #include <boost/interprocess/permissions.hpp>
  22. #if defined(BOOST_INTERPROCESS_NAMED_MUTEX_USES_POSIX_SEMAPHORES)
  23. #include <boost/interprocess/sync/posix/named_mutex.hpp>
  24. #define BOOST_INTERPROCESS_USE_POSIX_SEMAPHORES
  25. #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  26. #include <boost/interprocess/sync/windows/named_mutex.hpp>
  27. #define BOOST_INTERPROCESS_USE_WINDOWS
  28. #else
  29. #include <boost/interprocess/sync/shm/named_mutex.hpp>
  30. #endif
  31. //!\file
  32. //!Describes a named mutex class for inter-process synchronization
  33. namespace boost {
  34. namespace interprocess {
  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 named_mutex
  40. {
  41. /// @cond
  42. //Non-copyable
  43. named_mutex();
  44. named_mutex(const named_mutex &);
  45. named_mutex &operator=(const named_mutex &);
  46. friend class named_condition;
  47. /// @endcond
  48. public:
  49. //!Creates a global interprocess_mutex with a name.
  50. //!Throws interprocess_exception on error.
  51. 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. //!named_mutex(create_only_t, ... )
  55. //!If the mutex is already created, this call is equivalent
  56. //!named_mutex(open_only_t, ... )
  57. //!Does not throw
  58. 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. 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. ~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. /// @cond
  88. private:
  89. friend class ipcdetail::interprocess_tester;
  90. void dont_close_on_destruction();
  91. public:
  92. #if defined(BOOST_INTERPROCESS_USE_POSIX_SEMAPHORES)
  93. typedef ipcdetail::posix_named_mutex internal_mutex_type;
  94. #undef BOOST_INTERPROCESS_USE_POSIX_SEMAPHORES
  95. #elif defined(BOOST_INTERPROCESS_USE_WINDOWS)
  96. typedef ipcdetail::windows_named_mutex internal_mutex_type;
  97. #undef BOOST_INTERPROCESS_USE_WINDOWS
  98. #else
  99. typedef ipcdetail::shm_named_mutex internal_mutex_type;
  100. #endif
  101. internal_mutex_type &internal_mutex()
  102. { return m_mut; }
  103. internal_mutex_type m_mut;
  104. /// @endcond
  105. };
  106. /// @cond
  107. inline named_mutex::named_mutex(create_only_t, const char *name, const permissions &perm)
  108. : m_mut(create_only_t(), name, perm)
  109. {}
  110. inline named_mutex::named_mutex(open_or_create_t, const char *name, const permissions &perm)
  111. : m_mut(open_or_create_t(), name, perm)
  112. {}
  113. inline named_mutex::named_mutex(open_only_t, const char *name)
  114. : m_mut(open_only_t(), name)
  115. {}
  116. inline void named_mutex::dont_close_on_destruction()
  117. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
  118. inline named_mutex::~named_mutex()
  119. {}
  120. inline void named_mutex::lock()
  121. { m_mut.lock(); }
  122. inline void named_mutex::unlock()
  123. { m_mut.unlock(); }
  124. inline bool named_mutex::try_lock()
  125. { return m_mut.try_lock(); }
  126. inline bool named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  127. { return m_mut.timed_lock(abs_time); }
  128. inline bool named_mutex::remove(const char *name)
  129. { return internal_mutex_type::remove(name); }
  130. /// @endcond
  131. } //namespace interprocess {
  132. } //namespace boost {
  133. #include <boost/interprocess/detail/config_end.hpp>
  134. #endif //BOOST_INTERPROCESS_NAMED_MUTEX_HPP