[RISCV] Decompose locally repeating shuffles (without exact VLEN) (#125735)
[llvm-project.git] / libcxx / test / std / thread / thread.mutex / thread.mutex.requirements / thread.mutex.requirements.mutex / thread.mutex.recursive / try_lock.pass.cpp
blob96073eb345306d3581b9e89977cf6d9f4d19f0ed
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 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
10 // UNSUPPORTED: no-threads
12 // <mutex>
14 // class recursive_mutex;
16 // bool try_lock();
18 #include <mutex>
19 #include <atomic>
20 #include <cassert>
21 #include <chrono>
22 #include <thread>
24 #include "make_test_thread.h"
26 bool is_lockable(std::recursive_mutex& m) {
27 bool did_lock;
28 std::thread t = support::make_test_thread([&] {
29 did_lock = m.try_lock();
30 if (did_lock)
31 m.unlock(); // undo side effects
32 });
33 t.join();
35 return did_lock;
38 int main(int, char**) {
39 // Try to lock a mutex that is not locked yet. This should succeed.
41 std::recursive_mutex m;
42 bool succeeded = m.try_lock();
43 assert(succeeded);
44 m.unlock();
47 // Try to lock a mutex that is already locked by this thread. This should succeed and the mutex should only
48 // be unlocked after a matching number of calls to unlock() on the same thread.
50 std::recursive_mutex m;
51 int lock_count = 0;
52 for (int i = 0; i != 10; ++i) {
53 assert(m.try_lock());
54 ++lock_count;
56 while (lock_count != 0) {
57 assert(!is_lockable(m));
58 m.unlock();
59 --lock_count;
61 assert(is_lockable(m));
64 // Try to lock a mutex that is already locked by another thread. This should fail.
66 std::recursive_mutex m;
67 m.lock();
69 std::thread t = support::make_test_thread([&] {
70 for (int i = 0; i != 10; ++i) {
71 bool succeeded = m.try_lock();
72 assert(!succeeded);
74 });
75 t.join();
77 m.unlock();
80 return 0;