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

6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. #include "dataset/util/task.h"
  17. #include "common/utils.h"
  18. #include "dataset/util/task_manager.h"
  19. #include "dataset/util/de_error.h"
  20. #include "utils/log_adapter.h"
  21. namespace mindspore {
  22. namespace dataset {
  23. thread_local Task *gMyTask = nullptr;
  24. void Task::operator()() {
  25. #if !defined(_WIN32) && !defined(_WIN64)
  26. gMyTask = this;
  27. #endif
  28. id_ = this_thread::get_id();
  29. std::stringstream ss;
  30. ss << id_;
  31. MS_LOG(DEBUG) << my_name_ << " Thread ID " << ss.str() << " Started.";
  32. try {
  33. // Previously there is a timing hole where the thread is spawn but hit error immediately before we can set
  34. // the TaskGroup pointer and register. We move the registration logic to here (after we spawn) so we can
  35. // get the thread id.
  36. TaskGroup *vg = MyTaskGroup();
  37. rc_ = vg->GetIntrpService()->Register(ss.str(), this);
  38. if (rc_.IsOk()) {
  39. // Now we can run the given task.
  40. rc_ = fnc_obj_();
  41. }
  42. // Some error codes are ignored, e.g. interrupt. Others we just shutdown the group.
  43. if (rc_.IsError() && !rc_.IsInterrupted()) {
  44. ShutdownGroup();
  45. }
  46. } catch (const std::bad_alloc &e) {
  47. rc_ = Status(StatusCode::kOutOfMemory, __LINE__, __FILE__, e.what());
  48. ShutdownGroup();
  49. } catch (const std::exception &e) {
  50. rc_ = Status(StatusCode::kUnexpectedError, __LINE__, __FILE__, e.what());
  51. ShutdownGroup();
  52. }
  53. }
  54. void Task::ShutdownGroup() { // Wake up watch dog and shutdown the engine.
  55. {
  56. std::lock_guard<std::mutex> lk(mux_);
  57. caught_severe_exception_ = true;
  58. }
  59. TaskGroup *vg = MyTaskGroup();
  60. // If multiple threads hit severe errors in the same group. Keep the first one and
  61. // discard the rest.
  62. if (vg->rc_.IsOk()) {
  63. std::unique_lock<std::mutex> rcLock(vg->rc_mux_);
  64. // Check again after we get the lock
  65. if (vg->rc_.IsOk()) {
  66. vg->rc_ = rc_;
  67. rcLock.unlock();
  68. TaskManager::InterruptMaster(rc_);
  69. TaskManager::InterruptGroup(*this);
  70. }
  71. }
  72. }
  73. Status Task::GetTaskErrorIfAny() const {
  74. std::lock_guard<std::mutex> lk(mux_);
  75. if (caught_severe_exception_) {
  76. return rc_;
  77. } else {
  78. return Status::OK();
  79. }
  80. }
  81. Task::Task(const std::string &myName, const std::function<Status()> &f)
  82. : my_name_(myName),
  83. rc_(),
  84. fnc_obj_(f),
  85. task_group_(nullptr),
  86. is_master_(false),
  87. running_(false),
  88. caught_severe_exception_(false) {
  89. IntrpResource::ResetIntrpState();
  90. wp_.ResetIntrpState();
  91. wp_.Clear();
  92. }
  93. Status Task::Run() {
  94. Status rc;
  95. if (running_ == false) {
  96. try {
  97. thrd_ = std::async(std::launch::async, std::ref(*this));
  98. running_ = true;
  99. caught_severe_exception_ = false;
  100. } catch (const std::exception &e) {
  101. rc = Status(StatusCode::kUnexpectedError, __LINE__, __FILE__, e.what());
  102. }
  103. }
  104. return rc;
  105. }
  106. Status Task::Join(WaitFlag blocking) {
  107. if (running_) {
  108. RETURN_UNEXPECTED_IF_NULL(MyTaskGroup());
  109. auto interrupt_svc = MyTaskGroup()->GetIntrpService();
  110. try {
  111. if (blocking == WaitFlag::kBlocking) {
  112. // If we are asked to wait, then wait
  113. thrd_.get();
  114. } else if (blocking == WaitFlag::kNonBlocking) {
  115. // There is a race condition in the global resource tracking such that a thread can miss the
  116. // interrupt and becomes blocked on a conditional variable forever. As a result, calling
  117. // join() will not come back. We need some timeout version of join such that if the thread
  118. // doesn't come back in a reasonable of time, we will send the interrupt again.
  119. while (thrd_.wait_for(std::chrono::seconds(1)) != std::future_status::ready) {
  120. // We can't tell which conditional_variable this thread is waiting on. So we may need
  121. // to interrupt everything one more time.
  122. MS_LOG(INFO) << "Some threads not responding. Interrupt again";
  123. interrupt_svc->InterruptAll();
  124. }
  125. } else {
  126. RETURN_STATUS_UNEXPECTED("Unknown WaitFlag");
  127. }
  128. std::stringstream ss;
  129. ss << get_id();
  130. MS_LOG(DEBUG) << MyName() << " Thread ID " << ss.str() << " Stopped.";
  131. running_ = false;
  132. RETURN_IF_NOT_OK(wp_.Deregister());
  133. RETURN_IF_NOT_OK(interrupt_svc->Deregister(ss.str()));
  134. } catch (const std::exception &e) {
  135. RETURN_STATUS_UNEXPECTED(e.what());
  136. }
  137. }
  138. return Status::OK();
  139. }
  140. TaskGroup *Task::MyTaskGroup() { return task_group_; }
  141. void Task::set_task_group(TaskGroup *vg) { task_group_ = vg; }
  142. Task::~Task() { task_group_ = nullptr; }
  143. Status Task::OverrideInterruptRc(const Status &rc) {
  144. if (rc.IsInterrupted() && this_thread::is_master_thread()) {
  145. // If we are interrupted, override the return value if this is the master thread.
  146. // Master thread is being interrupted mostly because of some thread is reporting error.
  147. return TaskManager::GetMasterThreadRc();
  148. }
  149. return rc;
  150. }
  151. } // namespace dataset
  152. } // namespace mindspore