• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2011 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 #include <common_time/ICommonClock.h>
18 
19 #ifndef ANDROID_COMMON_CLOCK_SERVICE_H
20 #define ANDROID_COMMON_CLOCK_SERVICE_H
21 
22 namespace android {
23 
24 class CommonTimeServer;
25 
26 class CommonClockService : public BnCommonClock,
27                            public android::IBinder::DeathRecipient {
28   public:
29     static sp<CommonClockService> instantiate(CommonTimeServer& timeServer);
30 
31     virtual status_t dump(int fd, const Vector<String16>& args);
32 
33     virtual status_t isCommonTimeValid(bool* valid, uint32_t *timelineID);
34     virtual status_t commonTimeToLocalTime(int64_t  common_time,
35                                            int64_t* local_time);
36     virtual status_t localTimeToCommonTime(int64_t  local_time,
37                                            int64_t* common_time);
38     virtual status_t getCommonTime(int64_t* common_time);
39     virtual status_t getCommonFreq(uint64_t* freq);
40     virtual status_t getLocalTime(int64_t* local_time);
41     virtual status_t getLocalFreq(uint64_t* freq);
42     virtual status_t getEstimatedError(int32_t* estimate);
43     virtual status_t getTimelineID(uint64_t* id);
44     virtual status_t getState(ICommonClock::State* state);
45     virtual status_t getMasterAddr(struct sockaddr_storage* addr);
46 
47     virtual status_t registerListener(
48             const sp<ICommonClockListener>& listener);
49     virtual status_t unregisterListener(
50             const sp<ICommonClockListener>& listener);
51 
52     void notifyOnTimelineChanged(uint64_t timelineID);
53 
54   private:
CommonClockService(CommonTimeServer & timeServer)55     CommonClockService(CommonTimeServer& timeServer)
56         : mTimeServer(timeServer) { };
57 
58     virtual void binderDied(const wp<IBinder>& who);
59 
60     CommonTimeServer& mTimeServer;
61 
62     // locks used to synchronize access to the list of registered listeners.
63     // The callback lock is held whenever the list is used to perform callbacks
64     // or while the list is being modified.  The registration lock used to
65     // serialize access across registerListener, unregisterListener, and
66     // binderDied.
67     //
68     // The reason for two locks is that registerListener, unregisterListener,
69     // and binderDied each call into the core service and obtain the core
70     // service thread lock when they call reevaluateAutoDisableState.  The core
71     // service thread obtains the main thread lock whenever its thread is
72     // running, and sometimes needs to call notifyOnTimelineChanged which then
73     // obtains the callback lock.  If callers of registration functions were
74     // holding the callback lock when they called into the core service, we
75     // would have a classic A/B, B/A ordering deadlock.  To avoid this, the
76     // registration functions hold the registration lock for the duration of
77     // their call, but hold the callback lock only while they mutate the list.
78     // This way, the list's size cannot change (because of the registration
79     // lock) during the call into reevaluateAutoDisableState, but the core work
80     // thread can still safely call notifyOnTimelineChanged while holding the
81     // main thread lock.
82     Mutex mCallbackLock;
83     Mutex mRegistrationLock;
84 
85     Vector<sp<ICommonClockListener> > mListeners;
86 };
87 
88 };  // namespace android
89 
90 #endif  // ANDROID_COMMON_CLOCK_SERVICE_H
91