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>
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
)
32 BOOST_ASSERT(!cond_res
);
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
;
51 condition_variable_any(condition_variable
&);
52 condition_variable_any
& operator=(condition_variable
&);
55 condition_variable_any()
57 int const res
=pthread_mutex_init(&internal_mutex
,NULL
);
60 throw thread_resource_error();
62 int const res2
=pthread_cond_init(&cond
,NULL
);
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
)
80 detail::interruption_checker
check_for_interruption(&cond
);
82 boost::pthread::pthread_mutex_scoped_lock
internal_lock(&internal_mutex
);
84 res
=pthread_cond_wait(&cond
,&internal_mutex
);
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
);
106 detail::interruption_checker
check_for_interruption(&cond
);
108 boost::pthread::pthread_mutex_scoped_lock
internal_lock(&internal_mutex
);
110 res
=pthread_cond_timedwait(&cond
,&internal_mutex
,&timeout
);
120 throw condition_error();
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
)
141 if(!timed_wait(m
, wait_until
))
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
);
161 boost::pthread::pthread_mutex_scoped_lock
internal_lock(&internal_mutex
);
162 BOOST_VERIFY(!pthread_cond_signal(&cond
));
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>