Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-01 08:05:32

0001  //////////////////////////////////////////////////////////////////////////////
0002 //
0003 // (C) Copyright Ion Gaztanaga 2011-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_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_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/permissions.hpp>
0027 #include <boost/interprocess/detail/win32_api.hpp>
0028 #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
0029 #include <boost/interprocess/errors.hpp>
0030 #include <boost/interprocess/exceptions.hpp>
0031 #include <limits>
0032 
0033 namespace boost {
0034 namespace interprocess {
0035 namespace ipcdetail {
0036 
0037 class winapi_semaphore_functions
0038 {
0039    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0040 
0041    //Non-copyable
0042    winapi_semaphore_functions(const winapi_semaphore_functions &);
0043    winapi_semaphore_functions &operator=(const winapi_semaphore_functions &);
0044    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0045 
0046    public:
0047    winapi_semaphore_functions(void *hnd)
0048       : m_sem_hnd(hnd)
0049    {}
0050 
0051    void post(long count = 1)
0052    {
0053       long prev_count;
0054       winapi::release_semaphore(m_sem_hnd, count, &prev_count);
0055    }
0056 
0057    void wait()
0058    {  return winapi_wrapper_wait_for_single_object(m_sem_hnd);  }
0059 
0060    bool try_wait()
0061    {  return winapi_wrapper_try_wait_for_single_object(m_sem_hnd);  }
0062 
0063    template<class TimePoint>
0064    bool timed_wait(const TimePoint &abs_time)
0065    {  return winapi_wrapper_timed_wait_for_single_object(m_sem_hnd, abs_time);  }
0066 
0067    long value() const
0068    {
0069       long l_count, l_limit;
0070       if(!winapi::get_semaphore_info(m_sem_hnd, l_count, l_limit))
0071          return 0;
0072       return l_count;
0073    }
0074 
0075    long limit() const
0076    {
0077       long l_count, l_limit;
0078       if(!winapi::get_semaphore_info(m_sem_hnd, l_count, l_limit))
0079          return 0;
0080       return l_limit;
0081    }
0082 
0083    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0084    protected:
0085    void *m_sem_hnd;
0086    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0087 };
0088 
0089 
0090 //Swappable semaphore wrapper
0091 class winapi_semaphore_wrapper
0092    : public winapi_semaphore_functions
0093 {
0094    winapi_semaphore_wrapper(const winapi_semaphore_wrapper &);
0095    winapi_semaphore_wrapper &operator=(const winapi_semaphore_wrapper &);
0096 
0097    public:
0098 
0099    //Long is 32 bits in windows
0100    static const long MaxCount = long(0x7FFFFFFF);
0101 
0102    winapi_semaphore_wrapper(void *hnd = winapi::invalid_handle_value)
0103       : winapi_semaphore_functions(hnd)
0104    {}
0105 
0106    ~winapi_semaphore_wrapper()
0107    {  this->close(); }
0108 
0109    void *release()
0110    {
0111       void *hnd = m_sem_hnd;
0112       m_sem_hnd = winapi::invalid_handle_value;
0113       return hnd;
0114    }
0115 
0116    void *handle() const
0117    {  return m_sem_hnd; }
0118 
0119    template <class CharT>
0120    bool open_or_create( const CharT *name
0121                       , long sem_count
0122                       , long max_count
0123                       , const permissions &perm
0124                       , bool &created)
0125    {
0126       if(m_sem_hnd == winapi::invalid_handle_value){
0127          m_sem_hnd = winapi::open_or_create_semaphore
0128             ( name
0129             , sem_count
0130             , max_count
0131             , (winapi::interprocess_security_attributes*)perm.get_permissions()
0132             );
0133          created = winapi::get_last_error() != winapi::error_already_exists;
0134          return m_sem_hnd != winapi::invalid_handle_value;
0135       }
0136       else{
0137          return false;
0138       }
0139    }
0140 
0141    bool open_semaphore(const char *name)
0142    {
0143       if(m_sem_hnd == winapi::invalid_handle_value){
0144          m_sem_hnd = winapi::open_semaphore(name);
0145          return m_sem_hnd != winapi::invalid_handle_value;
0146       }
0147       else{
0148          return false;
0149       }
0150    }
0151 
0152    void close()
0153    {
0154       if(m_sem_hnd != winapi::invalid_handle_value){
0155          winapi::close_handle(m_sem_hnd);
0156          m_sem_hnd = winapi::invalid_handle_value;
0157       }
0158    }
0159 
0160    void swap(winapi_semaphore_wrapper &other)
0161    {  void *tmp = m_sem_hnd; m_sem_hnd = other.m_sem_hnd; other.m_sem_hnd = tmp;   }
0162 };
0163 
0164 }  //namespace ipcdetail {
0165 }  //namespace interprocess {
0166 }  //namespace boost {
0167 
0168 #include <boost/interprocess/detail/config_end.hpp>
0169 
0170 #endif   //BOOST_INTERPROCESS_DETAIL_WINAPI_SEMAPHORE_WRAPPER_HPP