]> git.proxmox.com Git - ceph.git/blobdiff - ceph/src/boost/boost/interprocess/sync/windows/named_sync.hpp
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / boost / boost / interprocess / sync / windows / named_sync.hpp
index 1425b4d3e6011ee4f77cc8b3fab93c25aa24afc0..28f498c6f734bffc00f29aeccb001dbaab99ad26 100644 (file)
@@ -42,6 +42,7 @@ class windows_named_sync_interface
    virtual const void *buffer_with_init_data_to_file() = 0;
    virtual void *buffer_to_store_init_data_from_file() = 0;
    virtual bool open(create_enum_t creation_type, const char *id_name) = 0;
+   virtual bool open(create_enum_t creation_type, const wchar_t *id_name) = 0;
    virtual void close() = 0;
    virtual ~windows_named_sync_interface() = 0;
 };
@@ -60,10 +61,12 @@ class windows_named_sync
 
    public:
    windows_named_sync();
-   void open_or_create(create_enum_t creation_type, const char *name, const permissions &perm, windows_named_sync_interface &sync_interface);
+   template <class CharT>
+   void open_or_create(create_enum_t creation_type, const CharT *name, const permissions &perm, windows_named_sync_interface &sync_interface);
    void close(windows_named_sync_interface &sync_interface);
 
    static bool remove(const char *name);
+   static bool remove(const wchar_t *name);
 
    #if !defined(BOOST_INTERPROCESS_DOXYGEN_INVOKED)
    private:
@@ -79,10 +82,9 @@ inline windows_named_sync::windows_named_sync()
 inline void windows_named_sync::close(windows_named_sync_interface &sync_interface)
 {
    const std::size_t buflen = sync_interface.get_data_size();
-   const std::size_t sizeof_file_info = sizeof(sync_id::internal_type) + buflen;
    winapi::interprocess_overlapped overlapped;
    if(winapi::lock_file_ex
-      (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){
+      (m_file_hnd, winapi::lockfile_exclusive_lock, 0, 1, 0, &overlapped)){
       if(winapi::set_file_pointer(m_file_hnd, sizeof(sync_id::internal_type), 0, winapi::file_begin)){
          const void *buf = sync_interface.buffer_with_final_data_to_file();
 
@@ -93,19 +95,21 @@ inline void windows_named_sync::close(windows_named_sync_interface &sync_interfa
       }
    }
    sync_interface.close();
+   //close_handle unlocks the lock
    if(m_file_hnd != winapi::invalid_handle_value){
       winapi::close_handle(m_file_hnd);
       m_file_hnd = winapi::invalid_handle_value;
    }
 }
 
+template <class CharT>
 inline void windows_named_sync::open_or_create
    ( create_enum_t creation_type
-   , const char *name
+   , const CharT *name
    , const permissions &perm
    , windows_named_sync_interface &sync_interface)
 {
-   std::string aux_str(name);
+   std::basic_string<CharT> aux_str(name);
    m_file_hnd  = winapi::invalid_handle_value;
    //Use a file to emulate POSIX lifetime semantics. After this logic
    //we'll obtain the ID of the native handle to open in aux_str
@@ -130,7 +134,7 @@ inline void windows_named_sync::open_or_create
          const std::size_t sizeof_file_info = sizeof(unique_id_type) + buflen;
          winapi::interprocess_overlapped overlapped;
          if(winapi::lock_file_ex
-            (m_file_hnd, winapi::lockfile_exclusive_lock, 0, sizeof_file_info, 0, &overlapped)){
+            (m_file_hnd, winapi::lockfile_exclusive_lock, 0, 1, 0, &overlapped)){
             __int64 filesize = 0;
             //Obtain the unique id to open the native semaphore.
             //If file size was created
@@ -162,7 +166,7 @@ inline void windows_named_sync::open_or_create
                }
                if(success){
                   //Now create a global semaphore name based on the unique id
-                  char unique_id_name[sizeof(unique_id_val)*2+1];
+                  CharT unique_id_name[sizeof(unique_id_val)*2+1];
                   std::size_t name_suffix_length = sizeof(unique_id_name);
                   bytes_to_str(&unique_id_val, sizeof(unique_id_val), &unique_id_name[0], name_suffix_length);
                   success = sync_interface.open(creation_type, unique_id_name);
@@ -170,10 +174,11 @@ inline void windows_named_sync::open_or_create
             }
 
             //Obtain OS error in case something has failed
-            err = system_error_code();
+            if(!success)
+               err = system_error_code();
 
             //If this fails we have no possible rollback so don't check the return
-            if(!winapi::unlock_file_ex(m_file_hnd, 0, sizeof_file_info, 0, &overlapped)){
+            if(!winapi::unlock_file_ex(m_file_hnd, 0, 1, 0, &overlapped)){
                err = system_error_code();
             }
          }
@@ -199,15 +204,28 @@ inline void windows_named_sync::open_or_create
 
 inline bool windows_named_sync::remove(const char *name)
 {
-   try{
+   BOOST_TRY{
       //Make sure a temporary path is created for shared memory
       std::string semfile;
       ipcdetail::shared_filepath(name, semfile);
       return winapi::unlink_file(semfile.c_str());
    }
-   catch(...){
+   BOOST_CATCH(...){
       return false;
+   } BOOST_CATCH_END
+}
+
+inline bool windows_named_sync::remove(const wchar_t *name)
+{
+   BOOST_TRY{
+      //Make sure a temporary path is created for shared memory
+      std::wstring semfile;
+      ipcdetail::shared_filepath(name, semfile);
+      return winapi::unlink_file(semfile.c_str());
    }
+   BOOST_CATCH(...){
+      return false;
+   } BOOST_CATCH_END
 }
 
 }  //namespace ipcdetail {