2022-09-28 17:39:23 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2022 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-04-10 17:13:30 +00:00
|
|
|
#include <cstddef>
|
2022-11-08 14:45:07 +05:30
|
|
|
#include <memory>
|
2023-04-10 17:13:30 +00:00
|
|
|
|
2022-09-28 17:39:23 +00:00
|
|
|
#define LOG_TAG "AHAL_EffectThread"
|
|
|
|
|
#include <android-base/logging.h>
|
|
|
|
|
#include <pthread.h>
|
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
|
|
#include "effect-impl/EffectThread.h"
|
2023-04-10 17:13:30 +00:00
|
|
|
#include "effect-impl/EffectTypes.h"
|
|
|
|
|
|
2022-09-28 17:39:23 +00:00
|
|
|
namespace aidl::android::hardware::audio::effect {
|
|
|
|
|
|
|
|
|
|
EffectThread::~EffectThread() {
|
2022-10-14 01:07:47 +00:00
|
|
|
destroyThread();
|
2023-04-10 17:13:30 +00:00
|
|
|
}
|
2022-09-28 17:39:23 +00:00
|
|
|
|
2024-01-09 20:50:53 +00:00
|
|
|
RetCode EffectThread::createThread(const std::string& name, int priority) {
|
2022-09-28 17:39:23 +00:00
|
|
|
if (mThread.joinable()) {
|
2023-04-10 17:13:30 +00:00
|
|
|
LOG(WARNING) << mName << __func__ << " thread already created, no-op";
|
2022-09-28 17:39:23 +00:00
|
|
|
return RetCode::SUCCESS;
|
|
|
|
|
}
|
2024-01-09 20:50:53 +00:00
|
|
|
|
2022-09-28 17:39:23 +00:00
|
|
|
mName = name;
|
|
|
|
|
mPriority = priority;
|
2022-11-08 14:45:07 +05:30
|
|
|
{
|
|
|
|
|
std::lock_guard lg(mThreadMutex);
|
2023-09-20 11:43:17 +05:30
|
|
|
mStop = true;
|
|
|
|
|
mExit = false;
|
2022-11-08 14:45:07 +05:30
|
|
|
}
|
2023-04-10 17:13:30 +00:00
|
|
|
|
2022-09-28 17:39:23 +00:00
|
|
|
mThread = std::thread(&EffectThread::threadLoop, this);
|
2024-03-14 22:04:02 +00:00
|
|
|
LOG(VERBOSE) << mName << __func__ << " priority " << mPriority << " done";
|
2022-09-28 17:39:23 +00:00
|
|
|
return RetCode::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 01:07:47 +00:00
|
|
|
RetCode EffectThread::destroyThread() {
|
2022-09-28 17:39:23 +00:00
|
|
|
{
|
2022-11-08 14:45:07 +05:30
|
|
|
std::lock_guard lg(mThreadMutex);
|
2022-09-28 17:39:23 +00:00
|
|
|
mStop = mExit = true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-09 20:50:53 +00:00
|
|
|
mCv.notify_one();
|
2022-09-28 17:39:23 +00:00
|
|
|
if (mThread.joinable()) {
|
|
|
|
|
mThread.join();
|
|
|
|
|
}
|
2022-11-08 14:45:07 +05:30
|
|
|
|
2024-03-14 22:04:02 +00:00
|
|
|
LOG(VERBOSE) << mName << __func__;
|
2022-09-28 17:39:23 +00:00
|
|
|
return RetCode::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-14 01:07:47 +00:00
|
|
|
RetCode EffectThread::startThread() {
|
2023-04-10 17:13:30 +00:00
|
|
|
{
|
|
|
|
|
std::lock_guard lg(mThreadMutex);
|
|
|
|
|
mStop = false;
|
|
|
|
|
mCv.notify_one();
|
|
|
|
|
}
|
2022-09-28 17:39:23 +00:00
|
|
|
|
2024-03-14 22:04:02 +00:00
|
|
|
LOG(VERBOSE) << mName << __func__;
|
2023-04-10 17:13:30 +00:00
|
|
|
return RetCode::SUCCESS;
|
2023-02-03 01:44:32 +00:00
|
|
|
}
|
|
|
|
|
|
2023-04-10 17:13:30 +00:00
|
|
|
RetCode EffectThread::stopThread() {
|
2022-09-28 17:39:23 +00:00
|
|
|
{
|
2022-11-08 14:45:07 +05:30
|
|
|
std::lock_guard lg(mThreadMutex);
|
2023-04-10 17:13:30 +00:00
|
|
|
mStop = true;
|
|
|
|
|
mCv.notify_one();
|
2022-09-28 17:39:23 +00:00
|
|
|
}
|
2023-02-03 01:44:32 +00:00
|
|
|
|
2024-03-14 22:04:02 +00:00
|
|
|
LOG(VERBOSE) << mName << __func__;
|
2022-09-28 17:39:23 +00:00
|
|
|
return RetCode::SUCCESS;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void EffectThread::threadLoop() {
|
2022-11-08 14:45:07 +05:30
|
|
|
pthread_setname_np(pthread_self(), mName.substr(0, kMaxTaskNameLen - 1).c_str());
|
2022-09-28 17:39:23 +00:00
|
|
|
setpriority(PRIO_PROCESS, 0, mPriority);
|
|
|
|
|
while (true) {
|
2023-04-10 17:13:30 +00:00
|
|
|
{
|
|
|
|
|
std::unique_lock l(mThreadMutex);
|
|
|
|
|
::android::base::ScopedLockAssertion lock_assertion(mThreadMutex);
|
|
|
|
|
mCv.wait(l, [&]() REQUIRES(mThreadMutex) { return mExit || !mStop; });
|
|
|
|
|
if (mExit) {
|
2024-03-14 22:04:02 +00:00
|
|
|
LOG(VERBOSE) << mName << " threadLoop EXIT!";
|
2023-04-10 17:13:30 +00:00
|
|
|
return;
|
|
|
|
|
}
|
2022-09-28 17:39:23 +00:00
|
|
|
}
|
2024-01-09 20:50:53 +00:00
|
|
|
process();
|
2022-11-08 14:45:07 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-28 17:39:23 +00:00
|
|
|
} // namespace aidl::android::hardware::audio::effect
|