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
1783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi#include "utils/threads.h"
1883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
1983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi//--------------------------------------------------------------------------------------------------
2083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivinamespace android {
2183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
226cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Triviclass CallbackProtector : public RefBase {
2383ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
2483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivipublic:
256cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    CallbackProtector();
266cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    virtual ~CallbackProtector();
2783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
2883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    /**
29485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten     * Indicates whether the CallbackProtector is non-NULL and it's safe to enter the callback.
306cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi     */
316cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    static bool enterCbIfOk(const sp<CallbackProtector> &protector);
326cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi
336cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    /**
34485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten     * Indicates whether it's safe to enter the callback. It would typically return false
35485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten     * if the associated object (AudioTrack, AudioPlayer, MediaPlayer) is about to be destroyed.
3683ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi     */
3783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    bool enterCb();
3883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
3983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    /**
406cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi     * This method must be paired to each call to enterCb() or enterCbIfOk(),
416cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi     * only it returned that it is safe enter the callback;
4283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi     */
4383ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    void exitCb();
4483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
4583ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    /**
46485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kasten     * Called to signal the associated object is about to be destroyed, so whenever a callback is
4783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi     * entered (see enterCb) it will be notified it is pointless to process the callback. This will
4883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi     * return immediately if there are no callbacks, and will block until current callbacks exit.
4983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi     */
5083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    void requestCbExitAndWait();
5183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
52f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten    /**
53f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten     * Similar to requestCbExitAndWait, but does not wait for current callbacks to exit.
54f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten     */
55f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten    void requestCbExit();
56f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten
57485a038f9f0f898227b8ab4218e94c5d56b6ed0bGlenn Kastenprivate:
5883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    Mutex mLock;
5983ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    Condition mCbExitedCondition;
6083ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
6183ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    bool mSafeToEnterCb;
6283ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
636cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    /** Counts the number of callbacks actively locking the associated AudioPlayer */
6483ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi    unsigned int mCbCount;
656cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi
66f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten#ifdef USE_DEBUG
67f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten    pthread_t mCallbackThread;
68f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten    pid_t mCallbackTid;
69f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten    pthread_t mRequesterThread;
70f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten    pid_t mRequesterTid;
71f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten#endif
72f4b45a37248899ae2d27bb172f8387fbf1edff8eGlenn Kasten
736cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    // disallow "evil" constructors
746cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    CallbackProtector(const CallbackProtector &);
756cce136651f6fd2c7aecd45bc553270152d75462Jean-Michel Trivi    CallbackProtector &operator=(const CallbackProtector &);
7683ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi};
7783ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi
7883ac345e264c1e22b7a2f1a110b2fe92473394ecJean-Michel Trivi} // namespace android
79