1 #ifndef BOOST_SYNC_TEST_CONDITION_TEST_COMMON_HPP
2 #define BOOST_SYNC_TEST_CONDITION_TEST_COMMON_HPP
3 // Copyright (C) 2007 Anthony Williams
4 // Copyright (C) 2013 Andrey Semashev
5 //
6 //  Distributed under the Boost Software License, Version 1.0. (See accompanying
7 //  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
8 
9 #include <boost/config.hpp>
10 #include <boost/thread/thread_time.hpp>
11 #include <boost/date_time/posix_time/posix_time_types.hpp>
12 #include <boost/sync/locks/unique_lock.hpp>
13 #include <boost/sync/mutexes/mutex.hpp>
14 #include <boost/sync/condition_variables/condition_variable.hpp>
15 #include <boost/sync/support/boost_date_time.hpp>
16 
17 unsigned const timeout_seconds = 5;
18 
19 struct wait_for_flag
20 {
21     struct check_flag
22     {
23         bool const& flag;
24 
check_flagwait_for_flag::check_flag25         explicit check_flag(bool const& flag_) : flag(flag_)
26         {
27         }
28 
check_flagwait_for_flag::check_flag29         check_flag(check_flag const& that) : flag(that.flag)
30         {
31         }
32 
operator ()wait_for_flag::check_flag33         bool operator()() const
34         {
35             return flag;
36         }
37 
38         BOOST_DELETED_FUNCTION(check_flag& operator=(check_flag const&))
39     };
40 
41     boost::sync::mutex mutex;
42     boost::sync::condition_variable cond_var;
43     bool flag;
44     unsigned woken;
45 
wait_for_flagwait_for_flag46     wait_for_flag():
47         flag(false),woken(0)
48     {
49     }
50 
wait_without_predicatewait_for_flag51     void wait_without_predicate()
52     {
53         boost::sync::unique_lock<boost::sync::mutex> lock(mutex);
54         while(!flag)
55         {
56             cond_var.wait(lock);
57         }
58         ++woken;
59     }
60 
wait_with_predicatewait_for_flag61     void wait_with_predicate()
62     {
63         boost::sync::unique_lock<boost::sync::mutex> lock(mutex);
64         cond_var.wait(lock,check_flag(flag));
65         if(flag)
66         {
67             ++woken;
68         }
69     }
70 
timed_wait_without_predicatewait_for_flag71     void timed_wait_without_predicate()
72     {
73         boost::system_time const timeout = boost::get_system_time() + boost::posix_time::seconds(timeout_seconds);
74 
75         boost::sync::unique_lock<boost::sync::mutex> lock(mutex);
76         while(!flag)
77         {
78             if(!cond_var.timed_wait(lock,timeout))
79             {
80                 return;
81             }
82         }
83         ++woken;
84     }
85 
timed_wait_with_predicatewait_for_flag86     void timed_wait_with_predicate()
87     {
88         boost::system_time const timeout = boost::get_system_time() + boost::posix_time::seconds(timeout_seconds);
89         boost::sync::unique_lock<boost::sync::mutex> lock(mutex);
90         if(cond_var.timed_wait(lock,timeout,check_flag(flag)) && flag)
91         {
92             ++woken;
93         }
94     }
relative_timed_wait_with_predicatewait_for_flag95     void relative_timed_wait_with_predicate()
96     {
97         boost::sync::unique_lock<boost::sync::mutex> lock(mutex);
98         if(cond_var.timed_wait(lock,boost::posix_time::seconds(timeout_seconds),check_flag(flag)) && flag)
99         {
100             ++woken;
101         }
102     }
103 
104     BOOST_DELETED_FUNCTION(wait_for_flag(wait_for_flag const&))
105     BOOST_DELETED_FUNCTION(wait_for_flag& operator=(wait_for_flag const&))
106 };
107 
108 #endif
109