pitrou commented on a change in pull request #9528: URL: https://github.com/apache/arrow/pull/9528#discussion_r589476690
########## File path: cpp/src/arrow/util/cancel.cc ########## @@ -0,0 +1,167 @@ +// 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/cancel.h" + +#include <atomic> +#include <mutex> +#include <sstream> +#include <utility> + +#include "arrow/result.h" +#include "arrow/util/atomic_shared_ptr.h" +#include "arrow/util/io_util.h" +#include "arrow/util/logging.h" +#include "arrow/util/visibility.h" + +namespace arrow { + +#if ATOMIC_INT_LOCK_FREE != 2 +#error Lock-free atomic int required for signal safety +#endif + +using internal::ReinstateSignalHandler; +using internal::SetSignalHandler; +using internal::SignalHandler; + +// NOTE: We care mainly about the making the common case (not cancelled) fast. + +struct StopSourceImpl { + std::atomic<int> requested_{0}; // will be -1 or signal number if requested + std::mutex mutex_; + Status cancel_error_; +}; + +StopSource::StopSource() : impl_(new StopSourceImpl) {} + +StopSource::~StopSource() = default; + +void StopSource::RequestStop() { RequestStop(Status::Cancelled("Operation cancelled")); } + +void StopSource::RequestStop(Status st) { + std::lock_guard<std::mutex> lock(impl_->mutex_); + DCHECK(!st.ok()); + if (!impl_->requested_) { + impl_->requested_ = -1; + impl_->cancel_error_ = std::move(st); + } +} + +void StopSource::RequestStopFromSignal(int signum) { + // Only async-signal-safe code allowed here + impl_->requested_.store(signum); Review comment: No, we need the mutex to protect `cancel_error_` (which we cannot store atomically). ---------------------------------------------------------------- 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: us...@infra.apache.org