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
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
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
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
43 #if defined(__GTHREADS)
50 #include <taskHookLib.h>
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. */
84 /* This is the structure pointed to by the pointer returned
85 by __gthread_get_tls_data. */
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 *);
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
134 static struct tls_keys tls_keys =
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. */
156 tls_delete_hook (void *tcb ATTRIBUTE_UNUSED)
158 struct tls_data *data = __gthread_get_tls_data ();
161 if (data && data->owner == &self_owner)
163 __gthread_enter_tls_dtor_context ();
164 for (key = 0; key < MAX_KEYS; key++)
166 if (data->generation[key] == tls_keys.generation[key])
168 tls_dtor dtor = tls_keys.dtor[key];
171 dtor (data->values[key]);
176 /* We can't handle an error here, so just leave the thread
177 marked as loaded if one occurs. */
178 if (__gthread_mutex_lock (&tls_lock) != ERROR)
180 active_tls_threads--;
181 if (active_tls_threads == 0)
182 taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
183 __gthread_mutex_unlock (&tls_lock);
186 __gthread_set_tls_data (0);
187 __gthread_leave_tls_dtor_context ();
191 /* Initialize global data used by the TLS system. */
195 __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock);
198 static void tls_destructor (void) __attribute__ ((destructor));
200 tls_destructor (void)
203 /* All threads but this one should have exited by now. */
204 tls_delete_hook (NULL);
206 /* Unregister the hook forcibly. The counter of active threads may
207 be incorrect, because constructors (like the C++ library's) and
208 destructors (like this one) run in the context of the shell rather
209 than in a task spawned from this module. */
210 taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
213 if (tls_init_guard.done && __gthread_mutex_lock (&tls_lock) != ERROR)
214 semDelete (tls_lock);
217 /* External interface */
219 /* Store in KEYP a value which can be passed to __gthread_setspecific/
220 __gthread_getspecific to store and retrieve a value which is
221 specific to each calling thread. If DTOR is not NULL, it will be
222 called when a thread terminates with a non-NULL specific value for
223 this key, with the value as its sole argument. */
226 __gthread_key_create (__gthread_key_t *keyp, tls_dtor dtor)
230 __gthread_once (&tls_init_guard, tls_init);
232 if (__gthread_mutex_lock (&tls_lock) == ERROR)
235 for (key = 0; key < MAX_KEYS; key++)
236 if (!KEY_VALID_P (key))
240 __gthread_mutex_unlock (&tls_lock);
244 tls_keys.generation[key]++; /* making it even */
245 tls_keys.dtor[key] = dtor;
247 __gthread_mutex_unlock (&tls_lock);
251 /* Invalidate KEY; it can no longer be used as an argument to
252 setspecific/getspecific. Note that this does NOT call destructor
253 functions for any live values for this key. */
255 __gthread_key_delete (__gthread_key_t key)
260 __gthread_once (&tls_init_guard, tls_init);
262 if (__gthread_mutex_lock (&tls_lock) == ERROR)
265 if (!KEY_VALID_P (key))
267 __gthread_mutex_unlock (&tls_lock);
271 tls_keys.generation[key]++; /* making it odd */
272 tls_keys.dtor[key] = 0;
274 __gthread_mutex_unlock (&tls_lock);
278 /* Retrieve the thread-specific value for KEY. If it has never been
279 set in this thread, or KEY is invalid, returns NULL.
281 It does not matter if this function races with key_create or
282 key_delete; the worst that can happen is you get a value other than
283 the one that a serialized implementation would have provided. */
286 __gthread_getspecific (__gthread_key_t key)
288 struct tls_data *data;
293 data = __gthread_get_tls_data ();
298 if (data->generation[key] != tls_keys.generation[key])
301 return data->values[key];
304 /* Set the thread-specific value for KEY. If KEY is invalid, or
305 memory allocation fails, returns -1, otherwise 0.
307 The generation count protects this function against races with
308 key_create/key_delete; the worst thing that can happen is that a
309 value is successfully stored into a dead generation (and then
310 immediately becomes invalid). However, we do have to make sure
311 to read tls_keys.generation[key] atomically. */
314 __gthread_setspecific (__gthread_key_t key, void *value)
316 struct tls_data *data;
317 unsigned int generation;
322 data = __gthread_get_tls_data ();
325 if (__gthread_mutex_lock (&tls_lock) == ERROR)
327 if (active_tls_threads == 0)
328 taskDeleteHookAdd ((FUNCPTR)tls_delete_hook);
329 active_tls_threads++;
330 __gthread_mutex_unlock (&tls_lock);
332 data = malloc (sizeof (struct tls_data));
336 memset (data, 0, sizeof (struct tls_data));
337 data->owner = &self_owner;
338 __gthread_set_tls_data (data);
341 generation = tls_keys.generation[key];
346 data->generation[key] = generation;
347 data->values[key] = value;
351 #endif /* __GTHREADS */