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.

task_manager.h 5.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * Copyright 2019 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 DATASET_UTIL_TASK_MANAGER_H_
  17. #define DATASET_UTIL_TASK_MANAGER_H_
  18. #include <semaphore.h>
  19. #include <signal.h> // for sig_atomic_t
  20. #include <condition_variable>
  21. #include <functional>
  22. #include <memory>
  23. #include <string>
  24. #include <set>
  25. #include "dataset/util/allocator.h"
  26. #include "dataset/util/intrp_service.h"
  27. #include "dataset/util/lock.h"
  28. #include "dataset/util/services.h"
  29. #include "dataset/util/status.h"
  30. #include "dataset/util/task.h"
  31. namespace mindspore {
  32. namespace dataset {
  33. namespace thread {
  34. using id = std::thread::id;
  35. } // namespace thread
  36. namespace this_thread {
  37. inline thread::id get_id() { return std::this_thread::get_id(); }
  38. } // namespace this_thread
  39. class TaskManager : public Service {
  40. public:
  41. friend class Services;
  42. friend class TaskGroup;
  43. ~TaskManager() override;
  44. TaskManager(const TaskManager &) = delete;
  45. TaskManager &operator=(const TaskManager &) = delete;
  46. static TaskManager &GetInstance() noexcept { return Services::getTaskMgrInstance(); }
  47. Status DoServiceStart() override;
  48. Status DoServiceStop() override;
  49. // A public global interrupt flag for signal handlers
  50. volatile sig_atomic_t global_interrupt_;
  51. // API
  52. // This takes the same parameter as Task constructor. Take a look
  53. // of the test-thread.cc for usage.
  54. Status CreateAsyncTask(const std::string &my_name, const std::function<Status()> &f, TaskGroup *vg, Task **);
  55. // Same usage as boot thread group
  56. Status join_all();
  57. void interrupt_all() noexcept;
  58. // Locate a particular Task.
  59. static Task *FindMe();
  60. static void InterruptGroup(Task &);
  61. static Status GetMasterThreadRc();
  62. static void InterruptMaster(const Status &rc = Status::OK());
  63. static void WakeUpWatchDog() {
  64. TaskManager &tm = TaskManager::GetInstance();
  65. (void)sem_post(&tm.sem_);
  66. }
  67. void ReturnFreeTask(Task *p) noexcept;
  68. Status GetFreeTask(const std::string &my_name, const std::function<Status()> &f, Task **p);
  69. Status WatchDog();
  70. private:
  71. RWLock lru_lock_;
  72. SpinLock free_lock_;
  73. SpinLock tg_lock_;
  74. std::shared_ptr<Task> master_;
  75. List<Task> lru_;
  76. List<Task> free_lst_;
  77. sem_t sem_;
  78. TaskGroup *watchdog_grp_;
  79. std::set<TaskGroup *> grp_list_;
  80. Task *watchdog_;
  81. TaskManager();
  82. };
  83. // A group of related tasks.
  84. class TaskGroup : public Service {
  85. public:
  86. friend class Task;
  87. friend class TaskManager;
  88. Status CreateAsyncTask(const std::string &my_name, const std::function<Status()> &f, Task **pTask = nullptr);
  89. void interrupt_all() noexcept;
  90. Status join_all();
  91. int size() const noexcept { return grp_list_.count; }
  92. Status DoServiceStart() override { return Status::OK(); }
  93. Status DoServiceStop() override;
  94. TaskGroup();
  95. ~TaskGroup() override;
  96. Status GetTaskErrorIfAny();
  97. std::shared_ptr<IntrpService> GetIntrpService();
  98. private:
  99. Status rc_;
  100. // Can't use rw_lock_ as we will lead to deadlatch. Create another mutex to serialize access to rc_.
  101. std::mutex rc_mux_;
  102. RWLock rw_lock_;
  103. List<Task> grp_list_;
  104. std::shared_ptr<IntrpService> intrp_svc_;
  105. };
  106. namespace this_thread {
  107. inline bool is_interrupted() {
  108. TaskManager &tm = TaskManager::GetInstance();
  109. if (tm.global_interrupt_ == 1) {
  110. return true;
  111. }
  112. Task *my_task = TaskManager::FindMe();
  113. return (my_task != nullptr) ? my_task->Interrupted() : false;
  114. }
  115. } // namespace this_thread
  116. #define RETURN_IF_INTERRUPTED() \
  117. do { \
  118. if (mindspore::dataset::this_thread::is_interrupted()) { \
  119. Task *myTask = TaskManager::FindMe(); \
  120. if (myTask->IsMasterThread() && myTask->CaughtSevereException()) { \
  121. return TaskManager::GetMasterThreadRc(); \
  122. } else { \
  123. return Status(StatusCode::kInterrupted); \
  124. } \
  125. } \
  126. } while (false)
  127. inline Status interruptible_wait(std::condition_variable *cv, std::unique_lock<std::mutex> *lk,
  128. const std::function<bool()> &pred) noexcept {
  129. if (!pred()) {
  130. do {
  131. RETURN_IF_INTERRUPTED();
  132. try {
  133. (void)cv->wait_for(*lk, std::chrono::milliseconds(1));
  134. } catch (std::exception &e) {
  135. // Anything thrown by wait_for is considered system error.
  136. RETURN_STATUS_UNEXPECTED(e.what());
  137. }
  138. } while (!pred());
  139. }
  140. return Status::OK();
  141. }
  142. } // namespace dataset
  143. } // namespace mindspore
  144. #endif // DATASET_UTIL_TASK_MANAGER_H_