1 /*
2 * Copyright (c) 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 <cstdlib>
17 #include <unistd.h>
18 #include "concurrent_task_log.h"
19 #include "qos_interface.h"
20 #include "qos.h"
21 using namespace OHOS::ConcurrentTask;
22
23 static constexpr int ERROR_NUM = -1;
24
25 namespace OHOS {
26 namespace QOS {
GetInstance()27 QosController& QosController::GetInstance()
28 {
29 static QosController instance;
30 return instance;
31 }
32
SetThreadQosForOtherThread(enum QosLevel level,int tid)33 int QosController::SetThreadQosForOtherThread(enum QosLevel level, int tid)
34 {
35 int qos = static_cast<int>(level);
36 if (level < QosLevel::QOS_BACKGROUND || level >= QosLevel::QOS_MAX) {
37 CONCUR_LOGE("[Qos] invalid qos level %{public}d", qos);
38 return ERROR_NUM;
39 }
40 int ret = QosApplyForOther(qos, tid);
41 if (ret == 0) {
42 CONCUR_LOGD("[Qos] qoslevel %{public}d apply for tid %{public}d success", qos, tid);
43 } else {
44 CONCUR_LOGE("[Qos] qoslevel %{public}d apply for tid %{public}d failure", qos, tid);
45 }
46
47 return ret;
48 }
49
ResetThreadQosForOtherThread(int tid)50 int QosController::ResetThreadQosForOtherThread(int tid)
51 {
52 int ret = QosLeaveForOther(tid);
53 if (ret == 0) {
54 CONCUR_LOGD("[Qos] qoslevel reset for tid %{public}d success", tid);
55 } else {
56 CONCUR_LOGE("[Qos] qoslevel reset for tid %{public}d failure", tid);
57 }
58
59 return ret;
60 }
61
SetThreadQos(enum QosLevel level)62 int SetThreadQos(enum QosLevel level)
63 {
64 int tid = gettid();
65 return QosController::GetInstance().SetThreadQosForOtherThread(level, tid);
66 }
67
SetQosForOtherThread(enum QosLevel level,int tid)68 int SetQosForOtherThread(enum QosLevel level, int tid)
69 {
70 return QosController::GetInstance().SetThreadQosForOtherThread(level, tid);
71 }
72
ResetThreadQos()73 int ResetThreadQos()
74 {
75 int tid = gettid();
76 return QosController::GetInstance().ResetThreadQosForOtherThread(tid);
77 }
78
ResetQosForOtherThread(int tid)79 int ResetQosForOtherThread(int tid)
80 {
81 return QosController::GetInstance().ResetThreadQosForOtherThread(tid);
82 }
83 } // namespace QOS
84 } // namespace OHOS
85