Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / thread / pthread / condition_variable_fwd.hpp
1 #ifndef BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
2 #define BOOST_THREAD_PTHREAD_CONDITION_VARIABLE_FWD_HPP
3 // Distributed under the Boost Software License, Version 1.0. (See
4 // accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // (C) Copyright 2007-8 Anthony Williams
7 // (C) Copyright 2011-2012 Vicente J. Botet Escriba
8
9 #include <boost/assert.hpp>
10 #include <boost/throw_exception.hpp>
11 #include <pthread.h>
12 #include <boost/thread/cv_status.hpp>
13 #include <boost/thread/mutex.hpp>
14 #include <boost/thread/lock_types.hpp>
15 #include <boost/thread/thread_time.hpp>
16 #include <boost/thread/pthread/timespec.hpp>
17 #if defined BOOST_THREAD_USES_DATETIME
18 #include <boost/thread/xtime.hpp>
19 #endif
20 #ifdef BOOST_THREAD_USES_CHRONO
21 #include <boost/chrono/system_clocks.hpp>
22 #include <boost/chrono/ceil.hpp>
23 #endif
24 #include <boost/thread/detail/delete.hpp>
25 #include <boost/date_time/posix_time/posix_time_duration.hpp>
26
27 #include <boost/config/abi_prefix.hpp>
28
29 namespace boost
30 {
31
32     class condition_variable
33     {
34     private:
35 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
36         pthread_mutex_t internal_mutex;
37 #endif
38         pthread_cond_t cond;
39
40     public:
41     //private: // used by boost::thread::try_join_until
42
43         inline bool do_wait_until(
44             unique_lock<mutex>& lock,
45             struct timespec const &timeout);
46
47         bool do_wait_for(
48             unique_lock<mutex>& lock,
49             struct timespec const &timeout)
50         {
51           return do_wait_until(lock, boost::detail::timespec_plus(timeout, boost::detail::timespec_now()));
52         }
53
54     public:
55       BOOST_THREAD_NO_COPYABLE(condition_variable)
56         condition_variable()
57         {
58 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
59             int const res=pthread_mutex_init(&internal_mutex,NULL);
60             if(res)
61             {
62                 boost::throw_exception(thread_resource_error(res, "boost::condition_variable::condition_variable() constructor failed in pthread_mutex_init"));
63             }
64 #endif
65             int const res2=pthread_cond_init(&cond,NULL);
66             if(res2)
67             {
68 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
69                 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
70 #endif
71                 boost::throw_exception(thread_resource_error(res2, "boost::condition_variable::condition_variable() constructor failed in pthread_cond_init"));
72             }
73         }
74         ~condition_variable()
75         {
76             int ret;
77 #if defined BOOST_THREAD_PROVIDES_INTERRUPTIONS
78             do {
79               ret = pthread_mutex_destroy(&internal_mutex);
80             } while (ret == EINTR);
81             BOOST_ASSERT(!ret);
82 #endif
83             do {
84               ret = pthread_cond_destroy(&cond);
85             } while (ret == EINTR);
86             BOOST_ASSERT(!ret);
87         }
88
89         void wait(unique_lock<mutex>& m);
90
91         template<typename predicate_type>
92         void wait(unique_lock<mutex>& m,predicate_type pred)
93         {
94             while(!pred()) wait(m);
95         }
96
97
98 #if defined BOOST_THREAD_USES_DATETIME
99         inline bool timed_wait(
100             unique_lock<mutex>& m,
101             boost::system_time const& abs_time)
102         {
103 #if defined BOOST_THREAD_WAIT_BUG
104             struct timespec const timeout=detail::to_timespec(abs_time + BOOST_THREAD_WAIT_BUG);
105             return do_wait_until(m, timeout);
106 #else
107             struct timespec const timeout=detail::to_timespec(abs_time);
108             return do_wait_until(m, timeout);
109 #endif
110         }
111         bool timed_wait(
112             unique_lock<mutex>& m,
113             xtime const& abs_time)
114         {
115             return timed_wait(m,system_time(abs_time));
116         }
117
118         template<typename duration_type>
119         bool timed_wait(
120             unique_lock<mutex>& m,
121             duration_type const& wait_duration)
122         {
123             return timed_wait(m,get_system_time()+wait_duration);
124         }
125
126         template<typename predicate_type>
127         bool timed_wait(
128             unique_lock<mutex>& m,
129             boost::system_time const& abs_time,predicate_type pred)
130         {
131             while (!pred())
132             {
133                 if(!timed_wait(m, abs_time))
134                     return pred();
135             }
136             return true;
137         }
138
139         template<typename predicate_type>
140         bool timed_wait(
141             unique_lock<mutex>& m,
142             xtime const& abs_time,predicate_type pred)
143         {
144             return timed_wait(m,system_time(abs_time),pred);
145         }
146
147         template<typename duration_type,typename predicate_type>
148         bool timed_wait(
149             unique_lock<mutex>& m,
150             duration_type const& wait_duration,predicate_type pred)
151         {
152             return timed_wait(m,get_system_time()+wait_duration,pred);
153         }
154 #endif
155
156 #ifdef BOOST_THREAD_USES_CHRONO
157
158         template <class Duration>
159         cv_status
160         wait_until(
161                 unique_lock<mutex>& lock,
162                 const chrono::time_point<chrono::system_clock, Duration>& t)
163         {
164           using namespace chrono;
165           typedef time_point<system_clock, nanoseconds> nano_sys_tmpt;
166           wait_until(lock,
167                         nano_sys_tmpt(ceil<nanoseconds>(t.time_since_epoch())));
168           return system_clock::now() < t ? cv_status::no_timeout :
169                                              cv_status::timeout;
170         }
171
172         template <class Clock, class Duration>
173         cv_status
174         wait_until(
175                 unique_lock<mutex>& lock,
176                 const chrono::time_point<Clock, Duration>& t)
177         {
178           using namespace chrono;
179           system_clock::time_point     s_now = system_clock::now();
180           typename Clock::time_point  c_now = Clock::now();
181           wait_until(lock, s_now + ceil<nanoseconds>(t - c_now));
182           return Clock::now() < t ? cv_status::no_timeout : cv_status::timeout;
183         }
184
185         template <class Clock, class Duration, class Predicate>
186         bool
187         wait_until(
188                 unique_lock<mutex>& lock,
189                 const chrono::time_point<Clock, Duration>& t,
190                 Predicate pred)
191         {
192             while (!pred())
193             {
194                 if (wait_until(lock, t) == cv_status::timeout)
195                     return pred();
196             }
197             return true;
198         }
199
200
201         template <class Rep, class Period>
202         cv_status
203         wait_for(
204                 unique_lock<mutex>& lock,
205                 const chrono::duration<Rep, Period>& d)
206         {
207           using namespace chrono;
208           system_clock::time_point s_now = system_clock::now();
209           steady_clock::time_point c_now = steady_clock::now();
210           wait_until(lock, s_now + ceil<nanoseconds>(d));
211           return steady_clock::now() - c_now < d ? cv_status::no_timeout :
212                                                    cv_status::timeout;
213
214         }
215
216
217         template <class Rep, class Period, class Predicate>
218         bool
219         wait_for(
220                 unique_lock<mutex>& lock,
221                 const chrono::duration<Rep, Period>& d,
222                 Predicate pred)
223         {
224           return wait_until(lock, chrono::steady_clock::now() + d, boost::move(pred));
225
226 //          while (!pred())
227 //          {
228 //              if (wait_for(lock, d) == cv_status::timeout)
229 //                  return pred();
230 //          }
231 //          return true;
232         }
233 #endif
234
235 #define BOOST_THREAD_DEFINES_CONDITION_VARIABLE_NATIVE_HANDLE
236         typedef pthread_cond_t* native_handle_type;
237         native_handle_type native_handle()
238         {
239             return &cond;
240         }
241
242         void notify_one() BOOST_NOEXCEPT;
243         void notify_all() BOOST_NOEXCEPT;
244
245 #ifdef BOOST_THREAD_USES_CHRONO
246         inline cv_status wait_until(
247             unique_lock<mutex>& lk,
248             chrono::time_point<chrono::system_clock, chrono::nanoseconds> tp)
249         {
250             using namespace chrono;
251             nanoseconds d = tp.time_since_epoch();
252             timespec ts = boost::detail::to_timespec(d);
253             if (do_wait_until(lk, ts)) return cv_status::no_timeout;
254             else return cv_status::timeout;
255         }
256 #endif
257     };
258
259     BOOST_THREAD_DECL void notify_all_at_thread_exit(condition_variable& cond, unique_lock<mutex> lk);
260
261 }
262
263
264 #include <boost/config/abi_suffix.hpp>
265
266 #endif