]> git.proxmox.com Git - ceph.git/blame - ceph/src/arrow/cpp/src/arrow/util/counting_semaphore.h
import quincy 17.2.0
[ceph.git] / ceph / src / arrow / cpp / src / arrow / util / counting_semaphore.h
CommitLineData
1d09f67e
TL
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18#ifndef ARROW_COUNTING_SEMAPHORE_H
19#define ARROW_COUNTING_SEMAPHORE_H
20
21#include <memory>
22
23#include "arrow/status.h"
24
25namespace arrow {
26namespace util {
27
28/// \brief Simple mutex-based counting semaphore with timeout
29class ARROW_EXPORT CountingSemaphore {
30 public:
31 /// \brief Create an instance with initial_avail starting permits
32 ///
33 /// \param[in] initial_avail The semaphore will start with this many permits available
34 /// \param[in] timeout_seconds A timeout to be applied to all operations. Operations
35 /// will return Status::Invalid if this timeout elapses
36 explicit CountingSemaphore(uint32_t initial_avail = 0, double timeout_seconds = 10);
37 ~CountingSemaphore();
38 /// \brief Block until num_permits permits are available
39 Status Acquire(uint32_t num_permits);
40 /// \brief Make num_permits permits available
41 Status Release(uint32_t num_permits);
42 /// \brief Wait until num_waiters are waiting on permits
43 ///
44 /// This method is non-standard but useful in unit tests to ensure sequencing
45 Status WaitForWaiters(uint32_t num_waiters);
46 /// \brief Immediately time out any waiters
47 ///
48 /// This method will return Status::OK only if there were no waiters to time out.
49 /// Once closed any operation on this instance will return an invalid status.
50 Status Close();
51
52 private:
53 class Impl;
54 std::unique_ptr<Impl> impl_;
55};
56
57} // namespace util
58} // namespace arrow
59
60#endif // ARROW_COUNTING_SEMAPHORE_H