named_mutex.hpp 3.1 KB

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