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

Commit 2209bcb1 authored by Chia-I Wu's avatar Chia-I Wu
Browse files

graphics: test buffer allocation for thread safety

Bug: 111604912
Test: VTS
Change-Id: I1b6cacc8d6ff5e1c145425ff6f32001090b07b06
parent f6fa2f0f
Loading
Loading
Loading
Loading
+34 −0
Original line number Diff line number Diff line
@@ -16,6 +16,10 @@

#define LOG_TAG "VtsHalGraphicsMapperV2_0TargetTest"

#include <chrono>
#include <thread>
#include <vector>

#include <VtsHalHidlTargetTestBase.h>
#include <android-base/logging.h>
#include <mapper-vts/2.0/MapperVts.h>
@@ -124,6 +128,36 @@ TEST_F(GraphicsMapperHidlTest, AllocatorAllocateNoLeak) {
    }
}

/**
 * Test that IAllocator::allocate is thread-safe.
 */
TEST_F(GraphicsMapperHidlTest, AllocatorAllocateThreaded) {
    BufferDescriptor descriptor;
    ASSERT_NO_FATAL_FAILURE(descriptor = mGralloc->createDescriptor(mDummyDescriptorInfo));

    std::atomic<bool> timeUp(false);
    std::atomic<uint64_t> allocationCount(0);
    auto threadLoop = [&]() {
        while (!timeUp) {
            mGralloc->getAllocator()->allocate(
                descriptor, 1, [&](const auto&, const auto&, const auto&) { allocationCount++; });
        }
    };

    std::vector<std::thread> threads;
    for (int i = 0; i < 8; i++) {
        threads.push_back(std::thread(threadLoop));
    }

    std::this_thread::sleep_for(std::chrono::seconds(3));
    timeUp = true;
    LOG(VERBOSE) << "Made " << allocationCount << " threaded allocations";

    for (auto& thread : threads) {
        thread.join();
    }
}

/**
 * Test IMapper::createDescriptor with valid descriptor info.
 */