Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-22 08:08:43

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