Browse Source

thread: make generic SDL_Condition a no-op if built with SDL_THREADS_DISABLED.

Otherwise, when you call SDL_CreateCondition() in something that can otherwise
survive in a single-threaded build, you'll get an error that seems fatal.

We already do this for mutexes and rwlocks (but not semaphores!)

Fixes #9558.
Ryan C. Gordon 1 year ago
parent
commit
fdcc3e1151
1 changed files with 11 additions and 3 deletions
  1. 11 3
      src/thread/generic/SDL_syscond.c

+ 11 - 3
src/thread/generic/SDL_syscond.c

@@ -52,9 +52,9 @@ typedef struct SDL_cond_generic
 /* Create a condition variable */
 SDL_Condition *SDL_CreateCondition_generic(void)
 {
-    SDL_cond_generic *cond;
+    SDL_cond_generic *cond = (SDL_cond_generic *)SDL_calloc(1, sizeof(*cond));
 
-    cond = (SDL_cond_generic *)SDL_malloc(sizeof(SDL_cond_generic));
+#ifndef SDL_THREADS_DISABLED
     if (cond) {
         cond->lock = SDL_CreateMutex();
         cond->wait_sem = SDL_CreateSemaphore(0);
@@ -65,6 +65,8 @@ SDL_Condition *SDL_CreateCondition_generic(void)
             cond = NULL;
         }
     }
+#endif
+
     return (SDL_Condition *)cond;
 }
 
@@ -94,6 +96,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond)
         return SDL_InvalidParamError("cond");
     }
 
+#ifndef SDL_THREADS_DISABLED
     /* If there are waiting threads not already signalled, then
        signal the condition and wait for the thread to respond.
      */
@@ -106,6 +109,7 @@ int SDL_SignalCondition_generic(SDL_Condition *_cond)
     } else {
         SDL_UnlockMutex(cond->lock);
     }
+#endif
 
     return 0;
 }
@@ -118,6 +122,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond)
         return SDL_InvalidParamError("cond");
     }
 
+#ifndef SDL_THREADS_DISABLED
     /* If there are waiting threads not already signalled, then
        signal the condition and wait for the thread to respond.
      */
@@ -140,6 +145,7 @@ int SDL_BroadcastCondition_generic(SDL_Condition *_cond)
     } else {
         SDL_UnlockMutex(cond->lock);
     }
+#endif
 
     return 0;
 }
@@ -168,12 +174,13 @@ Thread B:
 int SDL_WaitConditionTimeoutNS_generic(SDL_Condition *_cond, SDL_Mutex *mutex, Sint64 timeoutNS)
 {
     SDL_cond_generic *cond = (SDL_cond_generic *)_cond;
-    int retval;
+    int retval = 0;
 
     if (!cond) {
         return SDL_InvalidParamError("cond");
     }
 
+#ifndef SDL_THREADS_DISABLED
     /* Obtain the protection mutex, and increment the number of waiters.
        This allows the signal mechanism to only perform a signal if there
        are waiting threads.
@@ -211,6 +218,7 @@ int SDL_WaitConditionTimeoutNS_generic(SDL_Condition *_cond, SDL_Mutex *mutex, S
 
     /* Lock the mutex, as is required by condition variable semantics */
     SDL_LockMutex(mutex);
+#endif
 
     return retval;
 }