• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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 "dh_context.h"
24 #include "distributed_hardware_errno.h"
25 #include "distributed_hardware_log.h"
26 
27 namespace OHOS {
28 namespace DistributedHardware {
29 namespace {
30     const uint32_t MAX_TASK_QUEUE_LENGTH = 256;
31     constexpr const char *TRIGGER_TASK = "TriggerTask";
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     }
79 
80     return task;
81 }
82 
TriggerTask()83 void TaskExecutor::TriggerTask()
84 {
85     int32_t ret = pthread_setname_np(pthread_self(), TRIGGER_TASK);
86     if (ret != DH_FWK_SUCCESS) {
87         DHLOGE("TriggerTask setname failed.");
88     }
89     while (taskThreadFlag_) {
90         std::shared_ptr<Task> task = PopTask();
91         if (task == nullptr) {
92             DHLOGE("Pop a null task, error");
93             continue;
94         }
95 
96         auto taskFunc = [task]() {
97             task->DoTask();
98         };
99 
100         DHLOGI("Post task to EventBus: %{public}s", task->GetId().c_str());
101         if (DHContext::GetInstance().GetEventHandler() == nullptr) {
102             DHLOGE("eventHandler is nullptr");
103             continue;
104         }
105         DHContext::GetInstance().GetEventHandler()->PostTask(taskFunc, task->GetId());
106     }
107 }
108 } // namespace DistributedHardware
109 } // namespace OHOS
110