• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
25 
26 namespace boost {
27 namespace interprocess {
28 namespace ipcdetail {
29 
30 template<class Lock>
31 class internal_mutex_lock
32 {
33    typedef void (internal_mutex_lock::*unspecified_bool_type)();
34    public:
35 
36    typedef typename Lock::mutex_type::internal_mutex_type  mutex_type;
37 
38 
internal_mutex_lock(Lock & l)39    BOOST_INTERPROCESS_FORCEINLINE internal_mutex_lock(Lock &l)
40       : l_(l)
41    {}
42 
mutex() const43    BOOST_INTERPROCESS_FORCEINLINE mutex_type* mutex() const
44    {  return l_ ? &l_.mutex()->internal_mutex() : 0;  }
45 
lock()46    BOOST_INTERPROCESS_FORCEINLINE void lock()    { l_.lock(); }
47 
unlock()48    BOOST_INTERPROCESS_FORCEINLINE void unlock()  { l_.unlock(); }
49 
operator unspecified_bool_type() const50    BOOST_INTERPROCESS_FORCEINLINE operator unspecified_bool_type() const
51    {  return l_ ? &internal_mutex_lock::lock : 0;  }
52 
53    private:
54    Lock &l_;
55 };
56 
57 template <class Lock>
58 class lock_inverter
59 {
60    Lock &l_;
61    public:
lock_inverter(Lock & l)62    BOOST_INTERPROCESS_FORCEINLINE lock_inverter(Lock &l)
63       :  l_(l)
64    {}
65 
lock()66    BOOST_INTERPROCESS_FORCEINLINE void lock()    {   l_.unlock();   }
67 
unlock()68    BOOST_INTERPROCESS_FORCEINLINE void unlock()  {   l_.lock();     }
69 };
70 
71 template <class Lock>
72 class lock_to_sharable
73 {
74    Lock &l_;
75 
76    public:
lock_to_sharable(Lock & l)77    BOOST_INTERPROCESS_FORCEINLINE explicit lock_to_sharable(Lock &l)
78       :  l_(l)
79    {}
80 
lock()81    BOOST_INTERPROCESS_FORCEINLINE void lock()    {  l_.lock_sharable();     }
82 
try_lock()83    BOOST_INTERPROCESS_FORCEINLINE bool try_lock(){  return l_.try_lock_sharable(); }
84 
unlock()85    BOOST_INTERPROCESS_FORCEINLINE void unlock()  {  l_.unlock_sharable();   }
86 };
87 
88 template <class Lock>
89 class lock_to_wait
90 {
91    Lock &l_;
92 
93    public:
lock_to_wait(Lock & l)94    BOOST_INTERPROCESS_FORCEINLINE explicit lock_to_wait(Lock &l)
95       :  l_(l)
96    {}
lock()97    BOOST_INTERPROCESS_FORCEINLINE void lock()     {  l_.wait();     }
98 
try_lock()99    BOOST_INTERPROCESS_FORCEINLINE bool try_lock() {  return l_.try_wait(); }
100 
timed_lock(const boost::posix_time::ptime & abs_time)101    BOOST_INTERPROCESS_FORCEINLINE bool timed_lock(const boost::posix_time::ptime &abs_time)
102       {  return l_.timed_wait(abs_time);   }
103 };
104 
105 }  //namespace ipcdetail
106 }  //namespace interprocess
107 }  //namespace boost
108 
109 #include <boost/interprocess/detail/config_end.hpp>
110 
111 #endif   //BOOST_INTERPROCESS_DETAIL_LOCKS_HPP
112