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

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