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

Commit 08b02a27 authored by Marco Nelissen's avatar Marco Nelissen Committed by Android (Google) Code Review
Browse files

Merge "Multiple worker threads for SoundPool"

parents 3916fd94 7201d9cb
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;
};