From 0f839392389eac231b663c8c7f3c19f335a57eae Mon Sep 17 00:00:00 2001 From: Aleksandar Andrejevic Date: Fri, 6 Dec 2013 04:51:47 +0000 Subject: [PATCH] [NTVDM] Fix race conditions. svn path=/branches/ntvdm/; revision=61230 --- subsystems/ntvdm/ps2.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/subsystems/ntvdm/ps2.c b/subsystems/ntvdm/ps2.c index d2f9997bede..07c3fc71c29 100644 --- a/subsystems/ntvdm/ps2.c +++ b/subsystems/ntvdm/ps2.c @@ -32,10 +32,10 @@ static HANDLE InputThread = NULL; static BOOLEAN KeyboardQueuePush(BYTE ScanCode) { /* Check if the keyboard queue is full */ - if (!KeyboardQueueEmpty && (KeyboardQueueStart == KeyboardQueueEnd)) return FALSE; - WaitForSingleObject(QueueMutex, INFINITE); + if (!KeyboardQueueEmpty && (KeyboardQueueStart == KeyboardQueueEnd)) return FALSE; + /* Insert the value in the queue */ KeyboardQueue[KeyboardQueueEnd] = ScanCode; KeyboardQueueEnd++; @@ -50,11 +50,19 @@ static BOOLEAN KeyboardQueuePush(BYTE ScanCode) static BOOLEAN KeyboardQueuePop(BYTE *ScanCode) { + BOOLEAN Result = TRUE; + /* Make sure the keyboard queue is not empty */ if (KeyboardQueueEmpty) return FALSE; WaitForSingleObject(QueueMutex, INFINITE); + if (KeyboardQueueEmpty) + { + Result = FALSE; + goto Done; + } + /* Get the scan code */ *ScanCode = KeyboardQueue[KeyboardQueueStart]; @@ -68,8 +76,9 @@ static BOOLEAN KeyboardQueuePop(BYTE *ScanCode) KeyboardQueueEmpty = TRUE; } +Done: ReleaseMutex(QueueMutex); - return TRUE; + return Result; } /* PUBLIC FUNCTIONS ***********************************************************/