Mercurial > sdl-ios-xcode
annotate src/thread/linux/SDL_sysmutex.c @ 998:0e6627072f7a
Date: Wed, 24 Nov 2004 01:25:48 +0100
From: Stephane Marchesin
Subject: Re: [SDL] Problem compiling SDL 1.2.7
- there is a bug that was introduced in the kernel headers for 2.6.9
which is fixed in 2.6.10. This bug *will* byte when compiling the cdrom
subsystem. A patch that works around this bug is attached. Note that
users affected are not those running 2.6.9, but those using the 2.6.9
kernel headers for their system (i.e. whose libc is built against 2.6.9
headers).
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Tue, 30 Nov 2004 14:45:08 +0000 |
parents | b8d311d90021 |
children | c9b51268668f |
rev | line source |
---|---|
0 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
769
b8d311d90021
Updated copyright information for 2004 (Happy New Year!)
Sam Lantinga <slouken@libsdl.org>
parents:
297
diff
changeset
|
3 Copyright (C) 1997-2004 Sam Lantinga |
0 | 4 |
5 This library is free software; you can redistribute it and/or | |
6 modify it under the terms of the GNU Library General Public | |
7 License as published by the Free Software Foundation; either | |
8 version 2 of the License, or (at your option) any later version. | |
9 | |
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 Library General Public License for more details. | |
14 | |
15 You should have received a copy of the GNU Library General Public | |
16 License along with this library; if not, write to the Free | |
17 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
18 | |
19 Sam Lantinga | |
252
e8157fcb3114
Updated the source with the correct e-mail address
Sam Lantinga <slouken@libsdl.org>
parents:
0
diff
changeset
|
20 slouken@libsdl.org |
0 | 21 */ |
22 | |
23 #ifdef SAVE_RCSID | |
24 static char rcsid = | |
25 "@(#) $Id$"; | |
26 #endif | |
27 | |
28 #ifdef linux | |
29 /* Look to see if glibc is available, and if so, what version */ | |
30 #include <features.h> | |
31 | |
32 #if (__GLIBC__ == 2) && (__GLIBC_MINOR__ == 0) | |
33 #warning Working around a bug in glibc 2.0 pthreads | |
34 #undef SDL_USE_PTHREADS | |
35 /* The bug is actually a problem where threads are suspended, but don't | |
36 wake up when the thread manager sends them a signal. This is a problem | |
37 with thread creation too, but it happens less often. :-/ | |
38 We avoid this by using System V IPC for mutexes. | |
39 */ | |
40 #endif /* glibc 2.0 */ | |
41 #endif /* linux */ | |
42 | |
43 #ifdef SDL_USE_PTHREADS | |
44 | |
45 #include <stdlib.h> | |
46 #include <stdio.h> | |
47 #include <pthread.h> | |
48 | |
49 #include "SDL_error.h" | |
50 #include "SDL_thread.h" | |
51 | |
52 | |
53 struct SDL_mutex { | |
54 pthread_mutex_t id; | |
55 #ifdef PTHREAD_NO_RECURSIVE_MUTEX | |
56 int recursive; | |
57 pthread_t owner; | |
58 #endif | |
59 }; | |
60 | |
61 SDL_mutex *SDL_CreateMutex (void) | |
62 { | |
63 SDL_mutex *mutex; | |
64 pthread_mutexattr_t attr; | |
65 | |
66 /* Allocate the structure */ | |
67 mutex = (SDL_mutex *)calloc(1, sizeof(*mutex)); | |
68 if ( mutex ) { | |
69 pthread_mutexattr_init(&attr); | |
70 #ifdef PTHREAD_NO_RECURSIVE_MUTEX | |
71 /* No extra attributes necessary */ | |
72 #else | |
73 #ifdef linux | |
74 pthread_mutexattr_setkind_np(&attr, PTHREAD_MUTEX_RECURSIVE_NP); | |
75 #else | |
76 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE); | |
77 #endif | |
78 #endif /* PTHREAD_NO_RECURSIVE_MUTEX */ | |
79 if ( pthread_mutex_init(&mutex->id, &attr) != 0 ) { | |
80 SDL_SetError("pthread_mutex_init() failed"); | |
81 free(mutex); | |
82 mutex = NULL; | |
83 } | |
84 } else { | |
85 SDL_OutOfMemory(); | |
86 } | |
87 return(mutex); | |
88 } | |
89 | |
90 void SDL_DestroyMutex(SDL_mutex *mutex) | |
91 { | |
92 if ( mutex ) { | |
93 pthread_mutex_destroy(&mutex->id); | |
94 free(mutex); | |
95 } | |
96 } | |
97 | |
98 /* Lock the mutex */ | |
99 int SDL_mutexP(SDL_mutex *mutex) | |
100 { | |
101 int retval; | |
102 #ifdef PTHREAD_NO_RECURSIVE_MUTEX | |
103 pthread_t this_thread; | |
104 #endif | |
105 | |
106 if ( mutex == NULL ) { | |
107 SDL_SetError("Passed a NULL mutex"); | |
108 return -1; | |
109 } | |
110 | |
111 retval = 0; | |
112 #ifdef PTHREAD_NO_RECURSIVE_MUTEX | |
113 this_thread = pthread_self(); | |
114 if ( mutex->owner == this_thread ) { | |
115 ++mutex->recursive; | |
116 } else { | |
117 /* The order of operations is important. | |
118 We set the locking thread id after we obtain the lock | |
119 so unlocks from other threads will fail. | |
120 */ | |
121 if ( pthread_mutex_lock(&mutex->id) == 0 ) { | |
122 mutex->owner = this_thread; | |
123 mutex->recursive = 0; | |
124 } else { | |
125 SDL_SetError("pthread_mutex_lock() failed"); | |
126 retval = -1; | |
127 } | |
128 } | |
129 #else | |
130 if ( pthread_mutex_lock(&mutex->id) < 0 ) { | |
131 SDL_SetError("pthread_mutex_lock() failed"); | |
132 retval = -1; | |
133 } | |
134 #endif | |
135 return retval; | |
136 } | |
137 | |
138 int SDL_mutexV(SDL_mutex *mutex) | |
139 { | |
140 int retval; | |
141 | |
142 if ( mutex == NULL ) { | |
143 SDL_SetError("Passed a NULL mutex"); | |
144 return -1; | |
145 } | |
146 | |
147 retval = 0; | |
148 #ifdef PTHREAD_NO_RECURSIVE_MUTEX | |
149 /* We can only unlock the mutex if we own it */ | |
150 if ( pthread_self() == mutex->owner ) { | |
151 if ( mutex->recursive ) { | |
152 --mutex->recursive; | |
153 } else { | |
154 /* The order of operations is important. | |
155 First reset the owner so another thread doesn't lock | |
156 the mutex and set the ownership before we reset it, | |
157 then release the lock semaphore. | |
158 */ | |
159 mutex->owner = 0; | |
160 pthread_mutex_unlock(&mutex->id); | |
161 } | |
162 } else { | |
163 SDL_SetError("mutex not owned by this thread"); | |
164 retval = -1; | |
165 } | |
166 | |
167 #else | |
168 if ( pthread_mutex_unlock(&mutex->id) < 0 ) { | |
169 SDL_SetError("pthread_mutex_unlock() failed"); | |
170 retval = -1; | |
171 } | |
172 #endif /* PTHREAD_NO_RECURSIVE_MUTEX */ | |
173 | |
174 return retval; | |
175 } | |
176 | |
177 #else /* Use semaphore implementation */ | |
178 | |
179 #include "generic/SDL_sysmutex.c" | |
180 | |
181 #endif /* SDL_USE_PTHREADS */ |