#include "src/dawn/native/SystemHandle.h"
#include <utility>
#include "dawn/common/Log.h"
#if DAWN_PLATFORM_IS(WINDOWS)
#include "dawn/common/windows_with_undefs.h"
#elif DAWN_PLATFORM_IS(FUCHSIA)
#include <zircon/syscalls.h>
#elif DAWN_PLATFORM_IS(POSIX)
#include <unistd.h>
#endif
namespace dawn::native {
namespace {
#if DAWN_PLATFORM_IS(WINDOWS)
constexpr inline HANDLE kInvalidHandle = nullptr;
inline bool IsHandleValid(HANDLE handle) {
return handle != nullptr;
}
inline ResultOrError<HANDLE> DuplicateHandle(HANDLE handle) {
HANDLE currentProcess = ::GetCurrentProcess();
HANDLE outHandle;
DAWN_INTERNAL_ERROR_IF(!::DuplicateHandle(currentProcess, handle, currentProcess, &outHandle, 0,
FALSE, DUPLICATE_SAME_ACCESS),
"DuplicateHandle failed");
return outHandle;
}
inline MaybeError CloseHandle(HANDLE handle) {
DAWN_INTERNAL_ERROR_IF(!::CloseHandle(handle), "CloseHandle failed");
return {};
}
#elif DAWN_PLATFORM_IS(FUCHSIA)
constexpr inline zx_handle_t kInvalidHandle = 0;
inline bool IsHandleValid(zx_handle_t handle) {
return handle > 0;
}
inline ResultOrError<zx_handle_t> DuplicateHandle(zx_handle_t handle) {
zx_handle_t outHandle = ZX_HANDLE_INVALID;
DAWN_INTERNAL_ERROR_IF(zx_handle_duplicate(handle, ZX_RIGHT_SAME_RIGHTS, &outHandle) != ZX_OK,
"zx_handle_duplicate failed");
return outHandle;
}
inline MaybeError CloseHandle(zx_handle_t handle) {
DAWN_INTERNAL_ERROR_IF(zx_handle_close(handle) != ZX_OK, "zx_handle_close failed");
return {};
}
#elif DAWN_PLATFORM_IS(POSIX)
constexpr inline int kInvalidHandle = …;
inline bool IsHandleValid(int handle) { … }
inline ResultOrError<int> DuplicateHandle(int handle) { … }
inline MaybeError CloseHandle(int handle) { … }
#endif
}
SystemHandle::SystemHandle() : … { … }
SystemHandle::SystemHandle(Handle handle) : … { … }
SystemHandle::SystemHandle(ErrorTag tag) : … { … }
bool SystemHandle::IsValid() const { … }
SystemHandle::SystemHandle(SystemHandle&& rhs) { … }
SystemHandle& SystemHandle::operator=(SystemHandle&& rhs) { … }
SystemHandle::Handle SystemHandle::Get() const { … }
SystemHandle::Handle* SystemHandle::GetMut() { … }
SystemHandle::Handle SystemHandle::Detach() { … }
ResultOrError<SystemHandle> SystemHandle::Duplicate() const { … }
void SystemHandle::Close() { … }
SystemHandle::~SystemHandle() { … }
}