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: libcpp-has-no-threads 10 // ALLOW_RETRIES: 2 11 12 // <mutex> 13 14 // class recursive_timed_mutex; 15 16 // void lock(); 17 18 #include <mutex> 19 #include <thread> 20 #include <cstdlib> 21 #include <cassert> 22 23 #include "make_test_thread.h" 24 #include "test_macros.h" 25 26 std::recursive_timed_mutex m; 27 28 typedef std::chrono::system_clock Clock; 29 typedef Clock::time_point time_point; 30 typedef Clock::duration duration; 31 typedef std::chrono::milliseconds ms; 32 typedef std::chrono::nanoseconds ns; 33 f()34void f() 35 { 36 time_point t0 = Clock::now(); 37 m.lock(); 38 time_point t1 = Clock::now(); 39 m.lock(); 40 m.unlock(); 41 m.unlock(); 42 ns d = t1 - t0 - ms(250); 43 assert(d < ms(50)); // within 50ms 44 } 45 main(int,char **)46int main(int, char**) 47 { 48 m.lock(); 49 std::thread t = support::make_test_thread(f); 50 std::this_thread::sleep_for(ms(250)); 51 m.unlock(); 52 t.join(); 53 54 return 0; 55 } 56