diff --git a/core/libs/prx/libSceAjm.native/src/Ajm.cpp b/core/libs/prx/libSceAjm.native/src/Ajm.cpp index 50cbee420..3b30d8d53 100644 --- a/core/libs/prx/libSceAjm.native/src/Ajm.cpp +++ b/core/libs/prx/libSceAjm.native/src/Ajm.cpp @@ -16,6 +16,7 @@ extern "C" { #include #include #include +#include #include #include #include @@ -26,6 +27,7 @@ extern "C" { namespace { constexpr int SCE_AJM_ERROR_INVALID_INSTANCE = static_cast(0x80930003); +constexpr int SCE_AJM_ERROR_INVALID_BATCH = static_cast(0x80930004); constexpr int SCE_AJM_ERROR_INVALID_PARAMETER = static_cast(0x80930005); constexpr int SCE_AJM_ERROR_OUT_OF_RESOURCES = static_cast(0x80930007); @@ -127,6 +129,8 @@ std::map> g_instances; std::atomic g_nextContext{1}; std::atomic g_nextInstance{1}; std::atomic g_nextBatch{1}; +std::mutex g_batchLock; +std::set g_batches; enum class JobKind : std::uint32_t { Initialize = 1, @@ -985,14 +989,22 @@ int APS5_VABI sceAjmBatchStart(uint32_t context, const AjmBatchInfo* info, int p cursor += record.bytes; } if (error) std::memset(error, 0, sizeof(*error)); - *batch = g_nextBatch.fetch_add(1, std::memory_order_relaxed); + const auto id = g_nextBatch.fetch_add(1, std::memory_order_relaxed); + { + std::lock_guard lock(g_batchLock); + g_batches.insert(id); + } + *batch = id; return 0; } int APS5_VABI sceAjmBatchWait(uint32_t context, uint32_t batch, uint32_t timeout, AjmBatchError* error) { (void)context; - (void)batch; (void)timeout; + { + std::lock_guard lock(g_batchLock); + if (g_batches.erase(batch) == 0) return SCE_AJM_ERROR_INVALID_BATCH; + } if (error) std::memset(error, 0, sizeof(*error)); return 0; } diff --git a/core/libs/tests/GuestAjm.cpp b/core/libs/tests/GuestAjm.cpp index 8d8504257..71524e885 100644 --- a/core/libs/tests/GuestAjm.cpp +++ b/core/libs/tests/GuestAjm.cpp @@ -321,6 +321,24 @@ void TestCodecInfo(std::uint32_t context) { Require(sceAjmInstanceDestroy(context, instance) == 0); } +void TestBatchWaitRelease(std::uint32_t context) { + constexpr int invalidBatch = static_cast(0x80930004); + std::vector batch(64); + AjmBatchInfo info{}; + AjmBatchError error{}; + std::uint32_t first = 0; + std::uint32_t second = 0; + Require(sceAjmBatchInitialize(batch.data(), batch.size(), &info) == 0); + Require(sceAjmBatchStart(context, &info, 0, &error, &first) == 0); + Require(sceAjmBatchStart(context, &info, 0, &error, &second) == 0 && second != first); + Require(sceAjmBatchWait(context, second, 0, &error) == 0); + Require(sceAjmBatchWait(context, first, 0, &error) == 0); + Require(sceAjmBatchWait(context, first, 0, &error) == invalidBatch); + Require(sceAjmBatchWait(context, second, 0, &error) == invalidBatch); + Require(sceAjmBatchWait(context, 0, 0, &error) == invalidBatch); + Require(sceAjmBatchWait(context, second + 1, 0, &error) == invalidBatch); +} + } int main() { @@ -343,5 +361,6 @@ int main() { TestDecodeSingle(context); TestGaplessDecode(context); TestCodecInfo(context); + TestBatchWaitRelease(context); Require(sceAjmFinalize(context) == 0); }