• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 #ifndef LOG_TAG
17 #define LOG_TAG "AudioThreadHandler"
18 #endif
19 
20 #include "thread_handler.h"
21 
22 #include <future>
23 #include <unistd.h>
24 
25 #include "audio_effect_log.h"
26 #include "audio_schedule.h"
27 #include "nocopyable.h"
28 #include "thread_pool.h"
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 namespace {
33 const int32_t THREAD_NUM = 1;
34 const size_t MAX_TASK_NUM = 5;
35 } // namespace
36 
37 class ThreadHandlerImpl final : public ThreadHandler, public NoCopyable {
38 public:
39     explicit ThreadHandlerImpl(const std::string &threadName);
40     ~ThreadHandlerImpl() override;
41     void PostTask(const Task &task) override;
42     void EnsureTask(const Task &task) override;
43 
44 private:
45     std::shared_ptr<OHOS::ThreadPool> threadPool_ { nullptr };
46 };
47 
ThreadHandlerImpl(const std::string & threadName)48 ThreadHandlerImpl::ThreadHandlerImpl(const std::string &threadName)
49 {
50     threadPool_ = std::make_shared<OHOS::ThreadPool>(threadName);
51     if (threadPool_ == nullptr) {
52         AUDIO_ERR_LOG("create thread fail");
53         return;
54     }
55     threadPool_->Start(THREAD_NUM);
56     threadPool_->SetMaxTaskNum(MAX_TASK_NUM);
57 }
58 
~ThreadHandlerImpl()59 ThreadHandlerImpl::~ThreadHandlerImpl()
60 {
61     auto task = []() {
62         UnscheduleThreadInServer(getpid(), gettid());
63     };
64     threadPool_->AddTask(task);
65     threadPool_->Stop();
66     AUDIO_INFO_LOG("destroy thread handler succ");
67 }
68 
PostTask(const Task & task)69 void ThreadHandlerImpl::PostTask(const Task &task)
70 {
71     threadPool_->AddTask(task);
72 }
73 
EnsureTask(const Task & task)74 void ThreadHandlerImpl::EnsureTask(const Task &task)
75 {
76     std::promise<void> ensure;
77     auto callback = [&ensure]() {
78         ensure.set_value();
79         return;
80     };
81     threadPool_->AddTask(task);
82     threadPool_->AddTask(callback);
83     ensure.get_future().get();
84 }
85 
NewInstance(const std::string & threadName)86 std::shared_ptr<ThreadHandler> ThreadHandler::NewInstance(const std::string &threadName)
87 {
88     auto handler = std::make_shared<ThreadHandlerImpl>(threadName);
89     if (handler == nullptr) {
90         AUDIO_ERR_LOG("create thread handler impl fail");
91         return nullptr;
92     }
93 
94     auto task = []() {
95         ScheduleThreadInServer(getpid(), gettid());
96     };
97     handler->PostTask(task);
98 
99     return handler;
100 }
101 } // namespace AudioStandard
102 } // namespace OHOS