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
25namespace android {
26
27static sp<ISchedulingPolicyService> sSchedulingPolicyService;
28static const String16 _scheduling_policy("scheduling_policy");
29static Mutex sMutex;
30
31int 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
62}   // namespace android
63