mirror of
https://github.com/moonlight-stream/moonlight-common-c.git
synced 2026-12-03 03:31:27 +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 uint32_t currentEnetSequenceNumber;
|
||||||
static uint64_t firstFrameTimeMs;
|
static uint64_t firstFrameTimeMs;
|
||||||
|
|
||||||
|
static atomic_uint lastEnetRtt;
|
||||||
|
static atomic_uint lastEnetRttVariance;
|
||||||
|
|
||||||
static LINKED_BLOCKING_QUEUE referenceFrameControlQueue;
|
static LINKED_BLOCKING_QUEUE referenceFrameControlQueue;
|
||||||
static LINKED_BLOCKING_QUEUE frameFecStatusQueue;
|
static LINKED_BLOCKING_QUEUE frameFecStatusQueue;
|
||||||
static LINKED_BLOCKING_QUEUE asyncCallbackQueue;
|
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;
|
lastGoodFrame = 0;
|
||||||
lastSeenFrame = 0;
|
lastSeenFrame = 0;
|
||||||
disconnectPending = false;
|
disconnectPending = false;
|
||||||
@@ -1937,25 +1943,34 @@ bool isControlDataInTransit(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
bool LiGetEstimatedRttInfo(uint32_t* estimatedRtt, uint32_t* estimatedRttVariance) {
|
bool LiGetEstimatedRttInfo(uint32_t* estimatedRtt, uint32_t* estimatedRttVariance) {
|
||||||
bool ret = false;
|
// Opportunistically update the cached RTT stats if nobody is using ENet right now
|
||||||
|
if (PltTryLockMutex(&enetMutex)) {
|
||||||
// We do not acquire enetMutex here because we're just reading metrics
|
if (peer != NULL && peer->state == ENET_PEER_STATE_CONNECTED) {
|
||||||
// and observing a torn write every once in a while is totally fine.
|
atomic_store_explicit(&lastEnetRttVariance, peer->roundTripTimeVariance, memory_order_relaxed);
|
||||||
// The peer pointer points to memory reserved inside the client object,
|
atomic_store_explicit(&lastEnetRtt, peer->roundTripTime, memory_order_release);
|
||||||
// so it's guaranteed that it will never go away underneath us.
|
}
|
||||||
if (peer != NULL && peer->state == ENET_PEER_STATE_CONNECTED) {
|
else {
|
||||||
if (estimatedRtt != NULL) {
|
atomic_store_explicit(&lastEnetRttVariance, 0, memory_order_relaxed);
|
||||||
*estimatedRtt = peer->roundTripTime;
|
atomic_store_explicit(&lastEnetRtt, 0, memory_order_release);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (estimatedRttVariance != NULL) {
|
PltUnlockMutex(&enetMutex);
|
||||||
*estimatedRttVariance = peer->roundTripTimeVariance;
|
|
||||||
}
|
|
||||||
|
|
||||||
ret = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
// Starts the control stream
|
||||||
|
|||||||
@@ -166,6 +166,18 @@ void PltLockMutex(PLT_MUTEX* mutex) {
|
|||||||
#endif
|
#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) {
|
void PltUnlockMutex(PLT_MUTEX* mutex) {
|
||||||
#if defined(LC_WINDOWS)
|
#if defined(LC_WINDOWS)
|
||||||
ReleaseSRWLockExclusive(mutex);
|
ReleaseSRWLockExclusive(mutex);
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ typedef struct _PLT_EVENT {
|
|||||||
int PltCreateMutex(PLT_MUTEX* mutex);
|
int PltCreateMutex(PLT_MUTEX* mutex);
|
||||||
void PltDeleteMutex(PLT_MUTEX* mutex);
|
void PltDeleteMutex(PLT_MUTEX* mutex);
|
||||||
void PltLockMutex(PLT_MUTEX* mutex);
|
void PltLockMutex(PLT_MUTEX* mutex);
|
||||||
|
bool PltTryLockMutex(PLT_MUTEX* mutex);
|
||||||
void PltUnlockMutex(PLT_MUTEX* mutex);
|
void PltUnlockMutex(PLT_MUTEX* mutex);
|
||||||
|
|
||||||
int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THREAD* thread);
|
int PltCreateThread(const char* name, ThreadEntry entry, void* context, PLT_THREAD* thread);
|
||||||
|
|||||||
Reference in new issue
Block a user