Skip to content

Commit

Permalink
Merge pull request #14903 from ANR2ME/adhoc_matching
Browse files Browse the repository at this point in the history
[AdhocMatching] Fix race condition issue
  • Loading branch information
hrydgard committed Sep 21, 2021
2 parents a165823 + ee73831 commit fb3e330
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
10 changes: 2 additions & 8 deletions Core/HLE/proAdhoc.cpp
Expand Up @@ -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;
Expand All @@ -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;
}
Expand All @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions Core/HLE/sceNetAdhoc.cpp
Expand Up @@ -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);

Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit fb3e330

Please sign in to comment.