]>
git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/threadpool_imp.h
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/threadpool.h"
17 namespace ROCKSDB_NAMESPACE
{
19 class ThreadPoolImpl
: public ThreadPool
{
24 ThreadPoolImpl(ThreadPoolImpl
&&) = delete;
25 ThreadPoolImpl
& operator=(ThreadPoolImpl
&&) = delete;
27 // Implement ThreadPool interfaces
29 // Wait for all threads to finish.
30 // Discards all the jobs that did not
31 // start executing and waits for those running
33 void JoinAllThreads() override
;
35 // Set the number of background threads that will be executing the
37 void SetBackgroundThreads(int num
) override
;
38 int GetBackgroundThreads() override
;
40 // Get the number of jobs scheduled in the ThreadPool queue.
41 unsigned int GetQueueLen() const override
;
43 // Waits for all jobs to complete those
44 // that already started running and those that did not
46 void WaitForJobsAndJoinAllThreads() override
;
48 // Make threads to run at a lower kernel IO priority
49 // Currently only has effect on Linux
50 void LowerIOPriority();
52 // Make threads to run at a lower kernel CPU priority
53 // Currently only has effect on Linux
54 void LowerCPUPriority(CpuPriority pri
);
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
);
60 // Submit a fire and forget job
61 // These jobs can not be unscheduled
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
;
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
));
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
);
80 void SetHostEnv(Env
* env
);
82 Env
* GetHostEnv() const;
84 // Return the thread priority.
85 // This would allow its member-thread to know its priority.
86 Env::Priority
GetThreadPriority() const;
88 // Set the thread priority.
89 void SetThreadPriority(Env::Priority priority
);
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
94 int ReserveThreads(int threads_to_be_reserved
) override
;
96 // Release a specific number of threads
97 int ReleaseThreads(int threads_to_be_released
) override
;
99 static void PthreadCall(const char* label
, int result
);
104 // Current public virtual interface does not provide usable
105 // functionality and thus can not be used internally to
106 // facade different implementations.
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.
112 // Another option is to introduce a Env::MakeThreadPool() virtual interface
113 // and override the environment. This would require refactoring ThreadPool
116 // We can also combine these two approaches
117 std::unique_ptr
<Impl
> impl_
;
120 } // namespace ROCKSDB_NAMESPACE