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 #ifndef A_LOOPER_H_ 18 19 #define A_LOOPER_H_ 20 21 #include <media/stagefright/foundation/ABase.h> 22 #include <media/stagefright/foundation/AString.h> 23 #include <utils/Errors.h> 24 #include <utils/KeyedVector.h> 25 #include <utils/List.h> 26 #include <utils/RefBase.h> 27 #include <utils/threads.h> 28 29 namespace android { 30 31 struct AHandler; 32 struct AMessage; 33 struct AReplyToken; 34 35 struct ALooper : public RefBase { 36 typedef int32_t event_id; 37 typedef int32_t handler_id; 38 39 ALooper(); 40 41 // Takes effect in a subsequent call to start(). 42 void setName(const char *name); 43 44 handler_id registerHandler(const sp<AHandler> &handler); 45 void unregisterHandler(handler_id handlerID); 46 47 status_t start( 48 bool runOnCallingThread = false, 49 bool canCallJava = false, 50 int32_t priority = PRIORITY_DEFAULT 51 ); 52 53 status_t stop(); 54 55 static int64_t GetNowUs(); 56 getNameALooper57 const char *getName() const { 58 return mName.c_str(); 59 } 60 61 protected: 62 // overridable by test harness 63 virtual int64_t getNowUs(); 64 65 virtual ~ALooper(); 66 67 private: 68 friend struct AMessage; // post() 69 70 struct Event { 71 int64_t mWhenUs; 72 sp<AMessage> mMessage; 73 sp<RefBase> mToken; 74 }; 75 76 Mutex mLock; 77 Condition mQueueChangedCondition; 78 79 AString mName; 80 81 List<Event> mEventQueue; 82 83 struct LooperThread; 84 sp<LooperThread> mThread; 85 bool mRunningLocally; 86 87 // use a separate lock for reply handling, as it is always on another thread 88 // use a central lock, however, to avoid creating a mutex for each reply 89 Mutex mRepliesLock; 90 Condition mRepliesCondition; 91 92 // START --- methods used only by AMessage 93 94 // Posts a message on this looper with the given timeout. 95 void post(const sp<AMessage> &msg, int64_t delayUs); 96 97 // Post a message uniquely on this looper with the given timeout. 98 // This method ensures that there is exactly one message with the same token pending posted on 99 // this looper after the call returns. A null token will result in an EINVAL error status. 100 status_t postUnique(const sp<AMessage> &msg, const sp<RefBase> &token, int64_t delayUs); 101 102 // creates a reply token to be used with this looper 103 sp<AReplyToken> createReplyToken(); 104 // waits for a response for the reply token. If status is OK, the response 105 // is stored into the supplied variable. Otherwise, it is unchanged. 106 status_t awaitResponse(const sp<AReplyToken> &replyToken, sp<AMessage> *response); 107 // posts a reply for a reply token. If the reply could be successfully posted, 108 // it returns OK. Otherwise, it returns an error value. 109 status_t postReply(const sp<AReplyToken> &replyToken, const sp<AMessage> &msg); 110 111 // END --- methods used only by AMessage 112 113 bool loop(); 114 115 DISALLOW_EVIL_CONSTRUCTORS(ALooper); 116 }; 117 118 } // namespace android 119 120 #endif // A_LOOPER_H_ 121