From 3eb96005f8c5ae181dfcd554338a5744cc4c67df Mon Sep 17 00:00:00 2001 From: yupanwen Date: Thu, 28 Aug 2025 11:53:02 +0800 Subject: [PATCH] Add Function Signed-off-by: 136******92 Change-Id: I15c47cdd13bf09d381b300de6aa0de4c8c2484b4 --- .../native/audioutils/include/audio_utils.h | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/frameworks/native/audioutils/include/audio_utils.h b/frameworks/native/audioutils/include/audio_utils.h index 1c884eda97..f1e71d610d 100644 --- a/frameworks/native/audioutils/include/audio_utils.h +++ b/frameworks/native/audioutils/include/audio_utils.h @@ -15,7 +15,9 @@ #ifndef AUDIO_UTILS_H #define AUDIO_UTILS_H +#include #include +#include #include #include #include @@ -30,6 +32,8 @@ #include #include #include +#include +#include "audio_log.h" #include "securec.h" #include "audio_info.h" @@ -588,6 +592,32 @@ std::list> FromIpcInterrupts( const std::vector> &from); std::string GetBundleNameByToken(const uint32_t &tokenIdNum); + +template +void RunAsync(Func__&& func, Args__&&... args) +{ + std::thread th(func, args...); + pthread_setname_np(th.native_handle(), "OS_RUNASYNC"); + th.detach(); +} + +template +void TryAsync(const char *task, int32_t maxTry, int32_t delayMs, + std::function &&func, Args__&&... args) +{ + RunAsync([task, maxTry, delayMs](std::function &&func, Args__&&... args) { + for (int32_t i = 0; i < maxTry; ++i) { + if (i > 0) { + std::this_thread::sleep_for(std::chrono::milliseconds(delayMs)); + } + if (func()) { + return; + } + } + AUDIO_ERR_LOG("Try %{public}s over %{public}d times, failed", task, maxTry); + }, func, args...); +} + } // namespace AudioStandard } // namespace OHOS #endif // AUDIO_UTILS_H -- Gitee