From ea2927147f717efd31639dee9c2582fb750d6e9c Mon Sep 17 00:00:00 2001 From: liuchungang Date: Thu, 9 May 2024 11:38:48 +0800 Subject: [PATCH 1/2] GetThreadQosForOtherThread direct return when encountering error Signed-off-by: liuchungang --- frameworks/native/qos_ndk.cpp | 39 +++++++++-------------------------- interfaces/inner_api/qos.h | 1 + qos/qos.cpp | 21 ++++++++++++------- 3 files changed, 25 insertions(+), 36 deletions(-) diff --git a/frameworks/native/qos_ndk.cpp b/frameworks/native/qos_ndk.cpp index 46b0abc..a88baee 100644 --- a/frameworks/native/qos_ndk.cpp +++ b/frameworks/native/qos_ndk.cpp @@ -1,3 +1,4 @@ + /* * Copyright (c) 2024 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); @@ -23,37 +24,17 @@ static constexpr int ERROR_NUM = -1; using namespace OHOS::QOS; using namespace std; -const unordered_map qos_map = { - {QoS_Level::QOS_BACKGROUND, QosLevel::QOS_BACKGROUND}, - {QoS_Level::QOS_UTILITY, QosLevel::QOS_UTILITY}, - {QoS_Level::QOS_DEFAULT, QosLevel::QOS_DEFAULT}, - {QoS_Level::QOS_USER_INITIATED, QosLevel::QOS_USER_INITIATED}, - {QoS_Level::QOS_DEADLINE_REQUEST, QosLevel::QOS_DEADLINE_REQUEST}, - {QoS_Level::QOS_USER_INTERACTIVE, QosLevel::QOS_USER_INTERACTIVE} -}; - -const unordered_map qos_reverse_map = { - {QosLevel::QOS_BACKGROUND, QoS_Level::QOS_BACKGROUND}, - {QosLevel::QOS_UTILITY, QoS_Level::QOS_UTILITY}, - {QosLevel::QOS_DEFAULT, QoS_Level::QOS_DEFAULT}, - {QosLevel::QOS_USER_INITIATED, QoS_Level::QOS_USER_INITIATED}, - {QosLevel::QOS_DEADLINE_REQUEST, QoS_Level::QOS_DEADLINE_REQUEST}, - {QosLevel::QOS_USER_INTERACTIVE, QoS_Level::QOS_USER_INTERACTIVE} -}; - - int OH_QoS_SetThreadQoS(QoS_Level level) { - auto iter = qos_map.find(level); - if (iter == qos_map.end()) { + if (level < QOS_BACKGROUND || level > QOS_USER_INTERACTIVE) { return ERROR_NUM; } - return QosController::GetInstance().SetThreadQosForOtherThread(iter->second, gettid()); + return SetThreadQos(static_cast(level)); } -int OH_QoS_ResetThreadQoS(void) +int OH_QoS_ResetThreadQoS() { - return QosController::GetInstance().ResetThreadQosForOtherThread(gettid()); + return ResetThreadQos(); } int OH_QoS_GetThreadQoS(QoS_Level *level) @@ -61,15 +42,15 @@ int OH_QoS_GetThreadQoS(QoS_Level *level) if (level == nullptr) { return ERROR_NUM; } - enum QosLevel qosLevel; - int ret = QosController::GetInstance().GetThreadQosForOtherThread(qosLevel, gettid()); + QosLevel qosLevel; + int ret = GetThreadQos(qosLevel); if (ret < 0) { return ERROR_NUM; } - auto iter = qos_reverse_map.find(qosLevel); - if (iter == qos_reverse_map.end()) { + if (static_cast(qosLevel) < QoS_Level::QOS_BACKGROUND || + static_cast(qosLevel) > QoS_Level::QOS_USER_INTERACTIVE) { return ERROR_NUM; } - *level = iter->second; + *level = static_cast(qosLevel); return 0; } \ No newline at end of file diff --git a/interfaces/inner_api/qos.h b/interfaces/inner_api/qos.h index e8d3724..d011f44 100644 --- a/interfaces/inner_api/qos.h +++ b/interfaces/inner_api/qos.h @@ -52,6 +52,7 @@ int SetQosForOtherThread(enum QosLevel level, int tid); int ResetThreadQos(); int ResetQosForOtherThread(int tid); int GetThreadQos(enum QosLevel &level); +int GetQosForOtherThread(enum QosLevel &level, int tid); } // namespace QOS } // namespace OHOS diff --git a/qos/qos.cpp b/qos/qos.cpp index fc928a6..c5a3b67 100644 --- a/qos/qos.cpp +++ b/qos/qos.cpp @@ -72,19 +72,21 @@ int QosController::ResetThreadQosForOtherThread(int tid) int QosController::GetThreadQosForOtherThread(enum QosLevel &level, int tid) { - int qos; + int qos = -1; int ret = QosGetForOther(tid, qos); if (ret == 0) { + if (qos < static_cast(QosLevel::QOS_BACKGROUND) || + qos >= static_cast(QosLevel::QOS_MAX)) { + CONCUR_LOGE("[Qos] not set qoslevel for tid %{public}d", tid); + return ERROR_NUM; + } CONCUR_LOGD("[Qos] qoslevel get for tid %{public}d success", tid); + level = static_cast(qos); + return ret; } else { CONCUR_LOGE("[Qos] qoslevel get for tid %{public}d failure", tid); + return ret; } - if (qos < static_cast(QosLevel::QOS_BACKGROUND) || qos >= static_cast(QosLevel::QOS_MAX)) { - return ERROR_NUM; - } - level = static_cast(qos); - - return ret; } int SetThreadQos(enum QosLevel level) @@ -113,5 +115,10 @@ int GetThreadQos(enum QosLevel &level) { return QosController::GetInstance().GetThreadQosForOtherThread(level, gettid()); } + +int GetQosForOtherThread(enum QosLevel &level, int tid) +{ + return QosController::GetInstance().GetThreadQosForOtherThread(level, tid); +} } // namespace QOS } // namespace OHOS -- Gitee From 252ffa1d715791c9c17a03060b508e7ce23973ed Mon Sep 17 00:00:00 2001 From: Ethan Date: Tue, 28 May 2024 03:39:47 +0000 Subject: [PATCH 2/2] GetThreadQosForOtherThread direct return when encountering error Signed-off-by: Ethan --- frameworks/native/qos_ndk.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/native/qos_ndk.cpp b/frameworks/native/qos_ndk.cpp index a88baee..4523b00 100644 --- a/frameworks/native/qos_ndk.cpp +++ b/frameworks/native/qos_ndk.cpp @@ -32,7 +32,7 @@ int OH_QoS_SetThreadQoS(QoS_Level level) return SetThreadQos(static_cast(level)); } -int OH_QoS_ResetThreadQoS() +int OH_QoS_ResetThreadQoS(void) { return ResetThreadQos(); } -- Gitee