]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/concurrent_task_limiter_impl.cc
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / util / concurrent_task_limiter_impl.cc
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 #include "util/concurrent_task_limiter_impl.h"
11
12 #include "rocksdb/concurrent_task_limiter.h"
13
14 namespace ROCKSDB_NAMESPACE {
15
16 ConcurrentTaskLimiterImpl::ConcurrentTaskLimiterImpl(
17 const std::string& name, int32_t max_outstanding_task)
18 : name_(name),
19 max_outstanding_tasks_{max_outstanding_task},
20 outstanding_tasks_{0} {}
21
22 ConcurrentTaskLimiterImpl::~ConcurrentTaskLimiterImpl() {
23 assert(outstanding_tasks_ == 0);
24 }
25
26 const std::string& ConcurrentTaskLimiterImpl::GetName() const { return name_; }
27
28 void ConcurrentTaskLimiterImpl::SetMaxOutstandingTask(int32_t limit) {
29 max_outstanding_tasks_.store(limit, std::memory_order_relaxed);
30 }
31
32 void ConcurrentTaskLimiterImpl::ResetMaxOutstandingTask() {
33 max_outstanding_tasks_.store(-1, std::memory_order_relaxed);
34 }
35
36 int32_t ConcurrentTaskLimiterImpl::GetOutstandingTask() const {
37 return outstanding_tasks_.load(std::memory_order_relaxed);
38 }
39
40 std::unique_ptr<TaskLimiterToken> ConcurrentTaskLimiterImpl::GetToken(
41 bool force) {
42 int32_t limit = max_outstanding_tasks_.load(std::memory_order_relaxed);
43 int32_t tasks = outstanding_tasks_.load(std::memory_order_relaxed);
44 // force = true, bypass the throttle.
45 // limit < 0 means unlimited tasks.
46 while (force || limit < 0 || tasks < limit) {
47 if (outstanding_tasks_.compare_exchange_weak(tasks, tasks + 1)) {
48 return std::unique_ptr<TaskLimiterToken>(new TaskLimiterToken(this));
49 }
50 }
51 return nullptr;
52 }
53
54 ConcurrentTaskLimiter* NewConcurrentTaskLimiter(const std::string& name,
55 int32_t limit) {
56 return new ConcurrentTaskLimiterImpl(name, limit);
57 }
58
59 TaskLimiterToken::~TaskLimiterToken() {
60 --limiter_->outstanding_tasks_;
61 assert(limiter_->outstanding_tasks_ >= 0);
62 }
63
64 } // namespace ROCKSDB_NAMESPACE