Add SDL_TryLockMutex and implementations for all platforms
authorEdward Rudd <urkle@outoforder.cc>
Tue, 05 Mar 2013 18:54:55 -0500
changeset 6966090df160f6e0
parent 6965 ab4bb4e99961
child 6967 adc80d085ddf
Add SDL_TryLockMutex and implementations for all platforms
include/SDL_mutex.h
src/thread/generic/SDL_sysmutex.c
src/thread/nds/SDL_sysmutex.c
src/thread/pthread/SDL_sysmutex.c
src/thread/windows/SDL_sysmutex.c
     1.1 --- a/include/SDL_mutex.h	Tue Mar 05 18:01:59 2013 -0500
     1.2 +++ b/include/SDL_mutex.h	Tue Mar 05 18:54:55 2013 -0500
     1.3 @@ -74,6 +74,13 @@
     1.4  extern DECLSPEC int SDLCALL SDL_mutexP(SDL_mutex * mutex);
     1.5  
     1.6  /**
     1.7 + *  Try to lock the mutex
     1.8 + *  
     1.9 + *  \return 0, SDL_MUTEX_TIMEDOUT, or -1 on error
    1.10 + */
    1.11 +extern DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex * mutex);
    1.12 +
    1.13 +/**
    1.14   *  Unlock the mutex.
    1.15   *  
    1.16   *  \return 0, or -1 on error.
     2.1 --- a/src/thread/generic/SDL_sysmutex.c	Tue Mar 05 18:01:59 2013 -0500
     2.2 +++ b/src/thread/generic/SDL_sysmutex.c	Tue Mar 05 18:54:55 2013 -0500
     2.3 @@ -68,7 +68,7 @@
     2.4      }
     2.5  }
     2.6  
     2.7 -/* Lock the semaphore */
     2.8 +/* Lock the mutex */
     2.9  int
    2.10  SDL_mutexP(SDL_mutex * mutex)
    2.11  {
    2.12 @@ -99,6 +99,40 @@
    2.13  #endif /* SDL_THREADS_DISABLED */
    2.14  }
    2.15  
    2.16 +/* try Lock the mutex */
    2.17 +int
    2.18 +SDL_TryLockMutex(SDL_mutex * mutex)
    2.19 +{
    2.20 +#if SDL_THREADS_DISABLED
    2.21 +    return 0;
    2.22 +#else
    2.23 +    int retval = 0;
    2.24 +    SDL_threadID this_thread;
    2.25 +
    2.26 +    if (mutex == NULL) {
    2.27 +        SDL_SetError("Passed a NULL mutex");
    2.28 +        return -1;
    2.29 +    }
    2.30 +
    2.31 +    this_thread = SDL_ThreadID();
    2.32 +    if (mutex->owner == this_thread) {
    2.33 +        ++mutex->recursive;
    2.34 +    } else {
    2.35 +        /* The order of operations is important.
    2.36 +         We set the locking thread id after we obtain the lock
    2.37 +         so unlocks from other threads will fail.
    2.38 +         */
    2.39 +        retval = SDL_SemWait(mutex->sem);
    2.40 +        if (retval == 0) {
    2.41 +            mutex->owner = this_thread;
    2.42 +            mutex->recursive = 0;
    2.43 +        }
    2.44 +    }
    2.45 +
    2.46 +    return retval;
    2.47 +#endif /* SDL_THREADS_DISABLED */
    2.48 +}
    2.49 +
    2.50  /* Unlock the mutex */
    2.51  int
    2.52  SDL_mutexV(SDL_mutex * mutex)
     3.1 --- a/src/thread/nds/SDL_sysmutex.c	Tue Mar 05 18:01:59 2013 -0500
     3.2 +++ b/src/thread/nds/SDL_sysmutex.c	Tue Mar 05 18:54:55 2013 -0500
     3.3 @@ -76,7 +76,7 @@
     3.4      }
     3.5  }
     3.6  
     3.7 -/* Lock the semaphore */
     3.8 +/* Lock the mutex */
     3.9  int
    3.10  SDL_mutexP(SDL_mutex * mutex)
    3.11  {
    3.12 @@ -107,6 +107,40 @@
    3.13  #endif /* DISABLE_THREADS */
    3.14  }
    3.15  
    3.16 +/* Try Lock the mutex */
    3.17 +int
    3.18 +SDL_TryLockMutex(SDL_mutex * mutex)
    3.19 +{
    3.20 +#ifdef DISABLE_THREADS
    3.21 +    return 0;
    3.22 +#else
    3.23 +    int retval = 0;
    3.24 +    SDL_threadID this_thread;
    3.25 +
    3.26 +    if (mutex == NULL) {
    3.27 +        SDL_SetError("Passed a NULL mutex");
    3.28 +        return -1;
    3.29 +    }
    3.30 +
    3.31 +    this_thread = SDL_ThreadID();
    3.32 +    if (mutex->owner == this_thread) {
    3.33 +        ++mutex->recursive;
    3.34 +    } else {
    3.35 +        /* The order of operations is important.
    3.36 +         We set the locking thread id after we obtain the lock
    3.37 +         so unlocks from other threads will fail.
    3.38 +         */
    3.39 +        retval = SDL_SemTryWait(mutex->sem);
    3.40 +        if (ret == 0) {
    3.41 +            mutex->owner = this_thread;
    3.42 +            mutex->recursive = 0;
    3.43 +        }
    3.44 +    }
    3.45 +
    3.46 +    return retval;
    3.47 +#endif /* DISABLE_THREADS */
    3.48 +}
    3.49 +
    3.50  /* Unlock the mutex */
    3.51  int
    3.52  SDL_mutexV(SDL_mutex * mutex)
     4.1 --- a/src/thread/pthread/SDL_sysmutex.c	Tue Mar 05 18:01:59 2013 -0500
     4.2 +++ b/src/thread/pthread/SDL_sysmutex.c	Tue Mar 05 18:54:55 2013 -0500
     4.3 @@ -22,6 +22,7 @@
     4.4  
     4.5  #define _GNU_SOURCE
     4.6  #include <pthread.h>
     4.7 +#include <errno.h>
     4.8  
     4.9  #include "SDL_thread.h"
    4.10  
    4.11 @@ -118,6 +119,52 @@
    4.12  }
    4.13  
    4.14  int
    4.15 +SDL_TryLockMutex(SDL_mutex * mutex)
    4.16 +{
    4.17 +    int retval;
    4.18 +#if FAKE_RECURSIVE_MUTEX
    4.19 +    pthread_t this_thread;
    4.20 +#endif
    4.21 +
    4.22 +    if (mutex == NULL) {
    4.23 +        SDL_SetError("Passed a NULL mutex");
    4.24 +        return -1;
    4.25 +    }
    4.26 +
    4.27 +    retval = 0;
    4.28 +#if FAKE_RECURSIVE_MUTEX
    4.29 +    this_thread = pthread_self();
    4.30 +    if (mutex->owner == this_thead) {
    4.31 +        ++mutex->recursive;
    4.32 +    } else {
    4.33 +        /* The order of operations is important.
    4.34 +         We set the locking thread id after we obtain the lock
    4.35 +         so unlocks from other threads will fail.
    4.36 +         */
    4.37 +        if (pthread_mutex_lock(&mutex->id) == 0) {
    4.38 +            mutex->owner = this_thread;
    4.39 +            mutex->recursive = 0;
    4.40 +        } else if (errno == EBUSY) {
    4.41 +            retval = SDL_MUTEX_TIMEDOUT;
    4.42 +        } else {
    4.43 +            SDL_SetError("pthread_mutex_trylock() failed");
    4.44 +            retval = -1;
    4.45 +        }
    4.46 +    }
    4.47 +#else
    4.48 +    if (pthread_mutex_trylock(&mutex->id) != 0) {
    4.49 +        if (errno == EBUSY) {
    4.50 +            retval = SDL_MUTEX_TIMEDOUT;
    4.51 +        } else {
    4.52 +            SDL_SetError("pthread_mutex_trylock() failed");
    4.53 +            retval = -1;
    4.54 +        }
    4.55 +    }
    4.56 +#endif
    4.57 +    return retval;
    4.58 +}
    4.59 +
    4.60 +int
    4.61  SDL_mutexV(SDL_mutex * mutex)
    4.62  {
    4.63      int retval;
     5.1 --- a/src/thread/windows/SDL_sysmutex.c	Tue Mar 05 18:01:59 2013 -0500
     5.2 +++ b/src/thread/windows/SDL_sysmutex.c	Tue Mar 05 18:54:55 2013 -0500
     5.3 @@ -75,6 +75,22 @@
     5.4      return (0);
     5.5  }
     5.6  
     5.7 +/* TryLock the mutex */
     5.8 +int
     5.9 +SDL_TryLockMutex(SDL_mutex * mutex)
    5.10 +{
    5.11 +    if (mutex == NULL) {
    5.12 +        SDL_SetError("Passed a NULL mutex");
    5.13 +        return -1;
    5.14 +    }
    5.15 +
    5.16 +    int retval = 0;
    5.17 +    if (TryEnterCriticalSection(&mutex->cs) == 0) {
    5.18 +        retval = SDL_MUTEX_TIMEDOUT;
    5.19 +    }
    5.20 +    return retval;
    5.21 +}
    5.22 +
    5.23  /* Unlock the mutex */
    5.24  int
    5.25  SDL_mutexV(SDL_mutex * mutex)