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