1//===----------------------------------------------------------------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is dual licensed under the MIT and the University of Illinois Open
6// Source Licenses. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10// <mutex>
11
12// class recursive_timed_mutex;
13
14// template <class Clock, class Duration>
15//     bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time);
16
17#include <mutex>
18#include <thread>
19#include <cstdlib>
20#include <cassert>
21
22std::recursive_timed_mutex m;
23
24typedef std::chrono::steady_clock Clock;
25typedef Clock::time_point time_point;
26typedef Clock::duration duration;
27typedef std::chrono::milliseconds ms;
28typedef std::chrono::nanoseconds ns;
29
30void f1()
31{
32    time_point t0 = Clock::now();
33    assert(m.try_lock_until(Clock::now() + ms(300)) == true);
34    time_point t1 = Clock::now();
35    assert(m.try_lock());
36    m.unlock();
37    m.unlock();
38    ns d = t1 - t0 - ms(250);
39    assert(d < ms(50));  // within 50ms
40}
41
42void f2()
43{
44    time_point t0 = Clock::now();
45    assert(m.try_lock_until(Clock::now() + ms(250)) == false);
46    time_point t1 = Clock::now();
47    ns d = t1 - t0 - ms(250);
48    assert(d < ms(50));  // within 50ms
49}
50
51int main()
52{
53    {
54        m.lock();
55        std::thread t(f1);
56        std::this_thread::sleep_for(ms(250));
57        m.unlock();
58        t.join();
59    }
60    {
61        m.lock();
62        std::thread t(f2);
63        std::this_thread::sleep_for(ms(300));
64        m.unlock();
65        t.join();
66    }
67}
68