Refactor handle closing in Windows platform code

This commit is contained in:
mutouyun 2024-09-16 19:23:17 +08:00
parent 1fe8d93612
commit 7421b95939
4 changed files with 58 additions and 35 deletions

View File

@ -0,0 +1,38 @@
/**
* \file libipc/platform/win/close_handle.h
* \author mutouyun (orz@orzz.org)
*/
#pragma once
#include <Windows.h>
#include "libimp/log.h"
#include "libimp/system.h"
#include "libipc/def.h"
LIBIPC_NAMESPACE_BEG_
using namespace ::LIBIMP;
namespace winapi {
/**
* \brief Closes an open object handle.
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
*/
inline result<void> close_handle(HANDLE h) noexcept {
LIBIMP_LOG_();
if (h == NULL) {
log.error("handle is null.");
return std::make_error_code(std::errc::invalid_argument);
}
if (!::CloseHandle(h)) {
auto err = sys::error();
log.error("failed: CloseHandle(", h, "). error = ", err);
return err;
}
return std::error_code{};
}
} // namespace winapi
LIBIPC_NAMESPACE_END_

View File

@ -12,13 +12,17 @@
#include <Windows.h> #include <Windows.h>
#include "libimp/log.h" #include "libimp/log.h"
#include "libpmr/new.h"
#include "libipc/event.h" #include "libipc/event.h"
#include "get_sa.h" #include "get_sa.h"
#include "to_tchar.h" #include "to_tchar.h"
#include "close_handle.h"
LIBIPC_NAMESPACE_BEG_ LIBIPC_NAMESPACE_BEG_
using namespace ::LIBIMP; using namespace ::LIBIMP;
using namespace ::LIBPMR;
struct evt_handle { struct evt_handle {
std::string name; std::string name;
@ -51,26 +55,20 @@ result<evt_t> evt_open(std::string name) noexcept {
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err); log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", err);
return err; return err;
} }
return new evt_handle{std::move(name), h}; return $new<evt_handle>(std::move(name), h);
} }
/** /**
* \brief Closes an open object handle. * \brief Closes an open event object handle.
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
*/ */
result<void> evt_close(evt_t evt) noexcept { result<void> evt_close(evt_t evt) noexcept {
LIBIMP_LOG_(); LIBIMP_LOG_();
LIBIMP_UNUSED auto guard = std::unique_ptr<evt_handle>(evt); LIBIMP_UNUSED auto guard = std::unique_ptr<evt_handle, deleter>(evt);
if (!is_valid(evt)) { if (!is_valid(evt)) {
log.error("handle is null."); log.error("handle is null.");
return {}; return {};
} }
if (!::CloseHandle(evt->h_event)) { return winapi::close_handle(evt->h_event);
auto err = sys::error();
log.error("failed: CloseHandle(", evt->h_event, "). error = ", err);
return err;
}
return std::error_code{};
} }
/** /**

View File

@ -16,8 +16,10 @@
#include "get_sa.h" #include "get_sa.h"
#include "to_tchar.h" #include "to_tchar.h"
#include "close_handle.h"
LIBIPC_NAMESPACE_BEG_ LIBIPC_NAMESPACE_BEG_
using namespace ::LIBIMP; using namespace ::LIBIMP;
struct shm_handle { struct shm_handle {
@ -29,24 +31,6 @@ struct shm_handle {
namespace { namespace {
/**
* \brief Closes an open object handle.
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
*/
result<void> mmap_close(HANDLE h) {
LIBIMP_LOG_();
if (h == NULL) {
log.error("handle is null.");
return std::make_error_code(std::errc::invalid_argument);
}
if (!::CloseHandle(h)) {
auto err = sys::error();
log.error("failed: CloseHandle(", h, "). error = ", err);
return err;
}
return std::error_code{};
}
/** /**
* \brief Creates or opens a file mapping object for a specified file. * \brief Creates or opens a file mapping object for a specified file.
* *
@ -113,7 +97,7 @@ result<HANDLE> mmap_open(std::string const &file, std::size_t size, mode::type t
/// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS. /// (with its current size, not the specified size), and GetLastError returns ERROR_ALREADY_EXISTS.
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) { if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
log.info("the file being created already exists. file = ", file, ", type = ", type); log.info("the file being created already exists. file = ", file, ", type = ", type);
mmap_close(*h); winapi::close_handle(*h);
return sys::error(); return sys::error();
} }
return h; return h;
@ -174,7 +158,7 @@ result<void> mmap_release(HANDLE h, LPCVOID mem) {
if (!::UnmapViewOfFile(mem)) { if (!::UnmapViewOfFile(mem)) {
log.warning("failed: UnmapViewOfFile. error = ", sys::error()); log.warning("failed: UnmapViewOfFile. error = ", sys::error());
} }
return mmap_close(h); return winapi::close_handle(h);
} }
} // namespace } // namespace

View File

@ -9,13 +9,16 @@
#include "libimp/log.h" #include "libimp/log.h"
#include "libimp/system.h" #include "libimp/system.h"
#include "libpmr/new.h"
#include "libipc/shm.h" #include "libipc/shm.h"
#include "mmap_impl.h" #include "mmap_impl.h"
#include "close_handle.h"
LIBIPC_NAMESPACE_BEG_ LIBIPC_NAMESPACE_BEG_
using namespace ::LIBIMP; using namespace ::LIBIMP;
using namespace ::LIBPMR;
result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noexcept { result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noexcept {
LIBIMP_LOG_(); LIBIMP_LOG_();
@ -27,16 +30,16 @@ result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noex
auto mem = mmap_memof(*h); auto mem = mmap_memof(*h);
if (*mem == NULL) { if (*mem == NULL) {
log.error("failed: mmap_memof(", *h, ")."); log.error("failed: mmap_memof(", *h, ").");
mmap_close(*h); winapi::close_handle(*h);
return mem.error(); return mem.error();
} }
auto sz = mmap_sizeof(*mem); auto sz = mmap_sizeof(*mem);
if (!sz) { if (!sz) {
log.error("failed: mmap_sizeof(", *mem, ")."); log.error("failed: mmap_sizeof(", *mem, ").");
mmap_close(*h); winapi::close_handle(*h);
return sz.error(); return sz.error();
} }
return new shm_handle{std::move(name), *sz, *mem, *h}; return $new<shm_handle>(std::move(name), *sz, *mem, *h);
} }
result<void> shm_close(shm_t h) noexcept { result<void> shm_close(shm_t h) noexcept {
@ -47,7 +50,7 @@ result<void> shm_close(shm_t h) noexcept {
} }
auto shm = static_cast<shm_handle *>(h); auto shm = static_cast<shm_handle *>(h);
auto ret = mmap_release(shm->h_fmap, shm->memp); auto ret = mmap_release(shm->h_fmap, shm->memp);
delete shm; $delete(shm);
return ret; return ret;
} }