mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-11-28 00:34:06 +00:00
Fix data race in LiGetEstimatedRttInfo
This commit is contained in:
1 parent
d5b38a3ac6
commit
a0262e78eb
3 files changed
+43
-15
No files matched your search
+30
-15
@@ -126,6 +126,9 @@ static int lastConnectionStatusUpdate;
|
||||
static uint32_t currentEnetSequenceNumber;
|
||||
static uint64_t firstFrameTimeMs;
|
||||
|
||||
static atomic_uint lastEnetRtt;
|
||||
static atomic_uint lastEnetRttVariance;
|
||||
|
||||
static LINKED_BLOCKING_QUEUE referenceFrameControlQueue;
|
||||
static LINKED_BLOCKING_QUEUE frameFecStatusQueue;
|
||||
static LINKED_BLOCKING_QUEUE asyncCallbackQueue;
|
||||
@@ -358,6 +361,9 @@ int initializeControlStream(void) {
|
||||
}
|
||||
}
|
||||
|
||||
atomic_store_explicit(&lastEnetRtt, 0, memory_order_relaxed);
|
||||
atomic_store_explicit(&lastEnetRttVariance, 0, memory_order_relaxed);
|
||||
|
||||
lastGoodFrame = 0;
|
||||
lastSeenFrame = 0;
|
||||
disconnectPending = false;
|
||||
@@ -1937,25 +1943,34 @@ bool isControlDataInTransit(void) {
|
||||
}
|
||||
|
||||
bool LiGetEstimatedRttInfo(uint32_t* estimatedRtt, uint32_t* estimatedRttVariance) {
|
||||
bool ret = false;
|
||||
|
||||
// We do not acquire enetMutex here because we're just reading metrics
|
||||
// and observing a torn write every once in a while is totally fine.
|
||||
// The peer pointer points to memory reserved inside the client object,
|
||||
// so it's guaranteed that it will never go away underneath us.
|
||||
if (peer != NULL && peer->state == ENET_PEER_STATE_CONNECTED) {
|
||||
if (estimatedRtt != NULL) {
|
||||
*estimatedRtt = peer->roundTripTime;
|
||||
// Opportunistically update the cached RTT stats if nobody is using ENet right now
|
||||
if (PltTryLockMutex(&enetMutex)) {
|
||||
if (peer != NULL && peer->state == ENET_PEER_STATE_CONNECTED) {
|
||||
atomic_store_explicit(&lastEnetRttVariance, peer->roundTripTimeVariance, memory_order_relaxed);
|
||||
atomic_store_explicit(&lastEnetRtt, peer->roundTripTime, memory_order_release);
|
||||
}
|
||||
else {
|
||||
atomic_store_explicit(&lastEnetRttVariance, 0, memory_order_relaxed);
|
||||
atomic_store_explicit(&lastEnetRtt, 0, memory_order_release);
|
||||
}
|
||||
|
||||
if (estimatedRttVariance != NULL) {
|
||||
*estimatedRttVariance = peer->roundTripTimeVariance;
|
||||
}
|
||||
|
||||
ret = true;
|
||||
PltUnlockMutex(&enetMutex);
|
||||
}
|
||||
|
||||
return ret;
|
||||
// Return the cached values to the caller (if present)
|
||||
uint32_t lastRtt = atomic_load_explicit(&lastEnetRtt, memory_order_acquire);
|
||||
if (lastRtt == 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (estimatedRtt != NULL) {
|
||||
*estimatedRtt = lastRtt;
|
||||
}
|
||||
if (estimatedRttVariance != NULL) {
|
||||
*estimatedRttVariance = atomic_load_explicit(&lastEnetRttVariance, memory_order_relaxed);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
// Starts the control stream
|
||||
|
||||
@@ -166,6 +166,18 @@ void PltLockMutex(PLT_MUTEX* mutex) {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool PltTryLockMutex(PLT_MUTEX* mutex) {
|
||||
#if defined(LC_WINDOWS)
|
||||
return TryAcquireSRWLockExclusive(mutex);
|
||||
#elif defined(__WIIU__)
|
||||
return OSFastMutex_TryLock(mutex);
|
||||
#elif defined(__3DS__)
|
||||
return LightLock_TryLock(mutex) == 0;
|
||||
#else
|
||||
return pthread_mutex_trylock(mutex) == 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
void PltUnlockMutex(PLT_MUTEX* mutex) {
|
||||
#if defined(LC_WINDOWS)
|
||||
ReleaseSRWLockExclusive(mutex);
|
||||
|
||||
@@ -52,6 +52,7 @@ typedef struct _PLT_EVENT {
|
||||
int PltCreateMutex(PLT_MUTEX* mutex);
|
||||
void PltDeleteMutex(PLT_MUTEX* mutex);
|
||||
void PltLockMutex(PLT_MUTEX* mutex);
|
||||
bool PltTryLockMutex(PLT_MUTEX* mutex);
|
||||
void PltUnlockMutex(PLT_MUTEX* mutex);
|
||||
|
||||
int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THREAD* thread);
|
||||
|
||||
Reference in new issue
Block a user