named_mutex.hpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 create_only, const char *name, const permissions &perm = permissions());
  40. posix_named_mutex(open_or_create_t open_or_create, const char *name, const permissions &perm = permissions());
  41. posix_named_mutex(open_only_t open_only, const char *name);
  42. ~posix_named_mutex();
  43. void unlock();
  44. void lock();
  45. bool try_lock();
  46. bool timed_lock(const boost::posix_time::ptime &abs_time);
  47. static bool remove(const char *name);
  48. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  49. private:
  50. friend class interprocess_tester;
  51. void dont_close_on_destruction();
  52. posix_named_semaphore m_sem;
  53. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  54. };
  55. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  56. inline posix_named_mutex::posix_named_mutex(create_only_t, const char *name, const permissions &perm)
  57. : m_sem(create_only, name, 1, perm)
  58. {}
  59. inline posix_named_mutex::posix_named_mutex(open_or_create_t, const char *name, const permissions &perm)
  60. : m_sem(open_or_create, name, 1, perm)
  61. {}
  62. inline posix_named_mutex::posix_named_mutex(open_only_t, const char *name)
  63. : m_sem(open_only, name)
  64. {}
  65. inline void posix_named_mutex::dont_close_on_destruction()
  66. { interprocess_tester::dont_close_on_destruction(m_sem); }
  67. inline posix_named_mutex::~posix_named_mutex()
  68. {}
  69. inline void posix_named_mutex::lock()
  70. { m_sem.wait(); }
  71. inline void posix_named_mutex::unlock()
  72. { m_sem.post(); }
  73. inline bool posix_named_mutex::try_lock()
  74. { return m_sem.try_wait(); }
  75. inline bool posix_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
  76. { return m_sem.timed_wait(abs_time); }
  77. inline bool posix_named_mutex::remove(const char *name)
  78. { return posix_named_semaphore::remove(name); }
  79. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  80. } //namespace ipcdetail {
  81. } //namespace interprocess {
  82. } //namespace boost {
  83. #include <boost/interprocess/detail/config_end.hpp>
  84. #endif //BOOST_INTERPROCESS_POSIX_NAMED_MUTEX_HPP