fix doc example typo
[boost.git] / boost / thread / pthread / condition_variable.hpp
blob8ec6ae7fcfd14c14f8ce38c9da495b1a148cf76a
1 #ifndef BOOST_THREAD_CONDITION_VARIABLE_PTHREAD_HPP
2 #define BOOST_THREAD_CONDITION_VARIABLE_PTHREAD_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
8 #include "timespec.hpp"
9 #include "pthread_mutex_scoped_lock.hpp"
10 #include "thread_data.hpp"
11 #include "condition_variable_fwd.hpp"
13 #include <boost/config/abi_prefix.hpp>
15 namespace boost
17 inline void condition_variable::wait(unique_lock<mutex>& m)
19 detail::interruption_checker check_for_interruption(&cond);
20 BOOST_VERIFY(!pthread_cond_wait(&cond,m.mutex()->native_handle()));
23 inline bool condition_variable::timed_wait(unique_lock<mutex>& m,boost::system_time const& wait_until)
25 detail::interruption_checker check_for_interruption(&cond);
26 struct timespec const timeout=detail::get_timespec(wait_until);
27 int const cond_res=pthread_cond_timedwait(&cond,m.mutex()->native_handle(),&timeout);
28 if(cond_res==ETIMEDOUT)
30 return false;
32 BOOST_ASSERT(!cond_res);
33 return true;
36 inline void condition_variable::notify_one()
38 BOOST_VERIFY(!pthread_cond_signal(&cond));
41 inline void condition_variable::notify_all()
43 BOOST_VERIFY(!pthread_cond_broadcast(&cond));
46 class condition_variable_any
48 pthread_mutex_t internal_mutex;
49 pthread_cond_t cond;
51 condition_variable_any(condition_variable&);
52 condition_variable_any& operator=(condition_variable&);
54 public:
55 condition_variable_any()
57 int const res=pthread_mutex_init(&internal_mutex,NULL);
58 if(res)
60 throw thread_resource_error();
62 int const res2=pthread_cond_init(&cond,NULL);
63 if(res2)
65 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
66 throw thread_resource_error();
69 ~condition_variable_any()
71 BOOST_VERIFY(!pthread_mutex_destroy(&internal_mutex));
72 BOOST_VERIFY(!pthread_cond_destroy(&cond));
75 template<typename lock_type>
76 void wait(lock_type& m)
78 int res=0;
80 detail::interruption_checker check_for_interruption(&cond);
82 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
83 m.unlock();
84 res=pthread_cond_wait(&cond,&internal_mutex);
86 m.lock();
88 if(res)
90 throw condition_error();
94 template<typename lock_type,typename predicate_type>
95 void wait(lock_type& m,predicate_type pred)
97 while(!pred()) wait(m);
100 template<typename lock_type>
101 bool timed_wait(lock_type& m,boost::system_time const& wait_until)
103 struct timespec const timeout=detail::get_timespec(wait_until);
104 int res=0;
106 detail::interruption_checker check_for_interruption(&cond);
108 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
109 m.unlock();
110 res=pthread_cond_timedwait(&cond,&internal_mutex,&timeout);
112 m.lock();
114 if(res==ETIMEDOUT)
116 return false;
118 if(res)
120 throw condition_error();
122 return true;
124 template<typename lock_type>
125 bool timed_wait(lock_type& m,xtime const& wait_until)
127 return timed_wait(m,system_time(wait_until));
130 template<typename lock_type,typename duration_type>
131 bool timed_wait(lock_type& m,duration_type const& wait_duration)
133 return timed_wait(m,get_system_time()+wait_duration);
136 template<typename lock_type,typename predicate_type>
137 bool timed_wait(lock_type& m,boost::system_time const& wait_until,predicate_type pred)
139 while (!pred())
141 if(!timed_wait(m, wait_until))
142 return pred();
144 return true;
147 template<typename lock_type,typename predicate_type>
148 bool timed_wait(lock_type& m,xtime const& wait_until,predicate_type pred)
150 return timed_wait(m,system_time(wait_until),pred);
153 template<typename lock_type,typename duration_type,typename predicate_type>
154 bool timed_wait(lock_type& m,duration_type const& wait_duration,predicate_type pred)
156 return timed_wait(m,get_system_time()+wait_duration,pred);
159 void notify_one()
161 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
162 BOOST_VERIFY(!pthread_cond_signal(&cond));
165 void notify_all()
167 boost::pthread::pthread_mutex_scoped_lock internal_lock(&internal_mutex);
168 BOOST_VERIFY(!pthread_cond_broadcast(&cond));
174 #include <boost/config/abi_suffix.hpp>
176 #endif