]>
Commit | Line | Data |
---|---|---|
7c673cae | 1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
11fdf7f2 TL |
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). | |
7c673cae FG |
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 | ||
7c673cae | 11 | #include <functional> |
1e59de90 TL |
12 | #include <memory> |
13 | ||
14 | #include "rocksdb/env.h" | |
15 | #include "rocksdb/threadpool.h" | |
7c673cae | 16 | |
f67539c2 | 17 | namespace ROCKSDB_NAMESPACE { |
7c673cae FG |
18 | |
19 | class ThreadPoolImpl : public ThreadPool { | |
20 | public: | |
21 | ThreadPoolImpl(); | |
22 | ~ThreadPoolImpl(); | |
23 | ||
24 | ThreadPoolImpl(ThreadPoolImpl&&) = delete; | |
25 | ThreadPoolImpl& operator=(ThreadPoolImpl&&) = delete; | |
26 | ||
27 | // Implement ThreadPool interfaces | |
28 | ||
29 | // Wait for all threads to finish. | |
11fdf7f2 | 30 | // Discards all the jobs that did not |
7c673cae FG |
31 | // start executing and waits for those running |
32 | // to complete | |
33 | void JoinAllThreads() override; | |
34 | ||
35 | // Set the number of background threads that will be executing the | |
36 | // scheduled jobs. | |
37 | void SetBackgroundThreads(int num) override; | |
11fdf7f2 TL |
38 | int GetBackgroundThreads() override; |
39 | ||
7c673cae FG |
40 | // Get the number of jobs scheduled in the ThreadPool queue. |
41 | unsigned int GetQueueLen() const override; | |
42 | ||
43 | // Waits for all jobs to complete those | |
44 | // that already started running and those that did not | |
45 | // start yet | |
46 | void WaitForJobsAndJoinAllThreads() override; | |
47 | ||
11fdf7f2 | 48 | // Make threads to run at a lower kernel IO priority |
7c673cae FG |
49 | // Currently only has effect on Linux |
50 | void LowerIOPriority(); | |
51 | ||
11fdf7f2 TL |
52 | // Make threads to run at a lower kernel CPU priority |
53 | // Currently only has effect on Linux | |
20effc67 | 54 | void LowerCPUPriority(CpuPriority pri); |
11fdf7f2 | 55 | |
7c673cae FG |
56 | // Ensure there is at aleast num threads in the pool |
57 | // but do not kill threads if there are more | |
58 | void IncBackgroundThreadsIfNeeded(int num); | |
59 | ||
60 | // Submit a fire and forget job | |
61 | // These jobs can not be unscheduled | |
62 | ||
63 | // This allows to submit the same job multiple times | |
64 | void SubmitJob(const std::function<void()>&) override; | |
65 | // This moves the function in for efficiency | |
66 | void SubmitJob(std::function<void()>&&) override; | |
67 | ||
68 | // Schedule a job with an unschedule tag and unschedule function | |
69 | // Can be used to filter and unschedule jobs by a tag | |
70 | // that are still in the queue and did not start running | |
71 | void Schedule(void (*function)(void* arg1), void* arg, void* tag, | |
72 | void (*unschedFunction)(void* arg)); | |
73 | ||
74 | // Filter jobs that are still in a queue and match | |
75 | // the given tag. Remove them from a queue if any | |
76 | // and for each such job execute an unschedule function | |
77 | // if such was given at scheduling time. | |
78 | int UnSchedule(void* tag); | |
79 | ||
80 | void SetHostEnv(Env* env); | |
81 | ||
82 | Env* GetHostEnv() const; | |
83 | ||
84 | // Return the thread priority. | |
85 | // This would allow its member-thread to know its priority. | |
86 | Env::Priority GetThreadPriority() const; | |
87 | ||
88 | // Set the thread priority. | |
89 | void SetThreadPriority(Env::Priority priority); | |
90 | ||
1e59de90 TL |
91 | // Reserve a specific number of threads, prevent them from running other |
92 | // functions The number of reserved threads could be fewer than the desired | |
93 | // one | |
94 | int ReserveThreads(int threads_to_be_reserved) override; | |
95 | ||
96 | // Release a specific number of threads | |
97 | int ReleaseThreads(int threads_to_be_released) override; | |
98 | ||
7c673cae FG |
99 | static void PthreadCall(const char* label, int result); |
100 | ||
101 | struct Impl; | |
102 | ||
103 | private: | |
1e59de90 TL |
104 | // Current public virtual interface does not provide usable |
105 | // functionality and thus can not be used internally to | |
106 | // facade different implementations. | |
107 | // | |
108 | // We propose a pimpl idiom in order to easily replace the thread pool impl | |
109 | // w/o touching the header file but providing a different .cc potentially | |
110 | // CMake option driven. | |
111 | // | |
112 | // Another option is to introduce a Env::MakeThreadPool() virtual interface | |
113 | // and override the environment. This would require refactoring ThreadPool | |
114 | // usage. | |
115 | // | |
116 | // We can also combine these two approaches | |
117 | std::unique_ptr<Impl> impl_; | |
7c673cae FG |
118 | }; |
119 | ||
f67539c2 | 120 | } // namespace ROCKSDB_NAMESPACE |