Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-01 08:18:08

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