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 11 #ifndef BOOST_INTERPROCESS_DETAIL_LOCKS_HPP 12 #define BOOST_INTERPROCESS_DETAIL_LOCKS_HPP 13 14 #ifndef BOOST_CONFIG_HPP 15 # include <boost/config.hpp> 16 #endif 17 # 18 #if defined(BOOST_HAS_PRAGMA_ONCE) 19 # pragma once 20 #endif 21 22 #include <boost/interprocess/detail/config_begin.hpp> 23 #include <boost/interprocess/detail/workaround.hpp> 24 25 namespace boost { 26 namespace interprocess { 27 namespace ipcdetail { 28 29 template<class Lock> 30 class internal_mutex_lock 31 { 32 typedef void (internal_mutex_lock::*unspecified_bool_type)(); 33 public: 34 35 typedef typename Lock::mutex_type::internal_mutex_type mutex_type; 36 37 internal_mutex_lock(Lock & l)38 internal_mutex_lock(Lock &l) 39 : l_(l) 40 {} 41 mutex() const42 mutex_type* mutex() const 43 { return l_ ? &l_.mutex()->internal_mutex() : 0; } 44 lock()45 void lock() { l_.lock(); } 46 unlock()47 void unlock() { l_.unlock(); } 48 operator unspecified_bool_type() const49 operator unspecified_bool_type() const 50 { return l_ ? &internal_mutex_lock::lock : 0; } 51 52 private: 53 Lock &l_; 54 }; 55 56 template <class Lock> 57 class lock_inverter 58 { 59 Lock &l_; 60 public: lock_inverter(Lock & l)61 lock_inverter(Lock &l) 62 : l_(l) 63 {} lock()64 void lock() { l_.unlock(); } unlock()65 void unlock() { l_.lock(); } 66 }; 67 68 template <class Lock> 69 class lock_to_sharable 70 { 71 Lock &l_; 72 73 public: lock_to_sharable(Lock & l)74 explicit lock_to_sharable(Lock &l) 75 : l_(l) 76 {} lock()77 void lock() { l_.lock_sharable(); } try_lock()78 bool try_lock(){ return l_.try_lock_sharable(); } unlock()79 void unlock() { l_.unlock_sharable(); } 80 }; 81 82 template <class Lock> 83 class lock_to_wait 84 { 85 Lock &l_; 86 87 public: lock_to_wait(Lock & l)88 explicit lock_to_wait(Lock &l) 89 : l_(l) 90 {} lock()91 void lock() { l_.wait(); } try_lock()92 bool try_lock(){ return l_.try_wait(); } 93 }; 94 95 } //namespace ipcdetail 96 } //namespace interprocess 97 } //namespace boost 98 99 #include <boost/interprocess/detail/config_end.hpp> 100 101 #endif //BOOST_INTERPROCESS_DETAIL_LOCKS_HPP 102