]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 | // Copyright (C) 2011 Vicente J. Botet Escriba | |
11 | // | |
12 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
13 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
14 | ||
15 | // <boost/thread/locks.hpp> | |
16 | ||
17 | // template <class Mutex> class unique_lock; | |
18 | ||
19 | // template <class Clock, class Duration> | |
20 | // unique_lock(mutex_type& m, const chrono::time_point<Clock, Duration>& abs_time); | |
21 | ||
22 | #define BOOST_THREAD_PROVIDES_GENERIC_SHARED_MUTEX_ON_WIN | |
23 | ||
24 | #include <boost/thread/lock_types.hpp> | |
25 | #include <boost/thread/mutex.hpp> | |
26 | #include <boost/thread/thread.hpp> | |
27 | #include <boost/detail/lightweight_test.hpp> | |
28 | ||
29 | #if defined BOOST_THREAD_USES_CHRONO | |
30 | ||
31 | boost::timed_mutex m; | |
32 | ||
33 | typedef boost::chrono::steady_clock Clock; | |
34 | typedef Clock::time_point time_point; | |
35 | typedef Clock::duration duration; | |
36 | typedef boost::chrono::milliseconds ms; | |
37 | typedef boost::chrono::nanoseconds ns; | |
38 | ||
39 | void f1() | |
40 | { | |
41 | time_point t0 = Clock::now(); | |
42 | // This test is spurious as it depends on the time the thread system switches the threads | |
43 | boost::unique_lock<boost::timed_mutex> lk(m, Clock::now() + ms(300)+ms(1000)); | |
44 | BOOST_TEST(lk.owns_lock() == true); | |
45 | time_point t1 = Clock::now(); | |
46 | // This test is spurious as it depends on the time the thread system switches the threads | |
47 | ns d = t1 - t0 - ms(250); | |
48 | BOOST_TEST(d < ns(50000000)+ms(1000)); // within 50ms | |
49 | } | |
50 | ||
51 | void f2() | |
52 | { | |
53 | time_point t0 = Clock::now(); | |
54 | boost::unique_lock<boost::timed_mutex> lk(m, Clock::now() + ms(250)); | |
55 | BOOST_TEST(lk.owns_lock() == false); | |
56 | time_point t1 = Clock::now(); | |
57 | ns d = t1 - t0 - ms(250); | |
58 | // This test is spurious as it depends on the time the thread system switches the threads | |
59 | BOOST_TEST(d < ns(5000000)+ms(1000)); // within 5ms | |
60 | } | |
61 | ||
62 | int main() | |
63 | { | |
64 | { | |
65 | m.lock(); | |
66 | boost::thread t(f1); | |
67 | boost::this_thread::sleep_for(ms(250)); | |
68 | m.unlock(); | |
69 | t.join(); | |
70 | } | |
71 | { | |
72 | m.lock(); | |
73 | boost::thread t(f2); | |
b32b8144 | 74 | boost::this_thread::sleep_for(ms(350)); |
7c673cae FG |
75 | m.unlock(); |
76 | t.join(); | |
77 | } | |
78 | ||
79 | return boost::report_errors(); | |
80 | } | |
81 | ||
82 | #else | |
83 | #error "Test not applicable: BOOST_THREAD_USES_CHRONO not defined for this platform as not supported" | |
84 | #endif |