2009-01-28 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / gthr-mipssde.h
blobb8cf24ebc750c5ab244a3f36a12ce56eb8d7f03b
1 /* MIPS SDE threads compatibility routines for libgcc2 and libobjc. */
2 /* Compile this one with gcc. */
3 /* Copyright (C) 2006, 2007 Free Software Foundation, Inc.
4 Contributed by Nigel Stephens <nigel@mips.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* As a special exception, if you link this library with other files,
24 some of which are compiled with GCC, to produce an executable,
25 this library does not by itself cause the resulting executable
26 to be covered by the GNU General Public License.
27 This exception does not however invalidate any other reasons why
28 the executable file might be covered by the GNU General Public License. */
30 #ifndef GCC_GTHR_MIPSSDE_H
31 #define GCC_GTHR_MIPSSDE_H
33 /* MIPS SDE threading API specific definitions.
34 Easy, since the interface is pretty much one-to-one. */
36 #define __GTHREADS 1
38 #include <sdethread.h>
39 #include <unistd.h>
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
45 typedef __sdethread_key_t __gthread_key_t;
46 typedef __sdethread_once_t __gthread_once_t;
47 typedef __sdethread_mutex_t __gthread_mutex_t;
49 typedef struct {
50 long depth;
51 __sdethread_t owner;
52 __sdethread_mutex_t actual;
53 } __gthread_recursive_mutex_t;
55 #define __GTHREAD_MUTEX_INIT __SDETHREAD_MUTEX_INITIALIZER("gthr")
56 #define __GTHREAD_ONCE_INIT __SDETHREAD_ONCE_INIT
57 static inline int
58 __gthread_recursive_mutex_init_function(__gthread_recursive_mutex_t *mutex);
59 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
61 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
62 # define __gthrw(name) \
63 static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
64 # define __gthrw_(name) __gthrw_ ## name
65 #else
66 # define __gthrw(name)
67 # define __gthrw_(name) name
68 #endif
70 __gthrw(__sdethread_once)
71 __gthrw(__sdethread_key_create)
72 __gthrw(__sdethread_key_delete)
73 __gthrw(__sdethread_getspecific)
74 __gthrw(__sdethread_setspecific)
76 __gthrw(__sdethread_self)
78 __gthrw(__sdethread_mutex_lock)
79 __gthrw(__sdethread_mutex_trylock)
80 __gthrw(__sdethread_mutex_unlock)
82 __gthrw(__sdethread_mutex_init)
84 __gthrw(__sdethread_threading)
86 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
88 static inline int
89 __gthread_active_p (void)
91 return !!(void *)&__sdethread_threading;
94 #else /* not SUPPORTS_WEAK */
96 static inline int
97 __gthread_active_p (void)
99 return 1;
102 #endif /* SUPPORTS_WEAK */
104 static inline int
105 __gthread_once (__gthread_once_t *once, void (*func) (void))
107 if (__gthread_active_p ())
108 return __gthrw_(__sdethread_once) (once, func);
109 else
110 return -1;
113 static inline int
114 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
116 return __gthrw_(__sdethread_key_create) (key, dtor);
119 static inline int
120 __gthread_key_delete (__gthread_key_t key)
122 return __gthrw_(__sdethread_key_delete) (key);
125 static inline void *
126 __gthread_getspecific (__gthread_key_t key)
128 return __gthrw_(__sdethread_getspecific) (key);
131 static inline int
132 __gthread_setspecific (__gthread_key_t key, const void *ptr)
134 return __gthrw_(__sdethread_setspecific) (key, ptr);
137 static inline int
138 __gthread_mutex_destroy (__gthread_mutex_t * UNUSED(mutex))
140 return 0;
143 static inline int
144 __gthread_mutex_lock (__gthread_mutex_t *mutex)
146 if (__gthread_active_p ())
147 return __gthrw_(__sdethread_mutex_lock) (mutex);
148 else
149 return 0;
152 static inline int
153 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
155 if (__gthread_active_p ())
156 return __gthrw_(__sdethread_mutex_trylock) (mutex);
157 else
158 return 0;
161 static inline int
162 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
164 if (__gthread_active_p ())
165 return __gthrw_(__sdethread_mutex_unlock) (mutex);
166 else
167 return 0;
170 static inline int
171 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
173 mutex->depth = 0;
174 mutex->owner = __gthrw_(__sdethread_self) ();
175 return __gthrw_(__sdethread_mutex_init) (&mutex->actual, NULL);
178 static inline int
179 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
181 if (__gthread_active_p ())
183 __sdethread_t me = __gthrw_(__sdethread_self) ();
185 if (mutex->owner != me)
187 __gthrw_(__sdethread_mutex_lock) (&mutex->actual);
188 mutex->owner = me;
191 mutex->depth++;
193 return 0;
196 static inline int
197 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
199 if (__gthread_active_p ())
201 __sdethread_t me = __gthrw_(__sdethread_self) ();
203 if (mutex->owner != me)
205 if (__gthrw_(__sdethread_mutex_trylock) (&mutex->actual))
206 return 1;
207 mutex->owner = me;
210 mutex->depth++;
212 return 0;
215 static inline int
216 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
218 if (__gthread_active_p ())
220 if (--mutex->depth == 0)
222 mutex->owner = (__sdethread_t) 0;
223 __gthrw_(__sdethread_mutex_unlock) (&mutex->actual);
226 return 0;
229 #ifdef __cplusplus
231 #endif
233 #endif /* ! GCC_GTHR_MIPSSDE_H */