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