Skip to content

Commit

Permalink
Refactor handle closing in Windows platform code
Browse files Browse the repository at this point in the history
  • Loading branch information
mutouyun committed Sep 16, 2024
1 parent 1fe8d93 commit 7421b95
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 35 deletions.
38 changes: 38 additions & 0 deletions src/libipc/platform/win/close_handle.h
Original file line number Diff line number Diff line change
@@ -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_
18 changes: 8 additions & 10 deletions src/libipc/platform/win/event_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,17 @@
#include <Windows.h>

#include "libimp/log.h"
#include "libpmr/new.h"
#include "libipc/event.h"

#include "get_sa.h"
#include "to_tchar.h"
#include "close_handle.h"

LIBIPC_NAMESPACE_BEG_

using namespace ::LIBIMP;
using namespace ::LIBPMR;

struct evt_handle {
std::string name;
Expand Down Expand Up @@ -51,26 +55,20 @@ result<evt_t> evt_open(std::string name) noexcept {
log.error("failed: CreateEvent(FALSE, FALSE, ", name, "). error = ", 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.
* \see https://docs.microsoft.com/en-us/windows/win32/api/handleapi/nf-handleapi-closehandle
* \brief Closes an open event object handle.
*/
result<void> evt_close(evt_t evt) noexcept {
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)) {
log.error("handle is null.");
return {};
}
if (!::CloseHandle(evt->h_event)) {
auto err = sys::error();
log.error("failed: CloseHandle(", evt->h_event, "). error = ", err);
return err;
}
return std::error_code{};
return winapi::close_handle(evt->h_event);
}

/**
Expand Down
24 changes: 4 additions & 20 deletions src/libipc/platform/win/mmap_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

#include "get_sa.h"
#include "to_tchar.h"
#include "close_handle.h"

LIBIPC_NAMESPACE_BEG_

using namespace ::LIBIMP;

struct shm_handle {
Expand All @@ -29,24 +31,6 @@ struct shm_handle {

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.
*
Expand Down Expand Up @@ -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.
if ((type == mode::create) && (::GetLastError() == ERROR_ALREADY_EXISTS)) {
log.info("the file being created already exists. file = ", file, ", type = ", type);
mmap_close(*h);
winapi::close_handle(*h);
return sys::error();
}
return h;
Expand Down Expand Up @@ -174,7 +158,7 @@ result<void> mmap_release(HANDLE h, LPCVOID mem) {
if (!::UnmapViewOfFile(mem)) {
log.warning("failed: UnmapViewOfFile. error = ", sys::error());
}
return mmap_close(h);
return winapi::close_handle(h);
}

} // namespace
Expand Down
13 changes: 8 additions & 5 deletions src/libipc/platform/win/shm_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,16 @@

#include "libimp/log.h"
#include "libimp/system.h"

#include "libpmr/new.h"
#include "libipc/shm.h"

#include "mmap_impl.h"
#include "close_handle.h"

LIBIPC_NAMESPACE_BEG_

using namespace ::LIBIMP;
using namespace ::LIBPMR;

result<shm_t> shm_open(std::string name, std::size_t size, mode::type type) noexcept {
LIBIMP_LOG_();
Expand All @@ -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);
if (*mem == NULL) {
log.error("failed: mmap_memof(", *h, ").");
mmap_close(*h);
winapi::close_handle(*h);
return mem.error();
}
auto sz = mmap_sizeof(*mem);
if (!sz) {
log.error("failed: mmap_sizeof(", *mem, ").");
mmap_close(*h);
winapi::close_handle(*h);
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 {
Expand All @@ -47,7 +50,7 @@ result<void> shm_close(shm_t h) noexcept {
}
auto shm = static_cast<shm_handle *>(h);
auto ret = mmap_release(shm->h_fmap, shm->memp);
delete shm;
$delete(shm);
return ret;
}

Expand Down

0 comments on commit 7421b95

Please sign in to comment.