Skip to content

[libc++] Refactor the std::unique_lock tests #102151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,7 @@

#include <mutex>

#include "test_macros.h"
#include "types.h"
#include "checking_mutex.h"

int main(int, char**) {
MyMutex mutex;
{
std::unique_lock lock(mutex);
ASSERT_SAME_TYPE(decltype(lock), std::unique_lock<MyMutex>);
}

return 0;
}
checking_mutex mux;
static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>);
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

// UNSUPPORTED: no-threads

// <mutex>

// Make sure std::unique_lock works with std::mutex as expected.

#include <atomic>
#include <cassert>
#include <mutex>

#include "make_test_thread.h"

std::atomic<bool> keep_waiting;
std::atomic<bool> child_thread_locked;
std::mutex mux;
bool main_thread_unlocked = false;
bool child_thread_unlocked = false;

void lock_thread() {
std::unique_lock<std::mutex> lock(mux);
assert(main_thread_unlocked);
main_thread_unlocked = false;
child_thread_unlocked = true;
}

void try_lock_thread() {
std::unique_lock<std::mutex> lock(mux, std::try_to_lock_t());
assert(lock.owns_lock());
child_thread_locked = true;

while (keep_waiting)
std::this_thread::sleep_for(std::chrono::milliseconds(10));

child_thread_unlocked = true;
}

