You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2021/08/23 04:07:36 UTC

[GitHub] [arrow] westonpace commented on a change in pull request #10968: ARROW-13680: [C++] Create an asynchronous nursery to simplify capture logic

westonpace commented on a change in pull request #10968:
URL: https://github.com/apache/arrow/pull/10968#discussion_r693638540



##########
File path: cpp/src/arrow/util/async_nursery.h
##########
@@ -0,0 +1,144 @@
+// 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.
+
+#ifndef ARROW_ASYNC_NURSERY_H
+#define ARROW_ASYNC_NURSERY_H
+
+#include <list>
+
+#include "arrow/result.h"
+#include "arrow/status.h"
+#include "arrow/util/future.h"
+#include "arrow/util/mutex.h"
+
+namespace arrow {
+namespace util {
+
+class OwnedAsyncCloseable;
+
+/// An object which should be asynchronously closed before it is destroyed
+///
+/// Any AsyncCloseable must be kept alive until its parent is destroyed (this is a given
+/// if the parent is a nursery).  For shorter lived tasks/objects consider
+/// OwnedAsyncCloseable adding a dependent task.
+class AsyncCloseable {
+ public:
+  /// \brief Construct an AsyncCloseable as a child of `parent`
+  explicit AsyncCloseable(AsyncCloseable* parent);
+  virtual ~AsyncCloseable();
+
+  /// Returns a future that is completed when this object is finished closing
+  Future<> OnClosed();
+
+ protected:
+  /// Closes the AsyncCloseable
+  /// This will first call DoClose and then simultaneously close all children and
+  /// tasks_
+  virtual Future<> Close();
+  /// Subclasses should override this and perform any cleanup that is not captured by
+  /// tasks_.  Once the future returned by this method finishes then this object is
+  /// eligible for destruction and any reference to `this` may be invalid
+  virtual Future<> DoClose() = 0;
+
+  /// This method is called by subclasses to add tasks which must complete before the
+  /// object can be safely deleted
+  void AddDependentTask(Future<> task);
+  /// This method can be called by subclasses for error checking purposes.  It will
+  /// return an invalid status if this object has started closing
+  Status CheckClosed() const;
+  /// This can be used for sanity checking that a callback is not run after close has
+  /// been finished.  It will assert if the object has been fully closed (and `this`
+  /// references are unsafe)
+  void AssertNotCloseComplete() const;
+
+ private:
+  Future<> CloseChildren();
+
+  std::list<AsyncCloseable*> children_;
+  std::list<AsyncCloseable*>::iterator self_itr_;
+  std::list<std::shared_ptr<AsyncCloseable>> owned_children_;
+  std::vector<Future<>> tasks_;
+  Future<> on_closed_;
+  std::atomic<bool> closed_{false};
+  std::atomic<bool> close_complete_{false};
+  util::Mutex mutex_;
+
+  friend OwnedAsyncCloseable;
+};
+
+/// An override of AsyncCloseable which is eligible for eviction.  Instances must be

Review comment:
       I removed the concept of eviction.




-- 
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: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org