sync_utils.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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/container/map.hpp>
  30. #include <boost/interprocess/detail/config_external_end.hpp>
  31. #include <boost/container/flat_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()
  79. { winapi::query_performance_counter(&rand_); }
  80. explicit sync_id(internal_type val)
  81. { rand_ = val; }
  82. const internal_type &internal_pod() const
  83. { return rand_; }
  84. internal_type &internal_pod()
  85. { return rand_; }
  86. friend std::size_t hash_value(const sync_id &m)
  87. { return static_cast<std::size_t>(m.rand_); }
  88. friend bool operator==(const sync_id &l, const sync_id &r)
  89. { return l.rand_ == r.rand_; }
  90. friend bool operator<(const sync_id &l, const sync_id &r)
  91. { return l.rand_ < r.rand_; }
  92. private:
  93. internal_type rand_;
  94. };
  95. class sync_handles
  96. {
  97. public:
  98. enum type { MUTEX, SEMAPHORE };
  99. private:
  100. //key: id -> mapped: HANDLE. Hash map to allow efficient sync operations
  101. typedef boost::container::flat_map<sync_id, void*> id_map_type;
  102. //key: ordered address of the sync type -> iterator from id_map_type. Ordered map to allow closing handles when unmapping
  103. typedef boost::container::flat_map<const void*, id_map_type::iterator> addr_map_type;
  104. static const std::size_t LengthOfGlobal = sizeof("Global\\boost.ipc")-1;
  105. static const std::size_t StrSize = LengthOfGlobal + (sizeof(sync_id)*2+1);
  106. typedef char NameBuf[StrSize];
  107. void fill_name(NameBuf &name, const sync_id &id)
  108. {
  109. const char *n = "Global\\boost.ipc";
  110. std::size_t i = 0;
  111. do{
  112. name[i] = n[i];
  113. ++i;
  114. } while(n[i]);
  115. std::size_t len = sizeof(NameBuf) - LengthOfGlobal;
  116. bytes_to_str(&id.internal_pod(), sizeof(id.internal_pod()), &name[LengthOfGlobal], len);
  117. }
  118. void throw_if_error(void *hnd_val)
  119. {
  120. if(!hnd_val){
  121. error_info err(static_cast<int>(winapi::get_last_error()));
  122. throw interprocess_exception(err);
  123. }
  124. }
  125. void* open_or_create_semaphore(const sync_id &id, unsigned int initial_count)
  126. {
  127. NameBuf name;
  128. fill_name(name, id);
  129. permissions unrestricted_security;
  130. unrestricted_security.set_unrestricted();
  131. winapi_semaphore_wrapper sem_wrapper;
  132. bool created;
  133. sem_wrapper.open_or_create
  134. (name, (long)initial_count, winapi_semaphore_wrapper::MaxCount, unrestricted_security, created);
  135. throw_if_error(sem_wrapper.handle());
  136. return sem_wrapper.release();
  137. }
  138. void* open_or_create_mutex(const sync_id &id)
  139. {
  140. NameBuf name;
  141. fill_name(name, id);
  142. permissions unrestricted_security;
  143. unrestricted_security.set_unrestricted();
  144. winapi_mutex_wrapper mtx_wrapper;
  145. mtx_wrapper.open_or_create(name, unrestricted_security);
  146. throw_if_error(mtx_wrapper.handle());
  147. return mtx_wrapper.release();
  148. }
  149. public:
  150. sync_handles()
  151. : num_handles_()
  152. {}
  153. ~sync_handles()
  154. {
  155. BOOST_ASSERT(num_handles_ == 0); //Sanity check that handle we don't leak handles
  156. }
  157. void *obtain_mutex(const sync_id &id, const void *mapping_address, bool *popen_created = 0)
  158. {
  159. id_map_type::value_type v(id, (void*)0);
  160. scoped_lock<spin_mutex> lock(mtx_);
  161. id_map_type::iterator it = umap_.insert(v).first;
  162. void *&hnd_val = it->second;
  163. if(!hnd_val){
  164. BOOST_ASSERT(map_.find(mapping_address) == map_.end());
  165. map_[mapping_address] = it;
  166. hnd_val = open_or_create_mutex(id);
  167. if(popen_created) *popen_created = true;
  168. ++num_handles_;
  169. }
  170. else if(popen_created){
  171. BOOST_ASSERT(map_.find(mapping_address) != map_.end());
  172. *popen_created = false;
  173. }
  174. return hnd_val;
  175. }
  176. void *obtain_semaphore(const sync_id &id, const void *mapping_address, unsigned int initial_count, bool *popen_created = 0)
  177. {
  178. id_map_type::value_type v(id, (void*)0);
  179. scoped_lock<spin_mutex> lock(mtx_);
  180. id_map_type::iterator it = umap_.insert(v).first;
  181. void *&hnd_val = it->second;
  182. if(!hnd_val){
  183. BOOST_ASSERT(map_.find(mapping_address) == map_.end());
  184. map_[mapping_address] = it;
  185. hnd_val = open_or_create_semaphore(id, initial_count);
  186. if(popen_created) *popen_created = true;
  187. ++num_handles_;
  188. }
  189. else if(popen_created){
  190. BOOST_ASSERT(map_.find(mapping_address) != map_.end());
  191. *popen_created = false;
  192. }
  193. return hnd_val;
  194. }
  195. void destroy_handle(const sync_id &id, const void *mapping_address)
  196. {
  197. scoped_lock<spin_mutex> lock(mtx_);
  198. id_map_type::iterator it = umap_.find(id);
  199. id_map_type::iterator itend = umap_.end();
  200. if(it != itend){
  201. winapi::close_handle(it->second);
  202. --num_handles_;
  203. std::size_t i = map_.erase(mapping_address);
  204. (void)i;
  205. BOOST_ASSERT(i == 1); //The entry should be there
  206. umap_.erase(it);
  207. }
  208. }
  209. void destroy_syncs_in_range(const void *addr, std::size_t size)
  210. {
  211. const void *low_id(addr);
  212. const void *hig_id(static_cast<const char*>(addr)+size);
  213. scoped_lock<spin_mutex> lock(mtx_);
  214. addr_map_type::iterator itlow(map_.lower_bound(low_id)),
  215. ithig(map_.lower_bound(hig_id)),
  216. it(itlow);
  217. for (; it != ithig; ++it){
  218. id_map_type::iterator uit = it->second;
  219. void * const hnd = uit->second;
  220. umap_.erase(uit);
  221. int ret = winapi::close_handle(hnd);
  222. --num_handles_;
  223. BOOST_ASSERT(ret != 0); (void)ret; //Sanity check that handle was ok
  224. }
  225. map_.erase(itlow, ithig);
  226. }
  227. private:
  228. spin_mutex mtx_;
  229. id_map_type umap_;
  230. addr_map_type map_;
  231. std::size_t num_handles_;
  232. };
  233. } //namespace ipcdetail {
  234. } //namespace interprocess {
  235. } //namespace boost {
  236. #include <boost/interprocess/detail/config_end.hpp>
  237. #endif //BOOST_INTERPROCESS_DETAIL_SYNC_UTILS_HPP