2 Simple DirectMedia Layer
3 Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
21 #include "SDL_config.h"
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;
49 /* Allocate the structure */
50 mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
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);
58 /* No extra attributes necessary */
60 if (pthread_mutex_init(&mutex->id, &attr) != 0) {
61 SDL_SetError("pthread_mutex_init() failed");
72 SDL_DestroyMutex(SDL_mutex * mutex)
75 pthread_mutex_destroy(&mutex->id);
82 SDL_LockMutex(SDL_mutex * mutex)
85 #if FAKE_RECURSIVE_MUTEX
86 pthread_t this_thread;
90 SDL_SetError("Passed a NULL mutex");
95 #if FAKE_RECURSIVE_MUTEX
96 this_thread = pthread_self();
97 if (mutex->owner == this_thread) {
100 /* The order of operations is important.
101 We set the locking thread id after we obtain the lock
102 so unlocks from other threads will fail.
104 if (pthread_mutex_lock(&mutex->id) == 0) {
105 mutex->owner = this_thread;
106 mutex->recursive = 0;
108 SDL_SetError("pthread_mutex_lock() failed");
113 if (pthread_mutex_lock(&mutex->id) < 0) {
114 SDL_SetError("pthread_mutex_lock() failed");
122 SDL_TryLockMutex(SDL_mutex * mutex)
125 #if FAKE_RECURSIVE_MUTEX
126 pthread_t this_thread;
130 SDL_SetError("Passed a NULL mutex");
135 #if FAKE_RECURSIVE_MUTEX
136 this_thread = pthread_self();
137 if (mutex->owner == this_thread) {
140 /* The order of operations is important.
141 We set the locking thread id after we obtain the lock
142 so unlocks from other threads will fail.
144 if (pthread_mutex_lock(&mutex->id) == 0) {
145 mutex->owner = this_thread;
146 mutex->recursive = 0;
147 } else if (errno == EBUSY) {
148 retval = SDL_MUTEX_TIMEDOUT;
150 SDL_SetError("pthread_mutex_trylock() failed");
155 if (pthread_mutex_trylock(&mutex->id) != 0) {
156 if (errno == EBUSY) {
157 retval = SDL_MUTEX_TIMEDOUT;
159 SDL_SetError("pthread_mutex_trylock() failed");
168 SDL_UnlockMutex(SDL_mutex * mutex)
173 SDL_SetError("Passed a NULL mutex");
178 #if FAKE_RECURSIVE_MUTEX
179 /* We can only unlock the mutex if we own it */
180 if (pthread_self() == mutex->owner) {
181 if (mutex->recursive) {
184 /* The order of operations is important.
185 First reset the owner so another thread doesn't lock
186 the mutex and set the ownership before we reset it,
187 then release the lock semaphore.
190 pthread_mutex_unlock(&mutex->id);
193 SDL_SetError("mutex not owned by this thread");
198 if (pthread_mutex_unlock(&mutex->id) < 0) {
199 SDL_SetError("pthread_mutex_unlock() failed");
202 #endif /* FAKE_RECURSIVE_MUTEX */
207 /* vi: set ts=4 sw=4 expandtab: */