Skip to content

Commit 19738bb

Browse files
committed
[libc++] Refactor the std::unique_lock tests
1 parent 1fb1a5d commit 19738bb

27 files changed

+503
-567
lines changed

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,7 @@
1616

1717
#include <mutex>
1818

19-
#include "test_macros.h"
20-
#include "types.h"
19+
#include "checking_mutex.h"
2120

22-
int main(int, char**) {
23-
MyMutex mutex;
24-
{
25-
std::unique_lock lock(mutex);
26-
ASSERT_SAME_TYPE(decltype(lock), std::unique_lock<MyMutex>);
27-
}
28-
29-
return 0;
30-
}
21+
checking_mutex mux;
22+
static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>);
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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+
// UNSUPPORTED: no-threads
10+
11+
// <mutex>
12+
13+
// Make sure std::unique_lock works with std::mutex as expected.
14+
15+
#include <atomic>
16+
#include <cassert>
17+
#include <mutex>
18+
19+
#include "make_test_thread.h"
20+
21+
std::atomic<bool> keep_waiting;
22+
std::atomic<bool> child_thread_locked;
23+
std::mutex mux;
24+
bool main_thread_unlocked = false;
25+
bool child_thread_unlocked = false;
26+
27+
void lock_thread() {
28+
std::unique_lock<std::mutex> lock(mux);
29+
assert(main_thread_unlocked);
30+
main_thread_unlocked = false;
31+
child_thread_unlocked = true;
32+
}
33+
34+
void try_lock_thread() {
35+
std::unique_lock<std::mutex> lock(mux, std::try_to_lock_t());
36+
assert(lock.owns_lock());
37+
child_thread_locked = true;
38+
39+
while (keep_waiting)
40+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
41+
42+
child_thread_unlocked = true;
43+
}
44+
45+
int main(int, char**) {
46+
{
47+
mux.lock();
48+
std::thread t = support::make_test_thread(lock_thread);
49+
main_thread_unlocked = true;
50+
mux.unlock();
51+
t.join();
52+
assert(child_thread_unlocked);
53+
}
54+
55+
{
56+
child_thread_unlocked = false;
57+
child_thread_locked = false;
58+
keep_waiting = true;
59+
std::thread t = support::make_test_thread(try_lock_thread);
60+
while (!child_thread_locked)
61+
std::this_thread::sleep_for(std::chrono::milliseconds(10));
62+
assert(!mux.try_lock());
63+
keep_waiting = false;
64+
t.join();
65+
assert(child_thread_unlocked);
66+
}
67+
68+
return 0;
69+
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
#include <mutex>
1616

17-
#include "../types.h"
17+
#include "checking_mutex.h"
1818

19-
static_assert(!std::is_copy_assignable<std::lock_guard<MyMutex> >::value, "");
19+
static_assert(!std::is_copy_assignable<std::lock_guard<checking_mutex> >::value, "");

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@
1414

1515
#include <mutex>
1616

17-
#include "../types.h"
17+
#include "checking_mutex.h"
1818

19-
static_assert(!std::is_copy_constructible<std::lock_guard<MyMutex> >::value, "");
19+
static_assert(!std::is_copy_constructible<std::lock_guard<checking_mutex> >::value, "");

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,17 @@
1414

1515
#include <cassert>
1616
#include <mutex>
17+
#include <type_traits>
1718

19+
#include "checking_mutex.h"
1820
#include "test_macros.h"
19-
#include "../types.h"
21+
22+
#if TEST_STD_VER >= 11
23+
static_assert(std::is_nothrow_default_constructible<std::unique_lock<checking_mutex>>::value, "");
24+
#endif
2025

2126
int main(int, char**) {
22-
std::unique_lock<MyMutex> ul;
27+
std::unique_lock<checking_mutex> ul;
2328
assert(!ul.owns_lock());
2429
assert(ul.mutex() == nullptr);
2530

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

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,37 +13,24 @@
1313
// unique_lock& operator=(unique_lock&& u);
1414

1515
#include <cassert>
16+
#include <memory>
1617
#include <mutex>
1718

18-
#include "nasty_containers.h"
19-
#include "../types.h"
20-
#include "test_macros.h"
19+
#include "checking_mutex.h"
2120

2221
int main(int, char**) {
23-
{
24-
typedef MyMutex M;
25-
M m0;
26-
M m1;
27-
std::unique_lock<M> lk0(m0);
28-
std::unique_lock<M> lk1(m1);
29-
lk1 = std::move(lk0);
30-
assert(lk1.mutex() == std::addressof(m0));
31-
assert(lk1.owns_lock() == true);
32-
assert(lk0.mutex() == nullptr);
33-
assert(lk0.owns_lock() == false);
34-
}
35-
{
36-
typedef nasty_mutex M;
37-
M m0;
38-
M m1;
39-
std::unique_lock<M> lk0(m0);
40-
std::unique_lock<M> lk1(m1);
41-
lk1 = std::move(lk0);
42-
assert(lk1.mutex() == std::addressof(m0));
43-
assert(lk1.owns_lock() == true);
44-
assert(lk0.mutex() == nullptr);
45-
assert(lk0.owns_lock() == false);
46-
}
22+
checking_mutex m0;
23+
checking_mutex m1;
24+
std::unique_lock<checking_mutex> lk0(m0);
25+
std::unique_lock<checking_mutex> lk1(m1);
26+
27+
auto& result = (lk1 = std::move(lk0));
28+
29+
assert(&result == &lk1);
30+
assert(lk1.mutex() == std::addressof(m0));
31+
assert(lk1.owns_lock());
32+
assert(lk0.mutex() == nullptr);
33+
assert(!lk0.owns_lock());
4734

4835
return 0;
4936
}

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

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

119
// <mutex>
1210

@@ -15,33 +13,26 @@
1513
// unique_lock(unique_lock&& u);
1614

1715
#include <cassert>
16+
#include <memory>
1817
#include <mutex>
18+
#include <type_traits>
1919

20-
#include "nasty_containers.h"
21-
#include "../types.h"
20+
#include "checking_mutex.h"
2221
#include "test_macros.h"
2322

23+
#if TEST_STD_VER >= 11
24+
static_assert(std::is_nothrow_move_constructible<std::unique_lock<checking_mutex>>::value, "");
25+
#endif
26+
2427
int main(int, char**) {
25-
{
26-
typedef MyMutex M;
27-
M m;
28-
std::unique_lock<M> lk0(m);
29-
std::unique_lock<M> lk = std::move(lk0);
30-
assert(lk.mutex() == std::addressof(m));
31-
assert(lk.owns_lock() == true);
32-
assert(lk0.mutex() == nullptr);
33-
assert(lk0.owns_lock() == false);
34-
}
35-
{
36-
typedef nasty_mutex M;
37-
M m;
38-
std::unique_lock<M> lk0(m);
39-
std::unique_lock<M> lk = std::move(lk0);
40-
assert(lk.mutex() == std::addressof(m));
41-
assert(lk.owns_lock() == true);
42-
assert(lk0.mutex() == nullptr);
43-
assert(lk0.owns_lock() == false);
44-
}
28+
checking_mutex m;
29+
std::unique_lock<checking_mutex> lk0(m);
30+
std::unique_lock<checking_mutex> lk = std::move(lk0);
31+
32+
assert(lk.mutex() == std::addressof(m));
33+
assert(lk.owns_lock());
34+
assert(lk0.mutex() == nullptr);
35+
assert(!lk0.owns_lock());
4536

4637
return 0;
4738
}

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

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

129
// <mutex>
1310

@@ -19,45 +16,22 @@
1916
// -> unique_lock<_Mutex>; // C++17
2017

2118
#include <cassert>
22-
#include <chrono>
23-
#include <cstdlib>
2419
#include <mutex>
25-
#include <thread>
2620

27-
#include "make_test_thread.h"
21+
#include "checking_mutex.h"
2822
#include "test_macros.h"
2923

30-
std::mutex m;
31-
32-
typedef std::chrono::system_clock Clock;
33-
typedef Clock::time_point time_point;
34-
typedef Clock::duration duration;
35-
typedef std::chrono::milliseconds ms;
36-
typedef std::chrono::nanoseconds ns;
37-
38-
void f()
39-
{
40-
time_point t0 = Clock::now();
41-
time_point t1;
42-
{
43-
std::unique_lock<std::mutex> ul(m);
44-
t1 = Clock::now();
45-
}
46-
ns d = t1 - t0 - ms(250);
47-
assert(d < ms(50)); // within 50ms
48-
}
24+
int main(int, char**) {
25+
checking_mutex mux;
4926

50-
int main(int, char**)
51-
{
52-
m.lock();
53-
std::thread t = support::make_test_thread(f);
54-
std::this_thread::sleep_for(ms(250));
55-
m.unlock();
56-
t.join();
27+
{
28+
std::unique_lock<checking_mutex> lock(mux);
29+
assert(mux.current_state == checking_mutex::locked_via_lock);
30+
}
31+
assert(mux.current_state == checking_mutex::unlocked);
5732

5833
#if TEST_STD_VER >= 17
59-
std::unique_lock ul(m);
60-
static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
34+
static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>, "");
6135
#endif
6236

6337
return 0;

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

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

119
// <mutex>
1210

@@ -15,29 +13,19 @@
1513
// unique_lock(mutex_type& m, adopt_lock_t);
1614

1715
#include <cassert>
16+
#include <memory>
1817
#include <mutex>
1918

20-
#include "nasty_containers.h"
21-
#include "../types.h"
22-
#include "test_macros.h"
19+
#include "checking_mutex.h"
2320

2421
int main(int, char**) {
25-
{
26-
typedef MyMutex M;
27-
M m;
28-
m.lock();
29-
std::unique_lock<M> lk(m, std::adopt_lock);
30-
assert(lk.mutex() == std::addressof(m));
31-
assert(lk.owns_lock() == true);
32-
}
33-
{
34-
typedef nasty_mutex M;
35-
M m;
36-
m.lock();
37-
std::unique_lock<M> lk(m, std::adopt_lock);
38-
assert(lk.mutex() == std::addressof(m));
39-
assert(lk.owns_lock() == true);
40-
}
22+
checking_mutex m;
23+
m.lock();
24+
m.last_try = checking_mutex::none;
25+
std::unique_lock<checking_mutex> lk(m, std::adopt_lock_t());
26+
assert(m.last_try == checking_mutex::none);
27+
assert(lk.mutex() == std::addressof(m));
28+
assert(lk.owns_lock());
4129

4230
return 0;
4331
}

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

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

119
// <mutex>
1210

@@ -15,27 +13,24 @@
1513
// unique_lock(mutex_type& m, defer_lock_t);
1614

1715
#include <cassert>
16+
#include <memory>
1817
#include <mutex>
18+
#include <type_traits>
1919

20-
#include "nasty_containers.h"
21-
#include "../types.h"
20+
#include "checking_mutex.h"
2221
#include "test_macros.h"
2322

23+
#if TEST_STD_VER >= 11
24+
static_assert(
25+
std::is_nothrow_constructible<std::unique_lock<checking_mutex>, checking_mutex&, std::defer_lock_t>::value, "");
26+
#endif
27+
2428
int main(int, char**) {
25-
{
26-
typedef MyMutex M;
27-
M m;
28-
std::unique_lock<M> lk(m, std::defer_lock);
29-
assert(lk.mutex() == std::addressof(m));
30-
assert(lk.owns_lock() == false);
31-
}
32-
{
33-
typedef nasty_mutex M;
34-
M m;
35-
std::unique_lock<M> lk(m, std::defer_lock);
36-
assert(lk.mutex() == std::addressof(m));
37-
assert(lk.owns_lock() == false);
38-
}
29+
checking_mutex m;
30+
std::unique_lock<checking_mutex> lk(m, std::defer_lock_t());
31+
assert(m.last_try == checking_mutex::none);
32+
assert(lk.mutex() == std::addressof(m));
33+
assert(lk.owns_lock() == false);
3934

4035
return 0;
4136
}

0 commit comments

Comments
 (0)