xref: /openbsd-src/gnu/gcc/gcc/gthr-tpf.h (revision 404b540a9034ac75a6199ad1a32d1bbc7a0d4210)
1 /* Threads compatibility routines for libgcc2 and libobjc.
2    Compile this one with gcc.
3    Copyright (C) 2004, 2005 Free Software Foundation, Inc.
4 
5 This file is part of GCC.
6 
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
11 
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, 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 
30 /* TPF needs its own version of gthr-*.h because TPF always links to
31    the thread library.  However, for performance reasons we still do not
32    want to issue thread api calls unless a check is made to see that we
33    are running as a thread.  */
34 
35 #ifndef GCC_GTHR_TPF_H
36 #define GCC_GTHR_TPF_H
37 
38 /* POSIX threads specific definitions.
39    Easy, since the interface is just one-to-one mapping.  */
40 
41 #define __GTHREADS 1
42 
43 /* Some implementations of <pthread.h> require this to be defined.  */
44 #ifndef _REENTRANT
45 #define _REENTRANT 1
46 #endif
47 
48 #include <pthread.h>
49 #include <unistd.h>
50 
51 typedef pthread_key_t __gthread_key_t;
52 typedef pthread_once_t __gthread_once_t;
53 typedef pthread_mutex_t __gthread_mutex_t;
54 typedef pthread_mutex_t __gthread_recursive_mutex_t;
55 
56 #if defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER)
57 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER
58 #elif defined(PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP)
59 #define __GTHREAD_RECURSIVE_MUTEX_INIT PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP
60 #endif
61 
62 #define __GTHREAD_MUTEX_INIT PTHREAD_MUTEX_INITIALIZER
63 #define __GTHREAD_ONCE_INIT PTHREAD_ONCE_INIT
64 #define __GTHREAD_RECURSIVE_MUTEX_INIT_FUNCTION __gthread_recursive_mutex_init_function
65 
66 #define NOTATHREAD   00
67 #define ECBBASEPTR (unsigned long int) *(unsigned int *)0x00000514u
68 #define ECBPG2PTR  ECBBASEPTR + 0x1000
69 #define CE2THRCPTR *((unsigned char *)(ECBPG2PTR + 16))
70 #define __tpf_pthread_active() (CE2THRCPTR != NOTATHREAD)
71 
72 #if SUPPORTS_WEAK && GTHREAD_USE_WEAK
73 # define __gthrw(name) \
74   static __typeof(name) __gthrw_ ## name __attribute__ ((__weakref__(#name)));
75 # define __gthrw_(name) __gthrw_ ## name
76 #else
77 # define __gthrw(name)
78 # define __gthrw_(name) name
79 #endif
80 
81 __gthrw(pthread_once)
__gthrw(pthread_key_create)82 __gthrw(pthread_key_create)
83 __gthrw(pthread_key_delete)
84 __gthrw(pthread_getspecific)
85 __gthrw(pthread_setspecific)
86 __gthrw(pthread_create)
87 
88 __gthrw(pthread_mutex_lock)
89 __gthrw(pthread_mutex_trylock)
90 __gthrw(pthread_mutex_unlock)
91 __gthrw(pthread_mutexattr_init)
92 __gthrw(pthread_mutexattr_settype)
93 __gthrw(pthread_mutexattr_destroy)
94 __gthrw(pthread_mutex_init)
95 
96 static inline int
97 __gthread_active_p (void)
98 {
99   return 1;
100 }
101 
102 static inline int
__gthread_once(__gthread_once_t * once,void (* func)(void))103 __gthread_once (__gthread_once_t *once, void (*func) (void))
104 {
105   if (__tpf_pthread_active ())
106     return __gthrw_(pthread_once) (once, func);
107   else
108     return -1;
109 }
110 
111 static inline int
__gthread_key_create(__gthread_key_t * key,void (* dtor)(void *))112 __gthread_key_create (__gthread_key_t *key, void (*dtor) (void *))
113 {
114   if (__tpf_pthread_active ())
115     return __gthrw_(pthread_key_create) (key, dtor);
116   else
117     return -1;
118 }
119 
120 static inline int
__gthread_key_delete(__gthread_key_t key)121 __gthread_key_delete (__gthread_key_t key)
122 {
123   if (__tpf_pthread_active ())
124     return __gthrw_(pthread_key_delete) (key);
125   else
126     return -1;
127 }
128 
129 static inline void *
__gthread_getspecific(__gthread_key_t key)130 __gthread_getspecific (__gthread_key_t key)
131 {
132   if (__tpf_pthread_active ())
133     return __gthrw_(pthread_getspecific) (key);
134   else
135     return NULL;
136 }
137 
138 static inline int
__gthread_setspecific(__gthread_key_t key,const void * ptr)139 __gthread_setspecific (__gthread_key_t key, const void *ptr)
140 {
141   if (__tpf_pthread_active ())
142     return __gthrw_(pthread_setspecific) (key, ptr);
143   else
144     return -1;
145 }
146 
147 static inline int
__gthread_mutex_lock(__gthread_mutex_t * mutex)148 __gthread_mutex_lock (__gthread_mutex_t *mutex)
149 {
150   if (__tpf_pthread_active ())
151     return __gthrw_(pthread_mutex_lock) (mutex);
152   else
153     return 0;
154 }
155 
156 static inline int
__gthread_mutex_trylock(__gthread_mutex_t * mutex)157 __gthread_mutex_trylock (__gthread_mutex_t *mutex)
158 {
159   if (__tpf_pthread_active ())
160     return __gthrw_(pthread_mutex_trylock) (mutex);
161   else
162     return 0;
163 }
164 
165 static inline int
__gthread_mutex_unlock(__gthread_mutex_t * mutex)166 __gthread_mutex_unlock (__gthread_mutex_t *mutex)
167 {
168   if (__tpf_pthread_active ())
169     return __gthrw_(pthread_mutex_unlock) (mutex);
170   else
171     return 0;
172 }
173 
174 static inline int
__gthread_recursive_mutex_lock(__gthread_recursive_mutex_t * mutex)175 __gthread_recursive_mutex_lock (__gthread_recursive_mutex_t *mutex)
176 {
177   if (__tpf_pthread_active ())
178     return __gthread_mutex_lock (mutex);
179   else
180     return 0;
181 }
182 
183 static inline int
__gthread_recursive_mutex_trylock(__gthread_recursive_mutex_t * mutex)184 __gthread_recursive_mutex_trylock (__gthread_recursive_mutex_t *mutex)
185 {
186   if (__tpf_pthread_active ())
187     return __gthread_mutex_trylock (mutex);
188   else
189     return 0;
190 }
191 
192 static inline int
__gthread_recursive_mutex_unlock(__gthread_recursive_mutex_t * mutex)193 __gthread_recursive_mutex_unlock (__gthread_recursive_mutex_t *mutex)
194 {
195   if (__tpf_pthread_active ())
196     return __gthread_mutex_unlock (mutex);
197   else
198     return 0;
199 }
200 
201 static inline int
__gthread_recursive_mutex_init_function(__gthread_recursive_mutex_t * mutex)202 __gthread_recursive_mutex_init_function (__gthread_recursive_mutex_t *mutex)
203 {
204   if (__tpf_pthread_active ())
205     {
206       pthread_mutexattr_t attr;
207       int r;
208 
209       r = __gthrw_(pthread_mutexattr_init) (&attr);
210       if (!r)
211 	r = __gthrw_(pthread_mutexattr_settype) (&attr, PTHREAD_MUTEX_RECURSIVE);
212       if (!r)
213 	r = __gthrw_(pthread_mutex_init) (mutex, &attr);
214       if (!r)
215 	r = __gthrw_(pthread_mutexattr_destroy) (&attr);
216       return r;
217     }
218   return 0;
219 }
220 
221 
222 #endif /* ! GCC_GTHR_TPF_H */
223