named_sync.hpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2011-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_WINDOWS_NAMED_SYNC_HPP
  11. #define BOOST_INTERPROCESS_WINDOWS_NAMED_SYNC_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/creation_tags.hpp>
  22. #include <boost/interprocess/permissions.hpp>
  23. #include <boost/interprocess/detail/shared_dir_helpers.hpp>
  24. #include <boost/interprocess/sync/windows/sync_utils.hpp>
  25. #include <boost/interprocess/errors.hpp>
  26. #include <boost/interprocess/exceptions.hpp>
  27. #include <string>
  28. #include <boost/assert.hpp>
  29. namespace boost {
  30. namespace interprocess {
  31. namespace ipcdetail {
  32. class windows_named_sync_interface
  33. {
  34. public:
  35. virtual std::size_t get_data_size() const = 0;
  36. virtual const void *buffer_with_final_data_to_file() = 0;
  37. virtual const void *buffer_with_init_data_to_file() = 0;
  38. virtual void *buffer_to_store_init_data_from_file() = 0;
  39. virtual bool open(create_enum_t creation_type, const char *id_name) = 0;
  40. virtual bool open(create_enum_t creation_type, const wchar_t *id_name) = 0;
  41. virtual void close() = 0;
  42. virtual ~windows_named_sync_interface() = 0;
  43. };
  44. inline windows_named_sync_interface::~windows_named_sync_interface()
  45. {}
  46. class windows_named_sync
  47. {
  48. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  49. //Non-copyable
  50. windows_named_sync(const windows_named_sync &);
  51. windows_named_sync &operator=(const windows_named_sync &);
  52. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  53. public:
  54. windows_named_sync();
  55. template <class CharT>
  56. void open_or_create(create_enum_t creation_type, const CharT *name, const permissions &perm, windows_named_sync_interface &sync_interface);
  57. void close(windows_named_sync_interface &sync_interface);
  58. static bool remove(const char *name);
  59. static bool remove(const wchar_t *name);
  60. #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
  61. private:
  62. void *m_file_hnd;
  63. #endif //#ifndef BOOST_INTERPROCESS_DOXYGEN_INVOKED
  64. };
  65. inline windows_named_sync::windows_named_sync()
  66. : m_file_hnd(winapi::invalid_handle_value)
  67. {}
  68. inline void windows_named_sync::close(windows_named_sync_interface &sync_interface)
  69. {
  70. const std::size_t buflen = sync_interface.get_data_size();
  71. const std::size_t sizeof_file_info = sizeof(sync_id::internal_type) + buflen;
  72. winapi::interprocess_overlapped overlapped;
  73. if(winapi::lock_file_ex
  74. (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){
  75. if(winapi::set_file_pointer(m_file_hnd, sizeof(sync_id::internal_type), 0, winapi::file_begin)){
  76. const void *buf = sync_interface.buffer_with_final_data_to_file();
  77. unsigned long written_or_read = 0;
  78. if(winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0)){
  79. //...
  80. }
  81. }
  82. }
  83. sync_interface.close();
  84. if(m_file_hnd != winapi::invalid_handle_value){
  85. winapi::close_handle(m_file_hnd);
  86. m_file_hnd = winapi::invalid_handle_value;
  87. }
  88. }
  89. template <class CharT>
  90. inline void windows_named_sync::open_or_create
  91. ( create_enum_t creation_type
  92. , const CharT *name
  93. , const permissions &perm
  94. , windows_named_sync_interface &sync_interface)
  95. {
  96. std::basic_string<CharT> aux_str(name);
  97. m_file_hnd = winapi::invalid_handle_value;
  98. //Use a file to emulate POSIX lifetime semantics. After this logic
  99. //we'll obtain the ID of the native handle to open in aux_str
  100. {
  101. create_shared_dir_cleaning_old_and_get_filepath(name, aux_str);
  102. //Create a file with required permissions.
  103. m_file_hnd = winapi::create_file
  104. ( aux_str.c_str()
  105. , winapi::generic_read | winapi::generic_write
  106. , creation_type == DoOpen ? winapi::open_existing :
  107. (creation_type == DoCreate ? winapi::create_new : winapi::open_always)
  108. , 0
  109. , (winapi::interprocess_security_attributes*)perm.get_permissions());
  110. //Obtain OS error in case something has failed
  111. error_info err;
  112. bool success = false;
  113. if(m_file_hnd != winapi::invalid_handle_value){
  114. //Now lock the file
  115. const std::size_t buflen = sync_interface.get_data_size();
  116. typedef __int64 unique_id_type;
  117. const std::size_t sizeof_file_info = sizeof(unique_id_type) + buflen;
  118. winapi::interprocess_overlapped overlapped;
  119. if(winapi::lock_file_ex
  120. (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){
  121. __int64 filesize = 0;
  122. //Obtain the unique id to open the native semaphore.
  123. //If file size was created
  124. if(winapi::get_file_size(m_file_hnd, filesize)){
  125. unsigned long written_or_read = 0;
  126. unique_id_type unique_id_val;
  127. if(static_cast<std::size_t>(filesize) != sizeof_file_info){
  128. winapi::set_end_of_file(m_file_hnd);
  129. winapi::query_performance_counter(&unique_id_val);
  130. const void *buf = sync_interface.buffer_with_init_data_to_file();
  131. //Write unique ID in file. This ID will be used to calculate the semaphore name
  132. if(winapi::write_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) &&
  133. written_or_read == sizeof(unique_id_val) &&
  134. winapi::write_file(m_file_hnd, buf, buflen, &written_or_read, 0) &&
  135. written_or_read == buflen ){
  136. success = true;
  137. }
  138. winapi::get_file_size(m_file_hnd, filesize);
  139. BOOST_ASSERT(std::size_t(filesize) == sizeof_file_info);
  140. }
  141. else{
  142. void *buf = sync_interface.buffer_to_store_init_data_from_file();
  143. if(winapi::read_file(m_file_hnd, &unique_id_val, sizeof(unique_id_val), &written_or_read, 0) &&
  144. written_or_read == sizeof(unique_id_val) &&
  145. winapi::read_file(m_file_hnd, buf, buflen, &written_or_read, 0) &&
  146. written_or_read == buflen ){
  147. success = true;
  148. }
  149. }
  150. if(success){
  151. //Now create a global semaphore name based on the unique id
  152. CharT unique_id_name[sizeof(unique_id_val)*2+1];
  153. std::size_t name_suffix_length = sizeof(unique_id_name);
  154. bytes_to_str(&unique_id_val, sizeof(unique_id_val), &unique_id_name[0], name_suffix_length);
  155. success = sync_interface.open(creation_type, unique_id_name);
  156. }
  157. }
  158. //Obtain OS error in case something has failed
  159. err = system_error_code();
  160. //If this fails we have no possible rollback so don't check the return
  161. if(!winapi::unlock_file_ex(m_file_hnd, 0, sizeof_file_info, 0, &overlapped)){
  162. err = system_error_code();
  163. }
  164. }
  165. else{
  166. //Obtain OS error in case something has failed
  167. err = system_error_code();
  168. }
  169. }
  170. else{
  171. err = system_error_code();
  172. }
  173. if(!success){
  174. if(m_file_hnd != winapi::invalid_handle_value){
  175. winapi::close_handle(m_file_hnd);
  176. m_file_hnd = winapi::invalid_handle_value;
  177. }
  178. //Throw as something went wrong
  179. throw interprocess_exception(err);
  180. }
  181. }
  182. }
  183. inline bool windows_named_sync::remove(const char *name)
  184. {
  185. try{
  186. //Make sure a temporary path is created for shared memory
  187. std::string semfile;
  188. ipcdetail::shared_filepath(name, semfile);
  189. return winapi::unlink_file(semfile.c_str());
  190. }
  191. catch(...){
  192. return false;
  193. }
  194. }
  195. inline bool windows_named_sync::remove(const wchar_t *name)
  196. {
  197. try{
  198. //Make sure a temporary path is created for shared memory
  199. std::wstring semfile;
  200. ipcdetail::shared_filepath(name, semfile);
  201. return winapi::unlink_file(semfile.c_str());
  202. }
  203. catch(...){
  204. return false;
  205. }
  206. }
  207. } //namespace ipcdetail {
  208. } //namespace interprocess {
  209. } //namespace boost {
  210. #include <boost/interprocess/detail/config_end.hpp>
  211. #endif //BOOST_INTERPROCESS_WINDOWS_NAMED_SYNC_HPP