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