pitrou commented on a change in pull request #7179: URL: https://github.com/apache/arrow/pull/7179#discussion_r539523917
########## File path: cpp/src/arrow/util/cancel.h ########## @@ -0,0 +1,81 @@ +// 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 <functional> +#include <memory> +#include <string> + +#include "arrow/result.h" +#include "arrow/status.h" +#include "arrow/util/macros.h" +#include "arrow/util/visibility.h" + +namespace arrow { + +class StopToken; + +// A RAII wrapper that automatically registers and unregisters +// a callback to a StopToken. +class ARROW_MUST_USE_TYPE ARROW_EXPORT StopCallback { + public: + using Callable = std::function<void(const Status&)>; + StopCallback(StopToken* token, Callable cb); + ~StopCallback(); + + ARROW_DISALLOW_COPY_AND_ASSIGN(StopCallback); + StopCallback(StopCallback&&); + StopCallback& operator=(StopCallback&&); + + void Call(const Status&); + + protected: + StopToken* token_; + Callable cb_; +}; + +class ARROW_EXPORT StopToken { + public: + StopToken(); + ~StopToken(); + ARROW_DISALLOW_COPY_AND_ASSIGN(StopToken); + + Status Poll(); + bool IsStopRequested(); + + void RequestStop(); + void RequestStop(std::string message); + void RequestStop(Status error); + + // Register a callback that will be called whenever cancellation happens. + // Note the callback may be called immediately, if cancellation was already + // requested. The callback will be unregistered when the returned object + // is destroyed. + StopCallback SetCallback(StopCallback::Callable cb); Review comment: If the producer is sequencing several different operations with their own stop callbacks, this should make it easier than having to write a single overall callback that will dispatch to the right sub-callback. Or at least that's the thinking. We may want to revisit this later, once we start using this concretely :-) ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
