2 Simple DirectMedia Layer
3 Copyright (C) 1997-2016 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_internal.h"
29 #include "SDL_thread.h"
31 #if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
32 !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
33 #define FAKE_RECURSIVE_MUTEX 1
39 #if FAKE_RECURSIVE_MUTEX
49 pthread_mutexattr_t attr;
51 /* Allocate the structure */
52 mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
54 pthread_mutexattr_init(&attr);
55 #if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
56 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
57 #elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
58 pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
60 /* No extra attributes necessary */
62 if (pthread_mutex_init(&mutex->id, &attr) != 0) {
63 SDL_SetError("pthread_mutex_init() failed");
74 SDL_DestroyMutex(SDL_mutex * mutex)
77 pthread_mutex_destroy(&mutex->id);
84 SDL_LockMutex(SDL_mutex * mutex)
86 #if FAKE_RECURSIVE_MUTEX
87 pthread_t this_thread;
91 return SDL_SetError("Passed a NULL mutex");
94 #if FAKE_RECURSIVE_MUTEX
95 this_thread = pthread_self();
96 if (mutex->owner == this_thread) {
99 /* The order of operations is important.
100 We set the locking thread id after we obtain the lock
101 so unlocks from other threads will fail.
103 if (pthread_mutex_lock(&mutex->id) == 0) {
104 mutex->owner = this_thread;
105 mutex->recursive = 0;
107 return SDL_SetError("pthread_mutex_lock() failed");
111 if (pthread_mutex_lock(&mutex->id) < 0) {
112 return SDL_SetError("pthread_mutex_lock() failed");
119 SDL_TryLockMutex(SDL_mutex * mutex)
122 #if FAKE_RECURSIVE_MUTEX
123 pthread_t this_thread;
127 return SDL_SetError("Passed a NULL mutex");
131 #if FAKE_RECURSIVE_MUTEX
132 this_thread = pthread_self();
133 if (mutex->owner == this_thread) {
136 /* The order of operations is important.
137 We set the locking thread id after we obtain the lock
138 so unlocks from other threads will fail.
140 if (pthread_mutex_lock(&mutex->id) == 0) {
141 mutex->owner = this_thread;
142 mutex->recursive = 0;
143 } else if (errno == EBUSY) {
144 retval = SDL_MUTEX_TIMEDOUT;
146 retval = SDL_SetError("pthread_mutex_trylock() failed");
150 if (pthread_mutex_trylock(&mutex->id) != 0) {
151 if (errno == EBUSY) {
152 retval = SDL_MUTEX_TIMEDOUT;
154 retval = SDL_SetError("pthread_mutex_trylock() failed");
162 SDL_UnlockMutex(SDL_mutex * mutex)
165 return SDL_SetError("Passed a NULL mutex");
168 #if FAKE_RECURSIVE_MUTEX
169 /* We can only unlock the mutex if we own it */
170 if (pthread_self() == mutex->owner) {
171 if (mutex->recursive) {
174 /* The order of operations is important.
175 First reset the owner so another thread doesn't lock
176 the mutex and set the ownership before we reset it,
177 then release the lock semaphore.
180 pthread_mutex_unlock(&mutex->id);
183 return SDL_SetError("mutex not owned by this thread");
187 if (pthread_mutex_unlock(&mutex->id) < 0) {
188 return SDL_SetError("pthread_mutex_unlock() failed");
190 #endif /* FAKE_RECURSIVE_MUTEX */
195 /* vi: set ts=4 sw=4 expandtab: */