slouken@1361
|
1 |
/*
|
slouken@5535
|
2 |
Simple DirectMedia Layer
|
slouken@6885
|
3 |
Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
|
slouken@1361
|
4 |
|
slouken@5535
|
5 |
This software is provided 'as-is', without any express or implied
|
slouken@5535
|
6 |
warranty. In no event will the authors be held liable for any damages
|
slouken@5535
|
7 |
arising from the use of this software.
|
slouken@1361
|
8 |
|
slouken@5535
|
9 |
Permission is granted to anyone to use this software for any purpose,
|
slouken@5535
|
10 |
including commercial applications, and to alter it and redistribute it
|
slouken@5535
|
11 |
freely, subject to the following restrictions:
|
slouken@1361
|
12 |
|
slouken@5535
|
13 |
1. The origin of this software must not be misrepresented; you must not
|
slouken@5535
|
14 |
claim that you wrote the original software. If you use this software
|
slouken@5535
|
15 |
in a product, an acknowledgment in the product documentation would be
|
slouken@5535
|
16 |
appreciated but is not required.
|
slouken@5535
|
17 |
2. Altered source versions must be plainly marked as such, and must not be
|
slouken@5535
|
18 |
misrepresented as being the original software.
|
slouken@5535
|
19 |
3. This notice may not be removed or altered from any source distribution.
|
slouken@1361
|
20 |
*/
|
slouken@1402
|
21 |
#include "SDL_config.h"
|
slouken@1361
|
22 |
|
slouken@3605
|
23 |
#define _GNU_SOURCE
|
slouken@1361
|
24 |
#include <pthread.h>
|
urkle@6966
|
25 |
#include <errno.h>
|
slouken@1361
|
26 |
|
slouken@1361
|
27 |
#include "SDL_thread.h"
|
slouken@1361
|
28 |
|
slouken@1361
|
29 |
#if !SDL_THREAD_PTHREAD_RECURSIVE_MUTEX && \
|
slouken@1361
|
30 |
!SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
|
slouken@5481
|
31 |
#define FAKE_RECURSIVE_MUTEX 1
|
slouken@1361
|
32 |
#endif
|
slouken@1361
|
33 |
|
slouken@1895
|
34 |
struct SDL_mutex
|
slouken@1895
|
35 |
{
|
slouken@1895
|
36 |
pthread_mutex_t id;
|
slouken@1361
|
37 |
#if FAKE_RECURSIVE_MUTEX
|
slouken@1895
|
38 |
int recursive;
|
slouken@1895
|
39 |
pthread_t owner;
|
slouken@1361
|
40 |
#endif
|
slouken@1361
|
41 |
};
|
slouken@1361
|
42 |
|
slouken@1895
|
43 |
SDL_mutex *
|
slouken@1895
|
44 |
SDL_CreateMutex(void)
|
slouken@1361
|
45 |
{
|
slouken@1895
|
46 |
SDL_mutex *mutex;
|
slouken@1895
|
47 |
pthread_mutexattr_t attr;
|
slouken@1361
|
48 |
|
slouken@1895
|
49 |
/* Allocate the structure */
|
slouken@1895
|
50 |
mutex = (SDL_mutex *) SDL_calloc(1, sizeof(*mutex));
|
slouken@1895
|
51 |
if (mutex) {
|
slouken@1895
|
52 |
pthread_mutexattr_init(&attr);
|
slouken@1361
|
53 |
#if SDL_THREAD_PTHREAD_RECURSIVE_MUTEX
|
slouken@1895
|
54 |
pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
|
slouken@1361
|
55 |
#elif SDL_THREAD_PTHREAD_RECURSIVE_MUTEX_NP
|
slouken@1895
|
56 |
pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
|
slouken@1361
|
57 |
#else
|
slouken@1895
|
58 |
/* No extra attributes necessary */
|
slouken@1361
|
59 |
#endif
|
slouken@1895
|
60 |
if (pthread_mutex_init(&mutex->id, &attr) != 0) {
|
slouken@1895
|
61 |
SDL_SetError("pthread_mutex_init() failed");
|
slouken@1895
|
62 |
SDL_free(mutex);
|
slouken@1895
|
63 |
mutex = NULL;
|
slouken@1895
|
64 |
}
|
slouken@1895
|
65 |
} else {
|
slouken@1895
|
66 |
SDL_OutOfMemory();
|
slouken@1895
|
67 |
}
|
slouken@1895
|
68 |
return (mutex);
|
slouken@1361
|
69 |
}
|
slouken@1361
|
70 |
|
slouken@1895
|
71 |
void
|
slouken@1895
|
72 |
SDL_DestroyMutex(SDL_mutex * mutex)
|
slouken@1361
|
73 |
{
|
slouken@1895
|
74 |
if (mutex) {
|
slouken@1895
|
75 |
pthread_mutex_destroy(&mutex->id);
|
slouken@1895
|
76 |
SDL_free(mutex);
|
slouken@1895
|
77 |
}
|
slouken@1361
|
78 |
}
|
slouken@1361
|
79 |
|
slouken@1361
|
80 |
/* Lock the mutex */
|
slouken@1895
|
81 |
int
|
slouken@6977
|
82 |
SDL_LockMutex(SDL_mutex * mutex)
|
slouken@1361
|
83 |
{
|
slouken@1895
|
84 |
int retval;
|
slouken@1361
|
85 |
#if FAKE_RECURSIVE_MUTEX
|
slouken@1895
|
86 |
pthread_t this_thread;
|
slouken@1361
|
87 |
#endif
|
slouken@1361
|
88 |
|
slouken@1895
|
89 |
if (mutex == NULL) {
|
slouken@1895
|
90 |
SDL_SetError("Passed a NULL mutex");
|
slouken@1895
|
91 |
return -1;
|
slouken@1895
|
92 |
}
|
slouken@1361
|
93 |
|
slouken@1895
|
94 |
retval = 0;
|
slouken@1361
|
95 |
#if FAKE_RECURSIVE_MUTEX
|
slouken@1895
|
96 |
this_thread = pthread_self();
|
slouken@1895
|
97 |
if (mutex->owner == this_thread) {
|
slouken@1895
|
98 |
++mutex->recursive;
|
slouken@1895
|
99 |
} else {
|
slouken@1895
|
100 |
/* The order of operations is important.
|
slouken@1895
|
101 |
We set the locking thread id after we obtain the lock
|
slouken@1895
|
102 |
so unlocks from other threads will fail.
|
slouken@1895
|
103 |
*/
|
slouken@1895
|
104 |
if (pthread_mutex_lock(&mutex->id) == 0) {
|
slouken@1895
|
105 |
mutex->owner = this_thread;
|
slouken@1895
|
106 |
mutex->recursive = 0;
|
slouken@1895
|
107 |
} else {
|
slouken@1895
|
108 |
SDL_SetError("pthread_mutex_lock() failed");
|
slouken@1895
|
109 |
retval = -1;
|
slouken@1895
|
110 |
}
|
slouken@1895
|
111 |
}
|
slouken@1361
|
112 |
#else
|
slouken@1895
|
113 |
if (pthread_mutex_lock(&mutex->id) < 0) {
|
slouken@1895
|
114 |
SDL_SetError("pthread_mutex_lock() failed");
|
slouken@1895
|
115 |
retval = -1;
|
slouken@1895
|
116 |
}
|
slouken@1361
|
117 |
#endif
|
slouken@1895
|
118 |
return retval;
|
slouken@1361
|
119 |
}
|
slouken@1361
|
120 |
|
slouken@1895
|
121 |
int
|
urkle@6966
|
122 |
SDL_TryLockMutex(SDL_mutex * mutex)
|
urkle@6966
|
123 |
{
|
urkle@6966
|
124 |
int retval;
|
urkle@6966
|
125 |
#if FAKE_RECURSIVE_MUTEX
|
urkle@6966
|
126 |
pthread_t this_thread;
|
urkle@6966
|
127 |
#endif
|
urkle@6966
|
128 |
|
urkle@6966
|
129 |
if (mutex == NULL) {
|
urkle@6966
|
130 |
SDL_SetError("Passed a NULL mutex");
|
urkle@6966
|
131 |
return -1;
|
urkle@6966
|
132 |
}
|
urkle@6966
|
133 |
|
urkle@6966
|
134 |
retval = 0;
|
urkle@6966
|
135 |
#if FAKE_RECURSIVE_MUTEX
|
urkle@6966
|
136 |
this_thread = pthread_self();
|
aschiffler@6985
|
137 |
if (mutex->owner == this_thread) {
|
urkle@6966
|
138 |
++mutex->recursive;
|
urkle@6966
|
139 |
} else {
|
urkle@6966
|
140 |
/* The order of operations is important.
|
urkle@6966
|
141 |
We set the locking thread id after we obtain the lock
|
urkle@6966
|
142 |
so unlocks from other threads will fail.
|
urkle@6966
|
143 |
*/
|
urkle@6966
|
144 |
if (pthread_mutex_lock(&mutex->id) == 0) {
|
urkle@6966
|
145 |
mutex->owner = this_thread;
|
urkle@6966
|
146 |
mutex->recursive = 0;
|
urkle@6966
|
147 |
} else if (errno == EBUSY) {
|
urkle@6966
|
148 |
retval = SDL_MUTEX_TIMEDOUT;
|
urkle@6966
|
149 |
} else {
|
urkle@6966
|
150 |
SDL_SetError("pthread_mutex_trylock() failed");
|
urkle@6966
|
151 |
retval = -1;
|
urkle@6966
|
152 |
}
|
urkle@6966
|
153 |
}
|
urkle@6966
|
154 |
#else
|
urkle@6966
|
155 |
if (pthread_mutex_trylock(&mutex->id) != 0) {
|
urkle@6966
|
156 |
if (errno == EBUSY) {
|
urkle@6966
|
157 |
retval = SDL_MUTEX_TIMEDOUT;
|
urkle@6966
|
158 |
} else {
|
urkle@6966
|
159 |
SDL_SetError("pthread_mutex_trylock() failed");
|
urkle@6966
|
160 |
retval = -1;
|
urkle@6966
|
161 |
}
|
urkle@6966
|
162 |
}
|
urkle@6966
|
163 |
#endif
|
urkle@6966
|
164 |
return retval;
|
urkle@6966
|
165 |
}
|
urkle@6966
|
166 |
|
urkle@6966
|
167 |
int
|
slouken@6977
|
168 |
SDL_UnlockMutex(SDL_mutex * mutex)
|
slouken@1361
|
169 |
{
|
slouken@1895
|
170 |
int retval;
|
slouken@1361
|
171 |
|
slouken@1895
|
172 |
if (mutex == NULL) {
|
slouken@1895
|
173 |
SDL_SetError("Passed a NULL mutex");
|
slouken@1895
|
174 |
return -1;
|
slouken@1895
|
175 |
}
|
slouken@1361
|
176 |
|
slouken@1895
|
177 |
retval = 0;
|
slouken@1361
|
178 |
#if FAKE_RECURSIVE_MUTEX
|
slouken@1895
|
179 |
/* We can only unlock the mutex if we own it */
|
slouken@1895
|
180 |
if (pthread_self() == mutex->owner) {
|
slouken@1895
|
181 |
if (mutex->recursive) {
|
slouken@1895
|
182 |
--mutex->recursive;
|
slouken@1895
|
183 |
} else {
|
slouken@1895
|
184 |
/* The order of operations is important.
|
slouken@1895
|
185 |
First reset the owner so another thread doesn't lock
|
slouken@1895
|
186 |
the mutex and set the ownership before we reset it,
|
slouken@1895
|
187 |
then release the lock semaphore.
|
slouken@1895
|
188 |
*/
|
slouken@1895
|
189 |
mutex->owner = 0;
|
slouken@1895
|
190 |
pthread_mutex_unlock(&mutex->id);
|
slouken@1895
|
191 |
}
|
slouken@1895
|
192 |
} else {
|
slouken@1895
|
193 |
SDL_SetError("mutex not owned by this thread");
|
slouken@1895
|
194 |
retval = -1;
|
slouken@1895
|
195 |
}
|
slouken@1361
|
196 |
|
slouken@1361
|
197 |
#else
|
slouken@1895
|
198 |
if (pthread_mutex_unlock(&mutex->id) < 0) {
|
slouken@1895
|
199 |
SDL_SetError("pthread_mutex_unlock() failed");
|
slouken@1895
|
200 |
retval = -1;
|
slouken@1895
|
201 |
}
|
slouken@1361
|
202 |
#endif /* FAKE_RECURSIVE_MUTEX */
|
slouken@1361
|
203 |
|
slouken@1895
|
204 |
return retval;
|
slouken@1361
|
205 |
}
|
slouken@1895
|
206 |
|
slouken@1895
|
207 |
/* vi: set ts=4 sw=4 expandtab: */
|