Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:38:31

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