Imported Upstream version 1.57.0
[platform/upstream/boost.git] / boost / interprocess / sync / windows / winapi_mutex_wrapper.hpp
1  //////////////////////////////////////////////////////////////////////////////
2 //
3 // (C) Copyright Ion Gaztanaga 2011-2012. Distributed under the Boost
4 // Software License, Version 1.0. (See accompanying file
5 // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 //
7 // See http://www.boost.org/libs/interprocess for documentation.
8 //
9 //////////////////////////////////////////////////////////////////////////////
10
11 #ifndef BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
12 #define BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP
13
14 #if defined(_MSC_VER)
15 #  pragma once
16 #endif
17
18 #include <boost/interprocess/detail/config_begin.hpp>
19 #include <boost/interprocess/detail/workaround.hpp>
20 #include <boost/interprocess/creation_tags.hpp>
21 #include <boost/interprocess/permissions.hpp>
22 #include <boost/interprocess/detail/win32_api.hpp>
23 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
24 #include <boost/interprocess/sync/windows/winapi_wrapper_common.hpp>
25 #include <boost/interprocess/errors.hpp>
26 #include <boost/interprocess/exceptions.hpp>
27 #include <limits>
28
29 namespace boost {
30 namespace interprocess {
31 namespace ipcdetail {
32
33 class winapi_mutex_functions
34 {
35    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
36
37    //Non-copyable
38    winapi_mutex_functions(const winapi_mutex_functions &);
39    winapi_mutex_functions &operator=(const winapi_mutex_functions &);
40    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
41
42    public:
43    winapi_mutex_functions(void *mtx_hnd)
44       : m_mtx_hnd(mtx_hnd)
45    {}
46
47    void unlock()
48    {  winapi::release_mutex(m_mtx_hnd);   }
49
50    void lock()
51    {  return winapi_wrapper_wait_for_single_object(m_mtx_hnd);  }
52
53    bool try_lock()
54    {  return winapi_wrapper_try_wait_for_single_object(m_mtx_hnd);  }
55
56    bool timed_lock(const boost::posix_time::ptime &abs_time)
57    {  return winapi_wrapper_timed_wait_for_single_object(m_mtx_hnd, abs_time);  }
58
59    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
60    protected:
61    void *m_mtx_hnd;
62    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
63 };
64
65 //Swappable mutex wrapper
66 class winapi_mutex_wrapper
67    : public winapi_mutex_functions
68 {
69    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
70
71    //Non-copyable
72    winapi_mutex_wrapper(const winapi_mutex_wrapper &);
73    winapi_mutex_wrapper &operator=(const winapi_mutex_wrapper &);
74    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
75
76    //Note that Windows API does not return winapi::invalid_handle_value
77    //when failing to create/open a mutex, but a nullptr
78
79    public:
80    winapi_mutex_wrapper(void *mtx_hnd = 0)
81       : winapi_mutex_functions(mtx_hnd)
82    {}
83
84    ~winapi_mutex_wrapper()
85    {  this->close(); }
86
87    void *release()
88    {
89       void *hnd = m_mtx_hnd;
90       m_mtx_hnd = 0;
91       return hnd;
92    }
93
94    void *handle() const
95    {  return m_mtx_hnd; }
96
97    bool open_or_create(const char *name, const permissions &perm)
98    {
99       if(m_mtx_hnd == 0){
100          m_mtx_hnd = winapi::open_or_create_mutex
101             ( name
102             , false
103             , (winapi::interprocess_security_attributes*)perm.get_permissions()
104             );
105          return m_mtx_hnd != 0;
106       }
107       else{
108          return false;
109       }
110    }
111
112    void close()
113    {
114       if(m_mtx_hnd != 0){
115          winapi::close_handle(m_mtx_hnd);
116          m_mtx_hnd = 0;
117       }
118    }
119
120    void swap(winapi_mutex_wrapper &other)
121    {  void *tmp = m_mtx_hnd; m_mtx_hnd = other.m_mtx_hnd; other.m_mtx_hnd = tmp;   }
122 };
123
124 }  //namespace ipcdetail {
125 }  //namespace interprocess {
126 }  //namespace boost {
127
128 #include <boost/interprocess/detail/config_end.hpp>
129
130 #endif   //BOOST_INTERPROCESS_DETAIL_WINAPI_MUTEX_WRAPPER_HPP