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



##########
File path: cpp/src/arrow/util/async_nursery.h
##########
@@ -0,0 +1,155 @@
+// 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.
+
+#pragma once
+
+#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 Nursery;
+class AsyncCloseablePimpl;
+
+template <typename T>
+struct DestroyingDeleter {
+  void operator()(T* p) { p->Destroy(); }
+};
+
+/// 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 ARROW_EXPORT AsyncCloseable : public 
std::enable_shared_from_this<AsyncCloseable> {
+ public:
+  AsyncCloseable();
+  explicit AsyncCloseable(AsyncCloseable* parent);
+  virtual ~AsyncCloseable();
+
+  /// Returns a future that is completed when this object is finished closing
+  const Future<>& OnClosed();
+
+ protected:
+  /// Subclasses should override this and perform any cleanup.  Once the 
future returned
+  /// by this method finishes then this object is eligible for destruction and 
any
+  /// reference to `this` will 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(const 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;
+
+  Nursery* nursery_;
+
+ private:
+  void SetNursery(Nursery* nursery);
+  void Destroy();
+
+  Future<> on_closed_;
+  Future<> tasks_finished_;
+  std::atomic<bool> closed_{false};
+  std::atomic<uint32_t> num_tasks_outstanding_{1};
+
+  friend Nursery;
+  template <typename T>
+  friend struct DestroyingDeleter;
+  friend AsyncCloseablePimpl;
+};
+
+class ARROW_EXPORT AsyncCloseablePimpl {
+ protected:
+  void Init(AsyncCloseable* impl);
+
+ private:
+  void SetNursery(Nursery* nursery);
+  void Destroy();
+
+  AsyncCloseable* impl_;
+
+  friend Nursery;
+  template <typename T>
+  friend struct DestroyingDeleter;
+};
+
+class ARROW_EXPORT Nursery {
+ public:
+  // FIXME: Add static_assert that T extends AsyncCloseable for friendlier 
error message

Review comment:
       nit: this fixme is fixed




-- 
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-unsubscr...@arrow.apache.org

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


Reply via email to