locks.hpp 2.2 KB

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