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

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