Skip to content

Commit e9d5842

Browse files
authored
[libc++] Make std::unique_lock available with _LIBCPP_HAS_NO_THREADS (#99562)
This is a follow up to #98717, which made lock_guard available under _LIBCPP_HAS_NO_THREADS. We can make unique_lock available under similar circumstances. This patch follows the example in #98717, by: - Removing the preprocessor guards for _LIBCPP_HAS_NO_THREADS in the unique_lock header. - providing a set of custom mutex implementations in a local header. - using custom locks in tests that can be made to work under `no-threads`.
1 parent b5a7d3b commit e9d5842

22 files changed

+314
-382
lines changed

libcxx/include/__mutex/unique_lock.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
# pragma GCC system_header
2323
#endif
2424

25-
#ifndef _LIBCPP_HAS_NO_THREADS
26-
2725
_LIBCPP_BEGIN_NAMESPACE_STD
2826

2927
template <class _Mutex>
@@ -172,6 +170,4 @@ inline _LIBCPP_HIDE_FROM_ABI void swap(unique_lock<_Mutex>& __x, unique_lock<_Mu
172170

173171
_LIBCPP_END_NAMESPACE_STD
174172

175-
#endif // _LIBCPP_HAS_NO_THREADS
176-
177173
#endif // _LIBCPP___MUTEX_UNIQUE_LOCK_H

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/implicit_ctad.pass.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88

9-
// UNSUPPORTED: no-threads
109
// UNSUPPORTED: c++03, c++11, c++14
1110

1211
// <mutex>
@@ -18,12 +17,13 @@
1817
#include <mutex>
1918

2019
#include "test_macros.h"
20+
#include "types.h"
2121

2222
int main(int, char**) {
23-
std::mutex mutex;
23+
MyMutex mutex;
2424
{
2525
std::unique_lock lock(mutex);
26-
ASSERT_SAME_TYPE(decltype(lock), std::unique_lock<std::mutex>);
26+
ASSERT_SAME_TYPE(decltype(lock), std::unique_lock<MyMutex>);
2727
}
2828

2929
return 0;
Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,7 @@
1313
// unique_lock& operator=(unique_lock const&) = delete;
1414

1515
#include <mutex>
16-
#include <cassert>
1716

18-
int main(int, char**)
19-
{
20-
{
21-
typedef std::mutex M;
22-
M m0;
23-
M m1;
24-
std::unique_lock<M> lk0(m0);
25-
std::unique_lock<M> lk1(m1);
26-
lk1 = lk0;
27-
assert(lk1.mutex() == &m0);
28-
assert(lk1.owns_lock() == true);
29-
assert(lk0.mutex() == nullptr);
30-
assert(lk0.owns_lock() == false);
31-
}
17+
#include "../types.h"
3218

33-
return 0;
34-
}
19+
static_assert(!std::is_copy_assignable<std::lock_guard<MyMutex> >::value, "");
Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,7 @@
1313
// unique_lock(unique_lock const&) = delete;
1414

1515
#include <mutex>
16-
#include <cassert>
1716

18-
int main(int, char**)
19-
{
20-
{
21-
typedef std::mutex M;
22-
M m;
23-
std::unique_lock<M> lk0(m);
24-
std::unique_lock<M> lk = lk0;
25-
assert(lk.mutex() == &m);
26-
assert(lk.owns_lock() == true);
27-
assert(lk0.mutex() == nullptr);
28-
assert(lk0.owns_lock() == false);
29-
}
17+
#include "../types.h"
3018

31-
return 0;
32-
}
19+
static_assert(!std::is_copy_constructible<std::lock_guard<MyMutex> >::value, "");

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/default.pass.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: no-threads
108

119
// <mutex>
1210

1311
// template <class Mutex> class unique_lock;
1412

1513
// unique_lock();
1614

17-
#include <mutex>
1815
#include <cassert>
16+
#include <mutex>
1917

2018
#include "test_macros.h"
19+
#include "../types.h"
2120

22-
int main(int, char**)
23-
{
24-
std::unique_lock<std::mutex> ul;
25-
assert(!ul.owns_lock());
26-
assert(ul.mutex() == nullptr);
21+
int main(int, char**) {
22+
std::unique_lock<MyMutex> ul;
23+
assert(!ul.owns_lock());
24+
assert(ul.mutex() == nullptr);
2725

2826
return 0;
2927
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_assign.pass.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,23 @@
55
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
//
77
//===----------------------------------------------------------------------===//
8-
//
9-
// UNSUPPORTED: no-threads, c++03
108

119
// <mutex>
1210

1311
// template <class Mutex> class unique_lock;
1412

1513
// unique_lock& operator=(unique_lock&& u);
1614

17-
#include <mutex>
1815
#include <cassert>
19-
#include "nasty_containers.h"
16+
#include <mutex>
2017

18+
#include "nasty_containers.h"
19+
#include "../types.h"
2120
#include "test_macros.h"
2221

23-
int main(int, char**)
24-
{
25-
{
26-
typedef std::mutex M;
22+
int main(int, char**) {
23+
{
24+
typedef MyMutex M;
2725
M m0;
2826
M m1;
2927
std::unique_lock<M> lk0(m0);
@@ -33,8 +31,8 @@ int main(int, char**)
3331
assert(lk1.owns_lock() == true);
3432
assert(lk0.mutex() == nullptr);
3533
assert(lk0.owns_lock() == false);
36-
}
37-
{
34+
}
35+
{
3836
typedef nasty_mutex M;
3937
M m0;
4038
M m1;
@@ -45,7 +43,7 @@ int main(int, char**)
4543
assert(lk1.owns_lock() == true);
4644
assert(lk0.mutex() == nullptr);
4745
assert(lk0.owns_lock() == false);
48-
}
46+
}
4947

5048
return 0;
5149
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/move_ctor.pass.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// UNSUPPORTED: no-threads, c++03
9+
// UNSUPPORTED: c++03
1010

1111
// <mutex>
1212

1313
// template <class Mutex> class unique_lock;
1414

1515
// unique_lock(unique_lock&& u);
1616

17-
#include <mutex>
1817
#include <cassert>
19-
#include "nasty_containers.h"
18+
#include <mutex>
2019

20+
#include "nasty_containers.h"
21+
#include "../types.h"
2122
#include "test_macros.h"
2223

23-
int main(int, char**)
24-
{
25-
{
26-
typedef std::mutex M;
24+
int main(int, char**) {
25+
{
26+
typedef MyMutex M;
2727
M m;
2828
std::unique_lock<M> lk0(m);
2929
std::unique_lock<M> lk = std::move(lk0);
3030
assert(lk.mutex() == std::addressof(m));
3131
assert(lk.owns_lock() == true);
3232
assert(lk0.mutex() == nullptr);
3333
assert(lk0.owns_lock() == false);
34-
}
35-
{
34+
}
35+
{
3636
typedef nasty_mutex M;
3737
M m;
3838
std::unique_lock<M> lk0(m);
@@ -41,7 +41,7 @@ int main(int, char**)
4141
assert(lk.owns_lock() == true);
4242
assert(lk0.mutex() == nullptr);
4343
assert(lk0.owns_lock() == false);
44-
}
44+
}
4545

4646
return 0;
4747
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_adopt_lock.pass.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// UNSUPPORTED: no-threads
109
// UNSUPPORTED: c++03
1110

1211
// <mutex>
@@ -15,30 +14,30 @@
1514

1615
// unique_lock(mutex_type& m, adopt_lock_t);
1716

18-
#include <mutex>
1917
#include <cassert>
20-
#include "nasty_containers.h"
18+
#include <mutex>
2119

20+
#include "nasty_containers.h"
21+
#include "../types.h"
2222
#include "test_macros.h"
2323

24-
int main(int, char**)
25-
{
26-
{
27-
typedef std::mutex M;
24+
int main(int, char**) {
25+
{
26+
typedef MyMutex M;
2827
M m;
2928
m.lock();
3029
std::unique_lock<M> lk(m, std::adopt_lock);
3130
assert(lk.mutex() == std::addressof(m));
3231
assert(lk.owns_lock() == true);
33-
}
34-
{
32+
}
33+
{
3534
typedef nasty_mutex M;
3635
M m;
3736
m.lock();
3837
std::unique_lock<M> lk(m, std::adopt_lock);
3938
assert(lk.mutex() == std::addressof(m));
4039
assert(lk.owns_lock() == true);
41-
}
40+
}
4241

4342
return 0;
4443
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.cons/mutex_defer_lock.pass.cpp

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
//
77
//===----------------------------------------------------------------------===//
88
//
9-
// UNSUPPORTED: no-threads
109
// UNSUPPORTED: c++03
1110

1211
// <mutex>
@@ -15,28 +14,28 @@
1514

1615
// unique_lock(mutex_type& m, defer_lock_t);
1716

18-
#include <mutex>
1917
#include <cassert>
20-
#include "nasty_containers.h"
18+
#include <mutex>
2119

20+
#include "nasty_containers.h"
21+
#include "../types.h"
2222
#include "test_macros.h"
2323

24-
int main(int, char**)
25-
{
26-
{
27-
typedef std::mutex M;
24+
int main(int, char**) {
25+
{
26+
typedef MyMutex M;
2827
M m;
2928
std::unique_lock<M> lk(m, std::defer_lock);
3029
assert(lk.mutex() == std::addressof(m));
3130
assert(lk.owns_lock() == false);
32-
}
33-
{
31+
}
32+
{
3433
typedef nasty_mutex M;
3534
M m;
3635
std::unique_lock<M> lk(m, std::defer_lock);
3736
assert(lk.mutex() == std::addressof(m));
3837
assert(lk.owns_lock() == false);
39-
}
38+
}
4039

4140
return 0;
4241
}

libcxx/test/std/thread/thread.mutex/thread.lock/thread.lock.unique/thread.lock.unique.locking/lock.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void f()
5151
}
5252
catch (std::system_error& e)
5353
{
54-
assert(e.code().value() == EDEADLK);
54+
assert(e.code() == std::errc::resource_deadlock_would_occur);
5555
}
5656
#endif
5757
lk.unlock();
@@ -64,7 +64,7 @@ void f()
6464
}
6565
catch (std::system_error& e)
6666
{
67-
assert(e.code().value() == EPERM);
67+
assert(e.code() == std::errc::operation_not_permitted);
6868
}
6969
#endif
7070
}

0 commit comments

Comments
 (0)