sync_utils.hpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2005-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_DETAIL_SYNC_UTILS_HPP
  11. #define BOOST_INTERPROCESS_DETAIL_SYNC_UTILS_HPP
  12. #ifndef BOOST_CONFIG_HPP
  13. # include <boost/config.hpp>
  14. #endif
  15. #
  16. #if defined(BOOST_HAS_PRAGMA_ONCE)
  17. # pragma once
  18. #endif
  19. #include <boost/interprocess/detail/config_begin.hpp>
  20. #include <boost/interprocess/detail/workaround.hpp>
  21. #include <boost/interprocess/detail/win32_api.hpp>
  22. #include <boost/interprocess/sync/spin/mutex.hpp>
  23. #include <boost/interprocess/exceptions.hpp>
  24. #include <boost/interprocess/sync/scoped_lock.hpp>
  25. #include <boost/interprocess/sync/windows/winapi_semaphore_wrapper.hpp>
  26. #include <boost/interprocess/sync/windows/winapi_mutex_wrapper.hpp>
  27. //Shield against external warnings
  28. #include <boost/interprocess/detail/config_external_begin.hpp>
  29. #include <boost/unordered/unordered_map.hpp>
  30. #include <boost/interprocess/detail/config_external_end.hpp>
  31. #include <boost/container/map.hpp>
  32. #include <cstddef>
  33. namespace boost {
  34. namespace interprocess {
  35. namespace ipcdetail {
  36. inline bool bytes_to_str(const void *mem, const std::size_t mem_length, char *out_str, std::size_t &out_length)
  37. {
  38. const std::size_t need_mem = mem_length*2+1;
  39. if(out_length < need_mem){
  40. out_length = need_mem;
  41. return false;
  42. }
  43. const char Characters [] =
  44. { '0', '1', '2', '3', '4', '5', '6', '7'
  45. , '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
  46. std::size_t char_counter = 0;
  47. const char *buf = (const char *)mem;
  48. for(std::size_t i = 0; i != mem_length; ++i){
  49. out_str[char_counter++] = Characters[(buf[i]&0xF0)>>4];
  50. out_str[char_counter++] = Characters[(buf[i]&0x0F)];
  51. }
  52. out_str[char_counter] = 0;
  53. return true;
  54. }
  55. inline bool bytes_to_str(const void *mem, const std::size_t mem_length, wchar_t *out_str, std::size_t &out_length)
  56. {
  57. const std::size_t need_mem = mem_length*2+1;
  58. if(out_length < need_mem){
  59. out_length = need_mem;
  60. return false;
  61. }
  62. const wchar_t Characters [] =
  63. { L'0', L'1', L'2', L'3', L'4', L'5', L'6', L'7'
  64. , L'8', L'9', L'A', L'B', L'C', L'D', L'E', L'F' };
  65. std::size_t char_counter = 0;
  66. const char *buf = (const char *)mem;
  67. for(std::size_t i = 0; i != mem_length; ++i){
  68. out_str[char_counter++] = Characters[(buf[i]&0xF0)>>4];
  69. out_str[char_counter++] = Characters[(buf[i]&0x0F)];
  70. }
  71. out_str[char_counter] = 0;
  72. return true;
  73. }
  74. class sync_id
  75. {
  76. public:
  77. typedef __int64 internal_type;
  78. sync_id(const void *map_addr)
  79. : map_addr_(map_addr)
  80. { winapi::query_performance_counter(&rand_); }
  81. explicit sync_id(internal_type val, const void *map_addr)
  82. : map_addr_(map_addr)
  83. { rand_ = val; }
  84. const internal_type &internal_pod() const
  85. { return rand_; }
  86. internal_type &internal_pod()
  87. { return rand_; }
  88. const void *map_address() const
  89. { return map_addr_; }
  90. friend std::size_t hash_value(const sync_id &m)
  91. { return boost::hash_value(m.rand_); }
  92. friend bool operator==(const sync_id &l, const sync_id &r)
  93. { return l.rand_ == r.rand_ && l.map_addr_ == r.map_addr_; }
  94. private:
  95. internal_type rand_;
  96. const void * const map_addr_;
  97. };
  98. class sync_handles
  99. {
  100. public:
  101. enum type { MUTEX, SEMAPHORE };
  102. private:
  103. struct address_less
  104. {
  105. bool operator()(sync_id const * const l, sync_id const * const r) const
  106. { return l->map_address() < r->map_address(); }
  107. };
  108. typedef boost::unordered_map<sync_id, void*> umap_type;
  109. typedef boost::container::map<const sync_id*, umap_type::iterator, address_less> map_type;
  110. static const std::size_t LengthOfGlobal = sizeof("Global\\boost.ipc")-1;
  111. static const std::size_t StrSize = LengthOfGlobal + (sizeof(sync_id)*2+1);
  112. typedef char NameBuf[StrSize];
  113. void fill_name(NameBuf &name, const sync_id &id)
  114. {
  115. const char *n = "Global\\boost.ipc";
  116. std::size_t i = 0;
  117. do{
  118. name[i] = n[i];
  119. ++i;
  120. } while(n[i]);
  121. std::size_t len = sizeof(NameBuf) - LengthOfGlobal;
  122. bytes_to_str(&id.internal_pod(), sizeof(id.internal_pod()), &name[LengthOfGlobal], len);
  123. }
  124. void throw_if_error(void *hnd_val)
  125. {
  126. if(!hnd_val){
  127. error_info err(winapi::get_last_error());
  128. throw interprocess_exception(err);
  129. }
  130. }
  131. void* open_or_create_semaphore(const sync_id &id, unsigned int initial_count)
  132. {
  133. NameBuf name;
  134. fill_name(name, id);
  135. permissions unrestricted_security;
  136. unrestricted_security.set_unrestricted();
  137. winapi_semaphore_wrapper sem_wrapper;
  138. bool created;
  139. sem_wrapper.open_or_create
  140. (name, (long)initial_count, winapi_semaphore_wrapper::MaxCount, unrestricted_security, created);
  141. throw_if_error(sem_wrapper.handle());
  142. return sem_wrapper.release();
  143. }
  144. void* open_or_create_mutex(const sync_id &id)
  145. {
  146. NameBuf name;
  147. fill_name(name, id);
  148. permissions unrestricted_security;
  149. unrestricted_security.set_unrestricted();
  150. winapi_mutex_wrapper mtx_wrapper;
  151. mtx_wrapper.open_or_create(name, unrestricted_security);
  152. throw_if_error(mtx_wrapper.handle());
  153. return mtx_wrapper.release();
  154. }
  155. public:
  156. void *obtain_mutex(const sync_id &id, bool *popen_created = 0)
  157. {
  158. umap_type::value_type v(id, (void*)0);
  159. scoped_lock<spin_mutex> lock(mtx_);
  160. umap_type::iterator it = umap_.insert(v).first;
  161. void *&hnd_val = it->second;
  162. if(!hnd_val){
  163. map_[&it->first] = it;
  164. hnd_val = open_or_create_mutex(id);
  165. if(popen_created) *popen_created = true;
  166. }
  167. else if(popen_created){
  168. *popen_created = false;
  169. }
  170. return hnd_val;
  171. }
  172. void *obtain_semaphore(const sync_id &id, unsigned int initial_count, bool *popen_created = 0)
  173. {
  174. umap_type::value_type v(id, (void*)0);
  175. scoped_lock<spin_mutex> lock(mtx_);
  176. umap_type::iterator it = umap_.insert(v).first;
  177. void *&hnd_val = it->second;
  178. if(!hnd_val){
  179. map_[&it->first] = it;
  180. hnd_val = open_or_create_semaphore(id, initial_count);
  181. if(popen_created) *popen_created = true;
  182. }
  183. else if(popen_created){
  184. *popen_created = false;
  185. }
  186. return hnd_val;
  187. }
  188. void destroy_handle(const sync_id &id)
  189. {
  190. scoped_lock<spin_mutex> lock(mtx_);
  191. umap_type::iterator it = umap_.find(id);
  192. umap_type::iterator itend = umap_.end();
  193. if(it != itend){
  194. winapi::close_handle(it->second);
  195. const map_type::key_type &k = &it->first;
  196. map_.erase(k);
  197. umap_.erase(it);
  198. }
  199. }
  200. void destroy_syncs_in_range(const void *addr, std::size_t size)
  201. {
  202. const sync_id low_id(addr);
  203. const sync_id hig_id(static_cast<const char*>(addr)+size);
  204. scoped_lock<spin_mutex> lock(mtx_);
  205. map_type::iterator itlow(map_.lower_bound(&low_id)),
  206. ithig(map_.lower_bound(&hig_id));
  207. while(itlow != ithig){
  208. void * const hnd = umap_[*itlow->first];
  209. winapi::close_handle(hnd);
  210. umap_.erase(*itlow->first);
  211. itlow = map_.erase(itlow);
  212. }
  213. }
  214. private:
  215. spin_mutex mtx_;
  216. umap_type umap_;
  217. map_type map_;
  218. };
  219. } //namespace ipcdetail {
  220. } //namespace interprocess {
  221. } //namespace boost {
  222. #include <boost/interprocess/detail/config_end.hpp>
  223. #endif //BOOST_INTERPROCESS_DETAIL_SYNC_UTILS_HPP