]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/interprocess/include/boost/interprocess/sync/windows/named_sync.hpp
bump version to 12.2.2-pve1
[ceph.git] / ceph / src / boost / libs / interprocess / include / boost / interprocess / sync / windows / named_sync.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_WINDOWS_NAMED_SYNC_HPP
12 #define BOOST_INTERPROCESS_WINDOWS_NAMED_SYNC_HPP
13
14 #ifndef BOOST_CONFIG_HPP
15 # include <boost/config.hpp>
16 #endif
17 #
18 #if defined(BOOST_HAS_PRAGMA_ONCE)
19 # pragma once
20 #endif
21
22 #include <boost/interprocess/detail/config_begin.hpp>
23 #include <boost/interprocess/detail/workaround.hpp>
24 #include <boost/interprocess/creation_tags.hpp>
25 #include <boost/interprocess/permissions.hpp>
26 #include <boost/interprocess/detail/shared_dir_helpers.hpp>
27 #include <boost/interprocess/sync/windows/sync_utils.hpp>
28 #include <boost/interprocess/errors.hpp>
29 #include <boost/interprocess/exceptions.hpp>
30 #include <string>
31 #include <boost/assert.hpp>
32
33 namespace boost {
34 namespace interprocess {
35 namespace ipcdetail {
36
37 class windows_named_sync_interface
38 {
39 public:
40 virtual std::size_t get_data_size() const = 0;
41 virtual const void *buffer_with_final_data_to_file() = 0;
42 virtual const void *buffer_with_init_data_to_file() = 0;
43 virtual void *buffer_to_store_init_data_from_file() = 0;
44 virtual bool open(create_enum_t creation_type, const char *id_name) = 0;
45 virtual void close() = 0;
46 virtual ~windows_named_sync_interface() = 0;
47 };
48
49 inline windows_named_sync_interface::~windows_named_sync_interface()
50 {}
51
52 class windows_named_sync
53 {
54 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
55
56 //Non-copyable
57 windows_named_sync(const windows_named_sync &);
58 windows_named_sync &operator=(const windows_named_sync &);
59 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
60
61 public:
62 windows_named_sync();
63 void open_or_create(create_enum_t creation_type, const char *name, const permissions &perm, windows_named_sync_interface &sync_interface);
64 void close(windows_named_sync_interface &sync_interface);
65
66 static bool remove(const char *name);
67
68 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
69 private:
70 void *m_file_hnd;
71
72 #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
73 };
74
75 inline windows_named_sync::windows_named_sync()
76 : m_file_hnd(winapi::invalid_handle_value)
77 {}
78
79 inline void windows_named_sync::close(windows_named_sync_interface &sync_interface)
80 {
81 const std::size_t buflen = sync_interface.get_data_size();
82 const std::size_t sizeof_file_info = sizeof(sync_id::internal_type) + buflen;
83 winapi::interprocess_overlapped overlapped;
84 if(winapi::lock_file_ex
85 (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){
86 if(winapi::set_file_pointer_ex(m_file_hnd, sizeof(sync_id::internal_type), 0, winapi::file_begin)){
87 const void *buf = sync_interface.buffer_with_final_data_to_file();
88
89 unsigned long written_or_read = 0;
90 if(winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0)){
91 //...
92 }
93 }
94 }
95 sync_interface.close();
96 if(m_file_hnd != winapi::invalid_handle_value){
97 winapi::close_handle(m_file_hnd);
98 m_file_hnd = winapi::invalid_handle_value;
99 }
100 }
101
102 inline void windows_named_sync::open_or_create
103 ( create_enum_t creation_type
104 , const char *name
105 , const permissions &perm
106 , windows_named_sync_interface &sync_interface)
107 {
108 std::string aux_str(name);
109 m_file_hnd = winapi::invalid_handle_value;
110 //Use a file to emulate POSIX lifetime semantics. After this logic
111 //we'll obtain the ID of the native handle to open in aux_str
112 {
113 create_shared_dir_cleaning_old_and_get_filepath(name, aux_str);
114 //Create a file with required permissions.
115 m_file_hnd = winapi::create_file
116 ( aux_str.c_str()
117 , winapi::generic_read | winapi::generic_write
118 , creation_type == DoOpen ? winapi::open_existing :
119 (creation_type == DoCreate ? winapi::create_new : winapi::open_always)
120 , 0
121 , (winapi::interprocess_security_attributes*)perm.get_permissions());
122
123 //Obtain OS error in case something has failed
124 error_info err;
125 bool success = false;
126 if(m_file_hnd != winapi::invalid_handle_value){
127 //Now lock the file
128 const std::size_t buflen = sync_interface.get_data_size();
129 typedef __int64 unique_id_type;
130 const std::size_t sizeof_file_info = sizeof(unique_id_type) + buflen;
131 winapi::interprocess_overlapped overlapped;
132 if(winapi::lock_file_ex
133 (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){
134 __int64 filesize = 0;
135 //Obtain the unique id to open the native semaphore.
136 //If file size was created
137 if(winapi::get_file_size(m_file_hnd, filesize)){
138 unsigned long written_or_read = 0;
139 unique_id_type unique_id_val;
140 if(static_cast<std::size_t>(filesize) != sizeof_file_info){
141 winapi::set_end_of_file(m_file_hnd);
142 winapi::query_performance_counter(&unique_id_val);
143 const void *buf = sync_interface.buffer_with_init_data_to_file();
144 //Write unique ID in file. This ID will be used to calculate the semaphore name
145 if(winapi::write_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) &&
146 written_or_read == sizeof(unique_id_val) &&
147 winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0) &&
148 written_or_read == buflen ){
149 success = true;
150 }
151 winapi::get_file_size(m_file_hnd, filesize);
152 BOOST_ASSERT(std::size_t(filesize) == sizeof_file_info);
153 }
154 else{
155 void *buf = sync_interface.buffer_to_store_init_data_from_file();
156 if(winapi::read_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) &&
157 written_or_read == sizeof(unique_id_val) &&
158 winapi::read_file(m_file_hnd, buf, buflen, &written_or_read, 0) &&
159 written_or_read == buflen ){
160 success = true;
161 }
162 }
163 if(success){
164 //Now create a global semaphore name based on the unique id
165 char unique_id_name[sizeof(unique_id_val)*2+1];
166 std::size_t name_suffix_length = sizeof(unique_id_name);
167 bytes_to_str(&unique_id_val, sizeof(unique_id_val), &unique_id_name[0], name_suffix_length);
168 success = sync_interface.open(creation_type, unique_id_name);
169 }
170 }
171
172 //Obtain OS error in case something has failed
173 err = system_error_code();
174
175 //If this fails we have no possible rollback so don't check the return
176 if(!winapi::unlock_file_ex(m_file_hnd, 0, sizeof_file_info, 0, &overlapped)){
177 err = system_error_code();
178 }
179 }
180 else{
181 //Obtain OS error in case something has failed
182 err = system_error_code();
183 }
184 }
185 else{
186 err = system_error_code();
187 }
188
189 if(!success){
190 if(m_file_hnd != winapi::invalid_handle_value){
191 winapi::close_handle(m_file_hnd);
192 m_file_hnd = winapi::invalid_handle_value;
193 }
194 //Throw as something went wrong
195 throw interprocess_exception(err);
196 }
197 }
198 }
199
200 inline bool windows_named_sync::remove(const char *name)
201 {
202 try{
203 //Make sure a temporary path is created for shared memory
204 std::string semfile;
205 ipcdetail::shared_filepath(name, semfile);
206 return winapi::unlink_file(semfile.c_str());
207 }
208 catch(...){
209 return false;
210 }
211 }
212
213 } //namespace ipcdetail {
214 } //namespace interprocess {
215 } //namespace boost {
216
217 #include <boost/interprocess/detail/config_end.hpp>
218
219 #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_SYNC_HPP