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.
14 #include "rocksdb/env.h"
15 #include "rocksdb/concurrent_task_limiter.h"
19 class TaskLimiterToken
;
21 class ConcurrentTaskLimiterImpl
: public ConcurrentTaskLimiter
{
23 explicit ConcurrentTaskLimiterImpl(const std::string
& name
,
24 int32_t max_outstanding_task
);
26 virtual ~ConcurrentTaskLimiterImpl();
28 virtual const std::string
& GetName() const override
;
30 virtual void SetMaxOutstandingTask(int32_t limit
) override
;
32 virtual void ResetMaxOutstandingTask() override
;
34 virtual int32_t GetOutstandingTask() const override
;
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
);
42 friend class TaskLimiterToken
;
45 std::atomic
<int32_t> max_outstanding_tasks_
;
46 std::atomic
<int32_t> outstanding_tasks_
;
49 ConcurrentTaskLimiterImpl(const ConcurrentTaskLimiterImpl
&) = delete;
50 ConcurrentTaskLimiterImpl
& operator=(
51 const ConcurrentTaskLimiterImpl
&) = delete;
54 class TaskLimiterToken
{
56 explicit TaskLimiterToken(ConcurrentTaskLimiterImpl
* limiter
)
57 : limiter_(limiter
) {}
61 ConcurrentTaskLimiterImpl
* limiter_
;
64 TaskLimiterToken(const TaskLimiterToken
&) = delete;
65 void operator=(const TaskLimiterToken
&) = delete;
68 } // namespace rocksdb