Add SDL_TryLockMutex and implementations for all platforms
This commit is contained in:
parent
191718be44
commit
ac51aff110
5 changed files with 140 additions and 2 deletions
|
@ -22,6 +22,7 @@
|
|||
|
||||
#define _GNU_SOURCE
|
||||
#include <pthread.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "SDL_thread.h"
|
||||
|
||||
|
@ -117,6 +118,52 @@ SDL_mutexP(SDL_mutex * mutex)
|
|||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_TryLockMutex(SDL_mutex * mutex)
|
||||
{
|
||||
int retval;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
pthread_t this_thread;
|
||||
#endif
|
||||
|
||||
if (mutex == NULL) {
|
||||
SDL_SetError("Passed a NULL mutex");
|
||||
return -1;
|
||||
}
|
||||
|
||||
retval = 0;
|
||||
#if FAKE_RECURSIVE_MUTEX
|
||||
this_thread = pthread_self();
|
||||
if (mutex->owner == this_thead) {
|
||||
++mutex->recursive;
|
||||
} else {
|
||||
/* The order of operations is important.
|
||||
We set the locking thread id after we obtain the lock
|
||||
so unlocks from other threads will fail.
|
||||
*/
|
||||
if (pthread_mutex_lock(&mutex->id) == 0) {
|
||||
mutex->owner = this_thread;
|
||||
mutex->recursive = 0;
|
||||
} else if (errno == EBUSY) {
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
} else {
|
||||
SDL_SetError("pthread_mutex_trylock() failed");
|
||||
retval = -1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
if (pthread_mutex_trylock(&mutex->id) != 0) {
|
||||
if (errno == EBUSY) {
|
||||
retval = SDL_MUTEX_TIMEDOUT;
|
||||
} else {
|
||||
SDL_SetError("pthread_mutex_trylock() failed");
|
||||
retval = -1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
return retval;
|
||||
}
|
||||
|
||||
int
|
||||
SDL_mutexV(SDL_mutex * mutex)
|
||||
{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue