From 1d3503102490dc6a2e3f084d7269f362837398f5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 17 May 2024 12:33:12 +0200 Subject: [PATCH] lib/nfs/Connection: merge CancelAndClose() into Cancel() --- src/lib/nfs/Connection.cxx | 16 +++++----------- src/lib/nfs/Connection.hxx | 18 +++++++----------- src/lib/nfs/FileReader.cxx | 18 +++++++++--------- 3 files changed, 21 insertions(+), 31 deletions(-) diff --git a/src/lib/nfs/Connection.cxx b/src/lib/nfs/Connection.cxx index ee888698a..1901d43f2 100644 --- a/src/lib/nfs/Connection.cxx +++ b/src/lib/nfs/Connection.cxx @@ -112,7 +112,6 @@ NfsConnection::CancellableCallback::CancelAndScheduleClose(struct nfsfh *fh, assert(!open); assert(close_fh == nullptr); assert(!dispose_value); - assert(fh != nullptr); close_fh = fh; dispose_value = std::move(_dispose_value); @@ -371,9 +370,12 @@ NfsConnection::Read(struct nfsfh *fh, uint64_t offset, } void -NfsConnection::Cancel(NfsCallback &callback) noexcept +NfsConnection::Cancel(NfsCallback &callback, + struct nfsfh *fh, + DisposablePointer dispose_value) noexcept { - callbacks.Cancel(callback); + CancellableCallback &cancel = callbacks.Get(callback); + cancel.CancelAndScheduleClose(fh, std::move(dispose_value)); } static void @@ -399,14 +401,6 @@ NfsConnection::Close(struct nfsfh *fh) noexcept ScheduleSocket(); } -void -NfsConnection::CancelAndClose(struct nfsfh *fh, DisposablePointer dispose_value, - NfsCallback &callback) noexcept -{ - CancellableCallback &cancel = callbacks.Get(callback); - cancel.CancelAndScheduleClose(fh, std::move(dispose_value)); -} - void NfsConnection::PrepareDestroyContext() noexcept { diff --git a/src/lib/nfs/Connection.hxx b/src/lib/nfs/Connection.hxx index c6afb3118..1e019eee8 100644 --- a/src/lib/nfs/Connection.hxx +++ b/src/lib/nfs/Connection.hxx @@ -232,8 +232,14 @@ public: * #NfsCallback. * * Not thread-safe. + * + * @param fh if not nullptr, then close this NFS file handle + * after cancellation completes + * @param dispose_value an arbitrary value that will be + * disposed of after cancellation completes */ - void Cancel(NfsCallback &callback) noexcept; + void Cancel(NfsCallback &callback, + struct nfsfh *fh, DisposablePointer dispose_value) noexcept; /** * Close the specified file handle asynchronously. @@ -242,16 +248,6 @@ public: */ void Close(struct nfsfh *fh) noexcept; - /** - * Like Cancel(), but also close the specified NFS file - * handle. - * - * @param dispose_value an arbitrary value that will be - * disposed of after cancellation completes - */ - void CancelAndClose(struct nfsfh *fh, DisposablePointer dispose_value, - NfsCallback &callback) noexcept; - protected: virtual void OnNfsConnectionError(std::exception_ptr e) noexcept = 0; diff --git a/src/lib/nfs/FileReader.cxx b/src/lib/nfs/FileReader.cxx index 5982e1b3d..71bcc74c6 100644 --- a/src/lib/nfs/FileReader.cxx +++ b/src/lib/nfs/FileReader.cxx @@ -56,20 +56,20 @@ NfsFileReader::CancelOrClose() noexcept /* no async operation in progress: can close immediately */ connection->Close(fh); - else if (state > State::OPEN) + else if (state > State::OPEN) { /* one async operation in progress: cancel it and defer the nfs_close_async() call */ - connection->CancelAndClose(fh, + DisposablePointer dispose_value{}; + #ifdef LIBNFS_API_2 - ToDeleteArray(read_buffer.release()), -#else - {}, + dispose_value = ToDeleteArray(read_buffer.release()); #endif - *this); - else if (state > State::MOUNT) + + connection->Cancel(*this, fh, std::move(dispose_value)); + } else if (state > State::MOUNT) /* we don't have a file handle yet - just cancel the async operation */ - connection->Cancel(*this); + connection->Cancel(*this, nullptr, {}); state = State::INITIAL; } @@ -138,7 +138,7 @@ void NfsFileReader::CancelRead() noexcept { if (state == State::READ) { - connection->Cancel(*this); + connection->Cancel(*this, nullptr, {}); state = State::IDLE; } }