Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-07-13 08:14:47

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_ANY_HPP
0012 #define BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_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/sync/interprocess_mutex.hpp>
0037 #include <boost/interprocess/sync/scoped_lock.hpp>
0038 #include <boost/interprocess/timed_utils.hpp>
0039 #include <boost/interprocess/sync/detail/condition_any_algorithm.hpp>
0040 
0041 //!\file
0042 //!Describes process-shared variables interprocess_condition class
0043 
0044 namespace boost {
0045 namespace interprocess {
0046 namespace ipcdetail {
0047 
0048 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0049 class interprocess_tester;
0050 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0051 
0052 //! A global condition variable that can be created by name.
0053 //! This condition variable is designed to work with named_mutex and
0054 //! can't be placed in shared memory or memory mapped files.
0055 class shm_named_condition_any
0056 {
0057    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0058    //Non-copyable
0059    shm_named_condition_any();
0060    shm_named_condition_any(const shm_named_condition_any &);
0061    shm_named_condition_any &operator=(const shm_named_condition_any &);
0062    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0063    public:
0064    //!Creates a global condition with a name.
0065    //!If the condition can't be created throws interprocess_exception
0066    template <class CharT>
0067    shm_named_condition_any(create_only_t, const CharT *name, const permissions &perm = permissions())
0068       :  m_shmem  (create_only_t()
0069                   ,name
0070                   ,sizeof(internal_condition) +
0071                      open_create_impl_t::ManagedOpenOrCreateUserOffset
0072                   ,read_write
0073                   ,0
0074                   ,construct_func_t(DoCreate)
0075                   ,perm)
0076    {}
0077 
0078    //!Opens or creates a global condition with a name.
0079    //!If the condition is created, this call is equivalent to
0080    //!shm_named_condition_any(create_only_t, ... )
0081    //!If the condition is already created, this call is equivalent
0082    //!shm_named_condition_any(open_only_t, ... )
0083    //!Does not throw
0084    template <class CharT>
0085    shm_named_condition_any(open_or_create_t, const CharT *name, const permissions &perm = permissions())
0086       :  m_shmem  (open_or_create_t()
0087                   ,name
0088                   ,sizeof(internal_condition) +
0089                      open_create_impl_t::ManagedOpenOrCreateUserOffset
0090                   ,read_write
0091                   ,0
0092                   ,construct_func_t(DoOpenOrCreate)
0093                   ,perm)
0094    {}
0095 
0096    //!Opens a global condition with a name if that condition is previously
0097    //!created. If it is not previously created this function throws
0098    //!interprocess_exception.
0099    template <class CharT>
0100    shm_named_condition_any(open_only_t, const CharT *name)
0101       :  m_shmem  (open_only_t()
0102                   ,name
0103                   ,read_write
0104                   ,0
0105                   ,construct_func_t(DoOpen))
0106    {}
0107 
0108    //!Destroys *this and indicates that the calling process is finished using
0109    //!the resource. The destructor function will deallocate
0110    //!any system resources allocated by the system for use by this process for
0111    //!this resource. The resource can still be opened again calling
0112    //!the open constructor overload. To erase the resource from the system
0113    //!use remove().
0114    ~shm_named_condition_any()
0115    {}
0116 
0117    //!If there is a thread waiting on *this, change that
0118    //!thread's state to ready. Otherwise there is no effect.*/
0119    void notify_one()
0120    {  this->internal_cond().notify_one(); }
0121 
0122    //!Change the state of all threads waiting on *this to ready.
0123    //!If there are no waiting threads, notify_all() has no effect.
0124    void notify_all()
0125    {  this->internal_cond().notify_all(); }
0126 
0127    //!Releases the lock on the named_mutex object associated with lock, blocks
0128    //!the current thread of execution until readied by a call to
0129    //!this->notify_one() or this->notify_all(), and then reacquires the lock.
0130    template <typename L>
0131    void wait(L& lock)
0132    {  this->internal_cond().wait(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    {  this->internal_cond().wait(lock, 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, typename TimePoint>
0146    bool timed_wait(L& lock, const TimePoint &abs_time)
0147    {  return this->internal_cond().timed_wait(lock, abs_time); }
0148 
0149    //!The same as:   while (!pred()) {
0150    //!                  if (!timed_wait(lock, abs_time)) return pred();
0151    //!               } return true;
0152    template <typename L, typename TimePoint, typename Pr>
0153    bool timed_wait(L& lock, const TimePoint &abs_time, Pr pred)
0154    {  return this->internal_cond().timed_wait(lock, abs_time, pred); }
0155 
0156    //!Same as `timed_wait`, but this function is modeled after the
0157    //!standard library interface.
0158    template <typename L, class TimePoint>
0159    cv_status wait_until(L& lock, const TimePoint &abs_time)
0160    {  return this->timed_wait(lock, abs_time) ? cv_status::no_timeout : cv_status::timeout; }
0161 
0162    //!Same as `timed_wait`, but this function is modeled after the
0163    //!standard library interface.
0164    template <typename L, class TimePoint, typename Pr>
0165    bool wait_until(L& lock, const TimePoint &abs_time, Pr pred)
0166    {  return this->timed_wait(lock, abs_time, pred); }
0167 
0168    //!Same as `timed_wait`, but this function is modeled after the
0169    //!standard library interface and uses relative timeouts.
0170    template <typename L, class Duration>
0171    cv_status wait_for(L& lock, const Duration &dur)
0172    {  return this->wait_until(lock, ipcdetail::duration_to_ustime(dur)); }
0173 
0174    //!Same as `timed_wait`, but this function is modeled after the
0175    //!standard library interface and uses relative timeouts
0176    template <typename L, class Duration, typename Pr>
0177    bool wait_for(L& lock, const Duration &dur, Pr pred)
0178    {  return this->wait_until(lock, ipcdetail::duration_to_ustime(dur), pred); }
0179 
0180    //!Erases a named condition from the system.
0181    //!Returns false on error. Never throws.
0182    template <class CharT>
0183    static bool remove(const CharT *name)
0184    {  return shared_memory_object::remove(name); }
0185 
0186    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0187    private:
0188 
0189    class internal_condition_members
0190    {
0191       public:
0192       typedef interprocess_mutex       mutex_type;
0193       typedef interprocess_condition   condvar_type;
0194 
0195       condvar_type&  get_condvar() {  return m_cond;  }
0196       mutex_type&    get_mutex()   {  return m_mtx; }
0197 
0198       private:
0199       mutex_type     m_mtx;
0200       condvar_type   m_cond;
0201    };
0202 
0203    typedef ipcdetail::condition_any_wrapper<internal_condition_members> internal_condition;
0204 
0205    internal_condition &internal_cond()
0206    {  return *static_cast<internal_condition*>(m_shmem.get_user_address()); }
0207 
0208    friend class boost::interprocess::ipcdetail::interprocess_tester;
0209    void dont_close_on_destruction()
0210    {  interprocess_tester::dont_close_on_destruction(m_shmem);  }
0211 
0212    typedef ipcdetail::managed_open_or_create_impl<shared_memory_object, 0, true, false> open_create_impl_t;
0213    open_create_impl_t m_shmem;
0214 
0215    template <class T, class Arg> friend class boost::interprocess::ipcdetail::named_creation_functor;
0216    typedef boost::interprocess::ipcdetail::named_creation_functor<internal_condition> construct_func_t;
0217    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0218 };
0219 
0220 }  //namespace ipcdetail
0221 }  //namespace interprocess
0222 }  //namespace boost
0223 
0224 #include <boost/interprocess/detail/config_end.hpp>
0225 
0226 #endif // BOOST_INTERPROCESS_SHM_NAMED_CONDITION_ANY_HPP