16c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen/* 26c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * Copyright (C) 2011 The Android Open Source Project 36c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * 46c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * Licensed under the Apache License, Version 2.0 (the "License"); 56c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * you may not use this file except in compliance with the License. 66c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * You may obtain a copy of the License at 76c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * 86c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * http://www.apache.org/licenses/LICENSE-2.0 96c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * 106c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * Unless required by applicable law or agreed to in writing, software 116c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * distributed under the License is distributed on an "AS IS" BASIS, 126c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 136c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * See the License for the specific language governing permissions and 146c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen * limitations under the License. 156c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen */ 166c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 176c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#ifndef ANDROID_COMMON_CLOCK_SERVICE_H 186c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#define ANDROID_COMMON_CLOCK_SERVICE_H 196c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 20778c859bfdcce2fa6658c09e65656afc34d3b4e0Elliott Hughes#include <sys/socket.h> 21778c859bfdcce2fa6658c09e65656afc34d3b4e0Elliott Hughes#include <common_time/ICommonClock.h> 22778c859bfdcce2fa6658c09e65656afc34d3b4e0Elliott Hughes 236c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chennamespace android { 246c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 256c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chenclass CommonTimeServer; 266c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 276c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chenclass CommonClockService : public BnCommonClock, 286c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen public android::IBinder::DeathRecipient { 296c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen public: 306c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen static sp<CommonClockService> instantiate(CommonTimeServer& timeServer); 316c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 326c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t dump(int fd, const Vector<String16>& args); 336c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 346c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t isCommonTimeValid(bool* valid, uint32_t *timelineID); 356c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t commonTimeToLocalTime(int64_t common_time, 366c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen int64_t* local_time); 376c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t localTimeToCommonTime(int64_t local_time, 386c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen int64_t* common_time); 396c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getCommonTime(int64_t* common_time); 406c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getCommonFreq(uint64_t* freq); 416c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getLocalTime(int64_t* local_time); 426c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getLocalFreq(uint64_t* freq); 436c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getEstimatedError(int32_t* estimate); 446c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getTimelineID(uint64_t* id); 456c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getState(ICommonClock::State* state); 466c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t getMasterAddr(struct sockaddr_storage* addr); 476c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 486c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t registerListener( 496c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen const sp<ICommonClockListener>& listener); 506c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual status_t unregisterListener( 516c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen const sp<ICommonClockListener>& listener); 526c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 536c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen void notifyOnTimelineChanged(uint64_t timelineID); 546c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 556c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen private: 566c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen CommonClockService(CommonTimeServer& timeServer) 576c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen : mTimeServer(timeServer) { }; 586c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 596c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen virtual void binderDied(const wp<IBinder>& who); 606c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 616c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen CommonTimeServer& mTimeServer; 626c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 636c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // locks used to synchronize access to the list of registered listeners. 646c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // The callback lock is held whenever the list is used to perform callbacks 656c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // or while the list is being modified. The registration lock used to 666c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // serialize access across registerListener, unregisterListener, and 676c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // binderDied. 686c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // 696c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // The reason for two locks is that registerListener, unregisterListener, 706c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // and binderDied each call into the core service and obtain the core 716c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // service thread lock when they call reevaluateAutoDisableState. The core 726c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // service thread obtains the main thread lock whenever its thread is 736c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // running, and sometimes needs to call notifyOnTimelineChanged which then 746c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // obtains the callback lock. If callers of registration functions were 756c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // holding the callback lock when they called into the core service, we 766c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // would have a classic A/B, B/A ordering deadlock. To avoid this, the 776c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // registration functions hold the registration lock for the duration of 786c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // their call, but hold the callback lock only while they mutate the list. 796c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // This way, the list's size cannot change (because of the registration 806c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // lock) during the call into reevaluateAutoDisableState, but the core work 816c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // thread can still safely call notifyOnTimelineChanged while holding the 826c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen // main thread lock. 836c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen Mutex mCallbackLock; 846c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen Mutex mRegistrationLock; 856c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 866c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen Vector<sp<ICommonClockListener> > mListeners; 876c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen}; 886c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 896c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen}; // namespace android 906c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen 916c929510474caa14dc9d56826b2c65552861d6b3Mike J. Chen#endif // ANDROID_COMMON_CLOCK_SERVICE_H 92