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