Back to home page

EIC code displayed by LXR

 
 

    


Warning, /include/c++/v1/mutex is written in an unsupported language. File is not indexed.

0001 // -*- C++ -*-
0002 //===----------------------------------------------------------------------===//
0003 //
0004 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0005 // See https://llvm.org/LICENSE.txt for license information.
0006 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0007 //
0008 //===----------------------------------------------------------------------===//
0009 
0010 #ifndef _LIBCPP_MUTEX
0011 #define _LIBCPP_MUTEX
0012 
0013 /*
0014     mutex synopsis
0015 
0016 namespace std
0017 {
0018 
0019 class mutex
0020 {
0021 public:
0022      constexpr mutex() noexcept;
0023      ~mutex();
0024 
0025     mutex(const mutex&) = delete;
0026     mutex& operator=(const mutex&) = delete;
0027 
0028     void lock();
0029     bool try_lock();
0030     void unlock();
0031 
0032     typedef pthread_mutex_t* native_handle_type;
0033     native_handle_type native_handle();
0034 };
0035 
0036 class recursive_mutex
0037 {
0038 public:
0039      recursive_mutex();
0040      ~recursive_mutex();
0041 
0042     recursive_mutex(const recursive_mutex&) = delete;
0043     recursive_mutex& operator=(const recursive_mutex&) = delete;
0044 
0045     void lock();
0046     bool try_lock() noexcept;
0047     void unlock();
0048 
0049     typedef pthread_mutex_t* native_handle_type;
0050     native_handle_type native_handle();
0051 };
0052 
0053 class timed_mutex
0054 {
0055 public:
0056      timed_mutex();
0057      ~timed_mutex();
0058 
0059     timed_mutex(const timed_mutex&) = delete;
0060     timed_mutex& operator=(const timed_mutex&) = delete;
0061 
0062     void lock();
0063     bool try_lock();
0064     template <class Rep, class Period>
0065         bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
0066     template <class Clock, class Duration>
0067         bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
0068     void unlock();
0069 };
0070 
0071 class recursive_timed_mutex
0072 {
0073 public:
0074      recursive_timed_mutex();
0075      ~recursive_timed_mutex();
0076 
0077     recursive_timed_mutex(const recursive_timed_mutex&) = delete;
0078     recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
0079 
0080     void lock();
0081     bool try_lock() noexcept;
0082     template <class Rep, class Period>
0083         bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
0084     template <class Clock, class Duration>
0085         bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
0086     void unlock();
0087 };
0088 
0089 struct defer_lock_t { explicit defer_lock_t() = default; };
0090 struct try_to_lock_t { explicit try_to_lock_t() = default; };
0091 struct adopt_lock_t { explicit adopt_lock_t() = default; };
0092 
0093 inline constexpr defer_lock_t  defer_lock{};
0094 inline constexpr try_to_lock_t try_to_lock{};
0095 inline constexpr adopt_lock_t  adopt_lock{};
0096 
0097 template <class Mutex>
0098 class lock_guard
0099 {
0100 public:
0101     typedef Mutex mutex_type;
0102 
0103     explicit lock_guard(mutex_type& m);
0104     lock_guard(mutex_type& m, adopt_lock_t);
0105     ~lock_guard();
0106 
0107     lock_guard(lock_guard const&) = delete;
0108     lock_guard& operator=(lock_guard const&) = delete;
0109 };
0110 
0111 template <class... MutexTypes>
0112 class scoped_lock // C++17
0113 {
0114 public:
0115     using mutex_type = Mutex;  // Only if sizeof...(MutexTypes) == 1
0116 
0117     explicit scoped_lock(MutexTypes&... m);
0118     scoped_lock(adopt_lock_t, MutexTypes&... m);
0119     ~scoped_lock();
0120     scoped_lock(scoped_lock const&) = delete;
0121     scoped_lock& operator=(scoped_lock const&) = delete;
0122 private:
0123     tuple<MutexTypes&...> pm; // exposition only
0124 };
0125 
0126 template <class Mutex>
0127 class unique_lock
0128 {
0129 public:
0130     typedef Mutex mutex_type;
0131     unique_lock() noexcept;
0132     explicit unique_lock(mutex_type& m);
0133     unique_lock(mutex_type& m, defer_lock_t) noexcept;
0134     unique_lock(mutex_type& m, try_to_lock_t);
0135     unique_lock(mutex_type& m, adopt_lock_t);
0136     template <class Clock, class Duration>
0137         unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time);
0138     template <class Rep, class Period>
0139         unique_lock(mutex_type& m, const chrono::duration<Rep, Period>& rel_time);
0140     ~unique_lock();
0141 
0142     unique_lock(unique_lock const&) = delete;
0143     unique_lock& operator=(unique_lock const&) = delete;
0144 
0145     unique_lock(unique_lock&& u) noexcept;
0146     unique_lock& operator=(unique_lock&& u) noexcept;
0147 
0148     void lock();
0149     bool try_lock();
0150 
0151     template <class Rep, class Period>
0152         bool try_lock_for(const chrono::duration<Rep, Period>& rel_time);
0153     template <class Clock, class Duration>
0154         bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
0155 
0156     void unlock();
0157 
0158     void swap(unique_lock& u) noexcept;
0159     mutex_type* release() noexcept;
0160 
0161     bool owns_lock() const noexcept;
0162     explicit operator bool () const noexcept;
0163     mutex_type* mutex() const noexcept;
0164 };
0165 
0166 template <class Mutex>
0167   void swap(unique_lock<Mutex>& x, unique_lock<Mutex>& y) noexcept;
0168 
0169 template <class L1, class L2, class... L3>
0170   int try_lock(L1&, L2&, L3&...);
0171 template <class L1, class L2, class... L3>
0172   void lock(L1&, L2&, L3&...);
0173 
0174 struct once_flag
0175 {
0176     constexpr once_flag() noexcept;
0177 
0178     once_flag(const once_flag&) = delete;
0179     once_flag& operator=(const once_flag&) = delete;
0180 };
0181 
0182 template<class Callable, class ...Args>
0183   void call_once(once_flag& flag, Callable&& func, Args&&... args);
0184 
0185 }  // std
0186 
0187 */
0188 
0189 #if __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0190 #  include <__cxx03/mutex>
0191 #else
0192 #  include <__chrono/steady_clock.h>
0193 #  include <__chrono/time_point.h>
0194 #  include <__condition_variable/condition_variable.h>
0195 #  include <__config>
0196 #  include <__mutex/lock_guard.h>
0197 #  include <__mutex/mutex.h>
0198 #  include <__mutex/once_flag.h>
0199 #  include <__mutex/tag_types.h>
0200 #  include <__mutex/unique_lock.h>
0201 #  include <__thread/id.h>
0202 #  include <__thread/support.h>
0203 #  include <__utility/forward.h>
0204 #  include <limits>
0205 #  ifndef _LIBCPP_CXX03_LANG
0206 #    include <tuple>
0207 #  endif
0208 #  include <version>
0209 
0210 #  if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0211 #    pragma GCC system_header
0212 #  endif
0213 
0214 _LIBCPP_PUSH_MACROS
0215 #  include <__undef_macros>
0216 
0217 _LIBCPP_BEGIN_NAMESPACE_STD
0218 
0219 #  if _LIBCPP_HAS_THREADS
0220 
0221 class _LIBCPP_EXPORTED_FROM_ABI recursive_mutex {
0222   __libcpp_recursive_mutex_t __m_;
0223 
0224 public:
0225   recursive_mutex();
0226   ~recursive_mutex();
0227 
0228   recursive_mutex(const recursive_mutex&)            = delete;
0229   recursive_mutex& operator=(const recursive_mutex&) = delete;
0230 
0231   void lock();
0232   bool try_lock() _NOEXCEPT;
0233   void unlock() _NOEXCEPT;
0234 
0235   typedef __libcpp_recursive_mutex_t* native_handle_type;
0236 
0237   _LIBCPP_HIDE_FROM_ABI native_handle_type native_handle() { return &__m_; }
0238 };
0239 
0240 class _LIBCPP_EXPORTED_FROM_ABI timed_mutex {
0241   mutex __m_;
0242   condition_variable __cv_;
0243   bool __locked_;
0244 
0245 public:
0246   timed_mutex();
0247   ~timed_mutex();
0248 
0249   timed_mutex(const timed_mutex&)            = delete;
0250   timed_mutex& operator=(const timed_mutex&) = delete;
0251 
0252 public:
0253   void lock();
0254   bool try_lock() _NOEXCEPT;
0255   template <class _Rep, class _Period>
0256   _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
0257     return try_lock_until(chrono::steady_clock::now() + __d);
0258   }
0259   template <class _Clock, class _Duration>
0260   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
0261   try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
0262   void unlock() _NOEXCEPT;
0263 };
0264 
0265 template <class _Clock, class _Duration>
0266 bool timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
0267   using namespace chrono;
0268   unique_lock<mutex> __lk(__m_);
0269   bool __no_timeout = _Clock::now() < __t;
0270   while (__no_timeout && __locked_)
0271     __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
0272   if (!__locked_) {
0273     __locked_ = true;
0274     return true;
0275   }
0276   return false;
0277 }
0278 
0279 class _LIBCPP_EXPORTED_FROM_ABI recursive_timed_mutex {
0280   mutex __m_;
0281   condition_variable __cv_;
0282   size_t __count_;
0283   __thread_id __id_;
0284 
0285 public:
0286   recursive_timed_mutex();
0287   ~recursive_timed_mutex();
0288 
0289   recursive_timed_mutex(const recursive_timed_mutex&)            = delete;
0290   recursive_timed_mutex& operator=(const recursive_timed_mutex&) = delete;
0291 
0292   void lock();
0293   bool try_lock() _NOEXCEPT;
0294   template <class _Rep, class _Period>
0295   _LIBCPP_HIDE_FROM_ABI bool try_lock_for(const chrono::duration<_Rep, _Period>& __d) {
0296     return try_lock_until(chrono::steady_clock::now() + __d);
0297   }
0298   template <class _Clock, class _Duration>
0299   _LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS bool
0300   try_lock_until(const chrono::time_point<_Clock, _Duration>& __t);
0301   void unlock() _NOEXCEPT;
0302 };
0303 
0304 template <class _Clock, class _Duration>
0305 bool recursive_timed_mutex::try_lock_until(const chrono::time_point<_Clock, _Duration>& __t) {
0306   using namespace chrono;
0307   __thread_id __id = this_thread::get_id();
0308   unique_lock<mutex> __lk(__m_);
0309   if (__id == __id_) {
0310     if (__count_ == numeric_limits<size_t>::max())
0311       return false;
0312     ++__count_;
0313     return true;
0314   }
0315   bool __no_timeout = _Clock::now() < __t;
0316   while (__no_timeout && __count_ != 0)
0317     __no_timeout = __cv_.wait_until(__lk, __t) == cv_status::no_timeout;
0318   if (__count_ == 0) {
0319     __count_ = 1;
0320     __id_    = __id;
0321     return true;
0322   }
0323   return false;
0324 }
0325 
0326 template <class _L0, class _L1>
0327 _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1) {
0328   unique_lock<_L0> __u0(__l0, try_to_lock_t());
0329   if (__u0.owns_lock()) {
0330     if (__l1.try_lock()) {
0331       __u0.release();
0332       return -1;
0333     } else
0334       return 1;
0335   }
0336   return 0;
0337 }
0338 
0339 #    ifndef _LIBCPP_CXX03_LANG
0340 
0341 template <class _L0, class _L1, class _L2, class... _L3>
0342 _LIBCPP_HIDE_FROM_ABI int try_lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
0343   int __r = 0;
0344   unique_lock<_L0> __u0(__l0, try_to_lock);
0345   if (__u0.owns_lock()) {
0346     __r = std::try_lock(__l1, __l2, __l3...);
0347     if (__r == -1)
0348       __u0.release();
0349     else
0350       ++__r;
0351   }
0352   return __r;
0353 }
0354 
0355 #    endif // _LIBCPP_CXX03_LANG
0356 
0357 template <class _L0, class _L1>
0358 _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1) {
0359   while (true) {
0360     {
0361       unique_lock<_L0> __u0(__l0);
0362       if (__l1.try_lock()) {
0363         __u0.release();
0364         break;
0365       }
0366     }
0367     __libcpp_thread_yield();
0368     {
0369       unique_lock<_L1> __u1(__l1);
0370       if (__l0.try_lock()) {
0371         __u1.release();
0372         break;
0373       }
0374     }
0375     __libcpp_thread_yield();
0376   }
0377 }
0378 
0379 #    ifndef _LIBCPP_CXX03_LANG
0380 
0381 template <class _L0, class _L1, class _L2, class... _L3>
0382 void __lock_first(int __i, _L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
0383   while (true) {
0384     switch (__i) {
0385     case 0: {
0386       unique_lock<_L0> __u0(__l0);
0387       __i = std::try_lock(__l1, __l2, __l3...);
0388       if (__i == -1) {
0389         __u0.release();
0390         return;
0391       }
0392     }
0393       ++__i;
0394       __libcpp_thread_yield();
0395       break;
0396     case 1: {
0397       unique_lock<_L1> __u1(__l1);
0398       __i = std::try_lock(__l2, __l3..., __l0);
0399       if (__i == -1) {
0400         __u1.release();
0401         return;
0402       }
0403     }
0404       if (__i == sizeof...(_L3) + 1)
0405         __i = 0;
0406       else
0407         __i += 2;
0408       __libcpp_thread_yield();
0409       break;
0410     default:
0411       std::__lock_first(__i - 2, __l2, __l3..., __l0, __l1);
0412       return;
0413     }
0414   }
0415 }
0416 
0417 template <class _L0, class _L1, class _L2, class... _L3>
0418 inline _LIBCPP_HIDE_FROM_ABI void lock(_L0& __l0, _L1& __l1, _L2& __l2, _L3&... __l3) {
0419   std::__lock_first(0, __l0, __l1, __l2, __l3...);
0420 }
0421 
0422 #    endif // _LIBCPP_CXX03_LANG
0423 
0424 #    if _LIBCPP_STD_VER >= 17
0425 template <class... _Mutexes>
0426 class _LIBCPP_TEMPLATE_VIS scoped_lock;
0427 
0428 template <>
0429 class _LIBCPP_TEMPLATE_VIS scoped_lock<> {
0430 public:
0431   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock() {}
0432   ~scoped_lock() = default;
0433 
0434   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t) {}
0435 
0436   scoped_lock(scoped_lock const&)            = delete;
0437   scoped_lock& operator=(scoped_lock const&) = delete;
0438 };
0439 
0440 template <class _Mutex>
0441 class _LIBCPP_TEMPLATE_VIS _LIBCPP_THREAD_SAFETY_ANNOTATION(scoped_lockable) scoped_lock<_Mutex> {
0442 public:
0443   typedef _Mutex mutex_type;
0444 
0445 private:
0446   mutex_type& __m_;
0447 
0448 public:
0449   [[nodiscard]]
0450   _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(mutex_type& __m) _LIBCPP_THREAD_SAFETY_ANNOTATION(acquire_capability(__m))
0451       : __m_(__m) {
0452     __m_.lock();
0453   }
0454 
0455   ~scoped_lock() _LIBCPP_THREAD_SAFETY_ANNOTATION(release_capability()) { __m_.unlock(); }
0456 
0457   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(adopt_lock_t, mutex_type& __m)
0458       _LIBCPP_THREAD_SAFETY_ANNOTATION(requires_capability(__m))
0459       : __m_(__m) {}
0460 
0461   scoped_lock(scoped_lock const&)            = delete;
0462   scoped_lock& operator=(scoped_lock const&) = delete;
0463 };
0464 
0465 template <class... _MArgs>
0466 class _LIBCPP_TEMPLATE_VIS scoped_lock {
0467   static_assert(sizeof...(_MArgs) > 1, "At least 2 lock types required");
0468   typedef tuple<_MArgs&...> _MutexTuple;
0469 
0470 public:
0471   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI explicit scoped_lock(_MArgs&... __margs) : __t_(__margs...) {
0472     std::lock(__margs...);
0473   }
0474 
0475   [[nodiscard]] _LIBCPP_HIDE_FROM_ABI scoped_lock(adopt_lock_t, _MArgs&... __margs) : __t_(__margs...) {}
0476 
0477   _LIBCPP_HIDE_FROM_ABI ~scoped_lock() {
0478     typedef typename __make_tuple_indices<sizeof...(_MArgs)>::type _Indices;
0479     __unlock_unpack(_Indices{}, __t_);
0480   }
0481 
0482   scoped_lock(scoped_lock const&)            = delete;
0483   scoped_lock& operator=(scoped_lock const&) = delete;
0484 
0485 private:
0486   template <size_t... _Indx>
0487   _LIBCPP_HIDE_FROM_ABI static void __unlock_unpack(__tuple_indices<_Indx...>, _MutexTuple& __mt) {
0488     (std::get<_Indx>(__mt).unlock(), ...);
0489   }
0490 
0491   _MutexTuple __t_;
0492 };
0493 _LIBCPP_CTAD_SUPPORTED_FOR_TYPE(scoped_lock);
0494 
0495 #    endif // _LIBCPP_STD_VER >= 17
0496 #  endif   // _LIBCPP_HAS_THREADS
0497 
0498 _LIBCPP_END_NAMESPACE_STD
0499 
0500 _LIBCPP_POP_MACROS
0501 
0502 #  if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
0503 #    include <atomic>
0504 #    include <concepts>
0505 #    include <cstdlib>
0506 #    include <cstring>
0507 #    include <ctime>
0508 #    include <initializer_list>
0509 #    include <iosfwd>
0510 #    include <new>
0511 #    include <stdexcept>
0512 #    include <system_error>
0513 #    include <type_traits>
0514 #    include <typeinfo>
0515 #  endif
0516 #endif // __cplusplus < 201103L && defined(_LIBCPP_USE_FROZEN_CXX03_HEADERS)
0517 
0518 #endif // _LIBCPP_MUTEX