Skip to content
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

[libc++] Make std::lock_guard available with _LIBCPP_HAS_NO_THREADS #98717

Merged
merged 20 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -15,22 +15,18 @@
// lock_guard(mutex_type& m, adopt_lock_t);

#include <mutex>
#include <cstdlib>
#include <cassert>

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

int main(int, char**) {
MyMutex m;
{
MyMutex m;
m.lock();
std::lock_guard<MyMutex> lg(m, std::adopt_lock);
assert(m.locked);
}

m.lock();
m.unlock();
assert(!m.locked);
ldionne marked this conversation as resolved.
Show resolved Hide resolved

return 0;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
#include <mutex>
#include <type_traits>

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

int main(int, char**) {
MyMutex m;
std::lock_guard<MyMutex> lg(m); // makes sure this compiles and runs
#if TEST_STD_VER >= 17
static_assert(!std::is_convertible_v<MyMutex, std::lock_guard<MyMutex>>, "constructor must be explicit");
#endif
assert(!m.locked);
std::lock_guard<MyMutex> lg(m);
assert(m.locked);

static_assert(!std::is_convertible<MyMutex, std::lock_guard<MyMutex>>::value, "constructor must be explicit");

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

// <mutex>

// lock_guard
// template <class Mutex> class lock_guard;

// Make sure that the implicitly-generated CTAD works.

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,39 @@
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// UNSUPPORTED: no-threads

// <mutex>

// template <class Mutex> class lock_guard;

// explicit lock_guard(mutex_type& m);
// UNSUPPORTED: no-threads

// template<class _Mutex> lock_guard(lock_guard<_Mutex>)
// -> lock_guard<_Mutex>; // C++17
// Test the interoperation of std::lock_guard with std::mutex, since that is such
// a common use case.

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

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

std::mutex m;

void do_try_lock() {
assert(m.try_lock() == false);
}
void do_try_lock(std::mutex& m) { assert(m.try_lock() == false); }

int main(int, char**) {
{
std::mutex m;
std::lock_guard<std::mutex> lg(m);
std::thread t = support::make_test_thread(do_try_lock);
std::thread t = support::make_test_thread(do_try_lock, m);
t.join();
}

// This should work because the lock_guard unlocked the mutex when it was destroyed above.
m.lock();
m.unlock();

// Test CTAD
#if TEST_STD_VER >= 17
std::lock_guard lg(m);
static_assert((std::is_same<decltype(lg), std::lock_guard<decltype(m)>>::value), "" );
{
std::lock_guard lg(m);
static_assert(std::is_same<decltype(lg), std::lock_guard<std::mutex>>::value, "");
}
#endif

return 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <mutex>
#include <type_traits>

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

static_assert((std::is_same<std::lock_guard<MyMutex>::mutex_type, MyMutex>::value), "");
static_assert(std::is_same<std::lock_guard<MyMutex>::mutex_type, MyMutex>::value, "");
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#ifndef TEST_STD_THREAD_THREAD_MUTEX_THREAD_LOCK_THREAD_LOCK_GUARD_TYPES_H
#define TEST_STD_THREAD_THREAD_MUTEX_THREAD_LOCK_THREAD_LOCK_GUARD_TYPES_H

Expand Down
Loading