Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: // Chris@16: // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: // See http://www.boost.org/libs/interprocess for documentation. Chris@16: // Chris@16: ////////////////////////////////////////////////////////////////////////////// Chris@16: Chris@16: #ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP Chris@16: #define BOOST_INTERPROCESS_POSIX_CONDITION_HPP Chris@16: Chris@101: #ifndef BOOST_CONFIG_HPP Chris@101: # include Chris@101: #endif Chris@101: # Chris@101: #if defined(BOOST_HAS_PRAGMA_ONCE) Chris@16: # pragma once Chris@16: #endif Chris@16: Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace interprocess { Chris@16: namespace ipcdetail { Chris@16: Chris@16: class posix_condition Chris@16: { Chris@16: //Non-copyable Chris@16: posix_condition(const posix_condition &); Chris@16: posix_condition &operator=(const posix_condition &); Chris@16: Chris@16: public: Chris@16: //!Constructs a posix_condition. On error throws interprocess_exception. Chris@16: posix_condition(); Chris@16: Chris@16: //!Destroys *this Chris@16: //!liberating system resources. Chris@16: ~posix_condition(); Chris@16: Chris@16: //!If there is a thread waiting on *this, change that Chris@16: //!thread's state to ready. Otherwise there is no effect. Chris@16: void notify_one(); Chris@16: Chris@16: //!Change the state of all threads waiting on *this to ready. Chris@16: //!If there are no waiting threads, notify_all() has no effect. Chris@16: void notify_all(); Chris@16: Chris@16: //!Releases the lock on the posix_mutex object associated with lock, blocks Chris@16: //!the current thread of execution until readied by a call to Chris@16: //!this->notify_one() or this->notify_all(), and then reacquires the lock. Chris@16: template Chris@16: void wait(L& lock) Chris@16: { Chris@16: if (!lock) Chris@16: throw lock_exception(); Chris@16: this->do_wait(*lock.mutex()); Chris@16: } Chris@16: Chris@16: //!The same as: Chris@16: //!while (!pred()) wait(lock) Chris@16: template Chris@16: void wait(L& lock, Pr pred) Chris@16: { Chris@16: if (!lock) Chris@16: throw lock_exception(); Chris@16: Chris@16: while (!pred()) Chris@16: this->do_wait(*lock.mutex()); Chris@16: } Chris@16: Chris@16: //!Releases the lock on the posix_mutex object associated with lock, blocks Chris@16: //!the current thread of execution until readied by a call to Chris@16: //!this->notify_one() or this->notify_all(), or until time abs_time is reached, Chris@16: //!and then reacquires the lock. Chris@16: //!Returns: false if time abs_time is reached, otherwise true. Chris@16: template Chris@16: bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time) Chris@16: { Chris@101: if (!lock) Chris@101: throw lock_exception(); Chris@101: //Posix does not support infinity absolute time so handle it here Chris@16: if(abs_time == boost::posix_time::pos_infin){ Chris@16: this->wait(lock); Chris@16: return true; Chris@16: } Chris@16: return this->do_timed_wait(abs_time, *lock.mutex()); Chris@16: } Chris@16: Chris@16: //!The same as: while (!pred()) { Chris@16: //! if (!timed_wait(lock, abs_time)) return pred(); Chris@16: //! } return true; Chris@16: template Chris@16: bool timed_wait(L& lock, const boost::posix_time::ptime &abs_time, Pr pred) Chris@16: { Chris@101: if (!lock) Chris@101: throw lock_exception(); Chris@101: //Posix does not support infinity absolute time so handle it here Chris@16: if(abs_time == boost::posix_time::pos_infin){ Chris@16: this->wait(lock, pred); Chris@16: return true; Chris@16: } Chris@16: while (!pred()){ Chris@16: if (!this->do_timed_wait(abs_time, *lock.mutex())) Chris@16: return pred(); Chris@16: } Chris@16: return true; Chris@16: } Chris@16: Chris@16: Chris@16: void do_wait(posix_mutex &mut); Chris@16: Chris@16: bool do_timed_wait(const boost::posix_time::ptime &abs_time, posix_mutex &mut); Chris@16: Chris@16: private: Chris@16: pthread_cond_t m_condition; Chris@16: }; Chris@16: Chris@16: inline posix_condition::posix_condition() Chris@16: { Chris@16: int res; Chris@16: pthread_condattr_t cond_attr; Chris@16: res = pthread_condattr_init(&cond_attr); Chris@16: if(res != 0){ Chris@16: throw interprocess_exception("pthread_condattr_init failed"); Chris@16: } Chris@16: res = pthread_condattr_setpshared(&cond_attr, PTHREAD_PROCESS_SHARED); Chris@16: if(res != 0){ Chris@16: pthread_condattr_destroy(&cond_attr); Chris@16: throw interprocess_exception(res); Chris@16: } Chris@16: res = pthread_cond_init(&m_condition, &cond_attr); Chris@16: pthread_condattr_destroy(&cond_attr); Chris@16: if(res != 0){ Chris@16: throw interprocess_exception(res); Chris@16: } Chris@16: } Chris@16: Chris@16: inline posix_condition::~posix_condition() Chris@16: { Chris@16: int res = 0; Chris@16: res = pthread_cond_destroy(&m_condition); Chris@16: BOOST_ASSERT(res == 0); (void)res; Chris@16: } Chris@16: Chris@16: inline void posix_condition::notify_one() Chris@16: { Chris@16: int res = 0; Chris@16: res = pthread_cond_signal(&m_condition); Chris@16: BOOST_ASSERT(res == 0); (void)res; Chris@16: } Chris@16: Chris@16: inline void posix_condition::notify_all() Chris@16: { Chris@16: int res = 0; Chris@16: res = pthread_cond_broadcast(&m_condition); Chris@16: BOOST_ASSERT(res == 0); (void)res; Chris@16: } Chris@16: Chris@16: inline void posix_condition::do_wait(posix_mutex &mut) Chris@16: { Chris@16: pthread_mutex_t* pmutex = &mut.m_mut; Chris@16: int res = 0; Chris@16: res = pthread_cond_wait(&m_condition, pmutex); Chris@16: BOOST_ASSERT(res == 0); (void)res; Chris@16: } Chris@16: Chris@16: inline bool posix_condition::do_timed_wait Chris@16: (const boost::posix_time::ptime &abs_time, posix_mutex &mut) Chris@16: { Chris@16: timespec ts = ptime_to_timespec(abs_time); Chris@16: pthread_mutex_t* pmutex = &mut.m_mut; Chris@16: int res = 0; Chris@16: res = pthread_cond_timedwait(&m_condition, pmutex, &ts); Chris@16: BOOST_ASSERT(res == 0 || res == ETIMEDOUT); Chris@16: Chris@16: return res != ETIMEDOUT; Chris@16: } Chris@16: Chris@16: } //namespace ipcdetail Chris@16: } //namespace interprocess Chris@16: } //namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif //#ifndef BOOST_INTERPROCESS_POSIX_CONDITION_HPP