Fix race condition issue on AdhocMatching

This commit is contained in:
ANR2ME 2021-09-21 15:31:33 +07:00
parent 329c1c5e43
commit ee7383193b
2 changed files with 8 additions and 8 deletions

View file

@ -2252,7 +2252,7 @@ bool resolveIP(uint32_t ip, SceNetEtherAddr * mac) {
}
// Multithreading Lock
peerlock.lock();
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);
// Peer Reference
SceNetAdhocctlPeerInfo * peer = friends;
@ -2264,17 +2264,11 @@ bool resolveIP(uint32_t ip, SceNetEtherAddr * mac) {
// Copy Data
*mac = peer->mac_addr;
// Multithreading Unlock
peerlock.unlock();
// Return Success
return true;
}
}
// Multithreading Unlock
peerlock.unlock();
// Peer not found
return false;
}
@ -2293,7 +2287,7 @@ bool resolveMAC(SceNetEtherAddr * mac, uint32_t * ip) {
}
// Multithreading Lock
std::lock_guard<std::recursive_mutex> guard(peerlock);
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);
// Peer Reference
SceNetAdhocctlPeerInfo * peer = friends;

View file

@ -5390,6 +5390,9 @@ int sceNetAdhocMatchingSendData(int matchingId, const char *mac, int dataLen, u3
void* data = NULL;
if (Memory::IsValidAddress(dataAddr)) data = Memory::GetPointer(dataAddr);
// Lock the peer
std::lock_guard<std::recursive_mutex> peer_guard(peerlock);
// Find Target Peer
SceNetAdhocMatchingMemberInternal* peer = findPeer(context, (SceNetEtherAddr*)mac);
@ -7306,9 +7309,12 @@ int matchingInputThread(int matchingId) // TODO: The MatchingInput thread is usi
// FIXME: When using JPCSP + prx files, the "SceNetAdhocMatchingInput" thread is using blocking PdpRecv with infinite(0) timeout, which can be stopped/aborted using SetSocketAlert, while "SceNetAdhocMatchingEvent" thread is using non-blocking for sending
rxbuflen = context->rxbuflen;
senderport = 0;
// Lock the peer first before locking the socket to avoid race condiion
peerlock.lock();
context->socketlock->lock();
int recvresult = sceNetAdhocPdpRecv(context->socket, &sendermac, &senderport, context->rxbuf, &rxbuflen, 0, ADHOC_F_NONBLOCK);
context->socketlock->unlock();
peerlock.unlock();
// Received Data from a Sender that interests us
// Note: There are cases where the sender port might be re-mapped by router or ISP, so we shouldn't check the source port.