blob: 2f8504d602dc9f42a9cfe7e142f4bf39aa08e3cd [file] [log] [blame]
Louis Dionneeb8650a2021-11-17 21:25:011//===----------------------------------------------------------------------===//
Howard Hinnant3e519522010-05-11 19:42:162//
Chandler Carruth57b08b02019-01-19 10:56:403// 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
Howard Hinnant3e519522010-05-11 19:42:166//
7//===----------------------------------------------------------------------===//
8
Louis Dionnef87aa192022-02-14 18:41:099#include <__assert>
Louis Dionne053d9e52023-07-04 20:10:4510#include <__thread/id.h>
Daniel McIntoshbe8c2df2023-09-13 20:57:1311#include <__utility/exception_guard.h>
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3912#include <limits>
13#include <mutex>
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3914
Eric Fiseliere8fd1642015-08-18 21:08:5415#include "include/atomic_support.h"
Howard Hinnant3e519522010-05-11 19:42:1616
Louis Dionnecf7d4f52023-11-06 00:08:2417#if defined(__ELF__) && defined(_LIBCPP_LINK_PTHREAD_LIB)
18# pragma comment(lib, "pthread")
Petr Hosek996e62e2019-05-30 01:34:4119#endif
20
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3921_LIBCPP_PUSH_MACROS
22#include <__undef_macros>
23
Howard Hinnant3e519522010-05-11 19:42:1624_LIBCPP_BEGIN_NAMESPACE_STD
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:3925
Eric Fiselier8baf8382019-07-07 01:20:5426// ~mutex is defined elsewhere
Howard Hinnant3e519522010-05-11 19:42:1627
Louis Dionne9783f282023-12-18 19:01:3328void mutex::lock() {
29 int ec = __libcpp_mutex_lock(&__m_);
30 if (ec)
31 __throw_system_error(ec, "mutex lock failed");
Howard Hinnant3e519522010-05-11 19:42:1632}
33
Louis Dionne9783f282023-12-18 19:01:3334bool mutex::try_lock() noexcept { return __libcpp_mutex_trylock(&__m_); }
Howard Hinnant3e519522010-05-11 19:42:1635
Louis Dionne9783f282023-12-18 19:01:3336void mutex::unlock() noexcept {
37 int ec = __libcpp_mutex_unlock(&__m_);
38 (void)ec;
Konstantin Varlamov60824782024-01-23 02:12:5839 _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(
40 ec == 0, "call to mutex::unlock failed. A possible reason is that the mutex wasn't locked");
Howard Hinnant3e519522010-05-11 19:42:1641}
42
43// recursive_mutex
44
Louis Dionne9783f282023-12-18 19:01:3345recursive_mutex::recursive_mutex() {
46 int ec = __libcpp_recursive_mutex_init(&__m_);
47 if (ec)
48 __throw_system_error(ec, "recursive_mutex constructor failed");
Howard Hinnant3e519522010-05-11 19:42:1649}
50
Louis Dionne9783f282023-12-18 19:01:3351recursive_mutex::~recursive_mutex() {
52 int e = __libcpp_recursive_mutex_destroy(&__m_);
53 (void)e;
Konstantin Varlamov60824782024-01-23 02:12:5854 _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(e == 0, "call to ~recursive_mutex() failed");
Howard Hinnant3e519522010-05-11 19:42:1655}
56
Louis Dionne9783f282023-12-18 19:01:3357void recursive_mutex::lock() {
58 int ec = __libcpp_recursive_mutex_lock(&__m_);
59 if (ec)
60 __throw_system_error(ec, "recursive_mutex lock failed");
Howard Hinnant3e519522010-05-11 19:42:1661}
62
Louis Dionne9783f282023-12-18 19:01:3363void recursive_mutex::unlock() noexcept {
64 int e = __libcpp_recursive_mutex_unlock(&__m_);
65 (void)e;
Konstantin Varlamov60824782024-01-23 02:12:5866 _LIBCPP_ASSERT_VALID_EXTERNAL_API_CALL(
67 e == 0, "call to recursive_mutex::unlock() failed. A possible reason is that the mutex wasn't locked");
Howard Hinnant3e519522010-05-11 19:42:1668}
69
Louis Dionne9783f282023-12-18 19:01:3370bool recursive_mutex::try_lock() noexcept { return __libcpp_recursive_mutex_trylock(&__m_); }
Howard Hinnant3e519522010-05-11 19:42:1671
72// timed_mutex
73
Louis Dionne9783f282023-12-18 19:01:3374timed_mutex::timed_mutex() : __locked_(false) {}
75
76timed_mutex::~timed_mutex() { lock_guard<mutex> _(__m_); }
77
78void timed_mutex::lock() {
79 unique_lock<mutex> lk(__m_);
80 while (__locked_)
81 __cv_.wait(lk);
82 __locked_ = true;
Howard Hinnant3e519522010-05-11 19:42:1683}
84
Louis Dionne9783f282023-12-18 19:01:3385bool timed_mutex::try_lock() noexcept {
86 unique_lock<mutex> lk(__m_, try_to_lock);
87 if (lk.owns_lock() && !__locked_) {
Howard Hinnant3e519522010-05-11 19:42:1688 __locked_ = true;
Louis Dionne9783f282023-12-18 19:01:3389 return true;
90 }
91 return false;
Howard Hinnant3e519522010-05-11 19:42:1692}
93
Louis Dionne9783f282023-12-18 19:01:3394void timed_mutex::unlock() noexcept {
95 lock_guard<mutex> _(__m_);
96 __locked_ = false;
97 __cv_.notify_one();
Howard Hinnant3e519522010-05-11 19:42:1698}
99
100// recursive_timed_mutex
101
Louis Dionne9783f282023-12-18 19:01:33102recursive_timed_mutex::recursive_timed_mutex() : __count_(0), __id_{} {}
103
104recursive_timed_mutex::~recursive_timed_mutex() { lock_guard<mutex> _(__m_); }
105
106void recursive_timed_mutex::lock() {
107 __thread_id id = this_thread::get_id();
108 unique_lock<mutex> lk(__m_);
109 if (id == __id_) {
110 if (__count_ == numeric_limits<size_t>::max())
111 __throw_system_error(EAGAIN, "recursive_timed_mutex lock limit reached");
112 ++__count_;
113 return;
114 }
115 while (__count_ != 0)
116 __cv_.wait(lk);
117 __count_ = 1;
118 __id_ = id;
Howard Hinnant3e519522010-05-11 19:42:16119}
120
Louis Dionne9783f282023-12-18 19:01:33121bool recursive_timed_mutex::try_lock() noexcept {
122 __thread_id id = this_thread::get_id();
123 unique_lock<mutex> lk(__m_, try_to_lock);
124 if (lk.owns_lock() && (__count_ == 0 || id == __id_)) {
125 if (__count_ == numeric_limits<size_t>::max())
126 return false;
127 ++__count_;
Howard Hinnant3e519522010-05-11 19:42:16128 __id_ = id;
Louis Dionne9783f282023-12-18 19:01:33129 return true;
130 }
131 return false;
Howard Hinnant3e519522010-05-11 19:42:16132}
133
Louis Dionne9783f282023-12-18 19:01:33134void recursive_timed_mutex::unlock() noexcept {
135 unique_lock<mutex> lk(__m_);
136 if (--__count_ == 0) {
137 __id_.__reset();
138 lk.unlock();
139 __cv_.notify_one();
140 }
Howard Hinnant3e519522010-05-11 19:42:16141}
142
Howard Hinnant3e519522010-05-11 19:42:16143_LIBCPP_END_NAMESPACE_STD
Arthur O'Dwyerbbb0f2c2022-02-11 18:00:39144
145_LIBCPP_POP_MACROS