mutex.cpp revision 227825
1223328Sgavin//===------------------------- mutex.cpp ----------------------------------===//
2223328Sgavin//
3116424Smikeh//                     The LLVM Compiler Infrastructure
4116424Smikeh//
5223328Sgavin// This file is dual licensed under the MIT and the University of Illinois Open
6116424Smikeh// Source Licenses. See LICENSE.TXT for details.
7116424Smikeh//
8116424Smikeh//===----------------------------------------------------------------------===//
9116424Smikeh
10116424Smikeh#include "mutex"
11116424Smikeh#include "limits"
12116424Smikeh#include "system_error"
13116424Smikeh#include "cassert"
14116424Smikeh
15116424Smikeh_LIBCPP_BEGIN_NAMESPACE_STD
16116424Smikeh
17116424Smikehconst defer_lock_t  defer_lock = {};
18116424Smikehconst try_to_lock_t try_to_lock = {};
19116424Smikehconst adopt_lock_t  adopt_lock = {};
20116424Smikeh
21116424Smikehmutex::~mutex()
22116424Smikeh{
23116424Smikeh    int e = pthread_mutex_destroy(&__m_);
24116424Smikeh//     assert(e == 0);
25116424Smikeh}
26116424Smikeh
27116424Smikehvoid
28116424Smikehmutex::lock()
29116424Smikeh{
30116424Smikeh    int ec = pthread_mutex_lock(&__m_);
31116424Smikeh    if (ec)
32116424Smikeh        __throw_system_error(ec, "mutex lock failed");
33116424Smikeh}
34116424Smikeh
35116424Smikehbool
36116424Smikehmutex::try_lock()
37116424Smikeh{
38116424Smikeh    return pthread_mutex_trylock(&__m_) == 0;
39116424Smikeh}
40116424Smikeh
41116424Smikehvoid
42116424Smikehmutex::unlock()
43116424Smikeh{
44116424Smikeh    int ec = pthread_mutex_unlock(&__m_);
45116424Smikeh    assert(ec == 0);
46116424Smikeh}
47116424Smikeh
48116424Smikeh// recursive_mutex
49116424Smikeh
50116424Smikehrecursive_mutex::recursive_mutex()
51116424Smikeh{
52116424Smikeh    pthread_mutexattr_t attr;
53116424Smikeh    int ec = pthread_mutexattr_init(&attr);
54116424Smikeh    if (ec)
55128671Smikeh        goto fail;
56116424Smikeh    ec = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
57116424Smikeh    if (ec)
58116424Smikeh    {
59116424Smikeh        pthread_mutexattr_destroy(&attr);
60116424Smikeh        goto fail;
61116424Smikeh    }
62116424Smikeh    ec = pthread_mutex_init(&__m_, &attr);
63223328Sgavin    if (ec)
64116424Smikeh    {
65116424Smikeh        pthread_mutexattr_destroy(&attr);
66116424Smikeh        goto fail;
67116424Smikeh    }
68116424Smikeh    ec = pthread_mutexattr_destroy(&attr);
69116424Smikeh    if (ec)
70116424Smikeh    {
71116424Smikeh        pthread_mutex_destroy(&__m_);
72116424Smikeh        goto fail;
73116424Smikeh    }
74116424Smikeh    return;
75116424Smikehfail:
76116424Smikeh    __throw_system_error(ec, "recursive_mutex constructor failed");
77116424Smikeh}
78116424Smikeh
79116424Smikehrecursive_mutex::~recursive_mutex()
80116424Smikeh{
81116424Smikeh    int e = pthread_mutex_destroy(&__m_);
82116424Smikeh    assert(e == 0);
83116424Smikeh}
84116424Smikeh
85116424Smikehvoid
86116424Smikehrecursive_mutex::lock()
87223328Sgavin{
88223328Sgavin    int ec = pthread_mutex_lock(&__m_);
89223328Sgavin    if (ec)
90223328Sgavin        __throw_system_error(ec, "recursive_mutex lock failed");
91223328Sgavin}
92223328Sgavin
93223328Sgavinvoid
94223328Sgavinrecursive_mutex::unlock()
95116424Smikeh{
96116424Smikeh    int e = pthread_mutex_unlock(&__m_);
97116424Smikeh    assert(e == 0);
98116424Smikeh}
99116424Smikeh
100116424Smikehbool
101223328Sgavinrecursive_mutex::try_lock()
102116424Smikeh{
103    return pthread_mutex_trylock(&__m_) == 0;
104}
105
106// timed_mutex
107
108timed_mutex::timed_mutex()
109    : __locked_(false)
110{
111}
112
113timed_mutex::~timed_mutex()
114{
115    lock_guard<mutex> _(__m_);
116}
117
118void
119timed_mutex::lock()
120{
121    unique_lock<mutex> lk(__m_);
122    while (__locked_)
123        __cv_.wait(lk);
124    __locked_ = true;
125}
126
127bool
128timed_mutex::try_lock()
129{
130    unique_lock<mutex> lk(__m_, try_to_lock);
131    if (lk.owns_lock() && !__locked_)
132    {
133        __locked_ = true;
134        return true;
135    }
136    return false;
137}
138
139void
140timed_mutex::unlock()
141{
142    lock_guard<mutex> _(__m_);
143    __locked_ = false;
144    __cv_.notify_one();
145}
146
147// recursive_timed_mutex
148
149recursive_timed_mutex::recursive_timed_mutex()
150    : __count_(0),
151      __id_(0)
152{
153}
154
155recursive_timed_mutex::~recursive_timed_mutex()
156{
157    lock_guard<mutex> _(__m_);
158}
159
160void
161recursive_timed_mutex::lock()
162{
163    pthread_t id = pthread_self();
164    unique_lock<mutex> lk(__m_);
165    if (pthread_equal(id, __id_))
166    {
167        if (__count_ == numeric_limits<size_t>::max())
168            __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
169        ++__count_;
170        return;
171    }
172    while (__count_ != 0)
173        __cv_.wait(lk);
174    __count_ = 1;
175    __id_ = id;
176}
177
178bool
179recursive_timed_mutex::try_lock()
180{
181    pthread_t id = pthread_self();
182    unique_lock<mutex> lk(__m_, try_to_lock);
183    if (lk.owns_lock() && (__count_ == 0 || pthread_equal(id, __id_)))
184    {
185        if (__count_ == numeric_limits<size_t>::max())
186            return false;
187        ++__count_;
188        __id_ = id;
189        return true;
190    }
191    return false;
192}
193
194void
195recursive_timed_mutex::unlock()
196{
197    unique_lock<mutex> lk(__m_);
198    if (--__count_ == 0)
199    {
200        __id_ = 0;
201        lk.unlock();
202        __cv_.notify_one();
203    }
204}
205
206// If dispatch_once_f ever handles C++ exceptions, and if one can get to it
207// without illegal macros (unexpected macros not beginning with _UpperCase or
208// __lowercase), and if it stops spinning waiting threads, then call_once should
209// call into dispatch_once_f instead of here. Relevant radar this code needs to
210// keep in sync with:  7741191.
211
212static pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER;
213static pthread_cond_t  cv  = PTHREAD_COND_INITIALIZER;
214
215void
216__call_once(volatile unsigned long& flag, void* arg, void(*func)(void*))
217{
218    pthread_mutex_lock(&mut);
219    while (flag == 1)
220        pthread_cond_wait(&cv, &mut);
221    if (flag == 0)
222    {
223#ifndef _LIBCPP_NO_EXCEPTIONS
224        try
225        {
226#endif  // _LIBCPP_NO_EXCEPTIONS
227            flag = 1;
228            pthread_mutex_unlock(&mut);
229            func(arg);
230            pthread_mutex_lock(&mut);
231            flag = ~0ul;
232            pthread_mutex_unlock(&mut);
233            pthread_cond_broadcast(&cv);
234#ifndef _LIBCPP_NO_EXCEPTIONS
235        }
236        catch (...)
237        {
238            pthread_mutex_lock(&mut);
239            flag = 0ul;
240            pthread_mutex_unlock(&mut);
241            pthread_cond_broadcast(&cv);
242            throw;
243        }
244#endif  // _LIBCPP_NO_EXCEPTIONS
245    }
246    else
247        pthread_mutex_unlock(&mut);
248}
249
250_LIBCPP_END_NAMESPACE_STD
251