named_recursive_mutex.hpp 6.2 KB

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