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 | // bool try_lock(); |
| 17 | |
| 18 | #include <mutex> |
| 19 | #include <cassert> |
| 20 | |
| 21 | bool try_lock_called = false; |
| 22 | |
| 23 | struct mutex |
| 24 | { |
| 25 | bool try_lock() |
| 26 | { |
| 27 | try_lock_called = !try_lock_called; |
| 28 | return try_lock_called; |
| 29 | } |
| 30 | void unlock() {} |
| 31 | }; |
| 32 | |
| 33 | mutex m; |
| 34 | |
| 35 | int main() |
| 36 | { |
| 37 | std::unique_lock<mutex> lk(m, std::defer_lock); |
| 38 | assert(lk.try_lock() == true); |
| 39 | assert(try_lock_called == true); |
| 40 | assert(lk.owns_lock() == true); |
| 41 | try |
| 42 | { |
| 43 | lk.try_lock(); |
| 44 | assert(false); |
| 45 | } |
| 46 | catch (std::system_error& e) |
| 47 | { |
| 48 | assert(e.code().value() == EDEADLK); |
| 49 | } |
| 50 | lk.unlock(); |
| 51 | assert(lk.try_lock() == false); |
| 52 | assert(try_lock_called == false); |
| 53 | assert(lk.owns_lock() == false); |
| 54 | lk.release(); |
| 55 | try |
| 56 | { |
| 57 | lk.try_lock(); |
| 58 | assert(false); |
| 59 | } |
| 60 | catch (std::system_error& e) |
| 61 | { |
| 62 | assert(e.code().value() == EPERM); |
| 63 | } |
| 64 | } |