From d6971983aa2c759db64c4b40ea07b0478e318eab Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sat, 6 Apr 2019 15:26:59 -0700 Subject: [PATCH] Use an interruptible sleep to allow our threads to exit sooner --- src/AudioStream.c | 4 ++-- src/ControlStream.c | 4 ++-- src/Platform.c | 12 ++++++++++++ src/PlatformThreads.h | 3 ++- src/VideoStream.c | 4 ++-- 5 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/AudioStream.c b/src/AudioStream.c index 6ed91e6..ae2a493 100644 --- a/src/AudioStream.c +++ b/src/AudioStream.c @@ -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); } } } diff --git a/src/ControlStream.c b/src/ControlStream.c index 58bc77a..78435a1 100644 --- a/src/ControlStream.c +++ b/src/ControlStream.c @@ -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); diff --git a/src/Platform.c b/src/Platform.c index 2b7c9dc..c74e0ed 100644 --- a/src/Platform.c +++ b/src/Platform.c @@ -3,6 +3,10 @@ #include +// 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); diff --git a/src/PlatformThreads.h b/src/PlatformThreads.h index 1292267..a6b6675 100644 --- a/src/PlatformThreads.h +++ b/src/PlatformThreads.h @@ -62,4 +62,5 @@ void PltRunThreadProc(void); #define PLT_WAIT_SUCCESS 0 #define PLT_WAIT_INTERRUPTED 1 -void PltSleepMs(int ms); \ No newline at end of file +void PltSleepMs(int ms); +void PltSleepMsInterruptible(PLT_THREAD* thread, int ms); diff --git a/src/VideoStream.c b/src/VideoStream.c index 1ef1871..c5afa37 100644 --- a/src/VideoStream.c +++ b/src/VideoStream.c @@ -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); } } }