concurrent_task_limiter_impl.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. #pragma once
  10. #include <atomic>
  11. #include <memory>
  12. #include "rocksdb/env.h"
  13. #include "rocksdb/concurrent_task_limiter.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. class TaskLimiterToken;
  16. class ConcurrentTaskLimiterImpl : public ConcurrentTaskLimiter {
  17. public:
  18. explicit ConcurrentTaskLimiterImpl(const std::string& name,
  19. int32_t max_outstanding_task);
  20. // No copying allowed
  21. ConcurrentTaskLimiterImpl(const ConcurrentTaskLimiterImpl&) = delete;
  22. ConcurrentTaskLimiterImpl& operator=(const ConcurrentTaskLimiterImpl&) =
  23. delete;
  24. virtual ~ConcurrentTaskLimiterImpl();
  25. virtual const std::string& GetName() const override;
  26. virtual void SetMaxOutstandingTask(int32_t limit) override;
  27. virtual void ResetMaxOutstandingTask() override;
  28. virtual int32_t GetOutstandingTask() const override;
  29. // Request token for adding a new task.
  30. // If force == true, it requests a token bypassing throttle.
  31. // Returns nullptr if it got throttled.
  32. virtual std::unique_ptr<TaskLimiterToken> GetToken(bool force);
  33. private:
  34. friend class TaskLimiterToken;
  35. std::string name_;
  36. std::atomic<int32_t> max_outstanding_tasks_;
  37. std::atomic<int32_t> outstanding_tasks_;
  38. };
  39. class TaskLimiterToken {
  40. public:
  41. explicit TaskLimiterToken(ConcurrentTaskLimiterImpl* limiter)
  42. : limiter_(limiter) {}
  43. ~TaskLimiterToken();
  44. private:
  45. ConcurrentTaskLimiterImpl* limiter_;
  46. // no copying allowed
  47. TaskLimiterToken(const TaskLimiterToken&) = delete;
  48. void operator=(const TaskLimiterToken&) = delete;
  49. };
  50. } // namespace ROCKSDB_NAMESPACE