int main(int, char**) {
{
mux.lock();
std::thread t = support::make_test_thread(lock_thread);
main_thread_unlocked = true;
mux.unlock();
t.join();
assert(child_thread_unlocked);
}

{
child_thread_unlocked = false;
child_thread_locked = false;
keep_waiting = true;
std::thread t = support::make_test_thread(try_lock_thread);
while (!child_thread_locked)
std::this_thread::sleep_for(std::chrono::milliseconds(10));
assert(!mux.try_lock());
keep_waiting = false;
t.join();
assert(child_thread_unlocked);
}

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

#include <mutex>

#include "../types.h"
#include "checking_mutex.h"

static_assert(!std::is_copy_assignable<std::lock_guard<MyMutex> >::value, "");
static_assert(!std::is_copy_assignable<std::lock_guard<checking_mutex> >::value, "");
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@

#include <mutex>

#include "../types.h"
#include "checking_mutex.h"

static_assert(!std::is_copy_constructible<std::lock_guard<MyMutex> >::value, "");
static_assert(!std::is_copy_constructible<std::lock_guard<checking_mutex> >::value, "");
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@

#include <cassert>
#include <mutex>
#include <type_traits>

#include "checking_mutex.h"
#include "test_macros.h"
#include "../types.h"

#if TEST_STD_VER >= 11
static_assert(std::is_nothrow_default_constructible<std::unique_lock<checking_mutex>>::value, "");
#endif

int main(int, char**) {
std::unique_lock<MyMutex> ul;
std::unique_lock<checking_mutex> ul;
assert(!ul.owns_lock());
assert(ul.mutex() == nullptr);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,24 @@
// unique_lock& operator=(unique_lock&& u);

#include <cassert>
#include <memory>
#include <mutex>

#include "nasty_containers.h"
#include "../types.h"
#include "test_macros.h"
#include "checking_mutex.h"

int main(int, char**) {
{
typedef MyMutex M;
M m0;
M m1;
std::unique_lock<M> lk0(m0);
std::unique_lock<M> lk1(m1);
lk1 = std::move(lk0);
assert(lk1.mutex() == std::addressof(m0));
assert(lk1.owns_lock() == true);
assert(lk0.mutex() == nullptr);
assert(lk0.owns_lock() == false);
}
{
typedef nasty_mutex M;
M m0;
M m1;
std::unique_lock<M> lk0(m0);
std::unique_lock<M> lk1(m1);
lk1 = std::move(lk0);
assert(lk1.mutex() == std::addressof(m0));
assert(lk1.owns_lock() == true);
assert(lk0.mutex() == nullptr);
assert(lk0.owns_lock() == false);
}
checking_mutex m0;
checking_mutex m1;
std::unique_lock<checking_mutex> lk0(m0);
std::unique_lock<checking_mutex> lk1(m1);

auto& result = (lk1 = std::move(lk0));

assert(&result == &lk1);
assert(lk1.mutex() == std::addressof(m0));
assert(lk1.owns_lock());
assert(lk0.mutex() == nullptr);
assert(!lk0.owns_lock());

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03

// <mutex>

Expand All @@ -15,33 +13,26 @@
// unique_lock(unique_lock&& u);

#include <cassert>
#include <memory>
#include <mutex>
#include <type_traits>

#include "nasty_containers.h"
#include "../types.h"
#include "checking_mutex.h"
#include "test_macros.h"

#if TEST_STD_VER >= 11
static_assert(std::is_nothrow_move_constructible<std::unique_lock<checking_mutex>>::value, "");
#endif

int main(int, char**) {
{
typedef MyMutex M;
M m;
std::unique_lock<M> lk0(m);
std::unique_lock<M> lk = std::move(lk0);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == true);
assert(lk0.mutex() == nullptr);
assert(lk0.owns_lock() == false);
}
{
typedef nasty_mutex M;
M m;
std::unique_lock<M> lk0(m);
std::unique_lock<M> lk = std::move(lk0);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == true);
assert(lk0.mutex() == nullptr);
assert(lk0.owns_lock() == false);
}
checking_mutex m;
std::unique_lock<checking_mutex> lk0(m);
std::unique_lock<checking_mutex> lk = std::move(lk0);

assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock());
assert(lk0.mutex() == nullptr);
assert(!lk0.owns_lock());

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads
// ALLOW_RETRIES: 2

// <mutex>

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

#include <cassert>
#include <chrono>
#include <cstdlib>
#include <mutex>
#include <thread>

#include "make_test_thread.h"
#include "checking_mutex.h"
#include "test_macros.h"

std::mutex m;

typedef std::chrono::system_clock Clock;
typedef Clock::time_point time_point;
typedef Clock::duration duration;
typedef std::chrono::milliseconds ms;
typedef std::chrono::nanoseconds ns;

void f()
{
time_point t0 = Clock::now();
time_point t1;
{
std::unique_lock<std::mutex> ul(m);
t1 = Clock::now();
}
ns d = t1 - t0 - ms(250);
assert(d < ms(50)); // within 50ms
}
int main(int, char**) {
checking_mutex mux;

int main(int, char**)
{
m.lock();
std::thread t = support::make_test_thread(f);
std::this_thread::sleep_for(ms(250));
m.unlock();
t.join();
{
std::unique_lock<checking_mutex> lock(mux);
assert(mux.current_state == checking_mutex::locked_via_lock);
}
assert(mux.current_state == checking_mutex::unlocked);

#if TEST_STD_VER >= 17
std::unique_lock ul(m);
static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
static_assert(std::is_same_v<std::unique_lock<checking_mutex>, decltype(std::unique_lock{mux})>, "");
#endif

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03

// <mutex>

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

#include <cassert>
#include <memory>
#include <mutex>

#include "nasty_containers.h"
#include "../types.h"
#include "test_macros.h"
#include "checking_mutex.h"

int main(int, char**) {
{
typedef MyMutex M;
M m;
m.lock();
std::unique_lock<M> lk(m, std::adopt_lock);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == true);
}
{
typedef nasty_mutex M;
M m;
m.lock();
std::unique_lock<M> lk(m, std::adopt_lock);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == true);
}
checking_mutex m;
m.lock();
m.last_try = checking_mutex::none;
std::unique_lock<checking_mutex> lk(m, std::adopt_lock_t());
assert(m.last_try == checking_mutex::none);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock());

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: c++03

// <mutex>

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

#include <cassert>
#include <memory>
#include <mutex>
#include <type_traits>

#include "nasty_containers.h"
#include "../types.h"
#include "checking_mutex.h"
#include "test_macros.h"

#if TEST_STD_VER >= 11
static_assert(
std::is_nothrow_constructible<std::unique_lock<checking_mutex>, checking_mutex&, std::defer_lock_t>::value, "");
#endif

int main(int, char**) {
{
typedef MyMutex M;
M m;
std::unique_lock<M> lk(m, std::defer_lock);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == false);
}
{
typedef nasty_mutex M;
M m;
std::unique_lock<M> lk(m, std::defer_lock);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == false);
}
checking_mutex m;
std::unique_lock<checking_mutex> lk(m, std::defer_lock_t());
assert(m.last_try == checking_mutex::none);
assert(lk.mutex() == std::addressof(m));
assert(lk.owns_lock() == false);

return 0;
}
Loading
Loading