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

Commit a5a6a1b7 authored by Andy Hung's avatar Andy Hung
Browse files

SoundPool: Separate thread priority for SoundDecoder and StreamManager

Allows more refined settings.
Restore sound decoding thread priority from audio to normal.

Test: atest SoundPoolOggTest SoundPoolAacTest SoundPoolMidiTest SoundPoolHapticTest
Bug: 319405227
Change-Id: I5885920bb0e6238ef96c1e71562c154a21bf180d
parent 62b94e7e
Loading
Loading
Loading
Loading
+3 −2
Original line number Diff line number Diff line
@@ -29,14 +29,15 @@ static constexpr size_t kMaxQueueSize = 128;
// before the SoundDecoder thread closes.
static constexpr int32_t kWaitTimeBeforeCloseMs = 1000;

SoundDecoder::SoundDecoder(SoundManager* soundManager, size_t threads)
SoundDecoder::SoundDecoder(SoundManager* soundManager, size_t threads, int32_t threadPriority)
    : mSoundManager(soundManager)
{
    ALOGV("%s(%p, %zu)", __func__, soundManager, threads);
    // ThreadPool is created, but we don't launch any threads.
    mThreadPool = std::make_unique<ThreadPool>(
            std::min(threads, (size_t)std::thread::hardware_concurrency()),
            "SoundDecoder_");
            "SoundDecoder_",
            threadPriority);
}

SoundDecoder::~SoundDecoder()
+1 −1
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ namespace android::soundpool {
 */
class SoundDecoder {
public:
    SoundDecoder(SoundManager* soundManager, size_t threads);
    SoundDecoder(SoundManager* soundManager, size_t threads, int32_t threadPriority);
    ~SoundDecoder();
    void loadSound(int32_t soundID) NO_THREAD_SAFETY_ANALYSIS; // uses unique_lock
    void quit();
+1 −1
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ namespace android::soundpool {
static const size_t kDecoderThreads = std::thread::hardware_concurrency() >= 4 ? 2 : 1;

SoundManager::SoundManager()
    : mDecoder{std::make_unique<SoundDecoder>(this, kDecoderThreads)}
    : mDecoder{std::make_unique<SoundDecoder>(this, kDecoderThreads, ANDROID_PRIORITY_NORMAL)}
{
    ALOGV("%s()", __func__);
}
+2 −1
Original line number Diff line number Diff line
@@ -126,7 +126,8 @@ StreamManager::StreamManager(
    mThreadPool = std::make_unique<ThreadPool>(
            std::min((size_t)streams,  // do not make more threads than streams to play
                    std::min(threads, (size_t)std::thread::hardware_concurrency())),
            "SoundPool_");
            "SoundPool_",
            ANDROID_PRIORITY_AUDIO);
}

#pragma clang diagnostic pop
+9 −5
Original line number Diff line number Diff line
@@ -46,9 +46,9 @@ namespace android::soundpool {
 */
class JavaThread {
public:
    JavaThread(std::function<void()> f, const char *name)
    JavaThread(std::function<void()> f, const char *name, int32_t threadPriority)
        : mF{std::move(f)} {
        createThreadEtc(staticFunction, this, name, ANDROID_PRIORITY_AUDIO);
        createThreadEtc(staticFunction, this, name, threadPriority);
    }

    JavaThread(JavaThread &&) = delete; // uses "this" ptr, not moveable.
@@ -109,9 +109,11 @@ private:
 */
class ThreadPool {
public:
    ThreadPool(size_t maxThreadCount, std::string name)
    ThreadPool(size_t maxThreadCount, std::string name,
            int32_t threadPriority = ANDROID_PRIORITY_NORMAL)
        : mMaxThreadCount(maxThreadCount)
        , mName{std::move(name)} { }
        , mName{std::move(name)}
        , mThreadPriority(threadPriority) {}

    ~ThreadPool() { quit(); }

@@ -159,7 +161,8 @@ public:
            const int32_t id = mNextThreadId;
            mThreads.emplace_back(std::make_unique<JavaThread>(
                    [this, id, mf = std::move(f)] { mf(id); --mActiveThreadCount; },
                    (mName + std::to_string(id)).c_str()));
                    (mName + std::to_string(id)).c_str(),
                    mThreadPriority));
            ++mActiveThreadCount;
            return id;
        }
@@ -180,6 +183,7 @@ public:
private:
    const size_t            mMaxThreadCount;
    const std::string       mName;
    const int32_t           mThreadPriority;

    std::atomic_size_t      mActiveThreadCount = 0;