named_sync.hpp 8.1 KB

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