File indexing completed on 2025-01-18 09:38:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_INTERPROCESS_DETAIL_SYNC_UTILS_HPP
0012 #define BOOST_INTERPROCESS_DETAIL_SYNC_UTILS_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/detail/win32_api.hpp>
0025 #include <boost/interprocess/sync/spin/mutex.hpp>
0026 #include <boost/interprocess/exceptions.hpp>
0027 #include <boost/interprocess/sync/scoped_lock.hpp>
0028 #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
0029 #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
0030
0031
0032 #include <boost/interprocess/detail/config_external_begin.hpp>
0033 #include <boost/unordered/unordered_map.hpp>
0034 #include <boost/interprocess/detail/config_external_end.hpp>
0035 #include <boost/container/flat_map.hpp>
0036
0037 #include <cstddef>
0038
0039 namespace boost {
0040 namespace interprocess {
0041 namespace ipcdetail {
0042
0043 inline bool bytes_to_str(const void *mem, const std::size_t mem_length, char *out_str, std::size_t &out_length)
0044 {
0045 const std::size_t need_mem = mem_length*2+1;
0046 if(out_length < need_mem){
0047 out_length = need_mem;
0048 return false;
0049 }
0050
0051 const char Characters [] =
0052 { '0', '1', '2', '3', '4', '5', '6', '7'
0053 , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
0054
0055 std::size_t char_counter = 0;
0056 const char *buf = (const char *)mem;
0057 for(std::size_t i = 0; i != mem_length; ++i){
0058 out_str[char_counter++] = Characters[(buf[i]&0xF0)>>4];
0059 out_str[char_counter++] = Characters[(buf[i]&0x0F)];
0060 }
0061 out_str[char_counter] = 0;
0062 return true;
0063 }
0064
0065 inline bool bytes_to_str(const void *mem, const std::size_t mem_length, wchar_t *out_str, std::size_t &out_length)
0066 {
0067 const std::size_t need_mem = mem_length*2+1;
0068 if(out_length < need_mem){
0069 out_length = need_mem;
0070 return false;
0071 }
0072
0073 const wchar_t Characters [] =
0074 { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
0075 , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
0076
0077 std::size_t char_counter = 0;
0078 const char *buf = (const char *)mem;
0079 for(std::size_t i = 0; i != mem_length; ++i){
0080 out_str[char_counter++] = Characters[(buf[i]&0xF0)>>4];
0081 out_str[char_counter++] = Characters[(buf[i]&0x0F)];
0082 }
0083 out_str[char_counter] = 0;
0084 return true;
0085 }
0086
0087 class sync_id
0088 {
0089 public:
0090 typedef __int64 internal_type;
0091 sync_id()
0092 { winapi::query_performance_counter(&rand_); }
0093
0094 explicit sync_id(internal_type val)
0095 { rand_ = val; }
0096
0097 const internal_type &internal_pod() const
0098 { return rand_; }
0099
0100 internal_type &internal_pod()
0101 { return rand_; }
0102
0103 friend std::size_t hash_value(const sync_id &m)
0104 { return boost::hash_value(m.rand_); }
0105
0106 friend bool operator==(const sync_id &l, const sync_id &r)
0107 { return l.rand_ == r.rand_; }
0108
0109 private:
0110 internal_type rand_;
0111 };
0112
0113 class sync_handles
0114 {
0115 public:
0116 enum type { MUTEX, SEMAPHORE };
0117
0118 private:
0119
0120
0121 typedef boost::unordered_map<sync_id, void*> umap_type;
0122
0123 typedef boost::container::flat_map<const void*, umap_type::iterator> map_type;
0124 static const std::size_t LengthOfGlobal = sizeof("Global\\boost.ipc")-1;
0125 static const std::size_t StrSize = LengthOfGlobal + (sizeof(sync_id)*2+1);
0126 typedef char NameBuf[StrSize];
0127
0128 void fill_name(NameBuf &name, const sync_id &id)
0129 {
0130 const char *n = "Global\\boost.ipc";
0131 std::size_t i = 0;
0132 do{
0133 name[i] = n[i];
0134 ++i;
0135 } while(n[i]);
0136 std::size_t len = sizeof(NameBuf) - LengthOfGlobal;
0137 bytes_to_str(&id.internal_pod(), sizeof(id.internal_pod()), &name[LengthOfGlobal], len);
0138 }
0139
0140 void throw_if_error(void *hnd_val)
0141 {
0142 if(!hnd_val){
0143 error_info err(static_cast<int>(winapi::get_last_error()));
0144 throw interprocess_exception(err);
0145 }
0146 }
0147
0148 void* open_or_create_semaphore(const sync_id &id, unsigned int initial_count)
0149 {
0150 NameBuf name;
0151 fill_name(name, id);
0152 permissions unrestricted_security;
0153 unrestricted_security.set_unrestricted();
0154 winapi_semaphore_wrapper sem_wrapper;
0155 bool created;
0156 sem_wrapper.open_or_create
0157 (name, (long)initial_count, winapi_semaphore_wrapper::MaxCount, unrestricted_security, created);
0158 throw_if_error(sem_wrapper.handle());
0159 return sem_wrapper.release();
0160 }
0161
0162 void* open_or_create_mutex(const sync_id &id)
0163 {
0164 NameBuf name;
0165 fill_name(name, id);
0166 permissions unrestricted_security;
0167 unrestricted_security.set_unrestricted();
0168 winapi_mutex_wrapper mtx_wrapper;
0169 mtx_wrapper.open_or_create(name, unrestricted_security);
0170 throw_if_error(mtx_wrapper.handle());
0171 return mtx_wrapper.release();
0172 }
0173
0174 public:
0175 sync_handles()
0176 : num_handles_()
0177 {}
0178
0179 ~sync_handles()
0180 {
0181 BOOST_ASSERT(num_handles_ == 0);
0182 }
0183
0184 void *obtain_mutex(const sync_id &id, const void *mapping_address, bool *popen_created = 0)
0185 {
0186 umap_type::value_type v(id, (void*)0);
0187 scoped_lock<spin_mutex> lock(mtx_);
0188 umap_type::iterator it = umap_.insert(v).first;
0189 void *&hnd_val = it->second;
0190 if(!hnd_val){
0191 BOOST_ASSERT(map_.find(mapping_address) == map_.end());
0192 map_[mapping_address] = it;
0193 hnd_val = open_or_create_mutex(id);
0194 if(popen_created) *popen_created = true;
0195 ++num_handles_;
0196 }
0197 else if(popen_created){
0198 BOOST_ASSERT(map_.find(mapping_address) != map_.end());
0199 *popen_created = false;
0200 }
0201
0202 return hnd_val;
0203 }
0204
0205 void *obtain_semaphore(const sync_id &id, const void *mapping_address, unsigned int initial_count, bool *popen_created = 0)
0206 {
0207 umap_type::value_type v(id, (void*)0);
0208 scoped_lock<spin_mutex> lock(mtx_);
0209 umap_type::iterator it = umap_.insert(v).first;
0210 void *&hnd_val = it->second;
0211 if(!hnd_val){
0212 BOOST_ASSERT(map_.find(mapping_address) == map_.end());
0213 map_[mapping_address] = it;
0214 hnd_val = open_or_create_semaphore(id, initial_count);
0215 if(popen_created) *popen_created = true;
0216 ++num_handles_;
0217 }
0218 else if(popen_created){
0219 BOOST_ASSERT(map_.find(mapping_address) != map_.end());
0220 *popen_created = false;
0221 }
0222 return hnd_val;
0223 }
0224
0225 void destroy_handle(const sync_id &id, const void *mapping_address)
0226 {
0227 scoped_lock<spin_mutex> lock(mtx_);
0228 umap_type::iterator it = umap_.find(id);
0229 umap_type::iterator itend = umap_.end();
0230
0231 if(it != itend){
0232 winapi::close_handle(it->second);
0233 --num_handles_;
0234 std::size_t i = map_.erase(mapping_address);
0235 (void)i;
0236 BOOST_ASSERT(i == 1);
0237 umap_.erase(it);
0238 }
0239 }
0240
0241 void destroy_syncs_in_range(const void *addr, std::size_t size)
0242 {
0243 const void *low_id(addr);
0244 const void *hig_id(static_cast<const char*>(addr)+size);
0245 scoped_lock<spin_mutex> lock(mtx_);
0246 map_type::iterator itlow(map_.lower_bound(low_id)),
0247 ithig(map_.lower_bound(hig_id)),
0248 it(itlow);
0249 for (; it != ithig; ++it){
0250 umap_type::iterator uit = it->second;
0251 void * const hnd = uit->second;
0252 umap_.erase(uit);
0253 int ret = winapi::close_handle(hnd);
0254 --num_handles_;
0255 BOOST_ASSERT(ret != 0); (void)ret;
0256 }
0257
0258 map_.erase(itlow, ithig);
0259 }
0260
0261 private:
0262 spin_mutex mtx_;
0263 umap_type umap_;
0264 map_type map_;
0265 std::size_t num_handles_;
0266 };
0267
0268
0269 }
0270 }
0271 }
0272
0273 #include <boost/interprocess/detail/config_end.hpp>
0274
0275 #endif