Mercurial > sdl-ios-xcode
comparison src/thread/riscos/SDL_sysmutex.c @ 1895:c121d94672cb
SDL 1.2 is moving to a branch, and SDL 1.3 is becoming the head.
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Mon, 10 Jul 2006 21:04:37 +0000 |
parents | d910939febfa |
children | 99210400e8b9 |
comparison
equal
deleted
inserted
replaced
1894:c69cee13dd76 | 1895:c121d94672cb |
---|---|
29 #include "../generic/SDL_sysmutex.c" | 29 #include "../generic/SDL_sysmutex.c" |
30 #else | 30 #else |
31 | 31 |
32 #include <pthread.h> | 32 #include <pthread.h> |
33 | 33 |
34 struct SDL_mutex { | 34 struct SDL_mutex |
35 pthread_mutex_t id; | 35 { |
36 pthread_mutex_t id; | |
36 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX | 37 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX |
37 int recursive; | 38 int recursive; |
38 pthread_t owner; | 39 pthread_t owner; |
39 #endif | 40 #endif |
40 }; | 41 }; |
41 | 42 |
42 SDL_mutex *SDL_CreateMutex (void) | 43 SDL_mutex * |
44 SDL_CreateMutex(void) | |
43 { | 45 { |
44 SDL_mutex *mutex; | 46 SDL_mutex *mutex; |
45 pthread_mutexattr_t attr; | 47 pthread_mutexattr_t attr; |
46 | 48 |
47 /* Allocate the structure */ | 49 /* Allocate the structure */ |
48 mutex = (SDL_mutex *)SDL_calloc(1, sizeof(*mutex)); | 50 mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex)); |
49 if ( mutex ) { | 51 if (mutex) { |
50 pthread_mutexattr_init(&attr); | 52 pthread_mutexattr_init(&attr); |
51 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX | 53 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX |
52 /* No extra attributes necessary */ | 54 /* No extra attributes necessary */ |
53 #else | 55 #else |
54 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | 56 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); |
55 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */ | 57 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */ |
56 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) { | 58 if (pthread_mutex_init(&mutex->id, &attr) != 0) { |
57 SDL_SetError("pthread_mutex_init() failed"); | 59 SDL_SetError("pthread_mutex_init() failed"); |
58 SDL_free(mutex); | 60 SDL_free(mutex); |
59 mutex = NULL; | 61 mutex = NULL; |
60 } | 62 } |
61 } else { | 63 } else { |
62 SDL_OutOfMemory(); | 64 SDL_OutOfMemory(); |
63 } | 65 } |
64 return(mutex); | 66 return (mutex); |
65 } | 67 } |
66 | 68 |
67 void SDL_DestroyMutex(SDL_mutex *mutex) | 69 void |
70 SDL_DestroyMutex(SDL_mutex * mutex) | |
68 { | 71 { |
69 if ( mutex ) { | 72 if (mutex) { |
70 pthread_mutex_destroy(&mutex->id); | 73 pthread_mutex_destroy(&mutex->id); |
71 SDL_free(mutex); | 74 SDL_free(mutex); |
72 } | 75 } |
73 } | 76 } |
74 | 77 |
75 /* Lock the mutex */ | 78 /* Lock the mutex */ |
76 int SDL_mutexP(SDL_mutex *mutex) | 79 int |
80 SDL_mutexP(SDL_mutex * mutex) | |
77 { | 81 { |
78 int retval; | 82 int retval; |
79 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX | 83 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX |
80 pthread_t this_thread; | 84 pthread_t this_thread; |
81 #endif | 85 #endif |
82 | 86 |
83 if ( mutex == NULL ) { | 87 if (mutex == NULL) { |
84 SDL_SetError("Passed a NULL mutex"); | 88 SDL_SetError("Passed a NULL mutex"); |
85 return -1; | 89 return -1; |
86 } | 90 } |
87 | 91 |
88 retval = 0; | 92 retval = 0; |
89 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX | 93 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX |
90 this_thread = pthread_self(); | 94 this_thread = pthread_self(); |
91 if ( mutex->owner == this_thread ) { | 95 if (mutex->owner == this_thread) { |
92 ++mutex->recursive; | 96 ++mutex->recursive; |
93 } else { | 97 } else { |
94 /* The order of operations is important. | 98 /* The order of operations is important. |
95 We set the locking thread id after we obtain the lock | 99 We set the locking thread id after we obtain the lock |
96 so unlocks from other threads will fail. | 100 so unlocks from other threads will fail. |
97 */ | 101 */ |
98 if ( pthread_mutex_lock(&mutex->id) == 0 ) { | 102 if (pthread_mutex_lock(&mutex->id) == 0) { |
99 mutex->owner = this_thread; | 103 mutex->owner = this_thread; |
100 mutex->recursive = 0; | 104 mutex->recursive = 0; |
101 } else { | 105 } else { |
102 SDL_SetError("pthread_mutex_lock() failed"); | 106 SDL_SetError("pthread_mutex_lock() failed"); |
103 retval = -1; | 107 retval = -1; |
104 } | 108 } |
105 } | 109 } |
106 #else | 110 #else |
107 if ( pthread_mutex_lock(&mutex->id) < 0 ) { | 111 if (pthread_mutex_lock(&mutex->id) < 0) { |
108 SDL_SetError("pthread_mutex_lock() failed"); | 112 SDL_SetError("pthread_mutex_lock() failed"); |
109 retval = -1; | 113 retval = -1; |
110 } | 114 } |
111 #endif | 115 #endif |
112 return retval; | 116 return retval; |
113 } | 117 } |
114 | 118 |
115 int SDL_mutexV(SDL_mutex *mutex) | 119 int |
120 SDL_mutexV(SDL_mutex * mutex) | |
116 { | 121 { |
117 int retval; | 122 int retval; |
118 | 123 |
119 if ( mutex == NULL ) { | 124 if (mutex == NULL) { |
120 SDL_SetError("Passed a NULL mutex"); | 125 SDL_SetError("Passed a NULL mutex"); |
121 return -1; | 126 return -1; |
122 } | 127 } |
123 | 128 |
124 retval = 0; | 129 retval = 0; |
125 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX | 130 #if SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX |
126 /* We can only unlock the mutex if we own it */ | 131 /* We can only unlock the mutex if we own it */ |
127 if ( pthread_self() == mutex->owner ) { | 132 if (pthread_self() == mutex->owner) { |
128 if ( mutex->recursive ) { | 133 if (mutex->recursive) { |
129 --mutex->recursive; | 134 --mutex->recursive; |
130 } else { | 135 } else { |
131 /* The order of operations is important. | 136 /* The order of operations is important. |
132 First reset the owner so another thread doesn't lock | 137 First reset the owner so another thread doesn't lock |
133 the mutex and set the ownership before we reset it, | 138 the mutex and set the ownership before we reset it, |
134 then release the lock semaphore. | 139 then release the lock semaphore. |
135 */ | 140 */ |
136 mutex->owner = 0; | 141 mutex->owner = 0; |
137 pthread_mutex_unlock(&mutex->id); | 142 pthread_mutex_unlock(&mutex->id); |
138 } | 143 } |
139 } else { | 144 } else { |
140 SDL_SetError("mutex not owned by this thread"); | 145 SDL_SetError("mutex not owned by this thread"); |
141 retval = -1; | 146 retval = -1; |
142 } | 147 } |
143 | 148 |
144 #else | 149 #else |
145 if ( pthread_mutex_unlock(&mutex->id) < 0 ) { | 150 if (pthread_mutex_unlock(&mutex->id) < 0) { |
146 SDL_SetError("pthread_mutex_unlock() failed"); | 151 SDL_SetError("pthread_mutex_unlock() failed"); |
147 retval = -1; | 152 retval = -1; |
148 } | 153 } |
149 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */ | 154 #endif /* SDL_THREAD_PTHREAD_NO_RECURSIVE_MUTEX */ |
150 | 155 |
151 return retval; | 156 return retval; |
152 } | 157 } |
153 #endif | 158 #endif |
159 /* vi: set ts=4 sw=4 expandtab: */ |