westonpace commented on a change in pull request #10955: URL: https://github.com/apache/arrow/pull/10955#discussion_r717482825
########## File path: cpp/src/arrow/util/counting_semaphore.cc ########## @@ -0,0 +1,126 @@ +// 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/counting_semaphore.h" + +#include <chrono> +#include <condition_variable> +#include <cstdint> +#include <iostream> +#include <mutex> + +#include "arrow/status.h" + +namespace arrow { +namespace util { + +class CountingSemaphore::Impl { + public: + Impl(uint32_t initial_avail, double timeout_seconds) + : num_permits_(initial_avail), timeout_seconds_(timeout_seconds) {} + + Status Acquire(uint32_t num_permits) { + std::unique_lock<std::mutex> lk(mutex_); + RETURN_NOT_OK(CheckClosed()); + num_waiters_ += num_permits; + waiter_cv_.notify_all(); + bool timed_out = !acquirer_cv_.wait_for( + lk, std::chrono::nanoseconds(static_cast<int64_t>(timeout_seconds_ * 1e9)), + [&] { return closed_ || num_permits <= num_permits_; }); + num_waiters_ -= num_permits; Review comment: The only thing waiting on waiter_cv_ is waiting for waiter_cv_ to get bigger (WaitForWaiters). Nothing is interested at the moment in num_waitiers_ decreasing. -- 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