Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-09-12 08:50:17

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_MUTEX_WRAPPER_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_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_mutex_functions
0038 {
0039    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0040 
0041    //Non-copyable
0042    winapi_mutex_functions(const winapi_mutex_functions &);
0043    winapi_mutex_functions &operator=(const winapi_mutex_functions &);
0044    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0045 
0046    public:
0047    winapi_mutex_functions(void *mtx_hnd)
0048       : m_mtx_hnd(mtx_hnd)
0049    {}
0050 
0051    void unlock()
0052    {  winapi::release_mutex(m_mtx_hnd);   }
0053 
0054    void lock()
0055    {  return winapi_wrapper_wait_for_single_object(m_mtx_hnd);  }
0056 
0057    bool try_lock()
0058    {  return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd);  }
0059 
0060    template<class TimePoint>
0061    bool timed_lock(const TimePoint &abs_time)
0062    {  return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time);  }
0063 
0064    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0065    protected:
0066    void *m_mtx_hnd;
0067    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0068 };
0069 
0070 //Swappable mutex wrapper
0071 class winapi_mutex_wrapper
0072    : public winapi_mutex_functions
0073 {
0074    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0075 
0076    //Non-copyable
0077    winapi_mutex_wrapper(const winapi_mutex_wrapper &);
0078    winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &);
0079    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
0080 
0081    //Note that Windows API does not return winapi::invalid_handle_value
0082    //when failing to create/open a mutex, but a nullptr
0083 
0084    public:
0085    winapi_mutex_wrapper(void *mtx_hnd = 0)
0086       : winapi_mutex_functions(mtx_hnd)
0087    {}
0088 
0089    ~winapi_mutex_wrapper()
0090    {  this->close(); }
0091 
0092    void *release()
0093    {
0094       void *hnd = m_mtx_hnd;
0095       m_mtx_hnd = 0;
0096       return hnd;
0097    }
0098 
0099    void *handle() const
0100    {  return m_mtx_hnd; }
0101 
0102    template<class CharT>
0103    bool open_or_create(const CharT *name, const permissions &perm)
0104    {
0105       if(m_mtx_hnd == 0){
0106          m_mtx_hnd = winapi::open_or_create_mutex
0107             ( name
0108             , false
0109             , (winapi::interprocess_security_attributes*)perm.get_permissions()
0110             );
0111          return m_mtx_hnd != 0;
0112       }
0113       else{
0114          return false;
0115       }
0116    }
0117 
0118    void close()
0119    {
0120       if(m_mtx_hnd != 0){
0121          winapi::close_handle(m_mtx_hnd);
0122          m_mtx_hnd = 0;
0123       }
0124    }
0125 
0126    void swap(winapi_mutex_wrapper &other)
0127    {  void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp;   }
0128 };
0129 
0130 }  //namespace ipcdetail {
0131 }  //namespace interprocess {
0132 }  //namespace boost {
0133 
0134 #include <boost/interprocess/detail/config_end.hpp>
0135 
0136 #endif   //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP