1 /* 2 * Copyright (c) 2021-2022 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 #ifndef OHOS_APP_DISPATCHER_TASK_H 16 #define OHOS_APP_DISPATCHER_TASK_H 17 18 #include <atomic> 19 #include <deque> 20 #include <memory> 21 #include <thread> 22 #include "concurrent_queue.h" 23 #include "revocable.h" 24 #include "runnable.h" 25 #include "task_listener.h" 26 #include "task_priority.h" 27 #include "task_stage.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 class BaseTaskDispatcher; 32 class Task : public Revocable { 33 public: 34 Task(const std::shared_ptr<Runnable> &runnable, const TaskPriority priority, 35 const std::shared_ptr<BaseTaskDispatcher> &baseTaskDispatcher); 36 37 virtual ~Task(); 38 39 /** 40 * @brief invoke the function to execute the task 41 */ 42 virtual void Run(); 43 44 /** 45 * @brief Gets the priority. 46 * @return The priority. 47 */ 48 TaskPriority GetPriority() const; 49 50 /** 51 * @brief Sets the sequence. 52 * @param sequence The sequence 53 */ 54 void SetSequence(long sequence); 55 56 /** 57 * @brief Gets the sequence. 58 * @return The sequence. 59 */ 60 long GetSequence() const; 61 62 /** 63 * @brief Revoke this task if hasn't run. 64 * @return true if set revoked or already revoked. False if the task has start executing. 65 */ 66 bool Revoke() override; 67 68 /** 69 * @brief Adds a task listener. 70 * @param listener The listener 71 */ 72 void AddTaskListener(const std::shared_ptr<TaskListener> &listener); 73 74 /** 75 * @brief Called when task is about to run. 76 */ 77 void BeforeTaskExecute(); 78 79 /** 80 * @brief Called when task is done. 81 */ 82 void AfterTaskExecute(); 83 84 /** 85 * @brief Called when task is canceled. 86 */ 87 void OnTaskCanceled(); 88 89 bool operator==(std::shared_ptr<Task> &rec) const; 90 91 protected: 92 std::shared_ptr<Runnable> runnable_; 93 94 private: 95 using RevokeResult = enum { 96 // Maybe already run. 97 FAIL, 98 // Convert |revoked| flag from false to true. 99 SUCCESS, 100 // The |revoked| flag is already set to true. 101 ALREADY_REVOKED 102 }; 103 104 /** 105 * @brief Return true if not executed or revoked, and if not executed or revoked, ensure |EXECUTED| to be set. 106 * @param - 107 * @return bool 108 */ 109 bool EnterExecute(); 110 111 RevokeResult SetRevoked(); 112 113 void ConcurrentQueueStatusUpdate(const TaskStage::TASKSTAGE taskstage); 114 115 private: 116 const static unsigned int EXECUTED = (1 << 0); 117 const static unsigned int REVOKED = (1 << 1); 118 long sequence_ = 0; 119 std::atomic<unsigned int> state_; 120 TaskPriority priority_; 121 std::shared_ptr<Revocable> revocable_ = nullptr; 122 ConcurrentQueue<std::shared_ptr<TaskListener>> taskListeners_; 123 std::shared_ptr<BaseTaskDispatcher> baseTaskDispatcher_; 124 }; 125 } // namespace AppExecFwk 126 } // namespace OHOS 127 128 #endif // OHOS_APP_DISPATCHER_TASK_H 129