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