named_recursive_mutex.hpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_RECURSIVE_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_NAMED_RECURSIVE_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/detail/posix_time_types_wrk.hpp>
  23. #include <boost/interprocess/permissions.hpp>
  24. #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
  25. #include <boost/interprocess/sync/windows/named_recursive_mutex.hpp>
  26. #define BOOST_INTERPROCESS_USE_WINDOWS
  27. #else
  28. #include <boost/interprocess/sync/shm/named_recursive_mutex.hpp>
  29. #endif
  30. //!\file
  31. //!Describes a named named_recursive_mutex class for inter-process synchronization
  32. namespace boost {
  33. namespace interprocess {
  34. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  35. namespace ipcdetail{ class interprocess_tester; }
  36. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  37. //!A recursive mutex with a global name, so it can be found from different
  38. //!processes. This mutex can't be placed in shared memory, and
  39. //!each process should have it's own named_recursive_mutex.
  40. class named_recursive_mutex
  41. {
  42. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  43. //Non-copyable
  44. named_recursive_mutex();
  45. named_recursive_mutex(const named_recursive_mutex &);
  46. named_recursive_mutex &operator=(const named_recursive_mutex &);
  47. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  48. public:
  49. //!Creates a global recursive_mutex with a name.
  50. //!If the recursive_mutex can't be created throws interprocess_exception
  51. named_recursive_mutex(create_only_t create_only, const char *name, const permissions &perm = permissions());
  52. //!Opens or creates a global recursive_mutex with a name.
  53. //!If the recursive_mutex is created, this call is equivalent to
  54. //!named_recursive_mutex(create_only_t, ... )
  55. //!If the recursive_mutex is already created, this call is equivalent
  56. //!named_recursive_mutex(open_only_t, ... )
  57. //!Does not throw
  58. named_recursive_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  59. //!Opens a global recursive_mutex with a name if that recursive_mutex is previously
  60. //!created. If it is not previously created this function throws
  61. //!interprocess_exception.
  62. named_recursive_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_recursive_mutex();
  70. //!Unlocks a previously locked
  71. //!named_recursive_mutex.
  72. void unlock();
  73. //!Locks named_recursive_mutex, sleeps when named_recursive_mutex is already locked.
  74. //!Throws interprocess_exception if a severe error is found.
  75. void lock();
  76. //!Tries to lock the named_recursive_mutex, returns false when named_recursive_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 named_recursive_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 recursive mutex
  85. //!from the system
  86. static bool remove(const char *name);
  87. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  88. private:
  89. friend class ipcdetail::interprocess_tester;
  90. void dont_close_on_destruction();
  91. #if defined(BOOST_INTERPROCESS_USE_WINDOWS)
  92. typedef ipcdetail::windows_named_recursive_mutex impl_t;
  93. #undef BOOST_INTERPROCESS_USE_WINDOWS
  94. #else
  95. typedef ipcdetail::shm_named_recursive_mutex impl_t;
  96. #endif
  97. impl_t m_mut;
  98. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  99. };
  100. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  101. inline named_recursive_mutex::~named_recursive_mutex()
  102. {}
  103. inline void named_recursive_mutex::dont_close_on_destruction()
  104. { ipcdetail::interprocess_tester::dont_close_on_destruction(m_mut); }
  105. inline named_recursive_mutex::named_recursive_mutex(create_only_t, const char *name, const permissions &perm)
  106. : m_mut (create_only, name, perm)
  107. {}
  108. inline named_recursive_mutex::named_recursive_mutex(open_or_create_t, const char *name, const permissions &perm)
  109. : m_mut (open_or_create, name, perm)
  110. {}
  111. inline named_recursive_mutex::named_recursive_mutex(open_only_t, const char *name)
  112. : m_mut (open_only, name)
  113. {}
  114. inline void named_recursive_mutex::lock()
  115. { m_mut.lock(); }
  116. inline void named_recursive_mutex::unlock()
  117. { m_mut.unlock(); }
  118. inline bool named_recursive_mutex::try_lock()
  119. { return m_mut.try_lock(); }
  120. inline bool named_recursive_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  121. { return m_mut.timed_lock(abs_time); }
  122. inline bool named_recursive_mutex::remove(const char *name)
  123. { return impl_t::remove(name); }
  124. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  125. } //namespace interprocess {
  126. } //namespace boost {
  127. #include <boost/interprocess/detail/config_end.hpp>
  128. #endif //BOOST_INTERPROCESS_NAMED_RECURSIVE_MUTEX_HPP