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

Commit 7201d9cb authored by Marco Nelissen's avatar Marco Nelissen
Browse files

Multiple worker threads for SoundPool

This makes SoundPool create multiple worker threads for loading sounds.
Threads are created on demand, and go away when no work is available.
Bug: 38031165
Test: manual

Change-Id: Ic73f2777215e4aa65aa1c41b283b7838cebd165d
parent b562e539
Loading
Loading
Loading
Loading
+19 −10
Original line number Diff line number Diff line
@@ -20,6 +20,8 @@

#include "SoundPoolThread.h"

static const int kMaxWorkers = 3;

namespace android {

void SoundPoolThread::write(SoundPoolMsg msg) {
@@ -31,14 +33,21 @@ void SoundPoolThread::write(SoundPoolMsg msg) {
    // if thread is quitting, don't add to queue
    if (mRunning) {
        mMsgQueue.push(msg);
        mCondition.signal();
        if (mNumWorkers < kMaxWorkers) {
            if (createThreadEtc(beginThread, this, "SoundPoolThread")) {
                mNumWorkers++;
                ALOGV("created worker thread");
            }
        }
    }
}

const SoundPoolMsg SoundPoolThread::read() {
    Mutex::Autolock lock(&mLock);
    while (mMsgQueue.size() == 0) {
        mCondition.wait(mLock);
    if (mMsgQueue.size() == 0) {
        mNumWorkers--;
        mCondition.signal();
        return SoundPoolMsg(SoundPoolMsg::KILL, 0);
    }
    SoundPoolMsg msg = mMsgQueue[0];
    mMsgQueue.removeAt(0);
@@ -51,20 +60,20 @@ void SoundPoolThread::quit() {
    if (mRunning) {
        mRunning = false;
        mMsgQueue.clear();
        mMsgQueue.push(SoundPoolMsg(SoundPoolMsg::KILL, 0));
        mCondition.signal();
        mCondition.broadcast(); // wake up any blocked writers
        while (mNumWorkers > 0) {
            mCondition.wait(mLock);
        }
    }
    ALOGV("return from quit");
}

SoundPoolThread::SoundPoolThread(SoundPool* soundPool) :
    mSoundPool(soundPool)
    mSoundPool(soundPool),
    mNumWorkers(0),
    mRunning(true)
{
    mMsgQueue.setCapacity(maxMessages);
    if (createThreadEtc(beginThread, this, "SoundPoolThread")) {
        mRunning = true;
    }
}

SoundPoolThread::~SoundPoolThread()
+1 −0
Original line number Diff line number Diff line
@@ -58,6 +58,7 @@ private:
    Condition               mCondition;
    Vector<SoundPoolMsg>    mMsgQueue;
    SoundPool*              mSoundPool;
    int32_t                 mNumWorkers;
    bool                    mRunning;
};