1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "SchedulingPolicyService"
18 //#define LOG_NDEBUG 0
19 
20 #include <binder/IServiceManager.h>
21 #include <utils/Mutex.h>
22 #include "ISchedulingPolicyService.h"
23 #include "mediautils/SchedulingPolicyService.h"
24 
25 namespace android {
26 
27 static sp<ISchedulingPolicyService> sSchedulingPolicyService;
28 static const String16 _scheduling_policy("scheduling_policy");
29 static Mutex sMutex;
30 
requestPriority(pid_t pid,pid_t tid,int32_t prio,bool isForApp,bool asynchronous)31 int requestPriority(pid_t pid, pid_t tid, int32_t prio, bool isForApp, bool asynchronous)
32 {
33     // FIXME merge duplicated code related to service lookup, caching, and error recovery
34     int ret;
35     for (;;) {
36         sMutex.lock();
37         sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
38         sMutex.unlock();
39         if (sps == 0) {
40             sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
41             if (binder == 0) {
42                 sleep(1);
43                 continue;
44             }
45             sps = interface_cast<ISchedulingPolicyService>(binder);
46             sMutex.lock();
47             sSchedulingPolicyService = sps;
48             sMutex.unlock();
49         }
50         ret = sps->requestPriority(pid, tid, prio, isForApp, asynchronous);
51         if (ret != DEAD_OBJECT) {
52             break;
53         }
54         ALOGW("SchedulingPolicyService died");
55         sMutex.lock();
56         sSchedulingPolicyService.clear();
57         sMutex.unlock();
58     }
59     return ret;
60 }
61 
requestCpusetBoost(bool enable,const sp<IInterface> & client)62 int requestCpusetBoost(bool enable, const sp<IInterface> &client)
63 {
64     int ret;
65     sMutex.lock();
66     sp<ISchedulingPolicyService> sps = sSchedulingPolicyService;
67     sMutex.unlock();
68     if (sps == 0) {
69         sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
70         if (binder == 0) {
71             return DEAD_OBJECT;
72         }
73         sps = interface_cast<ISchedulingPolicyService>(binder);
74         sMutex.lock();
75         sSchedulingPolicyService = sps;
76         sMutex.unlock();
77     }
78     ret = sps->requestCpusetBoost(enable, client);
79     if (ret != DEAD_OBJECT) {
80         return ret;
81     }
82     ALOGW("SchedulingPolicyService died");
83     sMutex.lock();
84     sSchedulingPolicyService.clear();
85     sMutex.unlock();
86     return ret;
87 }
88 
89 }   // namespace android
90