sync_utils.hpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. class sync_id
  56. {
  57. public:
  58. typedef __int64 internal_type;
  59. sync_id(const void *map_addr)
  60. : map_addr_(map_addr)
  61. { winapi::query_performance_counter(&rand_); }
  62. explicit sync_id(internal_type val, const void *map_addr)
  63. : map_addr_(map_addr)
  64. { rand_ = val; }
  65. const internal_type &internal_pod() const
  66. { return rand_; }
  67. internal_type &internal_pod()
  68. { return rand_; }
  69. const void *map_address() const
  70. { return map_addr_; }
  71. friend std::size_t hash_value(const sync_id &m)
  72. { return boost::hash_value(m.rand_); }
  73. friend bool operator==(const sync_id &l, const sync_id &r)
  74. { return l.rand_ == r.rand_ && l.map_addr_ == r.map_addr_; }
  75. private:
  76. internal_type rand_;
  77. const void * const map_addr_;
  78. };
  79. class sync_handles
  80. {
  81. public:
  82. enum type { MUTEX, SEMAPHORE };
  83. private:
  84. struct address_less
  85. {
  86. bool operator()(sync_id const * const l, sync_id const * const r) const
  87. { return l->map_address() < r->map_address(); }
  88. };
  89. typedef boost::unordered_map<sync_id, void*> umap_type;
  90. typedef boost::container::map<const sync_id*, umap_type::iterator, address_less> map_type;
  91. static const std::size_t LengthOfGlobal = sizeof("Global\\boost.ipc")-1;
  92. static const std::size_t StrSize = LengthOfGlobal + (sizeof(sync_id)*2+1);
  93. typedef char NameBuf[StrSize];
  94. void fill_name(NameBuf &name, const sync_id &id)
  95. {
  96. const char *n = "Global\\boost.ipc";
  97. std::size_t i = 0;
  98. do{
  99. name[i] = n[i];
  100. ++i;
  101. } while(n[i]);
  102. std::size_t len = sizeof(NameBuf) - LengthOfGlobal;
  103. bytes_to_str(&id.internal_pod(), sizeof(id.internal_pod()), &name[LengthOfGlobal], len);
  104. }
  105. void throw_if_error(void *hnd_val)
  106. {
  107. if(!hnd_val){
  108. error_info err(winapi::get_last_error());
  109. throw interprocess_exception(err);
  110. }
  111. }
  112. void* open_or_create_semaphore(const sync_id &id, unsigned int initial_count)
  113. {
  114. NameBuf name;
  115. fill_name(name, id);
  116. permissions unrestricted_security;
  117. unrestricted_security.set_unrestricted();
  118. winapi_semaphore_wrapper sem_wrapper;
  119. bool created;
  120. sem_wrapper.open_or_create
  121. (name, (long)initial_count, winapi_semaphore_wrapper::MaxCount, unrestricted_security, created);
  122. throw_if_error(sem_wrapper.handle());
  123. return sem_wrapper.release();
  124. }
  125. void* open_or_create_mutex(const sync_id &id)
  126. {
  127. NameBuf name;
  128. fill_name(name, id);
  129. permissions unrestricted_security;
  130. unrestricted_security.set_unrestricted();
  131. winapi_mutex_wrapper mtx_wrapper;
  132. mtx_wrapper.open_or_create(name, unrestricted_security);
  133. throw_if_error(mtx_wrapper.handle());
  134. return mtx_wrapper.release();
  135. }
  136. public:
  137. void *obtain_mutex(const sync_id &id, bool *popen_created = 0)
  138. {
  139. umap_type::value_type v(id, (void*)0);
  140. scoped_lock<spin_mutex> lock(mtx_);
  141. umap_type::iterator it = umap_.insert(v).first;
  142. void *&hnd_val = it->second;
  143. if(!hnd_val){
  144. map_[&it->first] = it;
  145. hnd_val = open_or_create_mutex(id);
  146. if(popen_created) *popen_created = true;
  147. }
  148. else if(popen_created){
  149. *popen_created = false;
  150. }
  151. return hnd_val;
  152. }
  153. void *obtain_semaphore(const sync_id &id, unsigned int initial_count, bool *popen_created = 0)
  154. {
  155. umap_type::value_type v(id, (void*)0);
  156. scoped_lock<spin_mutex> lock(mtx_);
  157. umap_type::iterator it = umap_.insert(v).first;
  158. void *&hnd_val = it->second;
  159. if(!hnd_val){
  160. map_[&it->first] = it;
  161. hnd_val = open_or_create_semaphore(id, initial_count);
  162. if(popen_created) *popen_created = true;
  163. }
  164. else if(popen_created){
  165. *popen_created = false;
  166. }
  167. return hnd_val;
  168. }
  169. void destroy_handle(const sync_id &id)
  170. {
  171. scoped_lock<spin_mutex> lock(mtx_);
  172. umap_type::iterator it = umap_.find(id);
  173. umap_type::iterator itend = umap_.end();
  174. if(it != itend){
  175. winapi::close_handle(it->second);
  176. const map_type::key_type &k = &it->first;
  177. map_.erase(k);
  178. umap_.erase(it);
  179. }
  180. }
  181. void destroy_syncs_in_range(const void *addr, std::size_t size)
  182. {
  183. const sync_id low_id(addr);
  184. const sync_id hig_id(static_cast<const char*>(addr)+size);
  185. scoped_lock<spin_mutex> lock(mtx_);
  186. map_type::iterator itlow(map_.lower_bound(&low_id)),
  187. ithig(map_.lower_bound(&hig_id));
  188. while(itlow != ithig){
  189. void * const hnd = umap_[*itlow->first];
  190. winapi::close_handle(hnd);
  191. umap_.erase(*itlow->first);
  192. itlow = map_.erase(itlow);
  193. }
  194. }
  195. private:
  196. spin_mutex mtx_;
  197. umap_type umap_;
  198. map_type map_;
  199. };
  200. } //namespace ipcdetail {
  201. } //namespace interprocess {
  202. } //namespace boost {
  203. #include <boost/interprocess/detail/config_end.hpp>
  204. #endif //BOOST_INTERPROCESS_DETAIL_SYNC_UTILS_HPP