thread_quota.h
Go to the documentation of this file.
1 // Copyright 2021 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef GRPC_CORE_LIB_RESOURCE_QUOTA_THREAD_QUOTA_H
16 #define GRPC_CORE_LIB_RESOURCE_QUOTA_THREAD_QUOTA_H
17 
19 
20 #include <cstddef>
21 #include <limits>
22 
23 #include "absl/base/thread_annotations.h"
24 
28 
29 namespace grpc_core {
30 
31 // Tracks the amount of threads in a resource quota.
32 class ThreadQuota : public RefCounted<ThreadQuota> {
33  public:
34  ThreadQuota();
35  ~ThreadQuota() override;
36 
37  ThreadQuota(const ThreadQuota&) = delete;
38  ThreadQuota& operator=(const ThreadQuota&) = delete;
39 
40  // Set the maximum number of threads that can be used by this quota.
41  // If there are more, new reservations will fail until the quota is available.
42  void SetMax(size_t new_max);
43 
44  // Try to allocate some number of threads.
45  // Returns true if the allocation succeeded, false otherwise.
46  bool Reserve(size_t num_threads);
47 
48  // Release some number of threads.
49  void Release(size_t num_threads);
50 
51  private:
53  size_t allocated_ ABSL_GUARDED_BY(mu_) = 0;
55 };
56 
58 
59 } // namespace grpc_core
60 
61 #endif // GRPC_CORE_LIB_RESOURCE_QUOTA_THREAD_QUOTA_H
grpc_core::ThreadQuota::ABSL_GUARDED_BY
size_t allocated_ ABSL_GUARDED_BY(mu_)=0
grpc_core::ThreadQuota::mu_
Mutex mu_
Definition: thread_quota.h:52
grpc_core
Definition: call_metric_recorder.h:31
grpc_core::ThreadQuota::Release
void Release(size_t num_threads)
Definition: thread_quota.cc:39
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
max
int max
Definition: bloaty/third_party/zlib/examples/enough.c:170
grpc_core::ThreadQuota::operator=
ThreadQuota & operator=(const ThreadQuota &)=delete
grpc_core::RefCounted
Definition: ref_counted.h:280
grpc_core::ThreadQuota::ThreadQuota
ThreadQuota()
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_core::ThreadQuota::Reserve
bool Reserve(size_t num_threads)
Definition: thread_quota.cc:32
grpc_core::ThreadQuota
Definition: thread_quota.h:32
ref_counted.h
grpc_core::ThreadQuota::~ThreadQuota
~ThreadQuota() override
num_threads
static volatile int num_threads
Definition: benchmark-thread.c:30
grpc_core::ThreadQuota::SetMax
void SetMax(size_t new_max)
Definition: thread_quota.cc:27
ref_counted_ptr.h
max_
const int max_
Definition: bloaty/third_party/googletest/googlemock/src/gmock-cardinalities.cc:88
sync.h
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:37