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

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