OSDN Git Service

Daily bump.
[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_destroy (__gthread_mutex_t * UNUSED(mutex))
139 {
140   return 0;
141 }
142
143 static inline int
144 __gthread_mutex_lock (__gthread_mutex_t *mutex)
145 {
146   if (__gthread_active_p ())
147     return __gthrw_(__sdethread_mutex_lock) (mutex);
148   else
149     return 0;
150 }
151
152 static inline int
153 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
154 {
155   if (__gthread_active_p ())
156     return __gthrw_(__sdethread_mutex_trylock) (mutex);
157   else
158     return 0;
159 }
160
161 static inline int
162 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
163 {
164   if (__gthread_active_p ())
165     return __gthrw_(__sdethread_mutex_unlock) (mutex);
166   else
167     return 0;
168 }
169
170 static inline int
171 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
172 {
173   mutex->depth = 0;
174   mutex->owner = __gthrw_(__sdethread_self) ();
175   return __gthrw_(__sdethread_mutex_init) (&mutex->actual, NULL);
176 }
177
178 static inline int
179 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
180 {
181   if (__gthread_active_p ())
182     {
183       __sdethread_t me = __gthrw_(__sdethread_self) ();
184
185       if (mutex->owner != me)
186         {
187           __gthrw_(__sdethread_mutex_lock) (&mutex->actual);
188           mutex->owner = me;
189         }
190
191       mutex->depth++;
192     }
193   return 0;
194 }
195
196 static inline int
197 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
198 {
199   if (__gthread_active_p ())
200     {
201       __sdethread_t me = __gthrw_(__sdethread_self) ();
202
203       if (mutex->owner != me)
204         {
205           if (__gthrw_(__sdethread_mutex_trylock) (&mutex->actual))
206             return 1;
207           mutex->owner = me;
208         }
209
210       mutex->depth++;
211     }
212   return 0;
213 }
214
215 static inline int
216 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
217 {
218   if (__gthread_active_p ())
219     {
220       if (--mutex->depth == 0)
221         {
222            mutex->owner = (__sdethread_t) 0;
223            __gthrw_(__sdethread_mutex_unlock) (&mutex->actual);
224         }
225     }
226   return 0;
227 }
228
229 #ifdef __cplusplus
230 }
231 #endif
232
233 #endif /* ! GCC_GTHR_MIPSSDE_H */