You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

thread_pool.h 2.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_CCSRC_COMMON_THREAD_POOL_H_
  17. #define MINDSPORE_CCSRC_COMMON_THREAD_POOL_H_
  18. #include <mutex>
  19. #include <condition_variable>
  20. #include <thread>
  21. #include <vector>
  22. #include <queue>
  23. #include <string>
  24. #include <atomic>
  25. #include <memory>
  26. #include <utility>
  27. #include <functional>
  28. #include <iostream>
  29. #include "utils/log_adapter.h"
  30. namespace mindspore {
  31. namespace common {
  32. const int kCoreThreadNum = 3;
  33. const int kDefaultMaxThreadNum = 8;
  34. enum Status { FAIL = -1, SUCCESS = 0 };
  35. using Task = std::function<int()>;
  36. class Queue {
  37. public:
  38. Queue() = default;
  39. ~Queue() = default;
  40. bool Enqueue(Task *task);
  41. bool Dequeue(Task **out);
  42. std::atomic_int task_size_ = {0};
  43. private:
  44. std::atomic_int head_ = {0};
  45. std::atomic_int tail_ = {0};
  46. Task *buffer_[2]{};
  47. };
  48. class ThreadPool {
  49. public:
  50. ~ThreadPool();
  51. ThreadPool(const ThreadPool &) = delete;
  52. ThreadPool &operator=(const ThreadPool &) = delete;
  53. static ThreadPool &GetInstance();
  54. bool SyncRun(const std::vector<Task> &tasks);
  55. size_t GetSyncRunThreadNum() { return max_thread_num_; }
  56. void ClearThreadPool();
  57. private:
  58. ThreadPool();
  59. bool SetThreadPool(int config_thread_num);
  60. void AddNewThread(int add_num);
  61. void AddRunThread(int num);
  62. void SubRunThread(int num);
  63. bool CheckResult();
  64. bool InnerSyncRun(const std::vector<Task> &tasks);
  65. void SyncRunLoop();
  66. int cur_thread_nums_{0};
  67. int cur_thread_run_nums_{0};
  68. int core_thread_num_{kCoreThreadNum};
  69. int max_thread_num_{kDefaultMaxThreadNum};
  70. std::mutex pool_mtx_;
  71. std::mutex thread_mtx_;
  72. std::condition_variable queue_ready_;
  73. std::atomic_bool exit_run_ = {false};
  74. std::vector<std::atomic_bool *> activate_list_{};
  75. std::vector<std::thread> thread_list_{};
  76. std::vector<std::shared_ptr<Queue>> queue_list_{};
  77. std::vector<std::pair<int, std::pair<bool, int>>> error_info_{};
  78. std::queue<Task> task_queue_;
  79. std::mutex task_mutex_;
  80. std::condition_variable task_cond_var_;
  81. int task_finished_count_{0};
  82. std::condition_variable finished_cond_var_;
  83. std::vector<std::thread> sync_run_threads_{};
  84. };
  85. } // namespace common
  86. } // namespace mindspore
  87. #endif // MINDSPORE_CCSRC_COMMON_THREAD_POOL_H_