1 //////////////////////////////////////////////////////////////////////////////
3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 // See http://www.boost.org/libs/interprocess for documentation.
9 //////////////////////////////////////////////////////////////////////////////
11 #ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP
12 #define BOOST_INTERPROCESS_POSIX_CONDITION_HPP
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
27 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
28 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
29 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
30 #include <boost/interprocess/sync/posix/mutex.hpp>
31 #include <boost/assert.hpp>
34 namespace interprocess {
40 posix_condition(const posix_condition &);
41 posix_condition &operator=(const posix_condition &);
44 //!Constructs a posix_condition. On error throws interprocess_exception.
48 //!liberating system resources.
51 //!If there is a thread waiting on *this, change that
52 //!thread's state to ready. Otherwise there is no effect.
55 //!Change the state of all threads waiting on *this to ready.
56 //!If there are no waiting threads, notify_all() has no effect.
59 //!Releases the lock on the posix_mutex object associated with lock, blocks
60 //!the current thread of execution until readied by a call to
61 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
66 throw lock_exception();
67 this->do_wait(*lock.mutex());
71 //!while (!pred()) wait(lock)
72 template <typename L, typename Pr>
73 void wait(L& lock, Pr pred)
76 throw lock_exception();
79 this->do_wait(*lock.mutex());
82 //!Releases the lock on the posix_mutex object associated with lock, blocks
83 //!the current thread of execution until readied by a call to
84 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
85 //!and then reacquires the lock.
86 //!Returns: false if time abs_time is reached, otherwise true.
88 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
91 throw lock_exception();
92 //Posix does not support infinity absolute time so handle it here
93 if(abs_time == boost::posix_time::pos_infin){
97 return this->do_timed_wait(abs_time, *lock.mutex());
100 //!The same as: while (!pred()) {
101 //! if (!timed_wait(lock, abs_time)) return pred();
103 template <typename L, typename Pr>
104 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
107 throw lock_exception();
108 //Posix does not support infinity absolute time so handle it here
109 if(abs_time == boost::posix_time::pos_infin){
110 this->wait(lock, pred);
114 if (!this->do_timed_wait(abs_time, *lock.mutex()))
121 void do_wait(posix_mutex &mut);
123 bool do_timed_wait(const boost::posix_time::ptime &abs_time, posix_mutex &mut);
126 pthread_cond_t m_condition;
129 inline posix_condition::posix_condition()
132 pthread_condattr_t cond_attr;
133 res = pthread_condattr_init(&cond_attr);
135 throw interprocess_exception("pthread_condattr_init failed");
137 res = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
139 pthread_condattr_destroy(&cond_attr);
140 throw interprocess_exception(res);
142 res = pthread_cond_init(&m_condition, &cond_attr);
143 pthread_condattr_destroy(&cond_attr);
145 throw interprocess_exception(res);
149 inline posix_condition::~posix_condition()
152 res = pthread_cond_destroy(&m_condition);
153 BOOST_ASSERT(res == 0); (void)res;
156 inline void posix_condition::notify_one()
159 res = pthread_cond_signal(&m_condition);
160 BOOST_ASSERT(res == 0); (void)res;
163 inline void posix_condition::notify_all()
166 res = pthread_cond_broadcast(&m_condition);
167 BOOST_ASSERT(res == 0); (void)res;
170 inline void posix_condition::do_wait(posix_mutex &mut)
172 pthread_mutex_t* pmutex = &mut.m_mut;
174 res = pthread_cond_wait(&m_condition, pmutex);
175 BOOST_ASSERT(res == 0); (void)res;
178 inline bool posix_condition::do_timed_wait
179 (const boost::posix_time::ptime &abs_time, posix_mutex &mut)
181 timespec ts = ptime_to_timespec(abs_time);
182 pthread_mutex_t* pmutex = &mut.m_mut;
184 res = pthread_cond_timedwait(&m_condition, pmutex, &ts);
185 BOOST_ASSERT(res == 0 || res == ETIMEDOUT);
187 return res != ETIMEDOUT;
190 } //namespace ipcdetail
191 } //namespace interprocess
194 #include <boost/interprocess/detail/config_end.hpp>
196 #endif //#ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP