183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi/* 283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project 383ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * 483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); 583ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * you may not use this file except in compliance with the License. 683ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * You may obtain a copy of the License at 783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * 883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0 983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * 1083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * Unless required by applicable law or agreed to in writing, software 1183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, 1283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1383ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * See the License for the specific language governing permissions and 1483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * limitations under the License. 1583ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi */ 1683ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 1710d8b6a5f9a7c24203e3ee228ec596d03c1aa78dGlenn Kasten#include "Configuration.h" 1883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi#include "utils/threads.h" 1983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 2083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi//-------------------------------------------------------------------------------------------------- 2183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivinamespace android { 2283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 236cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Triviclass CallbackProtector : public RefBase { 2483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 2583ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivipublic: 266cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi CallbackProtector(); 276cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi virtual ~CallbackProtector(); 2883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 2983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi /** 30485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten * Indicates whether the CallbackProtector is non-NULL and it's safe to enter the callback. 316cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi */ 326cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi static bool enterCbIfOk(const sp<CallbackProtector> &protector); 336cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi 346cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi /** 35485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten * Indicates whether it's safe to enter the callback. It would typically return false 36485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten * if the associated object (AudioTrack, AudioPlayer, MediaPlayer) is about to be destroyed. 3783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi */ 3883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi bool enterCb(); 3983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 4083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi /** 416cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi * This method must be paired to each call to enterCb() or enterCbIfOk(), 426cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi * only it returned that it is safe enter the callback; 4383ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi */ 4483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi void exitCb(); 4583ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 4683ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi /** 47485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten * Called to signal the associated object is about to be destroyed, so whenever a callback is 4883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * entered (see enterCb) it will be notified it is pointless to process the callback. This will 4983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi * return immediately if there are no callbacks, and will block until current callbacks exit. 5083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi */ 5183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi void requestCbExitAndWait(); 5283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 53f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten /** 54f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten * Similar to requestCbExitAndWait, but does not wait for current callbacks to exit. 55f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten */ 56f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten void requestCbExit(); 57f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten 58485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kastenprivate: 5983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi Mutex mLock; 6083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi Condition mCbExitedCondition; 6183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 6283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi bool mSafeToEnterCb; 6383ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 646cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi /** Counts the number of callbacks actively locking the associated AudioPlayer */ 6583ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi unsigned int mCbCount; 666cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi 67f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten#ifdef USE_DEBUG 68f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten pthread_t mCallbackThread; 69f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten pid_t mCallbackTid; 70f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten pthread_t mRequesterThread; 71f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten pid_t mRequesterTid; 72f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten#endif 73f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten 746cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi // disallow "evil" constructors 756cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi CallbackProtector(const CallbackProtector &); 766cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi CallbackProtector &operator=(const CallbackProtector &); 7783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi}; 7883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi 7983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi} // namespace android 80