• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 #include "task_queue.h"
17 
18 #include "daudio_errorcode.h"
19 #include "daudio_log.h"
20 
21 #undef DH_LOG_TAG
22 #define DH_LOG_TAG "TaskQueue"
23 
24 namespace OHOS {
25 namespace DistributedHardware {
Start()26 void TaskQueue::Start()
27 {
28     DHLOGI("Start task queue.");
29     taskQueueReady_ = true;
30     isQuitTaskQueue_ = false;
31     mainThreadLoop_ = std::thread(&TaskQueue::Run, this);
32     if (pthread_setname_np(mainThreadLoop_.native_handle(), MAIN_THREAD_LOOP) != DH_SUCCESS) {
33         DHLOGE("Main thread loop setname failed.");
34     }
35     while (!mainThreadLoop_.joinable()) {
36     }
37     DHLOGI("Start task queue success.");
38 }
39 
Stop()40 void TaskQueue::Stop()
41 {
42     DHLOGI("Stop task queue.");
43     isQuitTaskQueue_ = true;
44     if (mainThreadLoop_.joinable()) {
45         mainThreadLoop_.join();
46     }
47     DHLOGI("Stop task queue success.");
48 }
49 
Run()50 void TaskQueue::Run()
51 {
52     DHLOGI("Task queue running.");
53     while (taskQueueReady_) {
54         if (isQuitTaskQueue_ && taskQueue_.empty()) {
55             DHLOGI("Task queue quit.");
56             break;
57         }
58         std::shared_ptr<TaskImplInterface> task = nullptr;
59         {
60             std::unique_lock<std::mutex> lck(taskQueueMutex_);
61             taskQueueCond_.wait_for(lck, std::chrono::milliseconds(TASK_WAIT_TIME),
62                 [this]() { return !taskQueue_.empty(); });
63             if (taskQueue_.empty()) {
64                 continue;
65             }
66             Consume(task);
67         }
68         if (task == nullptr) {
69             continue;
70         }
71         task->Run();
72     }
73 }
74 
Consume(std::shared_ptr<TaskImplInterface> & task)75 void TaskQueue::Consume(std::shared_ptr<TaskImplInterface> &task)
76 {
77     task = taskQueue_.front();
78     taskQueue_.pop();
79 }
80 
Produce(std::shared_ptr<TaskImplInterface> & task)81 int32_t TaskQueue::Produce(std::shared_ptr<TaskImplInterface> &task)
82 {
83     if (task == nullptr) {
84         DHLOGE("The parameter is empty.");
85         return ERR_DH_AUDIO_NULLPTR;
86     }
87     std::lock_guard<std::mutex> lck(taskQueueMutex_);
88     if (taskQueue_.size() >= maxSize_) {
89         DHLOGD("task queue is full, size: %zu", taskQueue_.size());
90         return ERR_DH_AUDIO_SA_TASKQUEUE_FULL;
91     }
92     taskQueue_.push(task);
93     taskQueueCond_.notify_one();
94     return DH_SUCCESS;
95 }
96 } // DistributedHardware
97 } // OHOS
98