CallbackProtector.cpp revision 6cce136651f6fd2c7aecd45bc553270152d75462
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 "CallbackProtector.h" 18 19#include <media/stagefright/foundation/ADebug.h> 20 21//-------------------------------------------------------------------------------------------------- 22namespace android { 23 24 25CallbackProtector::CallbackProtector() : RefBase(), 26 mSafeToEnterCb(true), 27 mCbCount(0) { 28} 29 30 31CallbackProtector::~CallbackProtector() { 32 33} 34 35 36bool CallbackProtector::enterCbIfOk(const sp<CallbackProtector> &protector) { 37 if (protector != 0) { 38 return protector->enterCb(); 39 } else { 40 return false; 41 } 42} 43 44 45bool CallbackProtector::enterCb() { 46 Mutex::Autolock _l(mLock); 47 if (mSafeToEnterCb) { 48 mCbCount++; 49 } 50 return mSafeToEnterCb; 51} 52 53 54void CallbackProtector::exitCb() { 55 Mutex::Autolock _l(mLock); 56 57 CHECK(mCbCount > 0); 58 mCbCount--; 59 60 if (mCbCount == 0) { 61 mCbExitedCondition.broadcast(); 62 } 63} 64 65 66void CallbackProtector::requestCbExitAndWait() { 67 Mutex::Autolock _l(mLock); 68 mSafeToEnterCb = false; 69 while (mCbCount) { 70 mCbExitedCondition.wait(mLock); 71 } 72} 73 74 75} // namespace android 76