82 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// UNSUPPORTED: c++0310// UNSUPPORTED: no-threads11 12// <mutex>13 14// class recursive_mutex;15 16// bool try_lock();17 18#include <mutex>19#include <atomic>20#include <cassert>21#include <chrono>22#include <thread>23 24#include "make_test_thread.h"25 26bool 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 effects32 });33 t.join();34 35 return did_lock;36}37 38int main(int, char**) {39 // Try to lock a mutex that is not locked yet. This should succeed.40 {41 std::recursive_mutex m;42 bool succeeded = m.try_lock();43 assert(succeeded);44 m.unlock();45 }46 47 // Try to lock a mutex that is already locked by this thread. This should succeed and the mutex should only48 // be unlocked after a matching number of calls to unlock() on the same thread.49 {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;55 }56 while (lock_count != 0) {57 assert(!is_lockable(m));58 m.unlock();59 --lock_count;60 }61 assert(is_lockable(m));62 }63 64 // Try to lock a mutex that is already locked by another thread. This should fail.65 {66 std::recursive_mutex m;67 m.lock();68 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);73 }74 });75 t.join();76 77 m.unlock();78 }79 80 return 0;81}82