1 /*
2  * Copyright (c) 2021-2024 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 #include "task_executor.h"
17 
18 #include <pthread.h>
19 #include <thread>
20 
21 #include "ffrt.h"
22 
23 #include "constants.h"
24 #include "dh_context.h"
25 #include "distributed_hardware_errno.h"
26 #include "distributed_hardware_log.h"
27 
28 namespace OHOS {
29 namespace DistributedHardware {
30 namespace {
31     const uint32_t MAX_TASK_QUEUE_LENGTH = 256;
32 }
33 IMPLEMENT_SINGLE_INSTANCE(TaskExecutor);
TaskExecutor()34 TaskExecutor::TaskExecutor() : taskThreadFlag_(true)
35 {
36     DHLOGI("Ctor TaskExecutor");
37     ffrt::submit([this]() { this->TriggerTask(); });
38 }
39 
~TaskExecutor()40 TaskExecutor::~TaskExecutor()
41 {
42     DHLOGI("Dtor TaskExecutor");
43     taskThreadFlag_ = false;
44 }
45 
PushTask(const std::shared_ptr<Task> task)46 void TaskExecutor::PushTask(const std::shared_ptr<Task> task)
47 {
48     if (task == nullptr) {
49         DHLOGE("Task is null");
50         return;
51     }
52 
53     {
54         DHLOGI("Push task: %{public}s", task->GetId().c_str());
55         std::unique_lock<std::mutex> lock(taskQueueMtx_);
56         if (taskQueue_.size() > MAX_TASK_QUEUE_LENGTH) {
57             DHLOGE("Task queue is full");
58             return;
59         }
60         taskQueue_.push(task);
61     }
62 
63     condVar_.notify_one();
64 }
65 
PopTask()66 std::shared_ptr<Task> TaskExecutor::PopTask()
67 {
68     std::shared_ptr<Task> task = nullptr;
69 
70     std::unique_lock<std::mutex> lock(taskQueueMtx_);
71     condVar_.wait(lock, [this] {
72         return !(this->taskQueue_.empty());
73     });
74 
75     if (!taskQueue_.empty()) {
76         task = taskQueue_.front();
77         taskQueue_.pop();
78         DHLOGI("Pop task: %{public}s", task->GetId().c_str());
79     }
80 
81     return task;
82 }
83 
TriggerTask()84 void TaskExecutor::TriggerTask()
85 {
86     int32_t ret = pthread_setname_np(pthread_self(), TRIGGER_TASK);
87     if (ret != DH_FWK_SUCCESS) {
88         DHLOGE("TriggerTask setname failed.");
89     }
90     while (taskThreadFlag_) {
91         std::shared_ptr<Task> task = PopTask();
92         if (task == nullptr) {
93             DHLOGE("Pop a null task, error");
94             continue;
95         }
96 
97         auto taskFunc = [task]() {
98             task->DoTask();
99         };
100 
101         DHLOGI("Post task to EventBus: %{public}s", task->GetId().c_str());
102         DHContext::GetInstance().GetEventHandler()->PostTask(taskFunc, task->GetId());
103     }
104 }
105 } // namespace DistributedHardware
106 } // namespace OHOS
107