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 4.0 kB

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