2 SDL - Simple DirectMedia Layer
3 Copyright (C) 1997-2009 Sam Lantinga
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include "SDL_config.h"
24 /* RISC OS implementations uses pthreads based on linux code */
26 #include "SDL_thread.h"
28 #if SDL_THREADS_DISABLED
29 #include "../generic/SDL_sysmutex.c"
36 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
42 SDL_mutex *SDL_CreateMutex (void)
45 pthread_mutexattr_t attr;
47 /* Allocate the structure */
48 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
50 pthread_mutexattr_init(&attr);
51 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
52 /* No extra attributes necessary */
54 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
55 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
56 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
57 SDL_SetError("pthread_mutex_init() failed");
67 void SDL_DestroyMutex(SDL_mutex *mutex)
70 pthread_mutex_destroy(&mutex->id);
76 int SDL_mutexP(SDL_mutex *mutex)
79 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
80 pthread_t this_thread;
83 if ( mutex == NULL ) {
84 SDL_SetError("Passed a NULL mutex");
89 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
90 this_thread = pthread_self();
91 if ( mutex->owner == this_thread ) {
94 /* The order of operations is important.
95 We set the locking thread id after we obtain the lock
96 so unlocks from other threads will fail.
98 if ( pthread_mutex_lock(&mutex->id) == 0 ) {
99 mutex->owner = this_thread;
100 mutex->recursive = 0;
102 SDL_SetError("pthread_mutex_lock() failed");
107 if ( pthread_mutex_lock(&mutex->id) < 0 ) {
108 SDL_SetError("pthread_mutex_lock() failed");
115 int SDL_mutexV(SDL_mutex *mutex)
119 if ( mutex == NULL ) {
120 SDL_SetError("Passed a NULL mutex");
125 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
126 /* We can only unlock the mutex if we own it */
127 if ( pthread_self() == mutex->owner ) {
128 if ( mutex->recursive ) {
131 /* The order of operations is important.
132 First reset the owner so another thread doesn't lock
133 the mutex and set the ownership before we reset it,
134 then release the lock semaphore.
137 pthread_mutex_unlock(&mutex->id);
140 SDL_SetError("mutex not owned by this thread");
145 if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
146 SDL_SetError("pthread_mutex_unlock() failed");
149 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */