mips-protos.h (mips_regno_mode_ok_for_base_p): Give the STRICT_P argument type "bool...
[official-gcc.git] / gcc / config / vxlib-tls.c
blobe98355591f8079bd4834f5672dd639a962fafb1b
1 /* Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
2 Contributed by Zack Weinberg <zack@codesourcery.com>
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301, USA. */
21 /* As a special exception, if you link this library with other files,
22 some of which are compiled with GCC, to produce an executable,
23 this library does not by itself cause the resulting executable
24 to be covered by the GNU General Public License.
25 This exception does not however invalidate any other reasons why
26 the executable file might be covered by the GNU General Public License. */
28 /* Threads compatibility routines for libgcc2 for VxWorks.
29 These are out-of-line routines called from gthr-vxworks.h.
31 This file provides the TLS related support routines, calling specific
32 VxWorks kernel entry points for this purpose. The base VxWorks 5.x kernels
33 don't feature these entry points, and we provide gthr_supp_vxw_5x.c as an
34 option to fill this gap. Asking users to rebuild a kernel is not to be
35 taken lightly, still, so we have isolated these routines from the rest of
36 vxlib to ensure that the kernel dependencies are only dragged when really
37 necessary. */
39 #include "tconfig.h"
40 #include "tsystem.h"
41 #include "gthr.h"
43 #if defined(__GTHREADS)
44 #include <vxWorks.h>
45 #ifndef __RTP__
46 #include <vxLib.h>
47 #endif
48 #include <taskLib.h>
49 #ifndef __RTP__
50 #include <taskHookLib.h>
51 #else
52 # include <errno.h>
53 #endif
55 /* Thread-local storage.
57 We reserve a field in the TCB to point to a dynamically allocated
58 array which is used to store TLS values. A TLS key is simply an
59 offset in this array. The exact location of the TCB field is not
60 known to this code nor to vxlib.c -- all access to it indirects
61 through the routines __gthread_get_tls_data and
62 __gthread_set_tls_data, which are provided by the VxWorks kernel.
64 There is also a global array which records which keys are valid and
65 which have destructors.
67 A task delete hook is installed to execute key destructors. The
68 routines __gthread_enter_tls_dtor_context and
69 __gthread_leave_tls_dtor_context, which are also provided by the
70 kernel, ensure that it is safe to call free() on memory allocated
71 by the task being deleted. (This is a no-op on VxWorks 5, but
72 a major undertaking on AE.)
74 The task delete hook is only installed when at least one thread
75 has TLS data. This is a necessary precaution, to allow this module
76 to be unloaded - a module with a hook can not be removed.
78 Since this interface is used to allocate only a small number of
79 keys, the table size is small and static, which simplifies the
80 code quite a bit. Revisit this if and when it becomes necessary. */
82 #define MAX_KEYS 4
84 /* This is the structure pointed to by the pointer returned
85 by __gthread_get_tls_data. */
86 struct tls_data
88 int *owner;
89 void *values[MAX_KEYS];
90 unsigned int generation[MAX_KEYS];
93 /* To make sure we only delete TLS data associated with this object,
94 include a pointer to a local variable in the TLS data object. */
95 static int self_owner;
97 /* The number of threads for this module which have active TLS data.
98 This is protected by tls_lock. */
99 static int active_tls_threads;
101 /* kernel provided routines */
102 extern void *__gthread_get_tls_data (void);
103 extern void __gthread_set_tls_data (void *data);
105 extern void __gthread_enter_tls_dtor_context (void);
106 extern void __gthread_leave_tls_dtor_context (void);
109 /* This is a global structure which records all of the active keys.
111 A key is potentially valid (i.e. has been handed out by
112 __gthread_key_create) iff its generation count in this structure is
113 even. In that case, the matching entry in the dtors array is a
114 routine to be called when a thread terminates with a valid,
115 non-NULL specific value for that key.
117 A key is actually valid in a thread T iff the generation count
118 stored in this structure is equal to the generation count stored in
119 T's specific-value structure. */
121 typedef void (*tls_dtor) (void *);
123 struct tls_keys
125 tls_dtor dtor[MAX_KEYS];
126 unsigned int generation[MAX_KEYS];
129 #define KEY_VALID_P(key) !(tls_keys.generation[key] & 1)
131 /* Note: if MAX_KEYS is increased, this initializer must be updated
132 to match. All the generation counts begin at 1, which means no
133 key is valid. */
134 static struct tls_keys tls_keys =
136 { 0, 0, 0, 0 },
137 { 1, 1, 1, 1 }
140 /* This lock protects the tls_keys structure. */
141 static __gthread_mutex_t tls_lock;
143 static __gthread_once_t tls_init_guard = __GTHREAD_ONCE_INIT;
145 /* Internal routines. */
147 /* The task TCB has just been deleted. Call the destructor
148 function for each TLS key that has both a destructor and
149 a non-NULL specific value in this thread.
151 This routine does not need to take tls_lock; the generation
152 count protects us from calling a stale destructor. It does
153 need to read tls_keys.dtor[key] atomically. */
155 static void
156 tls_delete_hook (void *tcb ATTRIBUTE_UNUSED)
158 struct tls_data *data;
159 __gthread_key_t key;
161 #ifdef __RTP__
162 data = __gthread_get_tls_data ();
163 #else
164 /* In kernel mode, we can be called in the context of the thread
165 doing the killing, so must use the TCB to determine the data of
166 the thread being killed. */
167 data = __gthread_get_tsd_data (tcb);
168 #endif
170 if (data && data->owner == &self_owner)
172 __gthread_enter_tls_dtor_context ();
173 for (key = 0; key < MAX_KEYS; key++)
175 if (data->generation[key] == tls_keys.generation[key])
177 tls_dtor dtor = tls_keys.dtor[key];
179 if (dtor)
180 dtor (data->values[key]);
183 free (data);
185 /* We can't handle an error here, so just leave the thread
186 marked as loaded if one occurs. */
187 if (__gthread_mutex_lock (&tls_lock) != ERROR)
189 active_tls_threads--;
190 if (active_tls_threads == 0)
191 taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
192 __gthread_mutex_unlock (&tls_lock);
194 #ifdef __RTP__
195 __gthread_set_tls_data (0);
196 #else
197 __gthread_set_tsd_data (tcb, 0);
198 #endif
199 __gthread_leave_tls_dtor_context ();
203 /* Initialize global data used by the TLS system. */
204 static void
205 tls_init (void)
207 __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock);
210 static void tls_destructor (void) __attribute__ ((destructor));
211 static void
212 tls_destructor (void)
214 #ifdef __RTP__
215 /* All threads but this one should have exited by now. */
216 tls_delete_hook (NULL);
217 #else
218 /* Unregister the hook forcibly. The counter of active threads may
219 be incorrect, because constructors (like the C++ library's) and
220 destructors (like this one) run in the context of the shell rather
221 than in a task spawned from this module. */
222 taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
223 #endif
225 if (tls_init_guard.done && __gthread_mutex_lock (&tls_lock) != ERROR)
226 semDelete (tls_lock);
229 /* External interface */
231 /* Store in KEYP a value which can be passed to __gthread_setspecific/
232 __gthread_getspecific to store and retrieve a value which is
233 specific to each calling thread. If DTOR is not NULL, it will be
234 called when a thread terminates with a non-NULL specific value for
235 this key, with the value as its sole argument. */
238 __gthread_key_create (__gthread_key_t *keyp, tls_dtor dtor)
240 __gthread_key_t key;
242 __gthread_once (&tls_init_guard, tls_init);
244 if (__gthread_mutex_lock (&tls_lock) == ERROR)
245 return errno;
247 for (key = 0; key < MAX_KEYS; key++)
248 if (!KEY_VALID_P (key))
249 goto found_slot;
251 /* no room */
252 __gthread_mutex_unlock (&tls_lock);
253 return EAGAIN;
255 found_slot:
256 tls_keys.generation[key]++; /* making it even */
257 tls_keys.dtor[key] = dtor;
258 *keyp = key;
259 __gthread_mutex_unlock (&tls_lock);
260 return 0;
263 /* Invalidate KEY; it can no longer be used as an argument to
264 setspecific/getspecific. Note that this does NOT call destructor
265 functions for any live values for this key. */
267 __gthread_key_delete (__gthread_key_t key)
269 if (key >= MAX_KEYS)
270 return EINVAL;
272 __gthread_once (&tls_init_guard, tls_init);
274 if (__gthread_mutex_lock (&tls_lock) == ERROR)
275 return errno;
277 if (!KEY_VALID_P (key))
279 __gthread_mutex_unlock (&tls_lock);
280 return EINVAL;
283 tls_keys.generation[key]++; /* making it odd */
284 tls_keys.dtor[key] = 0;
286 __gthread_mutex_unlock (&tls_lock);
287 return 0;
290 /* Retrieve the thread-specific value for KEY. If it has never been
291 set in this thread, or KEY is invalid, returns NULL.
293 It does not matter if this function races with key_create or
294 key_delete; the worst that can happen is you get a value other than
295 the one that a serialized implementation would have provided. */
297 void *
298 __gthread_getspecific (__gthread_key_t key)
300 struct tls_data *data;
302 if (key >= MAX_KEYS)
303 return 0;
305 data = __gthread_get_tls_data ();
307 if (!data)
308 return 0;
310 if (data->generation[key] != tls_keys.generation[key])
311 return 0;
313 return data->values[key];
316 /* Set the thread-specific value for KEY. If KEY is invalid, or
317 memory allocation fails, returns -1, otherwise 0.
319 The generation count protects this function against races with
320 key_create/key_delete; the worst thing that can happen is that a
321 value is successfully stored into a dead generation (and then
322 immediately becomes invalid). However, we do have to make sure
323 to read tls_keys.generation[key] atomically. */
326 __gthread_setspecific (__gthread_key_t key, void *value)
328 struct tls_data *data;
329 unsigned int generation;
331 if (key >= MAX_KEYS)
332 return EINVAL;
334 data = __gthread_get_tls_data ();
335 if (!data)
337 if (__gthread_mutex_lock (&tls_lock) == ERROR)
338 return ENOMEM;
339 if (active_tls_threads == 0)
340 taskDeleteHookAdd ((FUNCPTR)tls_delete_hook);
341 active_tls_threads++;
342 __gthread_mutex_unlock (&tls_lock);
344 data = malloc (sizeof (struct tls_data));
345 if (!data)
346 return ENOMEM;
348 memset (data, 0, sizeof (struct tls_data));
349 data->owner = &self_owner;
350 __gthread_set_tls_data (data);
353 generation = tls_keys.generation[key];
355 if (generation & 1)
356 return EINVAL;
358 data->generation[key] = generation;
359 data->values[key] = value;
361 return 0;
363 #endif /* __GTHREADS */