Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-18 08:52:38

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_NAMED_CONDITION_HPP
0012 #define BOOST_INTERPROCESS_NAMED_CONDITION_HPP
0013 
0014 #ifndef BOOST_CONFIG_HPP
0015 #  include <boost/config.hpp>
0016 #endif
0017 0018 ">#
0019 #if defined(BOOST_HAS_PRAGMA_ONCE)
0020 #  pragma once
0021 #endif
0022 
0023 #include <boost/interprocess/detail/config_begin.hpp>
0024 #include <boost/interprocess/detail/workaround.hpp>
0025 
0026 #include <boost/interprocess/sync/cv_status.hpp>
0027 #include <boost/interprocess/creation_tags.hpp>
0028 #include <boost/interprocess/exceptions.hpp>
0029 #include <boost/interprocess/timed_utils.hpp>
0030 #include <boost/interprocess/detail/interprocess_tester.hpp>
0031 #include <boost/interprocess/permissions.hpp>
0032 #include <boost/interprocess/sync/detail/locks.hpp>
0033 #if !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
0034    #include <boost/interprocess/sync/windows/named_condition.hpp>
0035    #define BOOST_INTERPROCESS_NAMED_CONDITION_USE_WINAPI
0036 #else
0037    #include <boost/interprocess/sync/shm/named_condition.hpp>
0038 #endif
0039 
0040 //!\file
0041 //!Describes a named condition class for inter-process synchronization
0042 
0043 namespace boost {
0044 namespace interprocess {
0045 
0046 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0047 namespace ipcdetail{ class interprocess_tester; }
0048 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0049 
0050 //! A global condition variable that can be created by name.
0051 //! This condition variable is designed to work with named_mutex and
0052 //! can't be placed in shared memory or memory mapped files.
0053 class named_condition
0054 {
0055    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0056    //Non-copyable
0057    named_condition();
0058    named_condition(const named_condition &);
0059    named_condition &operator=(const named_condition &);
0060    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0061    public:
0062 
0063    //!Creates a global condition with a name.
0064    //!If the condition can't be created throws interprocess_exception
0065    named_condition(create_only_t, const char *name, const permissions &perm = permissions());
0066 
0067    //!Opens or creates a global condition with a name.
0068    //!If the condition is created, this call is equivalent to
0069    //!named_condition(create_only_t, ... )
0070    //!If the condition is already created, this call is equivalent
0071    //!named_condition(open_only_t, ... )
0072    //!Does not throw
0073    named_condition(open_or_create_t, const char *name, const permissions &perm = permissions());
0074 
0075    //!Opens a global condition with a name if that condition is previously
0076    //!created. If it is not previously created this function throws
0077    //!interprocess_exception.
0078    named_condition(open_only_t, const char *name);
0079 
0080    //!Opens a global condition with a name if that condition is previously
0081    //!created. If it is not previously created this function throws
0082    //!interprocess_exception.
0083 
0084    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0085    //!Creates a global condition with a name.
0086    //!If the condition can't be created throws interprocess_exception
0087    //! 
0088    //!Note: This function is only available on operating systems with
0089    //!      native wchar_t APIs (e.g. Windows).
0090    named_condition(create_only_t, const wchar_t *name, const permissions &perm = permissions());
0091 
0092    //!Opens or creates a global condition with a name.
0093    //!If the condition is created, this call is equivalent to
0094    //!named_condition(create_only_t, ... )
0095    //!If the condition is already created, this call is equivalent
0096    //!named_condition(open_only_t, ... )
0097    //!Does not throw
0098    //! 
0099    //!Note: This function is only available on operating systems with
0100    //!      native wchar_t APIs (e.g. Windows).
0101    named_condition(open_or_create_t, const wchar_t *name, const permissions &perm = permissions());
0102 
0103    //!Opens a global condition with a name if that condition is previously
0104    //!created. If it is not previously created this function throws
0105    //!interprocess_exception.
0106    //! 
0107    //!Note: This function is only available on operating systems with
0108    //!      native wchar_t APIs (e.g. Windows).
0109    named_condition(open_only_t, const wchar_t *name);
0110 
0111    #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0112 
0113    //!Destroys *this and indicates that the calling process is finished using
0114    //!the resource. The destructor function will deallocate
0115    //!any system resources allocated by the system for use by this process for
0116    //!this resource. The resource can still be opened again calling
0117    //!the open constructor overload. To erase the resource from the system
0118    //!use remove().
0119    ~named_condition();
0120 
0121    //!If there is a thread waiting on *this, change that
0122    //!thread's state to ready. Otherwise there is no effect.*/
0123    void notify_one();
0124 
0125    //!Change the state of all threads waiting on *this to ready.
0126    //!If there are no waiting threads, notify_all() has no effect.
0127    void notify_all();
0128 
0129    //!Releases the lock on the named_mutex object associated with lock, blocks
0130    //!the current thread of execution until readied by a call to
0131    //!this->notify_one() or this->notify_all(), and then reacquires the lock.
0132    template <typename L>
0133    void wait(L& lock);
0134 
0135    //!The same as:
0136    //!while (!pred()) wait(lock)
0137    template <typename L, typename Pr>
0138    void wait(L& lock, Pr pred);
0139 
0140    //!Releases the lock on the named_mutex object associated with lock, blocks
0141    //!the current thread of execution until readied by a call to
0142    //!this->notify_one() or this->notify_all(), or until time abs_time is reached,
0143    //!and then reacquires the lock.
0144    //!Returns: false if time abs_time is reached, otherwise true.
0145    template <typename L, class TimePoint>
0146    bool timed_wait(L& lock, const TimePoint &abs_time);
0147 
0148    //!The same as:   while (!pred()) {
0149    //!                  if (!timed_wait(lock, abs_time)) return pred();
0150    //!               } return true;
0151    template <typename L, class TimePoint, typename Pr>
0152    bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred);
0153 
0154    //!Same as `timed_wait`, but this function is modeled after the
0155    //!standard library interface.
0156    template <typename L, class TimePoint>
0157    cv_status wait_until(L& lock, const TimePoint &abs_time)
0158    {  return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
0159 
0160    //!Same as `timed_wait`, but this function is modeled after the
0161    //!standard library interface.
0162    template <typename L, class TimePoint, typename Pr>
0163    bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
0164    {  return this->timed_wait(lock, abs_time, pred); }
0165 
0166    //!Same as `timed_wait`, but this function is modeled after the
0167    //!standard library interface and uses relative timeouts.
0168    template <typename L, class Duration>
0169    cv_status wait_for(L& lock, const Duration &dur)
0170    {  return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
0171 
0172    //!Same as `timed_wait`, but this function is modeled after the
0173    //!standard library interface and uses relative timeouts
0174    template <typename L, class Duration, typename Pr>
0175    bool wait_for(L& lock, const Duration &dur, Pr pred)
0176    {  return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
0177 
0178    //!Erases a named condition from the system.
0179    //!Returns false on error. Never throws.
0180    static bool remove(const char *name);
0181 
0182    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0183 
0184    //!Erases a named condition from the system.
0185    //!Returns false on error. Never throws.
0186    //! 
0187    //!Note: This function is only available on operating systems with
0188    //!      native wchar_t APIs (e.g. Windows).
0189    static bool remove(const wchar_t *name);
0190 
0191    #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0192 
0193    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0194    private:
0195    #if defined(BOOST_INTERPROCESS_NAMED_CONDITION_USE_WINAPI)
0196    typedef ipcdetail::winapi_named_condition   condition_type;
0197    #else
0198    typedef ipcdetail::shm_named_condition       condition_type;
0199    #endif
0200    condition_type m_cond;
0201 
0202    friend class ipcdetail::interprocess_tester;
0203    void dont_close_on_destruction()
0204    {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_cond); }
0205    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0206 };
0207 
0208 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0209 
0210 inline named_condition::~named_condition()
0211 {}
0212 
0213 inline named_condition::named_condition(create_only_t, const char *name, const permissions &perm)
0214    :  m_cond(create_only_t(), name, perm)
0215 {}
0216 
0217 inline named_condition::named_condition(open_or_create_t, const char *name, const permissions &perm)
0218    :  m_cond(open_or_create_t(), name, perm)
0219 {}
0220 
0221 inline named_condition::named_condition(open_only_t, const char *name)
0222    :  m_cond(open_only_t(), name)
0223 {}
0224 
0225 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0226 
0227 inline named_condition::named_condition(create_only_t, const wchar_t *name, const permissions &perm)
0228    :  m_cond(create_only_t(), name, perm)
0229 {}
0230 
0231 inline named_condition::named_condition(open_or_create_t, const wchar_t *name, const permissions &perm)
0232    :  m_cond(open_or_create_t(), name, perm)
0233 {}
0234 
0235 inline named_condition::named_condition(open_only_t, const wchar_t *name)
0236    :  m_cond(open_only_t(), name)
0237 {}
0238 
0239 #endif //#if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0240 
0241 
0242 inline void named_condition::notify_one()
0243 {  m_cond.notify_one();  }
0244 
0245 inline void named_condition::notify_all()
0246 {  m_cond.notify_all();  }
0247 
0248 template <typename L>
0249 inline void named_condition::wait(L& lock)
0250 {
0251    ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0252    m_cond.wait(internal_lock);
0253 }
0254 
0255 template <typename L, typename Pr>
0256 inline void named_condition::wait(L& lock, Pr pred)
0257 {
0258    ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0259    m_cond.wait(internal_lock, pred);
0260 }
0261 
0262 template <typename L, typename TimePoint>
0263 inline bool named_condition::timed_wait
0264    (L& lock, const TimePoint &abs_time)
0265 {
0266    ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0267    return m_cond.timed_wait(internal_lock, abs_time);
0268 }
0269 
0270 template <typename L, typename TimePoint, typename Pr>
0271 inline bool named_condition::timed_wait
0272    (L& lock, const TimePoint &abs_time, Pr pred)
0273 {
0274    ipcdetail::internal_mutex_lock<L> internal_lock(lock);
0275    return m_cond.timed_wait(internal_lock, abs_time, pred);
0276 }
0277 
0278 inline bool named_condition::remove(const char *name)
0279 {
0280    return condition_type::remove(name);
0281 }
0282 
0283 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0284 
0285 inline bool named_condition::remove(const wchar_t *name)
0286 {
0287    return condition_type::remove(name);
0288 }
0289 
0290 #endif
0291 
0292 
0293 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0294 
0295 }  //namespace interprocess
0296 }  //namespace boost
0297 
0298 #include <boost/interprocess/detail/config_end.hpp>
0299 
0300 #endif // BOOST_INTERPROCESS_NAMED_CONDITION_HPP