TryLockMutex: Fix error handling for TryLockMutex
authorSam Lantinga <slouken@libsdl.org>
Mon, 23 Apr 2018 21:50:03 -0700
changeset 119502a7839691e3d
parent 11949 2db064e4a284
child 11951 28be2719184c
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.
src/thread/pthread/SDL_sysmutex.c
     1.1 --- a/src/thread/pthread/SDL_sysmutex.c	Mon Apr 23 20:24:12 2018 -0700
     1.2 +++ b/src/thread/pthread/SDL_sysmutex.c	Mon Apr 23 21:50:03 2018 -0700
     1.3 @@ -116,6 +116,7 @@
     1.4  SDL_TryLockMutex(SDL_mutex * mutex)
     1.5  {
     1.6      int retval;
     1.7 +    int result;
     1.8  #if FAKE_RECURSIVE_MUTEX
     1.9      pthread_t this_thread;
    1.10  #endif
    1.11 @@ -134,18 +135,20 @@
    1.12           We set the locking thread id after we obtain the lock
    1.13           so unlocks from other threads will fail.
    1.14           */
    1.15 -        if (pthread_mutex_trylock(&mutex->id) == 0) {
    1.16 +        result = pthread_mutex_trylock(&mutex->id);
    1.17 +        if (result == 0) {
    1.18              mutex->owner = this_thread;
    1.19              mutex->recursive = 0;
    1.20 -        } else if (errno == EBUSY) {
    1.21 +        } else if (result == EBUSY) {
    1.22              retval = SDL_MUTEX_TIMEDOUT;
    1.23          } else {
    1.24              retval = SDL_SetError("pthread_mutex_trylock() failed");
    1.25          }
    1.26      }
    1.27  #else
    1.28 -    if (pthread_mutex_trylock(&mutex->id) != 0) {
    1.29 -        if (errno == EBUSY) {
    1.30 +    result = pthread_mutex_trylock(&mutex->id);
    1.31 +    if (result != 0) {
    1.32 +        if (result == EBUSY) {
    1.33              retval = SDL_MUTEX_TIMEDOUT;
    1.34          } else {
    1.35              retval = SDL_SetError("pthread_mutex_trylock() failed");