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: [