Browse Source

TryLockMutex: Fix error handling for TryLockMutex

Christian Herzig

pthread_mutex_trylock() and by the way, pthread_mutex_lock() do not set errno.
Pthread-methods directly return error code as int. See related man-pages for
details.
Sam Lantinga 7 years ago
parent
commit
a1b8fa6071
1 changed files with 7 additions and 4 deletions
  1. 7 4
      src/thread/pthread/SDL_sysmutex.c

+ 7 - 4
src/thread/pthread/SDL_sysmutex.c

@@ -116,6 +116,7 @@ int
 SDL_TryLockMutex(SDL_mutex * mutex)
 SDL_TryLockMutex(SDL_mutex * mutex)
 {
 {
     int retval;
     int retval;
+    int result;
 #if FAKE_RECURSIVE_MUTEX
 #if FAKE_RECURSIVE_MUTEX
     pthread_t this_thread;
     pthread_t this_thread;
 #endif
 #endif
@@ -134,18 +135,20 @@ SDL_TryLockMutex(SDL_mutex * mutex)
          We set the locking thread id after we obtain the lock
          We set the locking thread id after we obtain the lock
          so unlocks from other threads will fail.
          so unlocks from other threads will fail.
          */
          */
-        if (pthread_mutex_trylock(&mutex->id) == 0) {
+        result = pthread_mutex_trylock(&mutex->id);
+        if (result == 0) {
             mutex->owner = this_thread;
             mutex->owner = this_thread;
             mutex->recursive = 0;
             mutex->recursive = 0;
-        } else if (errno == EBUSY) {
+        } else if (result == EBUSY) {
             retval = SDL_MUTEX_TIMEDOUT;
             retval = SDL_MUTEX_TIMEDOUT;
         } else {
         } else {
             retval = SDL_SetError("pthread_mutex_trylock() failed");
             retval = SDL_SetError("pthread_mutex_trylock() failed");
         }
         }
     }
     }
 #else
 #else
-    if (pthread_mutex_trylock(&mutex->id) != 0) {
-        if (errno == EBUSY) {
+    result = pthread_mutex_trylock(&mutex->id);
+    if (result != 0) {
+        if (result == EBUSY) {
             retval = SDL_MUTEX_TIMEDOUT;
             retval = SDL_MUTEX_TIMEDOUT;
         } else {
         } else {
             retval = SDL_SetError("pthread_mutex_trylock() failed");
             retval = SDL_SetError("pthread_mutex_trylock() failed");