named_recursive_mutex.hpp 5.6 KB

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