librf
mutex_v2.h
1 #pragma once
2 
3 namespace resumef
4 {
5 #ifndef DOXYGEN_SKIP_PROPERTY
6  namespace detail
7  {
8  struct mutex_v2_impl;
9  }
10 
11  inline namespace mutex_v2
12  {
13 #endif //DOXYGEN_SKIP_PROPERTY
14 
18 
22  constexpr adopt_manual_unlock_t adopt_manual_unlock;
23 
27  template<class... _Mtxs>
28  struct [[nodiscard]] batch_unlock_t;
29 
34  struct mutex_t
35  {
36  bool is_locked() const;
37 
38  struct lock_awaiter;
39  struct [[nodiscard]] awaiter;
40  struct [[nodiscard]] manual_awaiter;
41 
46  awaiter/*batch_unlock_t*/ lock() const noexcept;
47 
53  awaiter/*batch_unlock_t*/ operator co_await() const noexcept;
54 
61  manual_awaiter/*void*/ lock(adopt_manual_unlock_t manual_unlock_tag) const noexcept;
62 
63 
64  struct [[nodiscard]] try_awaiter;
65 
72  try_awaiter/*bool*/ try_lock() const noexcept;
73 
74  struct [[nodiscard]] unlock_awaiter;
75 
80  unlock_awaiter/*void*/ unlock() const noexcept;
81 
82 
83  struct [[nodiscard]] timeout_awaiter;
84 
90  template <class _Rep, class _Period>
91  timeout_awaiter/*bool*/ try_lock_for(const std::chrono::duration<_Rep, _Period>& dt) const noexcept;
92 
98  template <class _Rep, class _Period>
99  timeout_awaiter/*bool*/ try_lock_until(const std::chrono::time_point<_Rep, _Period>& tp) const noexcept;
100 
101 
107  void lock(void* unique_address) const;
108 
113  bool try_lock(void* unique_address) const;
114 
120  template <class _Rep, class _Period>
121  bool try_lock_for(const std::chrono::duration<_Rep, _Period>& dt, void* unique_address);
122 
128  template <class _Rep, class _Period>
129  bool try_lock_until(const std::chrono::time_point<_Rep, _Period>& tp, void* unique_address);
130 
135  void unlock(void* unique_address) const;
136 
137 
143  template<class... _Mtxs
144 #ifndef DOXYGEN_SKIP_PROPERTY
145  , typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
146 #endif //DOXYGEN_SKIP_PROPERTY
147  >
148  static future_t<batch_unlock_t<_Mtxs...>> lock(_Mtxs&... mtxs);
149 
156  template<class... _Mtxs
157 #ifndef DOXYGEN_SKIP_PROPERTY
158  , typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
159 #endif //DOXYGEN_SKIP_PROPERTY
160  >
161  static future_t<> lock(adopt_manual_unlock_t manual_unlock_tag, _Mtxs&... mtxs);
162 
168  template<class... _Mtxs
169 #ifndef DOXYGEN_SKIP_PROPERTY
170  , typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
171 #endif //DOXYGEN_SKIP_PROPERTY
172  >
173  static future_t<> unlock(_Mtxs&... mtxs);
174 
175 
182  template<class... _Mtxs
183 #ifndef DOXYGEN_SKIP_PROPERTY
184  , typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
185 #endif //DOXYGEN_SKIP_PROPERTY
186  >
187  static batch_unlock_t<_Mtxs...> lock(void* unique_address, _Mtxs&... mtxs);
188 
195  template<class... _Mtxs
196 #ifndef DOXYGEN_SKIP_PROPERTY
197  , typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
198 #endif //DOXYGEN_SKIP_PROPERTY
199  >
200  static void lock(adopt_manual_unlock_t manual_unlock_tag, void* unique_address, _Mtxs&... mtxs);
201 
207  template<class... _Mtxs
208 #ifndef DOXYGEN_SKIP_PROPERTY
209  , typename = std::enable_if_t<std::conjunction_v<std::is_same<remove_cvref_t<_Mtxs>, mutex_t>...>>
210 #endif //DOXYGEN_SKIP_PROPERTY
211  >
212  static void unlock(void* unique_address, _Mtxs&... mtxs);
213 
214  mutex_t();
215 
219  mutex_t(std::adopt_lock_t) noexcept;
220  ~mutex_t() noexcept;
221 
222  mutex_t(const mutex_t&) = default;
223  mutex_t(mutex_t&&) = default;
224  mutex_t& operator = (const mutex_t&) = default;
225  mutex_t& operator = (mutex_t&&) = default;
226 
227 #ifndef DOXYGEN_SKIP_PROPERTY
228  typedef std::shared_ptr<detail::mutex_v2_impl> mutex_impl_ptr;
229  typedef std::chrono::system_clock clock_type;
230  private:
231  struct _MutexAwaitAssembleT;
232 
233  template<class... _Mtxs> friend struct batch_unlock_t;
234 
235  mutex_impl_ptr _mutex;
236 #endif //DOXYGEN_SKIP_PROPERTY
237  };
238  }
239 }
resumef::adopt_manual_unlock_t
提示手工解锁,故相关的lock()函数不再返回batch_unlock_t。
Definition: mutex_v2.h:17
resumef::future_t
用于resumef协程的返回值。
Definition: future.h:14
resumef::batch_unlock_t
在析构的时候自动解锁mutex_t的辅助类。
Definition: mutex_v2.h:28
resumef::mutex_t
支持递归的锁。
Definition: mutex_v2.h:34