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_NAMED_SEMAPHORE_HPP
0012 #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_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 #include <boost/interprocess/creation_tags.hpp>
0025 #include <boost/interprocess/exceptions.hpp>
0026 #include <boost/interprocess/permissions.hpp>
0027 #include <boost/interprocess/detail/interprocess_tester.hpp>
0028 
0029 #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
0030 #include <boost/interprocess/sync/posix/named_semaphore.hpp>
0031 //Experimental...
0032 #elif !defined(BOOST_INTERPROCESS_FORCE_GENERIC_EMULATION) && defined (BOOST_INTERPROCESS_WINDOWS)
0033    #include <boost/interprocess/sync/windows/named_semaphore.hpp>
0034    #define BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI
0035 #else
0036    #include <boost/interprocess/sync/shm/named_semaphore.hpp>
0037 #endif
0038 
0039 //!\file
0040 //!Describes a named semaphore class for inter-process synchronization
0041 
0042 namespace boost {
0043 namespace interprocess {
0044 
0045 //!A semaphore with a global name, so it can be found from different
0046 //!processes. Allows several resource sharing patterns and efficient
0047 //!acknowledgment mechanisms.
0048 class named_semaphore
0049 {
0050    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0051 
0052    //Non-copyable
0053    named_semaphore();
0054    named_semaphore(const named_semaphore &);
0055    named_semaphore &operator=(const named_semaphore &);
0056    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0057 
0058    public:
0059    //!Creates a global semaphore with a name, and an initial count.
0060    //!If the semaphore can't be created throws interprocess_exception
0061    named_semaphore(create_only_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
0062 
0063    //!Opens or creates a global semaphore with a name, and an initial count.
0064    //!If the semaphore is created, this call is equivalent to
0065    //!named_semaphore(create_only_t, ...)
0066    //!If the semaphore is already created, this call is equivalent to
0067    //!named_semaphore(open_only_t, ... )
0068    //!and initialCount is ignored.
0069    named_semaphore(open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm = permissions());
0070 
0071    //!Opens a global semaphore with a name if that semaphore is previously.
0072    //!created. If it is not previously created this function throws
0073    //!interprocess_exception.
0074    named_semaphore(open_only_t, const char *name);
0075 
0076    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0077 
0078    //!Creates a global semaphore with a name, and an initial count.
0079    //!If the semaphore can't be created throws interprocess_exception
0080    //! 
0081    //!Note: This function is only available on operating systems with
0082    //!      native wchar_t APIs (e.g. Windows).
0083    named_semaphore(create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
0084 
0085    //!Opens or creates a global semaphore with a name, and an initial count.
0086    //!If the semaphore is created, this call is equivalent to
0087    //!named_semaphore(create_only_t, ...)
0088    //!If the semaphore is already created, this call is equivalent to
0089    //!named_semaphore(open_only_t, ... )
0090    //!and initialCount is ignored.
0091    //! 
0092    //!Note: This function is only available on operating systems with
0093    //!      native wchar_t APIs (e.g. Windows).
0094    named_semaphore(open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm = permissions());
0095 
0096    //!Opens a global semaphore with a name if that semaphore is previously.
0097    //!created. If it is not previously created this function throws
0098    //!interprocess_exception.
0099    //! 
0100    //!Note: This function is only available on operating systems with
0101    //!      native wchar_t APIs (e.g. Windows).
0102    named_semaphore(open_only_t, const wchar_t *name);
0103 
0104    #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0105 
0106    //!Destroys *this and indicates that the calling process is finished using
0107    //!the resource. The destructor function will deallocate
0108    //!any system resources allocated by the system for use by this process for
0109    //!this resource. The resource can still be opened again calling
0110    //!the open constructor overload. To erase the resource from the system
0111    //!use remove().
0112    ~named_semaphore();
0113 
0114    //!Increments the semaphore count. If there are processes/threads blocked waiting
0115    //!for the semaphore, then one of these processes will return successfully from
0116    //!its wait function. If there is an error an interprocess_exception exception is thrown.
0117    void post();
0118 
0119    //!Decrements the semaphore. If the semaphore value is not greater than zero,
0120    //!then the calling process/thread blocks until it can decrement the counter.
0121    //!If there is an error an interprocess_exception exception is thrown.
0122    void wait();
0123 
0124    //!Decrements the semaphore if the semaphore's value is greater than zero
0125    //!and returns true. If the value is not greater than zero returns false.
0126    //!If there is an error an interprocess_exception exception is thrown.
0127    bool try_wait();
0128 
0129    //!Decrements the semaphore if the semaphore's value is greater
0130    //!than zero and returns true. Otherwise, waits for the semaphore
0131    //!to the posted or the timeout expires. If the timeout expires, the
0132    //!function returns false. If the semaphore is posted the function
0133    //!returns true. If there is an error throws sem_exception
0134    template<class TimePoint>
0135    bool timed_wait(const TimePoint &abs_time);
0136 
0137    //!Erases a named semaphore from the system.
0138    //!Returns false on error. Never throws.
0139    static bool remove(const char *name);
0140 
0141    #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0142 
0143    //!Erases a named semaphore from the system.
0144    //!Returns false on error. Never throws.
0145    //! 
0146    //!Note: This function is only available on operating systems with
0147    //!      native wchar_t APIs (e.g. Windows).
0148    static bool remove(const wchar_t *name);
0149 
0150    #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0151 
0152    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0153    private:
0154    friend class ipcdetail::interprocess_tester;
0155    void dont_close_on_destruction();
0156 
0157    #if defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USES_POSIX_SEMAPHORES)
0158       typedef ipcdetail::posix_named_semaphore   impl_t;
0159    #elif defined(BOOST_INTERPROCESS_NAMED_SEMAPHORE_USE_WINAPI)
0160       typedef ipcdetail::winapi_named_semaphore impl_t;
0161    #else
0162       typedef ipcdetail::shm_named_semaphore     impl_t;
0163    #endif
0164    impl_t m_sem;
0165    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0166 };
0167 
0168 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0169 
0170 inline named_semaphore::named_semaphore
0171    (create_only_t, const char *name, unsigned int initialCount, const permissions &perm)
0172    :  m_sem(create_only, name, initialCount, perm)
0173 {}
0174 
0175 inline named_semaphore::named_semaphore
0176    (open_or_create_t, const char *name, unsigned int initialCount, const permissions &perm)
0177    :  m_sem(open_or_create, name, initialCount, perm)
0178 {}
0179 
0180 inline named_semaphore::named_semaphore(open_only_t, const char *name)
0181    :  m_sem(open_only, name)
0182 {}
0183 
0184 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0185 
0186 inline named_semaphore::named_semaphore
0187    (create_only_t, const wchar_t *name, unsigned int initialCount, const permissions &perm)
0188    :  m_sem(create_only, name, initialCount, perm)
0189 {}
0190 
0191 inline named_semaphore::named_semaphore
0192    (open_or_create_t, const wchar_t *name, unsigned int initialCount, const permissions &perm)
0193    :  m_sem(open_or_create, name, initialCount, perm)
0194 {}
0195 
0196 inline named_semaphore::named_semaphore(open_only_t, const wchar_t *name)
0197    :  m_sem(open_only, name)
0198 {}
0199 
0200 #endif //defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0201 
0202 inline named_semaphore::~named_semaphore()
0203 {}
0204 
0205 inline void named_semaphore::dont_close_on_destruction()
0206 {  ipcdetail::interprocess_tester::dont_close_on_destruction(m_sem);  }
0207 
0208 inline void named_semaphore::wait()
0209 {  m_sem.wait();  }
0210 
0211 inline void named_semaphore::post()
0212 {  m_sem.post();  }
0213 
0214 inline bool named_semaphore::try_wait()
0215 {  return m_sem.try_wait();  }
0216 
0217 template<class TimePoint>
0218 inline bool named_semaphore::timed_wait(const TimePoint &abs_time)
0219 {  return m_sem.timed_wait(abs_time);  }
0220 
0221 inline bool named_semaphore::remove(const char *name)
0222 {  return impl_t::remove(name);   }
0223 
0224 #if defined(BOOST_INTERPROCESS_WCHAR_NAMED_RESOURCES) || defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0225 
0226 inline bool named_semaphore::remove(const wchar_t *name)
0227 {  return impl_t::remove(name);   }
0228 
0229 #endif
0230 
0231 #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0232 
0233 }  //namespace interprocess {
0234 }  //namespace boost {
0235 
0236 
0237 #include <boost/interprocess/detail/config_end.hpp>
0238 
0239 #endif   //BOOST_INTERPROCESS_NAMED_SEMAPHORE_HPP