locks.hpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2012-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_DETAIL_LOCKS_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_LOCKS_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/detail/posix_time_types_wrk.hpp>
  22. namespace boost {
  23. namespace interprocess {
  24. namespace ipcdetail {
  25. template<class Lock>
  26. class internal_mutex_lock
  27. {
  28. typedef void (internal_mutex_lock::*unspecified_bool_type)();
  29. public:
  30. typedef typename Lock::mutex_type::internal_mutex_type mutex_type;
  31. BOOST_INTERPROCESS_FORCEINLINE internal_mutex_lock(Lock &l)
  32. : l_(l)
  33. {}
  34. BOOST_INTERPROCESS_FORCEINLINE mutex_type* mutex() const
  35. { return l_ ? &l_.mutex()->internal_mutex() : 0; }
  36. BOOST_INTERPROCESS_FORCEINLINE void lock() { l_.lock(); }
  37. BOOST_INTERPROCESS_FORCEINLINE void unlock() { l_.unlock(); }
  38. BOOST_INTERPROCESS_FORCEINLINE operator unspecified_bool_type() const
  39. { return l_ ? &internal_mutex_lock::lock : 0; }
  40. private:
  41. Lock &l_;
  42. };
  43. template <class Lock>
  44. class lock_inverter
  45. {
  46. Lock &l_;
  47. public:
  48. BOOST_INTERPROCESS_FORCEINLINE lock_inverter(Lock &l)
  49. : l_(l)
  50. {}
  51. BOOST_INTERPROCESS_FORCEINLINE void lock() { l_.unlock(); }
  52. BOOST_INTERPROCESS_FORCEINLINE void unlock() { l_.lock(); }
  53. };
  54. template <class Lock>
  55. class lock_to_sharable
  56. {
  57. Lock &l_;
  58. public:
  59. BOOST_INTERPROCESS_FORCEINLINE explicit lock_to_sharable(Lock &l)
  60. : l_(l)
  61. {}
  62. BOOST_INTERPROCESS_FORCEINLINE void lock() { l_.lock_sharable(); }
  63. BOOST_INTERPROCESS_FORCEINLINE bool try_lock(){ return l_.try_lock_sharable(); }
  64. BOOST_INTERPROCESS_FORCEINLINE void unlock() { l_.unlock_sharable(); }
  65. };
  66. template <class Lock>
  67. class lock_to_wait
  68. {
  69. Lock &l_;
  70. public:
  71. BOOST_INTERPROCESS_FORCEINLINE explicit lock_to_wait(Lock &l)
  72. : l_(l)
  73. {}
  74. BOOST_INTERPROCESS_FORCEINLINE void lock() { l_.wait(); }
  75. BOOST_INTERPROCESS_FORCEINLINE bool try_lock() { return l_.try_wait(); }
  76. BOOST_INTERPROCESS_FORCEINLINE bool timed_lock(const boost::posix_time::ptime &abs_time)
  77. { return l_.timed_wait(abs_time); }
  78. };
  79. } //namespace ipcdetail
  80. } //namespace interprocess
  81. } //namespace boost
  82. #include <boost/interprocess/detail/config_end.hpp>
  83. #endif //BOOST_INTERPROCESS_DETAIL_LOCKS_HPP