Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-05 08:35:57

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