Back to home page

EIC code displayed by LXR

 
 

    


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

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_SEMAPHORE_HPP
0012 #define BOOST_INTERPROCESS_SEMAPHORE_HPP
0013 
0014 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0015 
0016 #ifndef BOOST_CONFIG_HPP
0017 #  include <boost/config.hpp>
0018 #endif
0019 #
0020 #if defined(BOOST_HAS_PRAGMA_ONCE)
0021 #  pragma once
0022 #endif
0023 
0024 #include <boost/interprocess/detail/config_begin.hpp>
0025 #include <boost/interprocess/detail/workaround.hpp>
0026 
0027 #include <boost/interprocess/creation_tags.hpp>
0028 #include <boost/interprocess/exceptions.hpp>
0029 #include <boost/interprocess/sync/detail/locks.hpp>
0030 #include <boost/interprocess/sync/detail/common_algorithms.hpp>
0031 
0032 #if   !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && \
0033        defined(BOOST_INTERPROCESS_POSIX_PROCESS_SHARED)    && \
0034        defined(BOOST_INTERPROCESS_POSIX_UNNAMED_SEMAPHORES)
0035    #include <boost/interprocess/sync/posix/semaphore.hpp>
0036    #define BOOST_INTERPROCESS_SEMAPHORE_USE_POSIX
0037 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
0038    //Experimental...
0039    #include <boost/interprocess/sync/windows/semaphore.hpp>
0040    #define BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI
0041 #else
0042    //spin_semaphore is used
0043    #include <boost/interprocess/sync/spin/semaphore.hpp>
0044 #endif
0045 
0046 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0047 
0048 //!\file
0049 //!Describes a interprocess_semaphore class for inter-process synchronization
0050 
0051 namespace boost {
0052 namespace interprocess {
0053 
0054 //!Wraps a interprocess_semaphore that can be placed in shared memory and can be
0055 //!shared between processes. Allows timed lock tries
0056 class interprocess_semaphore
0057 {
0058    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0059    //Non-copyable
0060    interprocess_semaphore(const interprocess_semaphore &);
0061    interprocess_semaphore &operator=(const interprocess_semaphore &);
0062    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0063    public:
0064    //!Creates a interprocess_semaphore with the given initial count.
0065    //!interprocess_exception if there is an error.*/
0066    interprocess_semaphore(unsigned int initialCount);
0067 
0068    //!Destroys the interprocess_semaphore.
0069    //!Does not throw
0070    ~interprocess_semaphore();
0071 
0072    //!Increments the interprocess_semaphore count. If there are processes/threads blocked waiting
0073    //!for the interprocess_semaphore, then one of these processes will return successfully from
0074    //!its wait function. If there is an error an interprocess_exception exception is thrown.
0075    void post();
0076 
0077    //!Decrements the interprocess_semaphore. If the interprocess_semaphore value is not greater than zero,
0078    //!then the calling process/thread blocks until it can decrement the counter.
0079    //!If there is an error an interprocess_exception exception is thrown.
0080    void wait();
0081 
0082    //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater than zero
0083    //!and returns true. If the value is not greater than zero returns false.
0084    //!If there is an error an interprocess_exception exception is thrown.
0085    bool try_wait();
0086 
0087    //!Decrements the interprocess_semaphore if the interprocess_semaphore's value is greater
0088    //!than zero and returns true. Otherwise, waits for the interprocess_semaphore
0089    //!to the posted or the timeout expires. If the timeout expires, the
0090    //!function returns false. If the interprocess_semaphore is posted the function
0091    //!returns true. If there is an error throws sem_exception
0092    template<class TimePoint>
0093    bool timed_wait(const TimePoint &abs_time);
0094 
0095    //!Returns the interprocess_semaphore count
0096 //   int get_count() const;
0097    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0098    private:
0099    #if defined(BOOST_INTERPROCESS_SEMAPHORE_USE_POSIX)
0100       typedef ipcdetail::posix_semaphore internal_sem_t;
0101    #elif defined(BOOST_INTERPROCESS_SEMAPHORE_USE_WINAPI)
0102       typedef ipcdetail::winapi_semaphore internal_sem_t;
0103    #else
0104       typedef ipcdetail::spin_semaphore internal_sem_t;
0105    #endif   //#if defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION)
0106    internal_sem_t m_sem;
0107    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0108 };
0109 
0110 }  //namespace interprocess {
0111 }  //namespace boost {
0112 
0113 namespace boost {
0114 namespace interprocess {
0115 
0116 inline interprocess_semaphore::interprocess_semaphore(unsigned int initialCount)
0117    : m_sem(initialCount)
0118 {}
0119 
0120 inline interprocess_semaphore::~interprocess_semaphore(){}
0121 
0122 inline void interprocess_semaphore::wait()
0123 {
0124    ipcdetail::lock_to_wait<internal_sem_t> ltw(m_sem);
0125    timeout_when_locking_aware_lock(ltw);
0126 }
0127 
0128 inline bool interprocess_semaphore::try_wait()
0129 { return m_sem.try_wait(); }
0130 
0131 template<class TimePoint>
0132 inline bool interprocess_semaphore::timed_wait(const TimePoint &abs_time)
0133 { return m_sem.timed_wait(abs_time); }
0134 
0135 inline void interprocess_semaphore::post()
0136 { m_sem.post(); }
0137 
0138 }  //namespace interprocess {
0139 }  //namespace boost {
0140 
0141 #include <boost/interprocess/detail/config_end.hpp>
0142 
0143 #endif   //BOOST_INTERPROCESS_SEMAPHORE_HPP