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