Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-15 08:36:49

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