Use an interruptible sleep to allow our threads to exit sooner

This commit is contained in:
Cameron Gutman 2019-04-06 15:26:59 -07:00
parent 9220fcb8e8
commit d6971983aa
5 changed files with 20 additions and 7 deletions

View File

@ -114,10 +114,10 @@ static void UdpPingThreadProc(void* context) {
// Send less frequently if we've received data from our peer
if (receivedDataFromPeer) {
PltSleepMs(5000);
PltSleepMsInterruptible(&udpPingThread, 5000);
}
else {
PltSleepMs(1000);
PltSleepMsInterruptible(&udpPingThread, 1000);
}
}
}

View File

@ -511,7 +511,7 @@ static void controlReceiveThreadFunc(void* context) {
}
else {
// No events ready
PltSleepMs(100);
PltSleepMsInterruptible(&controlReceiveThread, 100);
continue;
}
}
@ -617,7 +617,7 @@ static void lossStatsThreadFunc(void* context) {
lossCountSinceLastReport = 0;
// Wait a bit
PltSleepMs(LOSS_REPORT_INTERVAL_MS);
PltSleepMsInterruptible(&lossStatsThread, LOSS_REPORT_INTERVAL_MS);
}
free(lossStatsPayload);

View File

@ -3,6 +3,10 @@
#include <enet/enet.h>
// The maximum amount of time before observing an interrupt
// in PltSleepMsInterruptible().
#define INTERRUPT_PERIOD_MS 50
int initializePlatformSockets(void);
void cleanupPlatformSockets(void);
@ -55,6 +59,14 @@ void PltSleepMs(int ms) {
#endif
}
void PltSleepMsInterruptible(PLT_THREAD* thread, int ms) {
while (ms > 0 && !PltIsThreadInterrupted(thread)) {
int msToSleep = ms < INTERRUPT_PERIOD_MS ? ms : INTERRUPT_PERIOD_MS;
PltSleepMs(msToSleep);
ms -= msToSleep;
}
}
int PltCreateMutex(PLT_MUTEX* mutex) {
#if defined(LC_WINDOWS)
*mutex = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS);

View File

@ -62,4 +62,5 @@ void PltRunThreadProc(void);
#define PLT_WAIT_SUCCESS 0
#define PLT_WAIT_INTERRUPTED 1
void PltSleepMs(int ms);
void PltSleepMs(int ms);
void PltSleepMsInterruptible(PLT_THREAD* thread, int ms);

View File

@ -60,10 +60,10 @@ static void UdpPingThreadProc(void* context) {
// Send less frequently if we've received data from our peer
if (receivedDataFromPeer) {
PltSleepMs(5000);
PltSleepMsInterruptible(&udpPingThread, 5000);
}
else {
PltSleepMs(500);
PltSleepMsInterruptible(&udpPingThread, 500);
}
}
}