SDL: rawinput: Only access SDL_RAWINPUT_mutex if initialized

From 881a6749261ab8c3600d3e144078e565cd23572f Mon Sep 17 00:00:00 2001
From: Cameron Gutman <[EMAIL REDACTED]>
Date: Thu, 18 Nov 2021 19:58:04 -0600
Subject: [PATCH] rawinput: Only access SDL_RAWINPUT_mutex if initialized

We can also ditch the lock in RAWINPUT_JoystickQuit() now that the joystick
subsystem quits drivers in reverse order. There's no chance of a racing call
to RAWINPUT_WindowProc() anymore.
---
 src/joystick/windows/SDL_rawinputjoystick.c | 11 ++++-------
 1 file changed, 4 insertions(+), 7 deletions(-)

diff --git a/src/joystick/windows/SDL_rawinputjoystick.c b/src/joystick/windows/SDL_rawinputjoystick.c
index 12dd1e7a42..d9ed3fd264 100644
--- a/src/joystick/windows/SDL_rawinputjoystick.c
+++ b/src/joystick/windows/SDL_rawinputjoystick.c
@@ -1857,9 +1857,9 @@ RAWINPUT_WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
 {
     LRESULT result = -1;
 
-    SDL_LockMutex(SDL_RAWINPUT_mutex);
-
     if (SDL_RAWINPUT_inited) {
+        SDL_LockMutex(SDL_RAWINPUT_mutex);
+
         switch (msg) {
             case WM_INPUT_DEVICE_CHANGE:
             {
@@ -1903,9 +1903,9 @@ RAWINPUT_WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
             result = 0;
             break;
         }
-    }
 
-    SDL_UnlockMutex(SDL_RAWINPUT_mutex);
+        SDL_UnlockMutex(SDL_RAWINPUT_mutex);
+    }
 
     if (result >= 0) {
         return result;
@@ -1920,8 +1920,6 @@ RAWINPUT_JoystickQuit(void)
         return;
     }
 
-    SDL_LockMutex(SDL_RAWINPUT_mutex);
-
     while (SDL_RAWINPUT_devices) {
         RAWINPUT_DelDevice(SDL_RAWINPUT_devices, SDL_FALSE);
     }
@@ -1932,7 +1930,6 @@ RAWINPUT_JoystickQuit(void)
 
     SDL_RAWINPUT_inited = SDL_FALSE;
 
-    SDL_UnlockMutex(SDL_RAWINPUT_mutex);
     SDL_DestroyMutex(SDL_RAWINPUT_mutex);
     SDL_RAWINPUT_mutex = NULL;
 }