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 #ifndef BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP
11 #define BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP
12 
13 #ifndef BOOST_CONFIG_HPP
14 #  include <boost/config.hpp>
15 #endif
16 #
17 #if defined(BOOST_HAS_PRAGMA_ONCE)
18 #  pragma once
19 #endif
20 
21 #include <boost/interprocess/detail/config_begin.hpp>
22 #include <boost/interprocess/detail/workaround.hpp>
23 #include <boost/interprocess/creation_tags.hpp>
24 #include <boost/interprocess/permissions.hpp>
25 #include <boost/interprocess/detail/interprocess_tester.hpp>
26 #include <boost/interprocess/detail/posix_time_types_wrk.hpp>
27 #include <boost/interprocess/sync/windows/sync_utils.hpp>
28 #include <boost/interprocess/sync/windows/named_sync.hpp>
29 #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
30 #include <boost/interprocess/errors.hpp>
31 #include <boost/interprocess/exceptions.hpp>
32 #include <limits>
33 
34 namespace boost {
35 namespace interprocess {
36 namespace ipcdetail {
37 
38 
39 
40 class windows_named_mutex
41 {
42    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
43 
44    //Non-copyable
45    windows_named_mutex();
46    windows_named_mutex(const windows_named_mutex &);
47    windows_named_mutex &operator=(const windows_named_mutex &);
48    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
49 
50    public:
51    windows_named_mutex(create_only_t, const char *name, const permissions &perm = permissions());
52 
53    windows_named_mutex(open_or_create_t, const char *name, const permissions &perm = permissions());
54 
55    windows_named_mutex(open_only_t, const char *name);
56 
57    ~windows_named_mutex();
58 
59    void unlock();
60    void lock();
61    bool try_lock();
62    bool timed_lock(const boost::posix_time::ptime &abs_time);
63 
64    static bool remove(const char *name);
65 
66    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
67    private:
68    friend class interprocess_tester;
69    void dont_close_on_destruction();
70    winapi_mutex_wrapper m_mtx_wrapper;
71    windows_named_sync m_named_sync;
72 
73    class named_mut_callbacks : public windows_named_sync_interface
74    {
75       public:
named_mut_callbacks(winapi_mutex_wrapper & mtx_wrapper)76       named_mut_callbacks(winapi_mutex_wrapper &mtx_wrapper)
77          : m_mtx_wrapper(mtx_wrapper)
78       {}
79 
get_data_size() const80       virtual std::size_t get_data_size() const
81       {  return 0u;   }
82 
buffer_with_init_data_to_file()83       virtual const void *buffer_with_init_data_to_file()
84       {  return 0; }
85 
buffer_with_final_data_to_file()86       virtual const void *buffer_with_final_data_to_file()
87       {  return 0; }
88 
buffer_to_store_init_data_from_file()89       virtual void *buffer_to_store_init_data_from_file()
90       {  return 0; }
91 
open(create_enum_t,const char * id_name)92       virtual bool open(create_enum_t, const char *id_name)
93       {
94          std::string aux_str  = "Global\\bipc.mut.";
95          aux_str += id_name;
96          //
97          permissions mut_perm;
98          mut_perm.set_unrestricted();
99          return m_mtx_wrapper.open_or_create(aux_str.c_str(), mut_perm);
100       }
101 
close()102       virtual void close()
103       {
104          m_mtx_wrapper.close();
105       }
106 
~named_mut_callbacks()107       virtual ~named_mut_callbacks()
108       {}
109 
110       private:
111       winapi_mutex_wrapper&     m_mtx_wrapper;
112    };
113    #endif   //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
114 };
115 
~windows_named_mutex()116 inline windows_named_mutex::~windows_named_mutex()
117 {
118    named_mut_callbacks callbacks(m_mtx_wrapper);
119    m_named_sync.close(callbacks);
120 }
121 
dont_close_on_destruction()122 inline void windows_named_mutex::dont_close_on_destruction()
123 {}
124 
windows_named_mutex(create_only_t,const char * name,const permissions & perm)125 inline windows_named_mutex::windows_named_mutex
126    (create_only_t, const char *name, const permissions &perm)
127    : m_mtx_wrapper()
128 {
129    named_mut_callbacks callbacks(m_mtx_wrapper);
130    m_named_sync.open_or_create(DoCreate, name, perm, callbacks);
131 }
132 
windows_named_mutex(open_or_create_t,const char * name,const permissions & perm)133 inline windows_named_mutex::windows_named_mutex
134    (open_or_create_t, const char *name, const permissions &perm)
135    : m_mtx_wrapper()
136 {
137    named_mut_callbacks callbacks(m_mtx_wrapper);
138    m_named_sync.open_or_create(DoOpenOrCreate, name, perm, callbacks);
139 }
140 
windows_named_mutex(open_only_t,const char * name)141 inline windows_named_mutex::windows_named_mutex(open_only_t, const char *name)
142    : m_mtx_wrapper()
143 {
144    named_mut_callbacks callbacks(m_mtx_wrapper);
145    m_named_sync.open_or_create(DoOpen, name, permissions(), callbacks);
146 }
147 
unlock()148 inline void windows_named_mutex::unlock()
149 {
150    m_mtx_wrapper.unlock();
151 }
152 
lock()153 inline void windows_named_mutex::lock()
154 {
155    m_mtx_wrapper.lock();
156 }
157 
try_lock()158 inline bool windows_named_mutex::try_lock()
159 {
160    return m_mtx_wrapper.try_lock();
161 }
162 
timed_lock(const boost::posix_time::ptime & abs_time)163 inline bool windows_named_mutex::timed_lock(const boost::posix_time::ptime &abs_time)
164 {
165    return m_mtx_wrapper.timed_lock(abs_time);
166 }
167 
remove(const char * name)168 inline bool windows_named_mutex::remove(const char *name)
169 {
170    return windows_named_sync::remove(name);
171 }
172 
173 }  //namespace ipcdetail {
174 }  //namespace interprocess {
175 }  //namespace boost {
176 
177 #include <boost/interprocess/detail/config_end.hpp>
178 
179 #endif   //BOOST_INTERPROCESS_WINDOWS_NAMED_MUTEX_HPP