From 43d4b1d4201e688a675a3e8ce2ca2bc011f50039 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Fri, 13 Sep 2024 10:55:17 +0200 Subject: [PATCH] Add EventDriverFiles.isUnique. See vibe-d/vibe-core#412 --- source/eventcore/driver.d | 3 +++ source/eventcore/drivers/threadedfile.d | 6 ++++++ source/eventcore/drivers/winapi/files.d | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/source/eventcore/driver.d b/source/eventcore/driver.d index 62410ea6..794c58fd 100644 --- a/source/eventcore/driver.d +++ b/source/eventcore/driver.d @@ -540,6 +540,9 @@ interface EventDriverFiles { */ bool isValid(FileFD handle) const @nogc; + /// Determines if the given file's reference count equals one. + bool isUnique(FileFD descriptor) const; + /** Increments the reference count of the given file. */ void addRef(FileFD descriptor); diff --git a/source/eventcore/drivers/threadedfile.d b/source/eventcore/drivers/threadedfile.d index 2e0663cd..7dc85f9c 100644 --- a/source/eventcore/drivers/threadedfile.d +++ b/source/eventcore/drivers/threadedfile.d @@ -417,6 +417,12 @@ final class ThreadedFileEventDriver(Events : EventDriverEvents, Core : EventDriv return m_files[handle.value].validationCounter == handle.validationCounter; } + final override bool isUnique(FileFD handle) + const { + if (!isValid(handle)) return false; + return m_files[handle.value].refCount == 1; + } + final override void addRef(FileFD descriptor) { if (!isValid(descriptor)) return; diff --git a/source/eventcore/drivers/winapi/files.d b/source/eventcore/drivers/winapi/files.d index dcdd791b..a00853f6 100644 --- a/source/eventcore/drivers/winapi/files.d +++ b/source/eventcore/drivers/winapi/files.d @@ -309,6 +309,14 @@ final class WinAPIEventDriverFiles : EventDriverFiles { return false; } + override bool isUnique(FileFD handle) + const { + if (!isValid(handle)) return false; + if (auto ps = idToHandle(handle) in m_core.m_handles) + return ps.refCount == 1; + return false; + } + override void addRef(FileFD descriptor) { if (!isValid(descriptor)) return;