Clementine
conditionally_enabled_mutex.hpp
1 //
2 // detail/conditionally_enabled_mutex.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2020 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP
12 #define ASIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include "asio/detail/mutex.hpp"
20 #include "asio/detail/noncopyable.hpp"
21 #include "asio/detail/scoped_lock.hpp"
22 
23 #include "asio/detail/push_options.hpp"
24 
25 namespace asio {
26 namespace detail {
27 
28 // Mutex adapter used to conditionally enable or disable locking.
30  : private noncopyable
31 {
32 public:
33  // Helper class to lock and unlock a mutex automatically.
35  : private noncopyable
36  {
37  public:
38  // Tag type used to distinguish constructors.
39  enum adopt_lock_t { adopt_lock };
40 
41  // Constructor adopts a lock that is already held.
43  : mutex_(m),
44  locked_(m.enabled_)
45  {
46  }
47 
48  // Constructor acquires the lock.
50  : mutex_(m)
51  {
52  if (m.enabled_)
53  {
54  mutex_.mutex_.lock();
55  locked_ = true;
56  }
57  else
58  locked_ = false;
59  }
60 
61  // Destructor releases the lock.
62  ~scoped_lock()
63  {
64  if (locked_)
65  mutex_.mutex_.unlock();
66  }
67 
68  // Explicitly acquire the lock.
69  void lock()
70  {
71  if (mutex_.enabled_ && !locked_)
72  {
73  mutex_.mutex_.lock();
74  locked_ = true;
75  }
76  }
77 
78  // Explicitly release the lock.
79  void unlock()
80  {
81  if (locked_)
82  {
83  mutex_.unlock();
84  locked_ = false;
85  }
86  }
87 
88  // Test whether the lock is held.
89  bool locked() const
90  {
91  return locked_;
92  }
93 
94  // Get the underlying mutex.
96  {
97  return mutex_.mutex_;
98  }
99 
100  private:
101  friend class conditionally_enabled_event;
103  bool locked_;
104  };
105 
106  // Constructor.
107  explicit conditionally_enabled_mutex(bool enabled)
108  : enabled_(enabled)
109  {
110  }
111 
112  // Destructor.
114  {
115  }
116 
117  // Determine whether locking is enabled.
118  bool enabled() const
119  {
120  return enabled_;
121  }
122 
123  // Lock the mutex.
124  void lock()
125  {
126  if (enabled_)
127  mutex_.lock();
128  }
129 
130  // Unlock the mutex.
131  void unlock()
132  {
133  if (enabled_)
134  mutex_.unlock();
135  }
136 
137 private:
138  friend class scoped_lock;
139  friend class conditionally_enabled_event;
140  asio::detail::mutex mutex_;
141  const bool enabled_;
142 };
143 
144 } // namespace detail
145 } // namespace asio
146 
147 #include "asio/detail/pop_options.hpp"
148 
149 #endif // ASIO_DETAIL_CONDITIONALLY_ENABLED_MUTEX_HPP
Definition: null_mutex.hpp:30
Definition: conditionally_enabled_event.hpp:31
Definition: noncopyable.hpp:25
Definition: conditionally_enabled_mutex.hpp:29
Definition: chrono.h:284
Definition: conditionally_enabled_mutex.hpp:34
Definition: any_io_executor.hpp:28