]>
git.proxmox.com Git - ceph.git/blob - 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).
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.
10 #include "util/concurrent_task_limiter_impl.h"
12 #include "rocksdb/concurrent_task_limiter.h"
14 namespace ROCKSDB_NAMESPACE
{
16 ConcurrentTaskLimiterImpl::ConcurrentTaskLimiterImpl(
17 const std::string
& name
, int32_t max_outstanding_task
)
19 max_outstanding_tasks_
{max_outstanding_task
},
20 outstanding_tasks_
{0} {}
22 ConcurrentTaskLimiterImpl::~ConcurrentTaskLimiterImpl() {
23 assert(outstanding_tasks_
== 0);
26 const std::string
& ConcurrentTaskLimiterImpl::GetName() const { return name_
; }
28 void ConcurrentTaskLimiterImpl::SetMaxOutstandingTask(int32_t limit
) {
29 max_outstanding_tasks_
.store(limit
, std::memory_order_relaxed
);
32 void ConcurrentTaskLimiterImpl::ResetMaxOutstandingTask() {
33 max_outstanding_tasks_
.store(-1, std::memory_order_relaxed
);
36 int32_t ConcurrentTaskLimiterImpl::GetOutstandingTask() const {
37 return outstanding_tasks_
.load(std::memory_order_relaxed
);
40 std::unique_ptr
<TaskLimiterToken
> ConcurrentTaskLimiterImpl::GetToken(
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));
54 ConcurrentTaskLimiter
* NewConcurrentTaskLimiter(const std::string
& name
,
56 return new ConcurrentTaskLimiterImpl(name
, limit
);
59 TaskLimiterToken::~TaskLimiterToken() {
60 --limiter_
->outstanding_tasks_
;
61 assert(limiter_
->outstanding_tasks_
>= 0);
64 } // namespace ROCKSDB_NAMESPACE