Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:33

0001 //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2005-2012. Distributed under the Boost
0004 // Software License, Version 1.0. (See accompanying file
0005 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 // See http://www.boost.org/libs/interprocess for documentation.
0008 //
0009 //////////////////////////////////////////////////////////////////////////////
0010 
0011 #ifndef BOOST_INTERPROCESS_CONDITION_HPP
0012 #define BOOST_INTERPROCESS_CONDITION_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 #  pragma once
0020 #endif
0021 
0022 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0023 
0024 #include <boost/interprocess/detail/config_begin.hpp>
0025 #include <boost/interprocess/detail/workaround.hpp>
0026 
0027 #include <boost/interprocess/sync/cv_status.hpp>
0028 #include <boost/interprocess/sync/interprocess_mutex.hpp>
0029 #include <boost/interprocess/sync/detail/locks.hpp>
0030 #include <boost/interprocess/exceptions.hpp>
0031 #include <boost/limits.hpp>
0032 #include <boost/assert.hpp>
0033 
0034 #if   !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)
0035    #include <boost/interprocess/sync/posix/condition.hpp>
0036    #define BOOST_INTERPROCESS_CONDITION_USE_POSIX
0037 //Experimental...
0038 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
0039    #include <boost/interprocess/sync/windows/condition.hpp>
0040    #define BOOST_INTERPROCESS_CONDITION_USE_WINAPI
0041 #else
0042    //spin_condition is used
0043    #include <boost/interprocess/sync/spin/condition.hpp>
0044 #endif
0045 
0046 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0047 
0048 //!\file
0049 //!Describes process-shared variables interprocess_condition class
0050 
0051 namespace boost {
0052 namespace interprocess {
0053 
0054 class named_condition;
0055 
0056 //!This class is a condition variable that can be placed in shared memory or
0057 //!memory mapped files.
0058 //!Destroys the object of type std::condition_variable_any
0059 //!
0060 //!Unlike std::condition_variable in C++11, it is NOT safe to invoke the destructor if all
0061 //!threads have been only notified. It is required that they have exited their respective wait
0062 //!functions.
0063 class interprocess_condition
0064 {
0065    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0066    //Non-copyable
0067    interprocess_condition(const interprocess_condition &);
0068    interprocess_condition &operator=(const interprocess_condition &);
0069    friend class named_condition;
0070    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0071 
0072    public:
0073    //!Constructs a interprocess_condition. On error throws interprocess_exception.
0074    interprocess_condition()
0075    {}
0076 
0077    //!Destroys *this
0078    //!liberating system resources.
0079    ~interprocess_condition()
0080    {}
0081 
0082    //!If there is a thread waiting on *this, change that
0083    //!thread's state to ready. Otherwise there is no effect.
0084    void notify_one()
0085    {  m_condition.notify_one();  }
0086 
0087    //!Change the state of all threads waiting on *this to ready.
0088    //!If there are no waiting threads, notify_all() has no effect.
0089    void notify_all()
0090    {  m_condition.notify_all();  }
0091 
0092    //!Releases the lock on the interprocess_mutex object associated with lock, blocks
0093    //!the current thread of execution until readied by a call to
0094    //!this->notify_one() or this->notify_all(), and then reacquires the lock.
0095    template <typename L>
0096    void wait(L& lock)
0097    {
0098       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0099       m_condition.wait(internal_lock);
0100    }
0101 
0102    //!The same as:
0103    //!while (!pred()) wait(lock)
0104    template <typename L, typename Pr>
0105    void wait(L& lock, Pr pred)
0106    {
0107       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0108       m_condition.wait(internal_lock, pred);
0109    }
0110 
0111    //!Releases the lock on the interprocess_mutex object associated with lock, blocks
0112    //!the current thread of execution until readied by a call to
0113    //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
0114    //!and then reacquires the lock.
0115    //!Returns: false if time abs_time is reached, otherwise true.
0116    template <typename L, class TimePoint>
0117    bool timed_wait(L& lock, const TimePoint &abs_time)
0118    {
0119       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0120       return m_condition.timed_wait(internal_lock, abs_time);
0121    }
0122 
0123    //!The same as:   while (!pred()) {
0124    //!                  if (!timed_wait(lock, abs_time)) return pred();
0125    //!               } return true;
0126    template <typename L, class TimePoint, typename Pr>
0127    bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
0128    {
0129       ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0130       return m_condition.timed_wait(internal_lock, abs_time, pred);
0131    }
0132 
0133    //!Same as `timed_wait`, but this function is modeled after the
0134    //!standard library interface.
0135    template <typename L, class TimePoint>
0136    cv_status wait_until(L& lock, const TimePoint &abs_time)
0137    {  return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
0138 
0139    //!Same as `timed_wait`, but this function is modeled after the
0140    //!standard library interface.
0141    template <typename L, class TimePoint, typename Pr>
0142    bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
0143    {  return this->timed_wait(lock, abs_time, pred); }
0144 
0145    //!Same as `timed_wait`, but this function is modeled after the
0146    //!standard library interface and uses relative timeouts.
0147    template <typename L, class Duration>
0148    cv_status wait_for(L& lock, const Duration &dur)
0149    {  return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
0150 
0151    //!Same as `timed_wait`, but this function is modeled after the
0152    //!standard library interface and uses relative timeouts
0153    template <typename L, class Duration, typename Pr>
0154    bool wait_for(L& lock, const Duration &dur, Pr pred)
0155    {  return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
0156 
0157    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0158 
0159    private:
0160    #if defined(BOOST_INTERPROCESS_CONDITION_USE_POSIX)
0161       ipcdetail::posix_condition m_condition;
0162    #elif defined(BOOST_INTERPROCESS_CONDITION_USE_WINAPI)
0163       ipcdetail::winapi_condition m_condition;
0164    #else
0165       ipcdetail::spin_condition m_condition;
0166    #endif
0167 
0168    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0169 };
0170 
0171 }  //namespace interprocess
0172 }  // namespace boost
0173 
0174 #include <boost/interprocess/detail/config_end.hpp>
0175 
0176 #endif // BOOST_INTERPROCESS_CONDITION_HPP