1 /* 2 * Copyright (c) 2023 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef HGM_TASK_HANDLE_THREAD_H 17 #define HGM_TASK_HANDLE_THREAD_H 18 19 #include <future> 20 #include <thread> 21 #include <unordered_map> 22 #include <unordered_set> 23 24 #include "refbase.h" 25 26 namespace ffrt { 27 class queue; 28 } 29 30 namespace OHOS::Rosen { 31 namespace HgmDetail { 32 template<typename Task> 33 class ScheduledTask : public RefBase { 34 public: Create(Task && task)35 static auto Create(Task&& task) 36 { 37 sptr<ScheduledTask<Task>> t(new ScheduledTask(std::forward<Task&&>(task))); 38 return std::make_pair(t, t->task_.get_future()); 39 } 40 Run()41 void Run() 42 { 43 task_(); 44 } 45 46 private: ScheduledTask(Task && task)47 explicit ScheduledTask(Task&& task) : task_(std::move(task)) {} 48 ~ScheduledTask() override = default; 49 50 using Return = std::invoke_result_t<Task>; 51 std::packaged_task<Return()> task_; 52 }; 53 } // namespace HgmDetail 54 55 class HgmTaskHandleThread { 56 public: 57 static HgmTaskHandleThread& Instance(); GetQueue()58 const std::shared_ptr<ffrt::queue> GetQueue() const 59 { 60 return queue_; 61 } 62 // IMPORTANT: std::move transfers ownership of the task to the queue_ 63 // After this call, the original task object is moved-from and must not be reused 64 void PostTask(const std::function<void()>& task, int64_t delayTime = 0); 65 bool PostSyncTask(const std::function<void()>& task); 66 void PostEvent(std::string eventId, const std::function<void()>& task, int64_t delayTime = 0); 67 void DetectMultiThreadingCalls(); 68 void RemoveEvent(std::string eventId); 69 template<typename Task, typename Return = std::invoke_result_t<Task>> ScheduleTask(Task && task)70 std::future<Return> ScheduleTask(Task&& task) 71 { 72 auto [scheduledTask, taskFuture] = HgmDetail::ScheduledTask<Task>::Create(std::forward<Task&&>(task)); 73 PostTask([t(std::move(scheduledTask))]() { t->Run(); }); 74 return std::move(taskFuture); 75 } 76 77 private: 78 HgmTaskHandleThread(); ~HgmTaskHandleThread()79 ~HgmTaskHandleThread() 80 { 81 queue_ = nullptr; 82 } 83 HgmTaskHandleThread(const HgmTaskHandleThread&); 84 HgmTaskHandleThread(const HgmTaskHandleThread&&); 85 HgmTaskHandleThread& operator=(const HgmTaskHandleThread&); 86 HgmTaskHandleThread& operator=(const HgmTaskHandleThread&&); 87 88 int32_t curThreadId_ = -1; 89 std::shared_ptr<ffrt::queue> queue_ = nullptr; 90 }; 91 } 92 #endif // HGM_TASK_HANDLE_THREAD_H 93