Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5804a76a authored by Lajos Molnar's avatar Lajos Molnar
Browse files

stagefright: use handler instead of handler-id in AMessage

This avoids locking gLooperRoster mutex on post() and deliver().

Bug: 19607784
Change-Id: If6d9d7884dbb08fc390983bda896d223803476ba
parent c10e7f12
Loading
Loading
Loading
Loading
+21 −3
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ struct AMessage;
struct AHandler : public RefBase {
    AHandler()
        : mID(0),
          mVerboseStats(false),
          mMessageCounter(0) {
    }

@@ -36,23 +37,40 @@ struct AHandler : public RefBase {
        return mID;
    }

    sp<ALooper> looper();
    sp<ALooper> looper() const {
        return mLooper.promote();
    }

    wp<ALooper> getLooper() const {
        return mLooper;
    }

    wp<AHandler> getHandler() const {
        // allow getting a weak reference to a const handler
        return const_cast<AHandler *>(this);
    }

protected:
    virtual void onMessageReceived(const sp<AMessage> &msg) = 0;

private:
    friend struct ALooperRoster;
    friend struct AMessage;      // deliverMessage()
    friend struct ALooperRoster; // setID()

    ALooper::handler_id mID;
    wp<ALooper> mLooper;

    void setID(ALooper::handler_id id) {
    inline void setID(ALooper::handler_id id, wp<ALooper> looper) {
        mID = id;
        mLooper = looper;
    }

    bool mVerboseStats;
    uint32_t mMessageCounter;
    KeyedVector<uint32_t, uint32_t> mMessages;

    void deliverMessage(const sp<AMessage> &msg);

    DISALLOW_EVIL_CONSTRUCTORS(AHandler);
};

+6 −2
Original line number Diff line number Diff line
@@ -53,11 +53,15 @@ struct ALooper : public RefBase {

    static int64_t GetNowUs();

    const char *getName() const {
        return mName.c_str();
    }

protected:
    virtual ~ALooper();

private:
    friend struct ALooperRoster;
    friend struct AMessage;       // post()

    struct Event {
        int64_t mWhenUs;
+2 −4
Original line number Diff line number Diff line
@@ -33,15 +33,13 @@ struct ALooperRoster {
    void unregisterHandler(ALooper::handler_id handlerID);
    void unregisterStaleHandlers();

    status_t postMessage(const sp<AMessage> &msg, int64_t delayUs = 0);
    void deliverMessage(const sp<AMessage> &msg);

    status_t postAndAwaitResponse(
            const sp<AMessage> &msg, sp<AMessage> *response);

    void postReply(uint32_t replyID, const sp<AMessage> &reply);

    sp<ALooper> findLooper(ALooper::handler_id handlerID);
    void getHandlerAndLooper(
            ALooper::handler_id handlerID, wp<AHandler> *handler, wp<ALooper> *looper);

    void dump(int fd, const Vector<String16>& args);

+15 −3
Original line number Diff line number Diff line
@@ -26,11 +26,14 @@
namespace android {

struct ABuffer;
struct AHandler;
struct AString;
struct Parcel;

struct AMessage : public RefBase {
    AMessage(uint32_t what = 0, ALooper::handler_id target = 0);
    AMessage();
    AMessage(uint32_t what, ALooper::handler_id target = 0);
    AMessage(uint32_t what, const sp<const AHandler> &handler);

    static sp<AMessage> FromParcel(const Parcel &parcel);
    void writeToParcel(Parcel *parcel) const;
@@ -39,7 +42,7 @@ struct AMessage : public RefBase {
    uint32_t what() const;

    void setTarget(ALooper::handler_id target);
    ALooper::handler_id target() const;
    void setTarget(const sp<const AHandler> &handler);

    void clear();

@@ -76,7 +79,7 @@ struct AMessage : public RefBase {
            const char *name,
            int32_t *left, int32_t *top, int32_t *right, int32_t *bottom) const;

    void post(int64_t delayUs = 0);
    status_t post(int64_t delayUs = 0);

    // Posts the message to its target and waits for a response (or error)
    // before returning.
@@ -117,9 +120,16 @@ protected:
    virtual ~AMessage();

private:
    friend struct ALooper; // deliver()

    uint32_t mWhat;

    // used only for debugging
    ALooper::handler_id mTarget;

    wp<AHandler> mHandler;
    wp<ALooper> mLooper;

    struct Rect {
        int32_t mLeft, mTop, mRight, mBottom;
    };
@@ -157,6 +167,8 @@ private:

    size_t findItemIndex(const char *name, size_t len) const;

    void deliver();

    DISALLOW_EVIL_CONSTRUCTORS(AMessage);
};

+13 −5
Original line number Diff line number Diff line
@@ -19,15 +19,23 @@
#include <utils/Log.h>

#include <media/stagefright/foundation/AHandler.h>

#include <media/stagefright/foundation/ALooperRoster.h>
#include <media/stagefright/foundation/AMessage.h>

namespace android {

sp<ALooper> AHandler::looper() {
    extern ALooperRoster gLooperRoster;
void AHandler::deliverMessage(const sp<AMessage> &msg) {
    onMessageReceived(msg);
    mMessageCounter++;

    return gLooperRoster.findLooper(id());
    if (mVerboseStats) {
        uint32_t what = msg->what();
        ssize_t idx = mMessages.indexOfKey(what);
        if (idx < 0) {
            mMessages.add(what, 1);
        } else {
            mMessages.editValueAt(idx)++;
        }
    }
}

}  // namespace android
Loading