Home | History | Annotate | Download | only in android
      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 "Configuration.h"
     18 #include "utils/threads.h"
     19 
     20 //--------------------------------------------------------------------------------------------------
     21 namespace android {
     22 
     23 class CallbackProtector : public RefBase {
     24 
     25 public:
     26     CallbackProtector();
     27     virtual ~CallbackProtector();
     28 
     29     /**
     30      * Indicates whether the CallbackProtector is non-NULL and it's safe to enter the callback.
     31      */
     32     static bool enterCbIfOk(const sp<CallbackProtector> &protector);
     33 
     34     /**
     35      * Indicates whether it's safe to enter the callback. It would typically return false
     36      * if the associated object (AudioTrack, AudioPlayer, MediaPlayer) is about to be destroyed.
     37      */
     38     bool enterCb();
     39 
     40     /**
     41      * This method must be paired to each call to enterCb() or enterCbIfOk(),
     42      * only it returned that it is safe enter the callback;
     43      */
     44     void exitCb();
     45 
     46     /**
     47      * Called to signal the associated object is about to be destroyed, so whenever a callback is
     48      * entered (see enterCb) it will be notified it is pointless to process the callback. This will
     49      * return immediately if there are no callbacks, and will block until current callbacks exit.
     50      */
     51     void requestCbExitAndWait();
     52 
     53     /**
     54      * Similar to requestCbExitAndWait, but does not wait for current callbacks to exit.
     55      */
     56     void requestCbExit();
     57 
     58 private:
     59     Mutex mLock;
     60     Condition mCbExitedCondition;
     61 
     62     bool mSafeToEnterCb;
     63 
     64     /** Counts the number of callbacks actively locking the associated AudioPlayer */
     65     unsigned int mCbCount;
     66 
     67 #ifdef USE_DEBUG
     68     pthread_t mCallbackThread;
     69     pid_t mCallbackTid;
     70     pthread_t mRequesterThread;
     71     pid_t mRequesterTid;
     72 #endif
     73 
     74     // disallow "evil" constructors
     75     CallbackProtector(const CallbackProtector &);
     76     CallbackProtector &operator=(const CallbackProtector &);
     77 };
     78 
     79 } // namespace android
     80