]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/concurrent_task_limiter_impl.h
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / util / concurrent_task_limiter_impl.h
1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9
10 #pragma once
11 #include <atomic>
12 #include <memory>
13
14 #include "rocksdb/env.h"
15 #include "rocksdb/concurrent_task_limiter.h"
16
17 namespace rocksdb {
18
19 class TaskLimiterToken;
20
21 class ConcurrentTaskLimiterImpl : public ConcurrentTaskLimiter {
22 public:
23 explicit ConcurrentTaskLimiterImpl(const std::string& name,
24 int32_t max_outstanding_task);
25
26 virtual ~ConcurrentTaskLimiterImpl();
27
28 virtual const std::string& GetName() const override;
29
30 virtual void SetMaxOutstandingTask(int32_t limit) override;
31
32 virtual void ResetMaxOutstandingTask() override;
33
34 virtual int32_t GetOutstandingTask() const override;
35
36 // Request token for adding a new task.
37 // If force == true, it requests a token bypassing throttle.
38 // Returns nullptr if it got throttled.
39 virtual std::unique_ptr<TaskLimiterToken> GetToken(bool force);
40
41 private:
42 friend class TaskLimiterToken;
43
44 std::string name_;
45 std::atomic<int32_t> max_outstanding_tasks_;
46 std::atomic<int32_t> outstanding_tasks_;
47
48 // No copying allowed
49 ConcurrentTaskLimiterImpl(const ConcurrentTaskLimiterImpl&) = delete;
50 ConcurrentTaskLimiterImpl& operator=(
51 const ConcurrentTaskLimiterImpl&) = delete;
52 };
53
54 class TaskLimiterToken {
55 public:
56 explicit TaskLimiterToken(ConcurrentTaskLimiterImpl* limiter)
57 : limiter_(limiter) {}
58 ~TaskLimiterToken();
59
60 private:
61 ConcurrentTaskLimiterImpl* limiter_;
62
63 // no copying allowed
64 TaskLimiterToken(const TaskLimiterToken&) = delete;
65 void operator=(const TaskLimiterToken&) = delete;
66 };
67
68 } // namespace rocksdb