11dc28b794587be22c90a97070d928f94586db638Glenn Kasten/*
21dc28b794587be22c90a97070d928f94586db638Glenn Kasten * Copyright (C) 2012 The Android Open Source Project
31dc28b794587be22c90a97070d928f94586db638Glenn Kasten *
41dc28b794587be22c90a97070d928f94586db638Glenn Kasten * Licensed under the Apache License, Version 2.0 (the "License");
51dc28b794587be22c90a97070d928f94586db638Glenn Kasten * you may not use this file except in compliance with the License.
61dc28b794587be22c90a97070d928f94586db638Glenn Kasten * You may obtain a copy of the License at
71dc28b794587be22c90a97070d928f94586db638Glenn Kasten *
81dc28b794587be22c90a97070d928f94586db638Glenn Kasten *      http://www.apache.org/licenses/LICENSE-2.0
91dc28b794587be22c90a97070d928f94586db638Glenn Kasten *
101dc28b794587be22c90a97070d928f94586db638Glenn Kasten * Unless required by applicable law or agreed to in writing, software
111dc28b794587be22c90a97070d928f94586db638Glenn Kasten * distributed under the License is distributed on an "AS IS" BASIS,
121dc28b794587be22c90a97070d928f94586db638Glenn Kasten * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131dc28b794587be22c90a97070d928f94586db638Glenn Kasten * See the License for the specific language governing permissions and
141dc28b794587be22c90a97070d928f94586db638Glenn Kasten * limitations under the License.
151dc28b794587be22c90a97070d928f94586db638Glenn Kasten */
161dc28b794587be22c90a97070d928f94586db638Glenn Kasten
171dc28b794587be22c90a97070d928f94586db638Glenn Kasten#include <binder/IServiceManager.h>
181dc28b794587be22c90a97070d928f94586db638Glenn Kasten#include <utils/Mutex.h>
191dc28b794587be22c90a97070d928f94586db638Glenn Kasten#include "ISchedulingPolicyService.h"
201dc28b794587be22c90a97070d928f94586db638Glenn Kasten#include "SchedulingPolicyService.h"
211dc28b794587be22c90a97070d928f94586db638Glenn Kasten
221dc28b794587be22c90a97070d928f94586db638Glenn Kastennamespace android {
231dc28b794587be22c90a97070d928f94586db638Glenn Kasten
241dc28b794587be22c90a97070d928f94586db638Glenn Kastenstatic sp<ISchedulingPolicyService> sSchedulingPolicyService;
251dc28b794587be22c90a97070d928f94586db638Glenn Kastenstatic const String16 _scheduling_policy("scheduling_policy");
261dc28b794587be22c90a97070d928f94586db638Glenn Kastenstatic Mutex sMutex;
271dc28b794587be22c90a97070d928f94586db638Glenn Kasten
281dc28b794587be22c90a97070d928f94586db638Glenn Kastenint requestPriority(pid_t pid, pid_t tid, int32_t prio)
291dc28b794587be22c90a97070d928f94586db638Glenn Kasten{
301dc28b794587be22c90a97070d928f94586db638Glenn Kasten    // FIXME merge duplicated code related to service lookup, caching, and error recovery
311dc28b794587be22c90a97070d928f94586db638Glenn Kasten    sp<ISchedulingPolicyService> sps;
321dc28b794587be22c90a97070d928f94586db638Glenn Kasten    for (;;) {
331dc28b794587be22c90a97070d928f94586db638Glenn Kasten        sMutex.lock();
341dc28b794587be22c90a97070d928f94586db638Glenn Kasten        sps = sSchedulingPolicyService;
351dc28b794587be22c90a97070d928f94586db638Glenn Kasten        sMutex.unlock();
361dc28b794587be22c90a97070d928f94586db638Glenn Kasten        if (sps != 0) {
371dc28b794587be22c90a97070d928f94586db638Glenn Kasten            break;
381dc28b794587be22c90a97070d928f94586db638Glenn Kasten        }
391dc28b794587be22c90a97070d928f94586db638Glenn Kasten        sp<IBinder> binder = defaultServiceManager()->checkService(_scheduling_policy);
401dc28b794587be22c90a97070d928f94586db638Glenn Kasten        if (binder != 0) {
411dc28b794587be22c90a97070d928f94586db638Glenn Kasten            sps = interface_cast<ISchedulingPolicyService>(binder);
421dc28b794587be22c90a97070d928f94586db638Glenn Kasten            sMutex.lock();
431dc28b794587be22c90a97070d928f94586db638Glenn Kasten            sSchedulingPolicyService = sps;
441dc28b794587be22c90a97070d928f94586db638Glenn Kasten            sMutex.unlock();
451dc28b794587be22c90a97070d928f94586db638Glenn Kasten            break;
461dc28b794587be22c90a97070d928f94586db638Glenn Kasten        }
471dc28b794587be22c90a97070d928f94586db638Glenn Kasten        sleep(1);
481dc28b794587be22c90a97070d928f94586db638Glenn Kasten    }
491dc28b794587be22c90a97070d928f94586db638Glenn Kasten    return sps->requestPriority(pid, tid, prio);
501dc28b794587be22c90a97070d928f94586db638Glenn Kasten}
511dc28b794587be22c90a97070d928f94586db638Glenn Kasten
521dc28b794587be22c90a97070d928f94586db638Glenn Kasten}   // namespace android
53