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

Commit 87b46795 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge "aaudio: name threads for easier debugging"

parents 349765b1 55978896
Loading
Loading
Loading
Loading
+11 −0
Original line number Diff line number Diff line
@@ -234,6 +234,17 @@ aaudio_result_t AudioStream::createThread(int64_t periodNanoseconds,
    if (err != 0) {
        return AAudioConvert_androidToAAudioResult(-errno);
    } else {
        // Name the thread with an increasing index, "AAudio_#", for debugging.
        static std::atomic<uint32_t> nextThreadIndex{1};
        char name[16]; // max length for a pthread_name
        uint32_t index = nextThreadIndex++;
        // Wrap the index so that we do not hit the 16 char limit
        // and to avoid hard-to-read large numbers.
        index = index % 100000;  // arbitrary
        snprintf(name, sizeof(name), "AAudio_%u", index);
        err = pthread_setname_np(mThread, name);
        ALOGW_IF((err != 0), "Could not set name of AAudio thread. err = %d", err);

        mHasThread = true;
        return AAUDIO_OK;
    }
+1 −1
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ using namespace aaudio; // TODO just import names needed

AAudioServiceStreamBase::AAudioServiceStreamBase(AAudioService &audioService)
        : mUpMessageQueue(nullptr)
        , mTimestampThread()
        , mTimestampThread("AATime")
        , mAtomicTimestamp()
        , mAudioService(audioService) {
    mMmapClient.clientUid = -1;
+19 −3
Original line number Diff line number Diff line
@@ -27,12 +27,26 @@

using namespace aaudio;

std::atomic<uint32_t> AAudioThread::mNextThreadIndex{1};

AAudioThread::AAudioThread()
    : mRunnable(nullptr)
    , mHasThread(false) {
AAudioThread::AAudioThread(const char *prefix) {
    setup(prefix);
}

AAudioThread::AAudioThread() {
    setup("AAudio");
}

void AAudioThread::setup(const char *prefix) {
    // mThread is a pthread_t of unknown size so we need memset().
    memset(&mThread, 0, sizeof(mThread));

    // Name the thread with an increasing index, "prefix_#", for debugging.
    uint32_t index = mNextThreadIndex++;
    // Wrap the index so that we do not hit the 16 char limit
    // and to avoid hard-to-read large numbers.
    index = index % 100000; // arbitrary
    snprintf(mName, sizeof(mName), "%s_%u", prefix, index);
}

void AAudioThread::dispatch() {
@@ -64,6 +78,8 @@ aaudio_result_t AAudioThread::start(Runnable *runnable) {
        ALOGE("start() - pthread_create() returned %d %s", err, strerror(err));
        return AAudioConvert_androidToAAudioResult(-err);
    } else {
        int err = pthread_setname_np(mThread, mName);
        ALOGW_IF((err != 0), "Could not set name of AAudioThread. err = %d", err);
        mHasThread = true;
        return AAUDIO_OK;
    }
+10 −3
Original line number Diff line number Diff line
@@ -43,7 +43,9 @@ class AAudioThread
{
public:
    AAudioThread();
    AAudioThread(Runnable *runnable);

    AAudioThread(const char *prefix);

    virtual ~AAudioThread() = default;

    /**
@@ -66,10 +68,15 @@ public:
    void dispatch(); // called internally from 'C' thread wrapper

private:
    Runnable    *mRunnable;
    bool         mHasThread;

    void setup(const char *prefix);

    Runnable    *mRunnable = nullptr;
    bool         mHasThread = false;
    pthread_t    mThread; // initialized in constructor

    static std::atomic<uint32_t> mNextThreadIndex;
    char         mName[16]; // max length for a pthread_name
};

} /* namespace aaudio */