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
+2 -2
View File
@@ -114,10 +114,10 @@ static void UdpPingThreadProc(void* context) {
// Send less frequently if we've received data from our peer // Send less frequently if we've received data from our peer
if (receivedDataFromPeer) { if (receivedDataFromPeer) {
PltSleepMs(5000); PltSleepMsInterruptible(&udpPingThread, 5000);
} }
else { else {
PltSleepMs(1000); PltSleepMsInterruptible(&udpPingThread, 1000);
} }
} }
} }
+2 -2
View File
@@ -511,7 +511,7 @@ static void controlReceiveThreadFunc(void* context) {
} }
else { else {
// No events ready // No events ready
PltSleepMs(100); PltSleepMsInterruptible(&controlReceiveThread, 100);
continue; continue;
} }
} }
@@ -617,7 +617,7 @@ static void lossStatsThreadFunc(void* context) {
lossCountSinceLastReport = 0; lossCountSinceLastReport = 0;
// Wait a bit // Wait a bit
PltSleepMs(LOSS_REPORT_INTERVAL_MS); PltSleepMsInterruptible(&lossStatsThread, LOSS_REPORT_INTERVAL_MS);
} }
free(lossStatsPayload); free(lossStatsPayload);
+12
View File
@@ -3,6 +3,10 @@
#include <enet/enet.h> #include <enet/enet.h>
// The maximum amount of time before observing an interrupt
// in PltSleepMsInterruptible().
#define INTERRUPT_PERIOD_MS 50
int initializePlatformSockets(void); int initializePlatformSockets(void);
void cleanupPlatformSockets(void); void cleanupPlatformSockets(void);
@@ -55,6 +59,14 @@ void PltSleepMs(int ms) {
#endif #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) { int PltCreateMutex(PLT_MUTEX* mutex) {
#if defined(LC_WINDOWS) #if defined(LC_WINDOWS)
*mutex = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS); *mutex = CreateMutexEx(NULL, NULL, 0, MUTEX_ALL_ACCESS);
+2 -1
View File
@@ -62,4 +62,5 @@ void PltRunThreadProc(void);
#define PLT_WAIT_SUCCESS 0 #define PLT_WAIT_SUCCESS 0
#define PLT_WAIT_INTERRUPTED 1 #define PLT_WAIT_INTERRUPTED 1
void PltSleepMs(int ms); void PltSleepMs(int ms);
void PltSleepMsInterruptible(PLT_THREAD* thread, int ms);
+2 -2
View File
@@ -60,10 +60,10 @@ static void UdpPingThreadProc(void* context) {
// Send less frequently if we've received data from our peer // Send less frequently if we've received data from our peer
if (receivedDataFromPeer) { if (receivedDataFromPeer) {
PltSleepMs(5000); PltSleepMsInterruptible(&udpPingThread, 5000);
} }
else { else {
PltSleepMs(500); PltSleepMsInterruptible(&udpPingThread, 500);
} }
} }
} }