diff --git a/rpcs3/Emu/Cell/Modules/sceNp.cpp b/rpcs3/Emu/Cell/Modules/sceNp.cpp index 733ffbbaa7..e177e7212a 100644 --- a/rpcs3/Emu/Cell/Modules/sceNp.cpp +++ b/rpcs3/Emu/Cell/Modules/sceNp.cpp @@ -401,6 +401,8 @@ error_code sceNpInit(u32 poolsize, vm::ptr poolptr) const auto nph = g_fxo->get>(); + std::lock_guard lock(nph->mutex_status); + if (nph->is_NP_init) { return SCE_NP_ERROR_ALREADY_INITIALIZED; @@ -432,6 +434,8 @@ error_code sceNpTerm() const auto nph = g_fxo->get>(); + std::lock_guard lock(nph->mutex_status); + if (!nph->is_NP_init) { return SCE_NP_ERROR_NOT_INITIALIZED; diff --git a/rpcs3/Emu/Cell/Modules/sceNp2.cpp b/rpcs3/Emu/Cell/Modules/sceNp2.cpp index 368ef13117..fbf617194b 100644 --- a/rpcs3/Emu/Cell/Modules/sceNp2.cpp +++ b/rpcs3/Emu/Cell/Modules/sceNp2.cpp @@ -209,9 +209,12 @@ error_code sceNp2Init(u32 poolsize, vm::ptr poolptr) const auto nph = g_fxo->get>(); - if (nph->is_NP2_init) { - return SCE_NP_ERROR_ALREADY_INITIALIZED; + std::lock_guard lock(nph->mutex_status); + if (nph->is_NP2_init) + { + return SCE_NP_ERROR_ALREADY_INITIALIZED; + } } const u32 result = std::bit_cast(sceNpInit(poolsize, poolptr)); @@ -264,9 +267,12 @@ error_code sceNp2Term(ppu_thread& ppu) const auto nph = g_fxo->get>(); - if (!nph->is_NP2_init) { - return SCE_NP_ERROR_NOT_INITIALIZED; + std::lock_guard lock(nph->mutex_status); + if (!nph->is_NP2_init) + { + return SCE_NP_ERROR_NOT_INITIALIZED; + } } // TODO: does this return on error_code ? @@ -291,20 +297,23 @@ error_code sceNpMatching2Term2() const auto nph = g_fxo->get>(); - if (!nph->is_NP2_init) { - return SCE_NP_ERROR_NOT_INITIALIZED; - } + std::lock_guard lock(nph->mutex_status); + if (!nph->is_NP2_init) + { + return SCE_NP_ERROR_NOT_INITIALIZED; + } - if (!nph->is_NP2_Match2_init) - { - return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; + if (!nph->is_NP2_Match2_init) + { + return SCE_NP_MATCHING2_ERROR_NOT_INITIALIZED; + } + + nph->is_NP2_Match2_init = false; } // TODO: for all contexts: sceNpMatching2DestroyContext - nph->is_NP2_Match2_init = false; - return CELL_OK; } diff --git a/rpcs3/Emu/NP/np_handler.h b/rpcs3/Emu/NP/np_handler.h index 5c01cffd22..9bf909c49d 100644 --- a/rpcs3/Emu/NP/np_handler.h +++ b/rpcs3/Emu/NP/np_handler.h @@ -114,6 +114,9 @@ public: // For signaling void req_sign_infos(const std::string& npid, u32 conn_id); + // Mutex for NP status change + shared_mutex mutex_status; + static constexpr std::string_view thread_name = "NP Handler Thread"; protected: