westonpace commented on a change in pull request #10968: URL: https://github.com/apache/arrow/pull/10968#discussion_r694382572
########## File path: cpp/src/arrow/util/async_nursery.cc ########## @@ -0,0 +1,127 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. + +#include "arrow/util/async_nursery.h" + +#include "arrow/util/logging.h" + +namespace arrow { +namespace util { + +AsyncCloseable::AsyncCloseable(AsyncCloseable* parent) : on_closed_(Future<>::Make()) { + if (parent) { + Mutex::Guard guard = mutex_.Lock(); + parent->children_.push_back(this); + self_itr_ = --parent->children_.end(); + } +} + +AsyncCloseable::~AsyncCloseable() { DCHECK(close_complete_.load()); } + +Future<> AsyncCloseable::OnClosed() { return on_closed_; } + +Future<> AsyncCloseable::Close() { + { + Mutex::Guard guard = mutex_.Lock(); + if (closed_.load()) { + return Future<>::MakeFinished(); + } + closed_.store(true); + } + return DoClose() + .Then([this] { + close_complete_.store(true); + on_closed_.MarkFinished(); + return CloseChildren(); + }) + .Then([] {}, + [this](const Status& err) { + close_complete_.store(true); + on_closed_.MarkFinished(err); + return err; + }); +} + +Future<> AsyncCloseable::CloseChildren() { + for (auto& child : children_) { + tasks_.push_back(child->Close()); + } + return AllComplete(tasks_); +} + +Status AsyncCloseable::CheckClosed() const { + if (closed_.load()) { + return Status::Invalid("Invalid operation after Close"); + } + return Status::OK(); +} + +void AsyncCloseable::AssertNotCloseComplete() const { DCHECK(!close_complete_); } + +void AsyncCloseable::AddDependentTask(Future<> task) { + tasks_.push_back(std::move(task)); +} + +OwnedAsyncCloseable::OwnedAsyncCloseable(AsyncCloseable* parent) + : AsyncCloseable(parent) { + parent_ = parent; +} + +void OwnedAsyncCloseable::Init() { + Mutex::Guard lock = parent_->mutex_.Lock(); + parent_->owned_children_.push_back(shared_from_this()); + owned_self_itr_ = --parent_->owned_children_.end(); +} + +void OwnedAsyncCloseable::Evict() { + { + Mutex::Guard lock = parent_->mutex_.Lock(); + if (parent_->closed_) { + // Parent is already closing, no need to do anything, the parent will call close on + // this instance eventually + return; + } + parent_->children_.erase(self_itr_); + } + // We need to add a dependent task to make sure our parent does not close itself Review comment: I think I've addressed these sorts of race conditions in the new AddDependentTask -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
