named_mutex.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_POSIX_NAMED_MUTEX_HPP
  11. #define BOOST_INTERPROCESS_POSIX_NAMED_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/interprocess_tester.hpp>
  24. #include <boost/interprocess/permissions.hpp>
  25. #include <boost/interprocess/timed_utils.hpp>
  26. #include <boost/interprocess/sync/posix/named_semaphore.hpp>
  27. namespace boost {
  28. namespace interprocess {
  29. namespace ipcdetail {
  30. class named_condition;
  31. class posix_named_mutex
  32. {
  33. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  34. posix_named_mutex();
  35. posix_named_mutex(const posix_named_mutex &);
  36. posix_named_mutex &operator=(const posix_named_mutex &);
  37. friend class named_condition;
  38. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  39. public:
  40. posix_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
  41. posix_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
  42. posix_named_mutex(open_only_t, const char *name);
  43. ~posix_named_mutex();
  44. void unlock();
  45. void lock();
  46. bool try_lock();
  47. template<class TimePoint>
  48. bool timed_lock(const TimePoint &abs_time);
  49. template<class TimePoint>
  50. bool try_lock_until(const TimePoint &abs_time)
  51. { return this->timed_lock(abs_time); }
  52. template<class Duration>
  53. bool try_lock_for(const Duration &dur)
  54. { return this->timed_lock(duration_to_ustime(dur)); }
  55. static bool remove(const char *name);
  56. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  57. private:
  58. friend class interprocess_tester;
  59. void dont_close_on_destruction();
  60. posix_named_semaphore m_sem;
  61. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  62. };
  63. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  64. inline posix_named_mutex::posix_named_mutex(create_only_t, const char *name, const permissions &perm)
  65. : m_sem(create_only, name, 1, perm)
  66. {}
  67. inline posix_named_mutex::posix_named_mutex(open_or_create_t, const char *name, const permissions &perm)
  68. : m_sem(open_or_create, name, 1, perm)
  69. {}
  70. inline posix_named_mutex::posix_named_mutex(open_only_t, const char *name)
  71. : m_sem(open_only, name)
  72. {}
  73. inline void posix_named_mutex::dont_close_on_destruction()
  74. { interprocess_tester::dont_close_on_destruction(m_sem); }
  75. inline posix_named_mutex::~posix_named_mutex()
  76. {}
  77. inline void posix_named_mutex::lock()
  78. { m_sem.wait(); }
  79. inline void posix_named_mutex::unlock()
  80. { m_sem.post(); }
  81. inline bool posix_named_mutex::try_lock()
  82. { return m_sem.try_wait(); }
  83. template<class TimePoint>
  84. inline bool posix_named_mutex::timed_lock(const TimePoint &abs_time)
  85. { return m_sem.timed_wait(abs_time); }
  86. inline bool posix_named_mutex::remove(const char *name)
  87. { return posix_named_semaphore::remove(name); }
  88. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  89. } //namespace ipcdetail {
  90. } //namespace interprocess {
  91. } //namespace boost {
  92. #include <boost/interprocess/detail/config_end.hpp>
  93. #endif //BOOST_INTERPROCESS_POSIX_NAMED_MUTEX_HPP