]>
Commit | Line | Data |
---|---|---|
c404fea2 TT |
1 | /* Threads compatibily routines for libgcc2. */ |
2 | /* Compile this one with gcc. */ | |
3 | /* Copyright (C) 1997 Free Software Foundation, Inc. | |
4 | ||
5 | This file is part of GNU CC. | |
6 | ||
7 | GNU CC is free software; you can redistribute it and/or modify | |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation; either version 2, or (at your option) | |
10 | any later version. | |
11 | ||
12 | GNU CC is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
18 | along with GNU CC; see the file COPYING. If not, write to | |
19 | the Free Software Foundation, 59 Temple Place - Suite 330, | |
20 | Boston, MA 02111-1307, USA. */ | |
21 | ||
22 | /* As a special exception, if you link this library with other files, | |
23 | some of which are compiled with GCC, to produce an executable, | |
24 | this library does not by itself cause the resulting executable | |
25 | to be covered by the GNU General Public License. | |
26 | This exception does not however invalidate any other reasons why | |
27 | the executable file might be covered by the GNU General Public License. */ | |
28 | ||
29 | #ifndef __libgcc_thr_h | |
30 | #define __libgcc_thr_h | |
31 | ||
32 | /* If this file is compiled with threads support, it must | |
33 | #define __GTHREADS 1 | |
34 | to indicate that threads support is present. | |
35 | ||
36 | The threads interface must define the following types: | |
37 | __gthread_key_t | |
38 | __gthread_once_t | |
39 | __gthread_mutex_t | |
40 | ||
41 | The threads interface must define the following macros: | |
42 | ||
43 | __GTHREAD_ONCE_INIT | |
44 | to initialize __gthread_once_t | |
45 | __GTHREAD_MUTEX_INIT | |
46 | to initialize __gthread_mutex_t to get a fast | |
47 | non-recursive mutex. | |
48 | ||
49 | The threads interface must define the following static functions: | |
50 | ||
51 | int __gthread_once (__gthread_once_t *once, void (*func) ()) | |
52 | ||
53 | int __gthread_key_create (__gthread_key_t *keyp, void (*dtor) (void *)) | |
54 | int __gthread_key_delete (__gthread_key_t key) | |
55 | ||
56 | void *__gthread_getspecific (__gthread_key_t key) | |
57 | int __gthread_setspecific (__gthread_key_t key, const void *ptr) | |
58 | ||
59 | int __gthread_mutex_lock (__gthread_mutex_t *mutex); | |
60 | int __gthread_mutex_trylock (__gthread_mutex_t *mutex); | |
61 | int __gthread_mutex_unlock (__gthread_mutex_t *mutex); | |
62 | ||
63 | All functions returning int should return 0 on success, -1 on error. | |
64 | ||
65 | Currently supported threads packages are | |
66 | POSIX threads with -D_PTHREADS | |
67 | DCE threads with -D_DCE_THREADS | |
68 | Solaris/UI threads with -D_SOLARIS_THREADS | |
69 | */ | |
70 | ||
71 | #if _PTHREADS | |
72 | /* POSIX threads specific definitions. | |
73 | Easy, since the interface is just one-to-one mapping. */ | |
74 | ||
75 | #define __GTHREADS 1 | |
76 | ||
77 | #include <pthread.h> | |
78 | ||
79 | typedef pthread_key_t __gthread_key_t; | |
80 | typedef pthread_once_t __gthread_once_t; | |
81 | typedef pthread_mutex_t __gthread_mutex_t; | |
82 | ||
83 | #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER | |
84 | #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT | |
85 | ||
86 | static inline int | |
87 | __gthread_once (__gthread_once_t *once, void (*func) ()) | |
88 | { | |
89 | return pthread_once (once, func); | |
90 | } | |
91 | ||
92 | static inline int | |
93 | __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *)) | |
94 | { | |
95 | return pthread_key_create (key, dtor); | |
96 | } | |
97 | ||
98 | static inline int | |
99 | __gthread_key_delete (__gthread_key_t key) | |
100 | { | |
101 | return pthread_key_delete (key); | |
102 | } | |
103 | ||
104 | static inline void * | |
105 | __gthread_getspecific (__gthread_key_t key) | |
106 | { | |
107 | return pthread_getspecific (key); | |
108 | } | |
109 | ||
110 | static inline int | |
111 | __gthread_setspecific (__gthread_key_t key, const void *ptr) | |
112 | { | |
113 | return pthread_setspecific (key, ptr); | |
114 | } | |
115 | ||
116 | static inline int | |
117 | __gthread_mutex_lock (__gthread_mutex_t *mutex) | |
118 | { | |
119 | return pthread_mutex_lock (mutex); | |
120 | } | |
121 | ||
122 | static inline int | |
123 | __gthread_mutex_trylock (__gthread_mutex_t *mutex) | |
124 | { | |
125 | return pthread_mutex_trylock (mutex); | |
126 | } | |
127 | ||
128 | static inline int | |
129 | __gthread_mutex_unlock (__gthread_mutex_t *mutex) | |
130 | { | |
131 | return pthread_mutex_unlock (mutex); | |
132 | } | |
133 | ||
134 | #elif _DCE_THREADS | |
135 | /* DCE threads interface. | |
136 | DCE threads are based on POSIX threads draft 4, and many things | |
137 | have changed since then. */ | |
138 | ||
139 | #define __GTHREADS 1 | |
140 | ||
141 | #include <pthread.h> | |
142 | ||
143 | typedef pthread_key_t __gthread_key_t; | |
144 | typedef pthread_once_t __gthread_once_t; | |
145 | typedef pthread_mutex_t __gthread_mutex_t; | |
146 | ||
147 | #define __GTHREAD_ONCE_INIT pthread_once_init | |
148 | /* Howto define __GTHREAD_MUTEX_INIT? */ | |
149 | ||
150 | static inline int | |
151 | __gthread_once (__gthread_once_t *once, void (*func) ()) | |
152 | { | |
153 | return pthread_once (once, func); | |
154 | } | |
155 | ||
156 | static inline int | |
157 | __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *)) | |
158 | { | |
159 | return pthread_keycreate (key, dtor); | |
160 | } | |
161 | ||
162 | static inline int | |
163 | __gthread_key_delete (__gthread_key_t key) | |
164 | { | |
165 | return pthread_key_delete (key); | |
166 | } | |
167 | ||
168 | static inline void * | |
169 | __gthread_getspecific (__gthread_key_t key) | |
170 | { | |
171 | void *ptr; | |
172 | if (pthread_getspecific (key, &ptr) == 0) | |
173 | return ptr; | |
174 | else | |
175 | return 0; | |
176 | } | |
177 | ||
178 | static inline int | |
179 | __gthread_setspecific (__gthread_key_t key, const void *ptr) | |
180 | { | |
181 | return pthread_setspecific (key, (void *) ptr); | |
182 | } | |
183 | ||
184 | static inline int | |
185 | __gthread_mutex_lock (__gthread_mutex_t *mutex) | |
186 | { | |
187 | return pthread_mutex_lock (mutex); | |
188 | } | |
189 | ||
190 | static inline int | |
191 | __gthread_mutex_trylock (__gthread_mutex_t *mutex) | |
192 | { | |
193 | return pthread_mutex_trylock (mutex); | |
194 | } | |
195 | ||
196 | static inline int | |
197 | __gthread_mutex_unlock (__gthread_mutex_t *mutex) | |
198 | { | |
199 | return pthread_mutex_unlock (mutex); | |
200 | } | |
201 | ||
202 | #elif _SOLARIS_THREADS | |
203 | /* Solaris threads as found in Solaris 2.[456]. | |
204 | Actually these are Unix International (UI) threads, but I don't | |
205 | know if anyone else implements these. */ | |
206 | ||
207 | #define __GTHREADS 1 | |
208 | ||
209 | #include <thread.h> | |
210 | #include <errno.h> | |
211 | ||
212 | typedef thread_key_t __gthread_key_t; | |
213 | typedef struct | |
214 | { | |
215 | mutex_t mutex; | |
216 | int once; | |
217 | } __gthread_once_t; | |
218 | typedef mutex_t __gthread_mutex_t; | |
219 | ||
220 | #define __GTHREAD_ONCE_INIT { DEFAULTMUTEX, 0 } | |
221 | #define __GTHREAD_MUTEX_INIT DEFAULTMUTEX | |
222 | ||
223 | static inline int | |
224 | __gthread_once (__gthread_once_t *once, void (*func) ()) | |
225 | { | |
226 | if (once == 0 || func == 0) | |
227 | { | |
228 | errno = EINVAL; | |
229 | return -1; | |
230 | } | |
231 | ||
232 | if (once->once == 0) | |
233 | { | |
234 | if (mutex_lock (&once->mutex) != 0) | |
235 | return -1; | |
236 | if (once->once == 0) | |
237 | { | |
238 | (*func) (); | |
239 | once->once ++; | |
240 | } | |
241 | mutex_unlock (&once->mutex); | |
242 | } | |
243 | return 0; | |
244 | } | |
245 | ||
246 | static inline int | |
247 | __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *)) | |
248 | { | |
249 | return thr_keycreate (key, dtor); | |
250 | } | |
251 | ||
252 | static inline int | |
253 | __gthread_key_delete (__gthread_key_t key) | |
254 | { | |
255 | /* Not possible. */ | |
256 | return -1; | |
257 | } | |
258 | ||
259 | static inline void * | |
260 | __gthread_getspecific (__gthread_key_t key) | |
261 | { | |
262 | void *ptr; | |
263 | if (thr_getspecific (key, &ptr) == 0) | |
264 | return ptr; | |
265 | else | |
266 | return 0; | |
267 | } | |
268 | ||
269 | static inline int | |
270 | __gthread_setspecific (__gthread_key_t key, const void *ptr) | |
271 | { | |
272 | return thr_setspecific (key, (void *) ptr); | |
273 | } | |
274 | ||
275 | static inline int | |
276 | __gthread_mutex_lock (__gthread_mutex_t *mutex) | |
277 | { | |
278 | return mutex_lock (mutex); | |
279 | } | |
280 | ||
281 | static inline int | |
282 | __gthread_mutex_trylock (__gthread_mutex_t *mutex) | |
283 | { | |
284 | return mutex_trylock (mutex); | |
285 | } | |
286 | ||
287 | static inline int | |
288 | __gthread_mutex_unlock (__gthread_mutex_t *mutex) | |
289 | { | |
290 | return mutex_unlock (mutex); | |
291 | } | |
292 | ||
293 | #else /* no threads */ | |
294 | ||
295 | /* Just provide compatibility for mutex handling. */ | |
296 | ||
297 | typedef int __gthread_mutex_t; | |
298 | ||
299 | #define __GTHREAD_MUTEX_INIT 0 | |
300 | ||
301 | static inline int | |
302 | __gthread_mutex_lock (__gthread_mutex_t *mutex) | |
303 | { | |
304 | return 0; | |
305 | } | |
306 | ||
307 | static inline int | |
308 | __gthread_mutex_trylock (__gthread_mutex_t *mutex) | |
309 | { | |
310 | return 0; | |
311 | } | |
312 | ||
313 | static inline int | |
314 | __gthread_mutex_unlock (__gthread_mutex_t *mutex) | |
315 | { | |
316 | return 0; | |
317 | } | |
318 | ||
319 | #endif /* no threads */ | |
320 | ||
321 | #endif /* not __libgcc_thr_h */ |