threadpool_imp.h 3.6 KB

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