From d9b1f6223ef54e59e02f64e2f2d3e17c1e669c36 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 23 Mar 2023 06:09:20 +0100 Subject: [PATCH] io/uring/Close: wrapper for io_uring_prep_close() --- src/io/uring/Close.cxx | 25 +++++++++++++++++++++++++ src/io/uring/Close.hxx | 21 +++++++++++++++++++++ src/io/uring/meson.build | 1 + 3 files changed, 47 insertions(+) create mode 100644 src/io/uring/Close.cxx create mode 100644 src/io/uring/Close.hxx diff --git a/src/io/uring/Close.cxx b/src/io/uring/Close.cxx new file mode 100644 index 000000000..b8d2e5fba --- /dev/null +++ b/src/io/uring/Close.cxx @@ -0,0 +1,25 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#include "Close.hxx" +#include "Queue.hxx" +#include "io/FileDescriptor.hxx" + +namespace Uring { + +void +Close(Queue *queue, FileDescriptor fd) noexcept +{ + if (auto *s = queue != nullptr ? queue->GetSubmitEntry() : nullptr) { + io_uring_prep_close(s, fd.Get()); + io_uring_sqe_set_data(s, nullptr); + queue->Submit(); + } else { + /* io_uring not available or queue full: fall back to + the classic close() system call */ + fd.Close(); + } +} + +} // namespace Uring diff --git a/src/io/uring/Close.hxx b/src/io/uring/Close.hxx new file mode 100644 index 000000000..9685fbb3e --- /dev/null +++ b/src/io/uring/Close.hxx @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: BSD-2-Clause +// Copyright CM4all GmbH +// author: Max Kellermann + +#pragma once + +class FileDescriptor; + +namespace Uring { + +class Queue; + +/** + * Schedule a close() on the given file descriptor. If no + * #io_uring_sqe is available, this function falls back to close(). + * No callback will be invoked. + */ +void +Close(Queue *queue, FileDescriptor fd) noexcept; + +} // namespace Uring diff --git a/src/io/uring/meson.build b/src/io/uring/meson.build index 9bfe24654..ed958f646 100644 --- a/src/io/uring/meson.build +++ b/src/io/uring/meson.build @@ -21,6 +21,7 @@ uring = static_library( 'Ring.cxx', 'Queue.cxx', 'Operation.cxx', + 'Close.cxx', 'ReadOperation.cxx', include_directories: inc, dependencies: [