27 #include "SDL_thread.h"
29 #if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
30 !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
31 #define FAKE_RECURSIVE_MUTEX 1
37 #if FAKE_RECURSIVE_MUTEX
47 pthread_mutexattr_t attr;
52 pthread_mutexattr_init(&attr);
53 #if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
54 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
55 #elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
56 pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
60 if (pthread_mutex_init(&mutex->id, &attr) != 0) {
75 pthread_mutex_destroy(&mutex->id);
84 #if FAKE_RECURSIVE_MUTEX
85 pthread_t this_thread;
92 #if FAKE_RECURSIVE_MUTEX
93 this_thread = pthread_self();
94 if (mutex->owner == this_thread) {
101 if (pthread_mutex_lock(&mutex->id) == 0) {
102 mutex->owner = this_thread;
103 mutex->recursive = 0;
109 if (pthread_mutex_lock(&mutex->id) < 0) {
120 #if FAKE_RECURSIVE_MUTEX
121 pthread_t this_thread;
129 #if FAKE_RECURSIVE_MUTEX
130 this_thread = pthread_self();
131 if (mutex->owner == this_thread) {
138 if (pthread_mutex_lock(&mutex->id) == 0) {
139 mutex->owner = this_thread;
140 mutex->recursive = 0;
141 }
else if (errno == EBUSY) {
144 retval =
SDL_SetError(
"pthread_mutex_trylock() failed");
148 if (pthread_mutex_trylock(&mutex->id) != 0) {
149 if (errno == EBUSY) {
152 retval =
SDL_SetError(
"pthread_mutex_trylock() failed");
166 #if FAKE_RECURSIVE_MUTEX
168 if (pthread_self() == mutex->owner) {
169 if (mutex->recursive) {
178 pthread_mutex_unlock(&mutex->id);
185 if (pthread_mutex_unlock(&mutex->id) < 0) {
DECLSPEC void *SDLCALL SDL_calloc(size_t nmemb, size_t size)
DECLSPEC int SDLCALL SDL_LockMutex(SDL_mutex *mutex)
DECLSPEC SDL_mutex *SDLCALL SDL_CreateMutex(void)
DECLSPEC void SDLCALL SDL_free(void *mem)
DECLSPEC int SDLCALL SDL_UnlockMutex(SDL_mutex *mutex)
DECLSPEC int SDLCALL SDL_TryLockMutex(SDL_mutex *mutex)
DECLSPEC int SDLCALL SDL_SetError(const char *fmt,...)
#define SDL_OutOfMemory()
struct SDL_mutex SDL_mutex
DECLSPEC void SDLCALL SDL_DestroyMutex(SDL_mutex *mutex)
#define SDL_MUTEX_TIMEDOUT