diff options
author | John Grossman <johngro@google.com> | 2012-02-05 13:36:33 -0800 |
---|---|---|
committer | John Grossman <johngro@google.com> | 2012-02-06 18:02:33 -0800 |
commit | 36d372fb6a35c65a441123ef31e14c086b3b4db8 (patch) | |
tree | 80a3ae13742451980270540c51f3c55849034c59 /libs | |
parent | 7d6934f3eb30fbcd41e753fa3bec38ed75c30925 (diff) | |
download | frameworks_base-36d372fb6a35c65a441123ef31e14c086b3b4db8.zip frameworks_base-36d372fb6a35c65a441123ef31e14c086b3b4db8.tar.gz frameworks_base-36d372fb6a35c65a441123ef31e14c086b3b4db8.tar.bz2 |
Explicitly manage common clock client lifetimes.
Change the CCHelper class to be an instanced instead of a static
pattern. The CCHelper instances all share an interface to the common
clock service and register/unregister a callback handler in response
to there being CCHelper instance in the system or not. This brings
usage of the CCHelper into like with the new auto-disable
functionality of the common time service. For any given process,
whenever there are CCHelper instances active, the process will
maintain a callback target to the common clock service and will be
considered to be an active client.
Also change all of the users of the CCHelper interface to manage the
lifecycle of their new CCHelper instances.
Change-Id: I7c28c5d70d9b07ba7407b4ac706e7e7d7253001b
Diffstat (limited to 'libs')
-rw-r--r-- | libs/common_time/cc_helper.cpp | 61 |
1 files changed, 54 insertions, 7 deletions
diff --git a/libs/common_time/cc_helper.cpp b/libs/common_time/cc_helper.cpp index 294a855..8d8556c 100644 --- a/libs/common_time/cc_helper.cpp +++ b/libs/common_time/cc_helper.cpp @@ -24,17 +24,66 @@ namespace android { Mutex CCHelper::lock_; sp<ICommonClock> CCHelper::common_clock_; +sp<ICommonClockListener> CCHelper::common_clock_listener_; +uint32_t CCHelper::ref_count_ = 0; bool CCHelper::verifyClock_l() { + bool ret = false; + if (common_clock_ == NULL) { common_clock_ = ICommonClock::getInstance(); if (common_clock_ == NULL) - return false; + goto bailout; } - return true; + if (ref_count_ > 0) { + if (common_clock_listener_ == NULL) { + common_clock_listener_ = new CommonClockListener(); + if (common_clock_listener_ == NULL) + goto bailout; + + if (OK != common_clock_->registerListener(common_clock_listener_)) + goto bailout; + } + } + + ret = true; + +bailout: + if (!ret) { + common_clock_listener_ = NULL; + common_clock_ = NULL; + } + return ret; +} + +CCHelper::CCHelper() { + Mutex::Autolock lock(&lock_); + ref_count_++; + verifyClock_l(); } +CCHelper::~CCHelper() { + Mutex::Autolock lock(&lock_); + + assert(ref_count_ > 0); + ref_count_--; + + // If we were the last CCHelper instance in the system, and we had + // previously register a listener, unregister it now so that the common time + // service has the chance to go into auto-disabled mode. + if (!ref_count_ && + (common_clock_ != NULL) && + (common_clock_listener_ != NULL)) { + common_clock_->unregisterListener(common_clock_listener_); + common_clock_listener_ = NULL; + } +} + +void CCHelper::CommonClockListener::onTimelineChanged(uint64_t timelineID) { + // do nothing; listener is only really used as a token so the server can + // find out when clients die. +} // Helper methods which attempts to make calls to the common time binder // service. If the first attempt fails with DEAD_OBJECT, the helpers will @@ -43,20 +92,18 @@ bool CCHelper::verifyClock_l() { // If the second attempt fails, or no connection can be made, the we let the // error propagate up the stack and let the caller deal with the situation as // best they can. - #define CCHELPER_METHOD(decl, call) \ status_t CCHelper::decl { \ Mutex::Autolock lock(&lock_); \ \ if (!verifyClock_l()) \ - return DEAD_OBJECT; \ + return DEAD_OBJECT; \ \ status_t status = common_clock_->call; \ if (DEAD_OBJECT == status) { \ - common_clock_ = NULL; \ if (!verifyClock_l()) \ - return DEAD_OBJECT; \ - status_t status = common_clock_->call; \ + return DEAD_OBJECT; \ + status = common_clock_->call; \ } \ \ return status; \ |