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.cc 2.6 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /**
  2. * Copyright (c) Huawei Technologies Co., Ltd. 2020. All rights reserved.
  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. #include "common/thread_pool.h"
  17. #include <atomic>
  18. #include <functional>
  19. #include <stdexcept>
  20. #include "register/register_types.h"
  21. #include "graph/ge_context.h"
  22. #include "mmpa/mmpa_api.h"
  23. namespace ge {
  24. namespace {
  25. const char* const kMultiThreadCompile = "MULTI_THREAD_COMPILE";
  26. const char* const kDisEnableFlag = "0";
  27. bool IsSingleThreadCompile() {
  28. std::string compile_thread;
  29. return ((ge::GetContext().GetOption(kMultiThreadCompile, compile_thread) == GRAPH_SUCCESS)
  30. && (compile_thread.compare(kDisEnableFlag) == 0));
  31. }
  32. }
  33. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ThreadPool::ThreadPool(uint32_t size) : is_stoped_(false) {
  34. idle_thrd_num_ = ((size < 1U) || IsSingleThreadCompile()) ? 1U : size;
  35. for (uint32_t i = 0; i < idle_thrd_num_; ++i) {
  36. pool_.emplace_back(ThreadFunc, this);
  37. }
  38. }
  39. FMK_FUNC_HOST_VISIBILITY FMK_FUNC_DEV_VISIBILITY ThreadPool::~ThreadPool() {
  40. is_stoped_.store(true);
  41. {
  42. std::unique_lock<std::mutex> lock{m_lock_};
  43. cond_var_.notify_all();
  44. }
  45. for (std::thread &thd : pool_) {
  46. if (thd.joinable()) {
  47. try {
  48. thd.join();
  49. } catch (const std::system_error &) {
  50. GELOGW("system_error");
  51. } catch (...) {
  52. GELOGW("exception");
  53. }
  54. }
  55. }
  56. }
  57. void ThreadPool::ThreadFunc(ThreadPool *const thread_pool) {
  58. if (thread_pool == nullptr) {
  59. return;
  60. }
  61. while (!thread_pool->is_stoped_) {
  62. std::function<void()> task;
  63. {
  64. std::unique_lock<std::mutex> lock{thread_pool->m_lock_};
  65. thread_pool->cond_var_.wait(
  66. lock, [thread_pool] { return thread_pool->is_stoped_.load() || !thread_pool->tasks_.empty(); });
  67. if (thread_pool->is_stoped_ && thread_pool->tasks_.empty()) {
  68. return;
  69. }
  70. task = std::move(thread_pool->tasks_.front());
  71. thread_pool->tasks_.pop();
  72. }
  73. --thread_pool->idle_thrd_num_;
  74. task();
  75. ++thread_pool->idle_thrd_num_;
  76. }
  77. }
  78. } // namespace ge