annotate DEPENDENCIES/generic/include/boost/interprocess/sync/posix/condition.hpp @ 16:2665513ce2d3

Add boost headers
author Chris Cannam
date Tue, 05 Aug 2014 11:11:38 +0100
parents
children c530137014c0
rev   line source
Chris@16 1 //////////////////////////////////////////////////////////////////////////////
Chris@16 2 //
Chris@16 3 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
Chris@16 4 // Software License, Version 1.0. (See accompanying file
Chris@16 5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
Chris@16 6 //
Chris@16 7 // See http://www.boost.org/libs/interprocess for documentation.
Chris@16 8 //
Chris@16 9 //////////////////////////////////////////////////////////////////////////////
Chris@16 10
Chris@16 11 #ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP
Chris@16 12 #define BOOST_INTERPROCESS_POSIX_CONDITION_HPP
Chris@16 13
Chris@16 14 #if (defined _MSC_VER) && (_MSC_VER >= 1200)
Chris@16 15 # pragma once
Chris@16 16 #endif
Chris@16 17
Chris@16 18 #include <boost/interprocess/detail/config_begin.hpp>
Chris@16 19 #include <boost/interprocess/detail/workaround.hpp>
Chris@16 20
Chris@16 21 #include <pthread.h>
Chris@16 22 #include <errno.h>
Chris@16 23 #include <boost/interprocess/sync/posix/pthread_helpers.hpp>
Chris@16 24 #include <boost/interprocess/sync/posix/ptime_to_timespec.hpp>
Chris@16 25 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
Chris@16 26 #include <boost/interprocess/sync/posix/mutex.hpp>
Chris@16 27 #include <boost/assert.hpp>
Chris@16 28
Chris@16 29 namespace boost {
Chris@16 30 namespace interprocess {
Chris@16 31 namespace ipcdetail {
Chris@16 32
Chris@16 33 class posix_condition
Chris@16 34 {
Chris@16 35 //Non-copyable
Chris@16 36 posix_condition(const posix_condition &);
Chris@16 37 posix_condition &operator=(const posix_condition &);
Chris@16 38
Chris@16 39 public:
Chris@16 40 //!Constructs a posix_condition. On error throws interprocess_exception.
Chris@16 41 posix_condition();
Chris@16 42
Chris@16 43 //!Destroys *this
Chris@16 44 //!liberating system resources.
Chris@16 45 ~posix_condition();
Chris@16 46
Chris@16 47 //!If there is a thread waiting on *this, change that
Chris@16 48 //!thread's state to ready. Otherwise there is no effect.
Chris@16 49 void notify_one();
Chris@16 50
Chris@16 51 //!Change the state of all threads waiting on *this to ready.
Chris@16 52 //!If there are no waiting threads, notify_all() has no effect.
Chris@16 53 void notify_all();
Chris@16 54
Chris@16 55 //!Releases the lock on the posix_mutex object associated with lock, blocks
Chris@16 56 //!the current thread of execution until readied by a call to
Chris@16 57 //!this->notify_one() or this->notify_all(), and then reacquires the lock.
Chris@16 58 template <typename L>
Chris@16 59 void wait(L& lock)
Chris@16 60 {
Chris@16 61 if (!lock)
Chris@16 62 throw lock_exception();
Chris@16 63 this->do_wait(*lock.mutex());
Chris@16 64 }
Chris@16 65
Chris@16 66 //!The same as:
Chris@16 67 //!while (!pred()) wait(lock)
Chris@16 68 template <typename L, typename Pr>
Chris@16 69 void wait(L& lock, Pr pred)
Chris@16 70 {
Chris@16 71 if (!lock)
Chris@16 72 throw lock_exception();
Chris@16 73
Chris@16 74 while (!pred())
Chris@16 75 this->do_wait(*lock.mutex());
Chris@16 76 }
Chris@16 77
Chris@16 78 //!Releases the lock on the posix_mutex object associated with lock, blocks
Chris@16 79 //!the current thread of execution until readied by a call to
Chris@16 80 //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
Chris@16 81 //!and then reacquires the lock.
Chris@16 82 //!Returns: false if time abs_time is reached, otherwise true.
Chris@16 83 template <typename L>
Chris@16 84 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time)
Chris@16 85 {
Chris@16 86 if(abs_time == boost::posix_time::pos_infin){
Chris@16 87 this->wait(lock);
Chris@16 88 return true;
Chris@16 89 }
Chris@16 90 if (!lock)
Chris@16 91 throw lock_exception();
Chris@16 92 return this->do_timed_wait(abs_time, *lock.mutex());
Chris@16 93 }
Chris@16 94
Chris@16 95 //!The same as: while (!pred()) {
Chris@16 96 //! if (!timed_wait(lock, abs_time)) return pred();
Chris@16 97 //! } return true;
Chris@16 98 template <typename L, typename Pr>
Chris@16 99 bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred)
Chris@16 100 {
Chris@16 101 if(abs_time == boost::posix_time::pos_infin){
Chris@16 102 this->wait(lock, pred);
Chris@16 103 return true;
Chris@16 104 }
Chris@16 105 if (!lock)
Chris@16 106 throw lock_exception();
Chris@16 107 while (!pred()){
Chris@16 108 if (!this->do_timed_wait(abs_time, *lock.mutex()))
Chris@16 109 return pred();
Chris@16 110 }
Chris@16 111
Chris@16 112 return true;
Chris@16 113 }
Chris@16 114
Chris@16 115
Chris@16 116 void do_wait(posix_mutex &mut);
Chris@16 117
Chris@16 118 bool do_timed_wait(const boost::posix_time::ptime &abs_time, posix_mutex &mut);
Chris@16 119
Chris@16 120 private:
Chris@16 121 pthread_cond_t m_condition;
Chris@16 122 };
Chris@16 123
Chris@16 124 inline posix_condition::posix_condition()
Chris@16 125 {
Chris@16 126 int res;
Chris@16 127 pthread_condattr_t cond_attr;
Chris@16 128 res = pthread_condattr_init(&cond_attr);
Chris@16 129 if(res != 0){
Chris@16 130 throw interprocess_exception("pthread_condattr_init failed");
Chris@16 131 }
Chris@16 132 res = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED);
Chris@16 133 if(res != 0){
Chris@16 134 pthread_condattr_destroy(&cond_attr);
Chris@16 135 throw interprocess_exception(res);
Chris@16 136 }
Chris@16 137 res = pthread_cond_init(&m_condition, &cond_attr);
Chris@16 138 pthread_condattr_destroy(&cond_attr);
Chris@16 139 if(res != 0){
Chris@16 140 throw interprocess_exception(res);
Chris@16 141 }
Chris@16 142 }
Chris@16 143
Chris@16 144 inline posix_condition::~posix_condition()
Chris@16 145 {
Chris@16 146 int res = 0;
Chris@16 147 res = pthread_cond_destroy(&m_condition);
Chris@16 148 BOOST_ASSERT(res == 0); (void)res;
Chris@16 149 }
Chris@16 150
Chris@16 151 inline void posix_condition::notify_one()
Chris@16 152 {
Chris@16 153 int res = 0;
Chris@16 154 res = pthread_cond_signal(&m_condition);
Chris@16 155 BOOST_ASSERT(res == 0); (void)res;
Chris@16 156 }
Chris@16 157
Chris@16 158 inline void posix_condition::notify_all()
Chris@16 159 {
Chris@16 160 int res = 0;
Chris@16 161 res = pthread_cond_broadcast(&m_condition);
Chris@16 162 BOOST_ASSERT(res == 0); (void)res;
Chris@16 163 }
Chris@16 164
Chris@16 165 inline void posix_condition::do_wait(posix_mutex &mut)
Chris@16 166 {
Chris@16 167 pthread_mutex_t* pmutex = &mut.m_mut;
Chris@16 168 int res = 0;
Chris@16 169 res = pthread_cond_wait(&m_condition, pmutex);
Chris@16 170 BOOST_ASSERT(res == 0); (void)res;
Chris@16 171 }
Chris@16 172
Chris@16 173 inline bool posix_condition::do_timed_wait
Chris@16 174 (const boost::posix_time::ptime &abs_time, posix_mutex &mut)
Chris@16 175 {
Chris@16 176 timespec ts = ptime_to_timespec(abs_time);
Chris@16 177 pthread_mutex_t* pmutex = &mut.m_mut;
Chris@16 178 int res = 0;
Chris@16 179 res = pthread_cond_timedwait(&m_condition, pmutex, &ts);
Chris@16 180 BOOST_ASSERT(res == 0 || res == ETIMEDOUT);
Chris@16 181
Chris@16 182 return res != ETIMEDOUT;
Chris@16 183 }
Chris@16 184
Chris@16 185 } //namespace ipcdetail
Chris@16 186 } //namespace interprocess
Chris@16 187 } //namespace boost
Chris@16 188
Chris@16 189 #include <boost/interprocess/detail/config_end.hpp>
Chris@16 190
Chris@16 191 #endif //#ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP