ALooperRoster.cpp revision 3f27436a9346f043f52265da1e6a74cde2bffd4d
1/* 2 * Copyright (C) 2010 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//#define LOG_NDEBUG 0 18#define LOG_TAG "ALooperRoster" 19#include <utils/Log.h> 20#include <utils/String8.h> 21 22#include "ALooperRoster.h" 23 24#include "ADebug.h" 25#include "AHandler.h" 26#include "AMessage.h" 27 28namespace android { 29 30static bool verboseStats = false; 31 32ALooperRoster::ALooperRoster() 33 : mNextHandlerID(1) { 34} 35 36ALooper::handler_id ALooperRoster::registerHandler( 37 const sp<ALooper> looper, const sp<AHandler> &handler) { 38 Mutex::Autolock autoLock(mLock); 39 40 if (handler->id() != 0) { 41 CHECK(!"A handler must only be registered once."); 42 return INVALID_OPERATION; 43 } 44 45 HandlerInfo info; 46 info.mLooper = looper; 47 info.mHandler = handler; 48 ALooper::handler_id handlerID = mNextHandlerID++; 49 mHandlers.add(handlerID, info); 50 51 handler->setID(handlerID, looper); 52 53 return handlerID; 54} 55 56void ALooperRoster::unregisterHandler(ALooper::handler_id handlerID) { 57 Mutex::Autolock autoLock(mLock); 58 59 ssize_t index = mHandlers.indexOfKey(handlerID); 60 61 if (index < 0) { 62 return; 63 } 64 65 const HandlerInfo &info = mHandlers.valueAt(index); 66 67 sp<AHandler> handler = info.mHandler.promote(); 68 69 if (handler != NULL) { 70 handler->setID(0, NULL); 71 } 72 73 mHandlers.removeItemsAt(index); 74} 75 76void ALooperRoster::unregisterStaleHandlers() { 77 78 Vector<sp<ALooper> > activeLoopers; 79 { 80 Mutex::Autolock autoLock(mLock); 81 82 for (size_t i = mHandlers.size(); i-- > 0;) { 83 const HandlerInfo &info = mHandlers.valueAt(i); 84 85 sp<ALooper> looper = info.mLooper.promote(); 86 if (looper == NULL) { 87 ALOGV("Unregistering stale handler %d", mHandlers.keyAt(i)); 88 mHandlers.removeItemsAt(i); 89 } else { 90 // At this point 'looper' might be the only sp<> keeping 91 // the object alive. To prevent it from going out of scope 92 // and having ~ALooper call this method again recursively 93 // and then deadlocking because of the Autolock above, add 94 // it to a Vector which will go out of scope after the lock 95 // has been released. 96 activeLoopers.add(looper); 97 } 98 } 99 } 100} 101 102static void makeFourCC(uint32_t fourcc, char *s) { 103 s[0] = (fourcc >> 24) & 0xff; 104 if (s[0]) { 105 s[1] = (fourcc >> 16) & 0xff; 106 s[2] = (fourcc >> 8) & 0xff; 107 s[3] = fourcc & 0xff; 108 s[4] = 0; 109 } else { 110 sprintf(s, "%u", fourcc); 111 } 112} 113 114void ALooperRoster::dump(int fd, const Vector<String16>& args) { 115 bool clear = false; 116 bool oldVerbose = verboseStats; 117 for (size_t i = 0; i < args.size(); i++) { 118 if (args[i] == String16("-c")) { 119 clear = true; 120 } else if (args[i] == String16("-von")) { 121 verboseStats = true; 122 } else if (args[i] == String16("-voff")) { 123 verboseStats = false; 124 } 125 } 126 String8 s; 127 if (verboseStats && !oldVerbose) { 128 s.append("(verbose stats collection enabled, stats will be cleared)\n"); 129 } 130 131 Mutex::Autolock autoLock(mLock); 132 size_t n = mHandlers.size(); 133 s.appendFormat(" %zu registered handlers:\n", n); 134 135 for (size_t i = 0; i < n; i++) { 136 s.appendFormat(" %d: ", mHandlers.keyAt(i)); 137 HandlerInfo &info = mHandlers.editValueAt(i); 138 sp<ALooper> looper = info.mLooper.promote(); 139 if (looper != NULL) { 140 s.append(looper->getName()); 141 sp<AHandler> handler = info.mHandler.promote(); 142 if (handler != NULL) { 143 handler->mVerboseStats = verboseStats; 144 s.appendFormat(": %u messages processed", handler->mMessageCounter); 145 if (verboseStats) { 146 for (size_t j = 0; j < handler->mMessages.size(); j++) { 147 char fourcc[15]; 148 makeFourCC(handler->mMessages.keyAt(j), fourcc); 149 s.appendFormat("\n %s: %u", 150 fourcc, 151 handler->mMessages.valueAt(j)); 152 } 153 } else { 154 handler->mMessages.clear(); 155 } 156 if (clear || (verboseStats && !oldVerbose)) { 157 handler->mMessageCounter = 0; 158 handler->mMessages.clear(); 159 } 160 } else { 161 s.append(": <stale handler>"); 162 } 163 } else { 164 s.append("<stale>"); 165 } 166 s.append("\n"); 167 } 168 write(fd, s.string(), s.size()); 169} 170 171} // namespace android 172