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 
11 // ALLOW_RETRIES: 2
12 
13 // <mutex>
14 
15 // template <class Mutex> class unique_lock;
16 
17 // explicit unique_lock(mutex_type& m);
18 
19 // template<class _Mutex> unique_lock(unique_lock<_Mutex>)
20 //     -> unique_lock<_Mutex>;  // C++17
21 
22 #include <mutex>
23 #include <thread>
24 #include <cstdlib>
25 #include <cassert>
26 
27 #include "make_test_thread.h"
28 #include "test_macros.h"
29 
30 std::mutex m;
31 
32 typedef std::chrono::system_clock Clock;
33 typedef Clock::time_point time_point;
34 typedef Clock::duration duration;
35 typedef std::chrono::milliseconds ms;
36 typedef std::chrono::nanoseconds ns;
37 
f()38 void f()
39 {
40     time_point t0 = Clock::now();
41     time_point t1;
42     {
43     std::unique_lock<std::mutex> ul(m);
44     t1 = Clock::now();
45     }
46     ns d = t1 - t0 - ms(250);
47     assert(d < ms(50));  // within 50ms
48 }
49 
main(int,char **)50 int main(int, char**)
51 {
52     m.lock();
53     std::thread t = support::make_test_thread(f);
54     std::this_thread::sleep_for(ms(250));
55     m.unlock();
56     t.join();
57 
58 #ifdef __cpp_deduction_guides
59     std::unique_lock ul(m);
60     static_assert((std::is_same<decltype(ul), std::unique_lock<decltype(m)>>::value), "" );
61 #endif
62 
63   return 0;
64 }
65