1 /******************************************************************************
3 * Copyright (C) 2012 Broadcom Corporation
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 ******************************************************************************/
19 /************************************************************************************
21 * Filename: bt_utils.c
23 * Description: Miscellaneous helper functions
26 ***********************************************************************************/
28 #define LOG_TAG "bt_utils"
36 #include <sys/resource.h>
39 #include <utils/ThreadDefs.h>
40 #include <cutils/sched_policy.h>
43 #include "btcore/include/module.h"
44 #include "osi/include/compat.h"
45 #include "osi/include/log.h"
46 #include "osi/include/properties.h"
48 /*******************************************************************************
49 ** Type definitions for callback functions
50 ********************************************************************************/
51 static pthread_once_t g_DoSchedulingGroupOnce[TASK_HIGH_MAX];
52 static BOOLEAN g_DoSchedulingGroup[TASK_HIGH_MAX];
53 static pthread_mutex_t gIdxLock;
55 static int g_TaskIDs[TASK_HIGH_MAX];
56 #define INVALID_TASK_ID (-1)
58 static future_t *init(void) {
60 pthread_mutexattr_t lock_attr;
62 for(i = 0; i < TASK_HIGH_MAX; i++) {
63 g_DoSchedulingGroupOnce[i] = PTHREAD_ONCE_INIT;
64 g_DoSchedulingGroup[i] = TRUE;
65 g_TaskIDs[i] = INVALID_TASK_ID;
68 pthread_mutexattr_init(&lock_attr);
69 pthread_mutex_init(&gIdxLock, &lock_attr);
73 static future_t *clean_up(void) {
74 pthread_mutex_destroy(&gIdxLock);
78 EXPORT_SYMBOL const module_t bt_utils_module = {
79 .name = BT_UTILS_MODULE,
89 /*****************************************************************************
91 ** Function check_do_scheduling_group
93 ** Description check if it is ok to change schedule group
97 *******************************************************************************/
98 static void check_do_scheduling_group(void) {
99 char buf[PROPERTY_VALUE_MAX];
100 int len = osi_property_get("debug.sys.noschedgroups", buf, "");
103 if (sscanf(buf, "%d", &temp) == 1) {
104 g_DoSchedulingGroup[g_TaskIdx] = temp == 0;
109 /*****************************************************************************
111 ** Function raise_priority_a2dp
113 ** Description Raise task priority for A2DP streaming
117 *******************************************************************************/
118 void raise_priority_a2dp(tHIGH_PRIORITY_TASK high_task) {
121 int priority = ANDROID_PRIORITY_AUDIO;
123 pthread_mutex_lock(&gIdxLock);
124 g_TaskIdx = high_task;
126 // TODO(armansito): Remove this conditional check once we find a solution
127 // for system/core on non-Android platforms.
128 #if defined(OS_GENERIC)
130 #else // !defined(OS_GENERIC)
131 pthread_once(&g_DoSchedulingGroupOnce[g_TaskIdx], check_do_scheduling_group);
132 if (g_DoSchedulingGroup[g_TaskIdx]) {
133 // set_sched_policy does not support tid == 0
134 rc = set_sched_policy(tid, SP_AUDIO_SYS);
136 #endif // defined(OS_GENERIC)
138 g_TaskIDs[high_task] = tid;
139 pthread_mutex_unlock(&gIdxLock);
142 LOG_WARN(LOG_TAG, "failed to change sched policy, tid %d, err: %d", tid, errno);
145 // always use urgent priority for HCI worker thread until we can adjust
146 // its prio individually. All other threads can be dynamically adjusted voa
147 // adjust_priority_a2dp()
149 priority = ANDROID_PRIORITY_URGENT_AUDIO;
151 if (setpriority(PRIO_PROCESS, tid, priority) < 0) {
152 LOG_WARN(LOG_TAG, "failed to change priority tid: %d to %d", tid, priority);
156 /*****************************************************************************
158 ** Function adjust_priority_a2dp
160 ** Description increase the a2dp consumer task priority temporarily when start
161 ** audio playing, to avoid overflow the audio packet queue, restore
162 ** the a2dp consumer task priority when stop audio playing.
166 *******************************************************************************/
167 void adjust_priority_a2dp(int start) {
168 int priority = start ? ANDROID_PRIORITY_URGENT_AUDIO : ANDROID_PRIORITY_AUDIO;
172 for (i = 0; i < TASK_HIGH_MAX; i++)
175 if (tid != INVALID_TASK_ID)
177 if (setpriority(PRIO_PROCESS, tid, priority) < 0)
179 LOG_WARN(LOG_TAG, "failed to change priority tid: %d to %d", tid, priority);