Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Jonathan Roelofs | 8d86b2e | 2014-09-05 19:45:05 +0000 | [diff] [blame] | 9 | // |
| 10 | // UNSUPPORTED: libcpp-has-no-threads |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 11 | |
| 12 | // <mutex> |
| 13 | |
| 14 | // template <class Mutex> class unique_lock; |
| 15 | |
| 16 | // template <class Clock, class Duration> |
| 17 | // bool try_lock_until(const chrono::time_point<Clock, Duration>& abs_time); |
| 18 | |
| 19 | #include <mutex> |
| 20 | #include <cassert> |
| 21 | |
| 22 | bool try_lock_until_called = false; |
| 23 | |
| 24 | struct mutex |
| 25 | { |
| 26 | template <class Clock, class Duration> |
| 27 | bool try_lock_until(const std::chrono::time_point<Clock, Duration>& abs_time) |
| 28 | { |
| 29 | typedef std::chrono::milliseconds ms; |
| 30 | assert(Clock::now() - abs_time < ms(5)); |
| 31 | try_lock_until_called = !try_lock_until_called; |
| 32 | return try_lock_until_called; |
| 33 | } |
| 34 | void unlock() {} |
| 35 | }; |
| 36 | |
| 37 | mutex m; |
| 38 | |
| 39 | int main() |
| 40 | { |
Howard Hinnant | f8f8521 | 2010-11-20 19:16:30 +0000 | [diff] [blame] | 41 | typedef std::chrono::steady_clock Clock; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 42 | std::unique_lock<mutex> lk(m, std::defer_lock); |
| 43 | assert(lk.try_lock_until(Clock::now()) == true); |
| 44 | assert(try_lock_until_called == true); |
| 45 | assert(lk.owns_lock() == true); |
| 46 | try |
| 47 | { |
| 48 | lk.try_lock_until(Clock::now()); |
| 49 | assert(false); |
| 50 | } |
| 51 | catch (std::system_error& e) |
| 52 | { |
| 53 | assert(e.code().value() == EDEADLK); |
| 54 | } |
| 55 | lk.unlock(); |
| 56 | assert(lk.try_lock_until(Clock::now()) == false); |
| 57 | assert(try_lock_until_called == false); |
| 58 | assert(lk.owns_lock() == false); |
| 59 | lk.release(); |
| 60 | try |
| 61 | { |
| 62 | lk.try_lock_until(Clock::now()); |
| 63 | assert(false); |
| 64 | } |
| 65 | catch (std::system_error& e) |
| 66 | { |
| 67 | assert(e.code().value() == EPERM); |
| 68 | } |
| 69 | } |