/Users/deen/code/yugabyte-db/src/yb/rocksdb/util/thread_posix.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | | // This source code is licensed under the BSD-style license found in the |
3 | | // LICENSE file in the root directory of this source tree. An additional grant |
4 | | // of patent rights can be found in the PATENTS file in the same directory. |
5 | | // |
6 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | #ifndef YB_ROCKSDB_UTIL_THREAD_POSIX_H |
24 | | #define YB_ROCKSDB_UTIL_THREAD_POSIX_H |
25 | | |
26 | | #pragma once |
27 | | |
28 | | #include <pthread.h> |
29 | | #include <string.h> |
30 | | |
31 | | #include <cstdarg> |
32 | | #include <deque> |
33 | | |
34 | | #include "yb/gutil/ref_counted.h" |
35 | | |
36 | | #include "yb/rocksdb/env.h" |
37 | | #include "yb/rocksdb/file.h" |
38 | | |
39 | | #include "yb/util/faststring.h" |
40 | | |
41 | | namespace yb { |
42 | | |
43 | | class Thread; |
44 | | |
45 | | } |
46 | | |
47 | | namespace rocksdb { |
48 | | |
49 | | class ThreadPool { |
50 | | public: |
51 | | ThreadPool(); |
52 | | ~ThreadPool(); |
53 | | |
54 | | void JoinAllThreads(); |
55 | | void LowerIOPriority(); |
56 | | void BGThread(size_t thread_id); |
57 | | void WakeUpAllThreads(); |
58 | | void IncBackgroundThreadsIfNeeded(int num); |
59 | | void SetBackgroundThreads(int num); |
60 | | void StartBGThreads(); |
61 | | void Schedule(void (*function)(void* arg1), void* arg, void* tag, |
62 | | void (*unschedFunction)(void* arg)); |
63 | | int UnSchedule(void* arg); |
64 | | |
65 | 33 | unsigned int GetQueueLen() const { |
66 | 33 | return queue_len_.load(std::memory_order_relaxed); |
67 | 33 | } |
68 | | |
69 | 52.6k | void SetHostEnv(Env* env) { env_ = env; } |
70 | 0 | Env* GetHostEnv() { return env_; } |
71 | | |
72 | | // Return true if there is at least one thread needs to terminate. |
73 | 716k | bool HasExcessiveThread() { |
74 | 716k | return static_cast<int>(bgthreads_.size()) > total_threads_limit_; |
75 | 716k | } |
76 | | |
77 | | // Return true iff the current thread is the excessive thread to terminate. |
78 | | // Always terminate the running thread that is added last, even if there are |
79 | | // more than one thread to terminate. |
80 | 545k | bool IsLastExcessiveThread(size_t thread_id) { |
81 | 545k | return HasExcessiveThread() && thread_id == bgthreads_.size() - 197 ; |
82 | 545k | } |
83 | | |
84 | | // Is one of the threads to terminate. |
85 | 170k | bool IsExcessiveThread(size_t thread_id) { |
86 | 170k | return static_cast<int>(thread_id) >= total_threads_limit_; |
87 | 170k | } |
88 | | |
89 | | // Return the thread priority. |
90 | | // This would allow its member-thread to know its priority. |
91 | 206k | Env::Priority GetThreadPriority() { return priority_; } |
92 | | |
93 | | // Set the thread priority. |
94 | 52.6k | void SetThreadPriority(Env::Priority priority) { priority_ = priority; } |
95 | | |
96 | | static void PthreadCall(const char* label, int result); |
97 | | |
98 | | private: |
99 | | // Entry per Schedule() call |
100 | | struct BGItem { |
101 | | void* arg; |
102 | | void (*function)(void*); |
103 | | void* tag; |
104 | | void (*unschedFunction)(void*); |
105 | | }; |
106 | | typedef std::deque<BGItem> BGQueue; |
107 | | |
108 | | pthread_mutex_t mu_; |
109 | | pthread_cond_t bgsignal_; |
110 | | int total_threads_limit_ = 1; |
111 | | std::vector<scoped_refptr<yb::Thread>> bgthreads_; |
112 | | BGQueue queue_; |
113 | | std::atomic_uint queue_len_{0}; // Queue length. Used for stats reporting |
114 | | bool exit_all_threads_ = false; |
115 | | bool low_io_priority_ = false; |
116 | | Env::Priority priority_ = static_cast<Env::Priority>(0); |
117 | | Env* env_ = nullptr; |
118 | | |
119 | | void SetBackgroundThreadsInternal(int num, bool allow_reduce); |
120 | | }; |
121 | | |
122 | | } // namespace rocksdb |
123 | | |
124 | | #endif // YB_ROCKSDB_UTIL_THREAD_POSIX_H |