comparison src/thread/riscos/SDL_sysmutex.c @ 1361:19418e4422cb

New configure-based build system. Still work in progress, but much improved
author Sam Lantinga <slouken@libsdl.org>
date Thu, 16 Feb 2006 10:11:48 +0000
parents c71e05b4dc2e
children d910939febfa
comparison
equal deleted inserted replaced
1360:70a9cfb4cf1b 1361:19418e4422cb
20 slouken@libsdl.org 20 slouken@libsdl.org
21 */ 21 */
22 22
23 /* RISC OS implementations uses pthreads based on linux code */ 23 /* RISC OS implementations uses pthreads based on linux code */
24 24
25 #ifdef DISABLE_THREADS 25 #include "SDL_thread.h"
26
27 #if SDL_THREADS_DISABLED
26 #include "../generic/SDL_sysmutex.c" 28 #include "../generic/SDL_sysmutex.c"
27 #else 29 #else
28 30
29 #include <pthread.h> 31 #include <pthread.h>
30 32
31 #include "SDL_thread.h"
32
33 struct SDL_mutex { 33 struct SDL_mutex {
34 pthread_mutex_t id; 34 pthread_mutex_t id;
35 #ifdef PTHREAD_NO_RECURSIVE_MUTEX 35 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
36 int recursive; 36 int recursive;
37 pthread_t owner; 37 pthread_t owner;
38 #endif 38 #endif
39 }; 39 };
40 40
45 45
46 /* Allocate the structure */ 46 /* Allocate the structure */
47 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex)); 47 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex));
48 if ( mutex ) { 48 if ( mutex ) {
49 pthread_mutexattr_init(&attr); 49 pthread_mutexattr_init(&attr);
50 #ifdef PTHREAD_NO_RECURSIVE_MUTEX 50 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
51 /* No extra attributes necessary */ 51 /* No extra attributes necessary */
52 #else 52 #else
53 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); 53 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
54 #endif /* PTHREAD_NO_RECURSIVE_MUTEX */ 54 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
55 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) { 55 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) {
56 SDL_SetError("pthread_mutex_init() failed"); 56 SDL_SetError("pthread_mutex_init() failed");
57 SDL_free(mutex); 57 SDL_free(mutex);
58 mutex = NULL; 58 mutex = NULL;
59 } 59 }
73 73
74 /* Lock the mutex */ 74 /* Lock the mutex */
75 int SDL_mutexP(SDL_mutex *mutex) 75 int SDL_mutexP(SDL_mutex *mutex)
76 { 76 {
77 int retval; 77 int retval;
78 #ifdef PTHREAD_NO_RECURSIVE_MUTEX 78 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
79 pthread_t this_thread; 79 pthread_t this_thread;
80 #endif 80 #endif
81 81
82 if ( mutex == NULL ) { 82 if ( mutex == NULL ) {
83 SDL_SetError("Passed a NULL mutex"); 83 SDL_SetError("Passed a NULL mutex");
84 return -1; 84 return -1;
85 } 85 }
86 86
87 retval = 0; 87 retval = 0;
88 #ifdef PTHREAD_NO_RECURSIVE_MUTEX 88 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
89 this_thread = pthread_self(); 89 this_thread = pthread_self();
90 if ( mutex->owner == this_thread ) { 90 if ( mutex->owner == this_thread ) {
91 ++mutex->recursive; 91 ++mutex->recursive;
92 } else { 92 } else {
93 /* The order of operations is important. 93 /* The order of operations is important.
119 SDL_SetError("Passed a NULL mutex"); 119 SDL_SetError("Passed a NULL mutex");
120 return -1; 120 return -1;
121 } 121 }
122 122
123 retval = 0; 123 retval = 0;
124 #ifdef PTHREAD_NO_RECURSIVE_MUTEX 124 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX
125 /* We can only unlock the mutex if we own it */ 125 /* We can only unlock the mutex if we own it */
126 if ( pthread_self() == mutex->owner ) { 126 if ( pthread_self() == mutex->owner ) {
127 if ( mutex->recursive ) { 127 if ( mutex->recursive ) {
128 --mutex->recursive; 128 --mutex->recursive;
129 } else { 129 } else {
143 #else 143 #else
144 if ( pthread_mutex_unlock(&mutex->id) < 0 ) { 144 if ( pthread_mutex_unlock(&mutex->id) < 0 ) {
145 SDL_SetError("pthread_mutex_unlock() failed"); 145 SDL_SetError("pthread_mutex_unlock() failed");
146 retval = -1; 146 retval = -1;
147 } 147 }
148 #endif /* PTHREAD_NO_RECURSIVE_MUTEX */ 148 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */
149 149
150 return retval; 150 return retval;
151 } 151 }
152 #endif 152 #endif