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.h 3.2 kB

6 years ago
6 years ago
6 years ago
6 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_H_
  17. #define DATASET_UTIL_TASK_H_
  18. #include <chrono>
  19. #include <exception>
  20. #include <functional>
  21. #include <future>
  22. #include <iostream>
  23. #include <memory>
  24. #include <mutex>
  25. #include <sstream>
  26. #include <stdexcept>
  27. #include <string>
  28. #include <thread>
  29. #include "dataset/util/de_error.h"
  30. #include "dataset/util/intrp_resource.h"
  31. #include "dataset/util/list.h"
  32. #include "dataset/util/memory_pool.h"
  33. #include "dataset/util/services.h"
  34. #include "dataset/util/wait_post.h"
  35. #include "utils/log_adapter.h"
  36. namespace mindspore {
  37. namespace dataset {
  38. class TaskManager;
  39. class Task : public IntrpResource {
  40. public:
  41. friend class TaskManager;
  42. friend class TaskGroup;
  43. enum class WaitFlag : int { kBlocking, kNonBlocking };
  44. Task(const std::string &myName, const std::function<Status()> &f);
  45. // Future objects are not copyable.
  46. Task(const Task &) = delete;
  47. ~Task() override;
  48. Task &operator=(const Task &) = delete;
  49. // Move constructor and Assignment are not supported.
  50. // Too many things in this class.
  51. Task(Task &&) = delete;
  52. Task &operator=(Task &&) = delete;
  53. Status GetTaskErrorIfAny() const;
  54. void ChangeName(const std::string &newName) { my_name_ = newName; }
  55. // To execute the _fncObj
  56. void operator()();
  57. Node<Task> node;
  58. Node<Task> group;
  59. Node<Task> free;
  60. // Run the task
  61. Status Run();
  62. Status Join(WaitFlag wf = WaitFlag::kBlocking);
  63. bool Running() const { return running_; }
  64. bool CaughtSevereException() const { return caught_severe_exception_; }
  65. bool IsMasterThread() const { return is_master_; }
  66. std::thread::id get_id() { return id_; }
  67. std::string MyName() { return my_name_; }
  68. // An operator used by std::find
  69. bool operator==(const Task &other) const { return (this == &other); }
  70. bool operator!=(const Task &other) const { return !(*this == other); }
  71. void Post() { wp_.Set(); }
  72. Status Wait() { return (wp_.Wait()); }
  73. static Status OverrideInterruptRc(const Status &rc);
  74. private:
  75. mutable std::mutex mux_;
  76. std::string my_name_;
  77. Status rc_;
  78. WaitPost wp_;
  79. // Task need to provide definition for this function. It
  80. // will be called by thread function.
  81. std::function<Status()> fnc_obj_;
  82. // Misc fields used by TaskManager.
  83. TaskGroup *task_group_;
  84. std::future<void> thrd_;
  85. std::thread::id id_;
  86. bool is_master_;
  87. volatile bool running_;
  88. volatile bool caught_severe_exception_;
  89. void ShutdownGroup();
  90. TaskGroup *MyTaskGroup();
  91. void set_task_group(TaskGroup *vg);
  92. };
  93. extern thread_local Task *gMyTask;
  94. } // namespace dataset
  95. } // namespace mindspore
  96. #endif // DATASET_UTIL_TASK_H_