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.2 kB

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