Back to home page

EIC code displayed by LXR

 
 

    


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