annotate DEPENDENCIES/generic/include/boost/interprocess/sync/windows/named_sync.hpp @ 125:34e428693f5d vext

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