sync_utils.hpp 8.5 KB

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