• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include <__assert>
10 #include <__thread/id.h>
11 #include <__utility/exception_guard.h>
12 #include <limits>
13 #include <mutex>
14 
15 #include "include/atomic_support.h"
16 
17 #if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
18 #  pragma comment(lib, "pthread")
19 #endif
20 
21 _LIBCPP_PUSH_MACROS
22 #include <__undef_macros>
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 // ~mutex is defined elsewhere
27 
28 void
lock()29 mutex::lock()
30 {
31     int ec = __libcpp_mutex_lock(&__m_);
32     if (ec)
33         __throw_system_error(ec, "mutex lock failed");
34 }
35 
36 bool
try_lock()37 mutex::try_lock() noexcept
38 {
39     return __libcpp_mutex_trylock(&__m_);
40 }
41 
42 void
unlock()43 mutex::unlock() noexcept
44 {
45     int ec = __libcpp_mutex_unlock(&__m_);
46     (void)ec;
47     _LIBCPP_ASSERT_UNCATEGORIZED(ec == 0, "call to mutex::unlock failed");
48 }
49 
50 // recursive_mutex
51 
recursive_mutex()52 recursive_mutex::recursive_mutex()
53 {
54     int ec = __libcpp_recursive_mutex_init(&__m_);
55     if (ec)
56         __throw_system_error(ec, "recursive_mutex constructor failed");
57 }
58 
~recursive_mutex()59 recursive_mutex::~recursive_mutex()
60 {
61     int e = __libcpp_recursive_mutex_destroy(&__m_);
62     (void)e;
63     _LIBCPP_ASSERT_UNCATEGORIZED(e == 0, "call to ~recursive_mutex() failed");
64 }
65 
66 void
lock()67 recursive_mutex::lock()
68 {
69     int ec = __libcpp_recursive_mutex_lock(&__m_);
70     if (ec)
71         __throw_system_error(ec, "recursive_mutex lock failed");
72 }
73 
74 void
unlock()75 recursive_mutex::unlock() noexcept
76 {
77     int e = __libcpp_recursive_mutex_unlock(&__m_);
78     (void)e;
79     _LIBCPP_ASSERT_UNCATEGORIZED(e == 0, "call to recursive_mutex::unlock() failed");
80 }
81 
82 bool
try_lock()83 recursive_mutex::try_lock() noexcept
84 {
85     return __libcpp_recursive_mutex_trylock(&__m_);
86 }
87 
88 // timed_mutex
89 
timed_mutex()90 timed_mutex::timed_mutex()
91     : __locked_(false)
92 {
93 }
94 
~timed_mutex()95 timed_mutex::~timed_mutex()
96 {
97     lock_guard<mutex> _(__m_);
98 }
99 
100 void
lock()101 timed_mutex::lock()
102 {
103     unique_lock<mutex> lk(__m_);
104     while (__locked_)
105         __cv_.wait(lk);
106     __locked_ = true;
107 }
108 
109 bool
try_lock()110 timed_mutex::try_lock() noexcept
111 {
112     unique_lock<mutex> lk(__m_, try_to_lock);
113     if (lk.owns_lock() && !__locked_)
114     {
115         __locked_ = true;
116         return true;
117     }
118     return false;
119 }
120 
121 void
unlock()122 timed_mutex::unlock() noexcept
123 {
124     lock_guard<mutex> _(__m_);
125     __locked_ = false;
126     __cv_.notify_one();
127 }
128 
129 // recursive_timed_mutex
130 
recursive_timed_mutex()131 recursive_timed_mutex::recursive_timed_mutex()
132     : __count_(0),
133       __id_{}
134 {
135 }
136 
~recursive_timed_mutex()137 recursive_timed_mutex::~recursive_timed_mutex()
138 {
139     lock_guard<mutex> _(__m_);
140 }
141 
142 void
lock()143 recursive_timed_mutex::lock()
144 {
145     __thread_id id = this_thread::get_id();
146     unique_lock<mutex> lk(__m_);
147     if (id ==__id_)
148     {
149         if (__count_ == numeric_limits<size_t>::max())
150             __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
151         ++__count_;
152         return;
153     }
154     while (__count_ != 0)
155         __cv_.wait(lk);
156     __count_ = 1;
157     __id_ = id;
158 }
159 
160 bool
try_lock()161 recursive_timed_mutex::try_lock() noexcept
162 {
163     __thread_id id = this_thread::get_id();
164     unique_lock<mutex> lk(__m_, try_to_lock);
165     if (lk.owns_lock() && (__count_ == 0 || id == __id_))
166     {
167         if (__count_ == numeric_limits<size_t>::max())
168             return false;
169         ++__count_;
170         __id_ = id;
171         return true;
172     }
173     return false;
174 }
175 
176 void
unlock()177 recursive_timed_mutex::unlock() noexcept
178 {
179     unique_lock<mutex> lk(__m_);
180     if (--__count_ == 0)
181     {
182         __id_.__reset();
183         lk.unlock();
184         __cv_.notify_one();
185     }
186 }
187 
188 _LIBCPP_END_NAMESPACE_STD
189 
190 _LIBCPP_POP_MACROS
191