File indexing completed on 2025-01-18 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_WINDOWS_NAMED_SYNC_HPP
0012 #define BOOST_INTERPROCESS_WINDOWS_NAMED_SYNC_HPP
0013
0014 #ifndef BOOST_CONFIG_HPP
0015 # include <boost/config.hpp>
0016 #endif
0017 #
0018 #if defined(BOOST_HAS_PRAGMA_ONCE)
0019 # pragma once
0020 #endif
0021
0022 #include <boost/interprocess/detail/config_begin.hpp>
0023 #include <boost/interprocess/detail/workaround.hpp>
0024 #include <boost/interprocess/creation_tags.hpp>
0025 #include <boost/interprocess/permissions.hpp>
0026 #include <boost/interprocess/detail/shared_dir_helpers.hpp>
0027 #include <boost/interprocess/sync/windows/sync_utils.hpp>
0028 #include <boost/interprocess/errors.hpp>
0029 #include <boost/interprocess/exceptions.hpp>
0030 #include <string>
0031 #include <boost/assert.hpp>
0032
0033 namespace boost {
0034 namespace interprocess {
0035 namespace ipcdetail {
0036
0037 class windows_named_sync_interface
0038 {
0039 public:
0040 virtual std::size_t get_data_size() const = 0;
0041 virtual const void *buffer_with_final_data_to_file() = 0;
0042 virtual const void *buffer_with_init_data_to_file() = 0;
0043 virtual void *buffer_to_store_init_data_from_file() = 0;
0044 virtual bool open(create_enum_t creation_type, const char *id_name) = 0;
0045 virtual bool open(create_enum_t creation_type, const wchar_t *id_name) = 0;
0046 virtual void close() = 0;
0047 virtual ~windows_named_sync_interface() = 0;
0048 };
0049
0050 inline windows_named_sync_interface::~windows_named_sync_interface()
0051 {}
0052
0053 class windows_named_sync
0054 {
0055 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0056
0057
0058 windows_named_sync(const windows_named_sync &);
0059 windows_named_sync &operator=(const windows_named_sync &);
0060 #endif
0061
0062 public:
0063 windows_named_sync();
0064 template <class CharT>
0065 void open_or_create(create_enum_t creation_type, const CharT *name, const permissions &perm, windows_named_sync_interface &sync_interface);
0066 void close(windows_named_sync_interface &sync_interface);
0067
0068 static bool remove(const char *name);
0069 static bool remove(const wchar_t *name);
0070
0071 #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
0072 private:
0073 void *m_file_hnd;
0074
0075 #endif
0076 };
0077
0078 inline windows_named_sync::windows_named_sync()
0079 : m_file_hnd(winapi::invalid_handle_value)
0080 {}
0081
0082 inline void windows_named_sync::close(windows_named_sync_interface &sync_interface)
0083 {
0084 const std::size_t buflen = sync_interface.get_data_size();
0085 winapi::interprocess_overlapped overlapped;
0086 if(winapi::lock_file_ex
0087 (m_file_hnd, winapi::lockfile_exclusive_lock, 0, 1, 0, &overlapped)){
0088 if(winapi::set_file_pointer(m_file_hnd, sizeof(sync_id::internal_type), 0, winapi::file_begin)){
0089 const void *buf = sync_interface.buffer_with_final_data_to_file();
0090
0091 unsigned long written_or_read = 0;
0092 if(winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0)){
0093
0094 }
0095 }
0096 }
0097 sync_interface.close();
0098
0099 if(m_file_hnd != winapi::invalid_handle_value){
0100 winapi::close_handle(m_file_hnd);
0101 m_file_hnd = winapi::invalid_handle_value;
0102 }
0103 }
0104
0105 template <class CharT>
0106 inline void windows_named_sync::open_or_create
0107 ( create_enum_t creation_type
0108 , const CharT *name
0109 , const permissions &perm
0110 , windows_named_sync_interface &sync_interface)
0111 {
0112 std::basic_string<CharT> aux_str(name);
0113 m_file_hnd = winapi::invalid_handle_value;
0114
0115
0116 {
0117 create_shared_dir_cleaning_old_and_get_filepath(name, aux_str);
0118
0119 m_file_hnd = winapi::create_file
0120 ( aux_str.c_str()
0121 , winapi::generic_read | winapi::generic_write
0122 , creation_type == DoOpen ? winapi::open_existing :
0123 (creation_type == DoCreate ? winapi::create_new : winapi::open_always)
0124 , 0
0125 , (winapi::interprocess_security_attributes*)perm.get_permissions());
0126
0127
0128 error_info err;
0129 bool success = false;
0130 if(m_file_hnd != winapi::invalid_handle_value){
0131
0132 const std::size_t buflen = sync_interface.get_data_size();
0133 typedef __int64 unique_id_type;
0134 const std::size_t sizeof_file_info = sizeof(unique_id_type) + buflen;
0135 winapi::interprocess_overlapped overlapped;
0136 if(winapi::lock_file_ex
0137 (m_file_hnd, winapi::lockfile_exclusive_lock, 0, 1, 0, &overlapped)){
0138 __int64 filesize = 0;
0139
0140
0141 if(winapi::get_file_size(m_file_hnd, filesize)){
0142 unsigned long written_or_read = 0;
0143 unique_id_type unique_id_val;
0144 if(static_cast<std::size_t>(filesize) != sizeof_file_info){
0145 winapi::set_end_of_file(m_file_hnd);
0146 winapi::query_performance_counter(&unique_id_val);
0147 const void *buf = sync_interface.buffer_with_init_data_to_file();
0148
0149 if(winapi::write_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) &&
0150 written_or_read == sizeof(unique_id_val) &&
0151 winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0) &&
0152 written_or_read == buflen ){
0153 success = true;
0154 }
0155 winapi::get_file_size(m_file_hnd, filesize);
0156 BOOST_ASSERT(std::size_t(filesize) == sizeof_file_info);
0157 }
0158 else{
0159 void *buf = sync_interface.buffer_to_store_init_data_from_file();
0160 if(winapi::read_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) &&
0161 written_or_read == sizeof(unique_id_val) &&
0162 winapi::read_file(m_file_hnd, buf, buflen, &written_or_read, 0) &&
0163 written_or_read == buflen ){
0164 success = true;
0165 }
0166 }
0167 if(success){
0168
0169 CharT unique_id_name[sizeof(unique_id_val)*2+1];
0170 std::size_t name_suffix_length = sizeof(unique_id_name);
0171 bytes_to_str(&unique_id_val, sizeof(unique_id_val), &unique_id_name[0], name_suffix_length);
0172 success = sync_interface.open(creation_type, unique_id_name);
0173 }
0174 }
0175
0176
0177 if(!success)
0178 err = system_error_code();
0179
0180
0181 if(!winapi::unlock_file_ex(m_file_hnd, 0, 1, 0, &overlapped)){
0182 err = system_error_code();
0183 }
0184 }
0185 else{
0186
0187 err = system_error_code();
0188 }
0189 }
0190 else{
0191 err = system_error_code();
0192 }
0193
0194 if(!success){
0195 if(m_file_hnd != winapi::invalid_handle_value){
0196 winapi::close_handle(m_file_hnd);
0197 m_file_hnd = winapi::invalid_handle_value;
0198 }
0199
0200 throw interprocess_exception(err);
0201 }
0202 }
0203 }
0204
0205 inline bool windows_named_sync::remove(const char *name)
0206 {
0207 BOOST_TRY{
0208
0209 std::string semfile;
0210 ipcdetail::shared_filepath(name, semfile);
0211 return winapi::unlink_file(semfile.c_str());
0212 }
0213 BOOST_CATCH(...){
0214 return false;
0215 } BOOST_CATCH_END
0216 }
0217
0218 inline bool windows_named_sync::remove(const wchar_t *name)
0219 {
0220 BOOST_TRY{
0221
0222 std::wstring semfile;
0223 ipcdetail::shared_filepath(name, semfile);
0224 return winapi::unlink_file(semfile.c_str());
0225 }
0226 BOOST_CATCH(...){
0227 return false;
0228 } BOOST_CATCH_END
0229 }
0230
0231 }
0232 }
0233 }
0234
0235 #include <boost/interprocess/detail/config_end.hpp>
0236
0237 #endif