OSDN Git Service

gcc/
[pf3gnuchains/gcc-fork.git] / gcc / gthr-mipssde.h
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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.  */
22
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.  */
29
30 #ifndef GCC_GTHR_MIPSSDE_H
31 #define GCC_GTHR_MIPSSDE_H
32
33 /* MIPS SDE threading API specific definitions.
34    Easy, since the interface is pretty much one-to-one.  */
35
36 #define __GTHREADS 1
37
38 #include <sdethread.h>
39 #include <unistd.h>
40
41 #ifdef __cplusplus
42 extern "C" {
43 #endif
44
45 typedef __sdethread_key_t __gthread_key_t;
46 typedef __sdethread_once_t __gthread_once_t;
47 typedef __sdethread_mutex_t __gthread_mutex_t;
48
49 typedef struct {
50   long depth;
51   __sdethread_t owner;
52   __sdethread_mutex_t actual;
53 } __gthread_recursive_mutex_t;
54
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
60
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
69
70 __gthrw(__sdethread_once)
71 __gthrw(__sdethread_key_create)
72 __gthrw(__sdethread_key_delete)
73 __gthrw(__sdethread_getspecific)
74 __gthrw(__sdethread_setspecific)
75
76 __gthrw(__sdethread_self)
77
78 __gthrw(__sdethread_mutex_lock)
79 __gthrw(__sdethread_mutex_trylock)
80 __gthrw(__sdethread_mutex_unlock)
81
82 __gthrw(__sdethread_mutex_init)
83
84 __gthrw(__sdethread_threading)
85
86 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
87
88 static inline int
89 __gthread_active_p (void)
90 {
91   return !!(void *)&__sdethread_threading;
92 }
93
94 #else /* not SUPPORTS_WEAK */
95
96 static inline int
97 __gthread_active_p (void)
98 {
99   return 1;
100 }
101
102 #endif /* SUPPORTS_WEAK */
103
104 static inline int
105 __gthread_once (__gthread_once_t *once, void (*func) (void))
106 {
107   if (__gthread_active_p ())
108     return __gthrw_(__sdethread_once) (once, func);
109   else
110     return -1;
111 }
112
113 static inline int
114 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
115 {
116   return __gthrw_(__sdethread_key_create) (key, dtor);
117 }
118
119 static inline int
120 __gthread_key_delete (__gthread_key_t key)
121 {
122   return __gthrw_(__sdethread_key_delete) (key);
123 }
124
125 static inline void *
126 __gthread_getspecific (__gthread_key_t key)
127 {
128   return __gthrw_(__sdethread_getspecific) (key);
129 }
130
131 static inline int
132 __gthread_setspecific (__gthread_key_t key, const void *ptr)
133 {
134   return __gthrw_(__sdethread_setspecific) (key, ptr);
135 }
136
137 static inline int
138 __gthread_mutex_lock (__gthread_mutex_t *mutex)
139 {
140   if (__gthread_active_p ())
141     return __gthrw_(__sdethread_mutex_lock) (mutex);
142   else
143     return 0;
144 }
145
146 static inline int
147 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
148 {
149   if (__gthread_active_p ())
150     return __gthrw_(__sdethread_mutex_trylock) (mutex);
151   else
152     return 0;
153 }
154
155 static inline int
156 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
157 {
158   if (__gthread_active_p ())
159     return __gthrw_(__sdethread_mutex_unlock) (mutex);
160   else
161     return 0;
162 }
163
164 static inline int
165 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
166 {
167   mutex->depth = 0;
168   mutex->owner = __gthrw_(__sdethread_self) ();
169   return __gthrw_(__sdethread_mutex_init) (&mutex->actual, NULL);
170 }
171
172 static inline int
173 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
174 {
175   if (__gthread_active_p ())
176     {
177       __sdethread_t me = __gthrw_(__sdethread_self) ();
178
179       if (mutex->owner != me)
180         {
181           __gthrw_(__sdethread_mutex_lock) (&mutex->actual);
182           mutex->owner = me;
183         }
184
185       mutex->depth++;
186     }
187   return 0;
188 }
189
190 static inline int
191 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
192 {
193   if (__gthread_active_p ())
194     {
195       __sdethread_t me = __gthrw_(__sdethread_self) ();
196
197       if (mutex->owner != me)
198         {
199           if (__gthrw_(__sdethread_mutex_trylock) (&mutex->actual))
200             return 1;
201           mutex->owner = me;
202         }
203
204       mutex->depth++;
205     }
206   return 0;
207 }
208
209 static inline int
210 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
211 {
212   if (__gthread_active_p ())
213     {
214       if (--mutex->depth == 0)
215         {
216            mutex->owner = (__sdethread_t) 0;
217            __gthrw_(__sdethread_mutex_unlock) (&mutex->actual);
218         }
219     }
220   return 0;
221 }
222
223 #ifdef __cplusplus
224 }
225 #endif
226
227 #endif /* ! GCC_GTHR_MIPSSDE_H */