1 /* Copyright (C) 2002-2013 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 3, 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 Under Section 7 of GPL version 3, you are granted additional
17 permissions described in the GCC Runtime Library Exception, version
18 3.1, as published by the Free Software Foundation.
20 You should have received a copy of the GNU General Public License and
21 a copy of the GCC Runtime Library Exception along with this program;
22 see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 <http://www.gnu.org/licenses/>. */
25 /* Threads compatibility routines for libgcc2 for VxWorks.
26 These are out-of-line routines called from gthr-vxworks.h.
28 This file provides the TLS related support routines, calling specific
29 VxWorks kernel entry points for this purpose. The base VxWorks 5.x kernels
30 don't feature these entry points, and we provide gthr_supp_vxw_5x.c as an
31 option to fill this gap. Asking users to rebuild a kernel is not to be
32 taken lightly, still, so we have isolated these routines from the rest of
33 vxlib to ensure that the kernel dependencies are only dragged when really
40 #if defined(__GTHREADS)
47 #include <taskHookLib.h>
52 /* Thread-local storage.
54 We reserve a field in the TCB to point to a dynamically allocated
55 array which is used to store TLS values. A TLS key is simply an
56 offset in this array. The exact location of the TCB field is not
57 known to this code nor to vxlib.c -- all access to it indirects
58 through the routines __gthread_get_tls_data and
59 __gthread_set_tls_data, which are provided by the VxWorks kernel.
61 There is also a global array which records which keys are valid and
62 which have destructors.
64 A task delete hook is installed to execute key destructors. The
65 routines __gthread_enter_tls_dtor_context and
66 __gthread_leave_tls_dtor_context, which are also provided by the
67 kernel, ensure that it is safe to call free() on memory allocated
68 by the task being deleted. (This is a no-op on VxWorks 5, but
69 a major undertaking on AE.)
71 The task delete hook is only installed when at least one thread
72 has TLS data. This is a necessary precaution, to allow this module
73 to be unloaded - a module with a hook can not be removed.
75 Since this interface is used to allocate only a small number of
76 keys, the table size is small and static, which simplifies the
77 code quite a bit. Revisit this if and when it becomes necessary. */
81 /* This is the structure pointed to by the pointer returned
82 by __gthread_get_tls_data. */
86 void *values[MAX_KEYS];
87 unsigned int generation[MAX_KEYS];
90 /* To make sure we only delete TLS data associated with this object,
91 include a pointer to a local variable in the TLS data object. */
92 static int self_owner;
94 /* Flag to check whether the delete hook is installed. Once installed
95 it is only removed when unloading this module. */
96 static volatile int delete_hook_installed;
98 /* kernel provided routines */
99 extern void *__gthread_get_tls_data (void);
100 extern void __gthread_set_tls_data (void *data);
102 extern void __gthread_enter_tls_dtor_context (void);
103 extern void __gthread_leave_tls_dtor_context (void);
107 extern void *__gthread_get_tsd_data (WIND_TCB *tcb);
108 extern void __gthread_set_tsd_data (WIND_TCB *tcb, void *data);
109 extern void __gthread_enter_tsd_dtor_context (WIND_TCB *tcb);
110 extern void __gthread_leave_tsd_dtor_context (WIND_TCB *tcb);
114 /* This is a global structure which records all of the active keys.
116 A key is potentially valid (i.e. has been handed out by
117 __gthread_key_create) iff its generation count in this structure is
118 even. In that case, the matching entry in the dtors array is a
119 routine to be called when a thread terminates with a valid,
120 non-NULL specific value for that key.
122 A key is actually valid in a thread T iff the generation count
123 stored in this structure is equal to the generation count stored in
124 T's specific-value structure. */
126 typedef void (*tls_dtor) (void *);
130 tls_dtor dtor[MAX_KEYS];
131 unsigned int generation[MAX_KEYS];
134 #define KEY_VALID_P(key) !(tls_keys.generation[key] & 1)
136 /* Note: if MAX_KEYS is increased, this initializer must be updated
137 to match. All the generation counts begin at 1, which means no
139 static struct tls_keys tls_keys =
145 /* This lock protects the tls_keys structure. */
146 static __gthread_mutex_t tls_lock;
148 static __gthread_once_t tls_init_guard = __GTHREAD_ONCE_INIT;
150 /* Internal routines. */
152 /* The task TCB has just been deleted. Call the destructor
153 function for each TLS key that has both a destructor and
154 a non-NULL specific value in this thread.
156 This routine does not need to take tls_lock; the generation
157 count protects us from calling a stale destructor. It does
158 need to read tls_keys.dtor[key] atomically. */
161 tls_delete_hook (void *tcb ATTRIBUTE_UNUSED)
163 struct tls_data *data;
167 data = __gthread_get_tls_data ();
169 /* In kernel mode, we can be called in the context of the thread
170 doing the killing, so must use the TCB to determine the data of
171 the thread being killed. */
172 data = __gthread_get_tsd_data (tcb);
175 if (data && data->owner == &self_owner)
178 __gthread_enter_tls_dtor_context ();
180 __gthread_enter_tsd_dtor_context (tcb);
182 for (key = 0; key < MAX_KEYS; key++)
184 if (data->generation[key] == tls_keys.generation[key])
186 tls_dtor dtor = tls_keys.dtor[key];
189 dtor (data->values[key]);
194 __gthread_leave_tls_dtor_context ();
196 __gthread_leave_tsd_dtor_context (tcb);
200 __gthread_set_tls_data (0);
202 __gthread_set_tsd_data (tcb, 0);
207 /* Initialize global data used by the TLS system. */
211 __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock);
214 static void tls_destructor (void) __attribute__ ((destructor));
216 tls_destructor (void)
219 /* All threads but this one should have exited by now. */
220 tls_delete_hook (NULL);
222 /* Unregister the hook. */
223 if (delete_hook_installed)
224 taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
226 if (tls_init_guard.done && __gthread_mutex_lock (&tls_lock) != ERROR)
227 semDelete (tls_lock);
230 /* External interface */
232 /* Store in KEYP a value which can be passed to __gthread_setspecific/
233 __gthread_getspecific to store and retrieve a value which is
234 specific to each calling thread. If DTOR is not NULL, it will be
235 called when a thread terminates with a non-NULL specific value for
236 this key, with the value as its sole argument. */
239 __gthread_key_create (__gthread_key_t *keyp, tls_dtor dtor)
243 __gthread_once (&tls_init_guard, tls_init);
245 if (__gthread_mutex_lock (&tls_lock) == ERROR)
248 for (key = 0; key < MAX_KEYS; key++)
249 if (!KEY_VALID_P (key))
253 __gthread_mutex_unlock (&tls_lock);
257 tls_keys.generation[key]++; /* making it even */
258 tls_keys.dtor[key] = dtor;
260 __gthread_mutex_unlock (&tls_lock);
264 /* Invalidate KEY; it can no longer be used as an argument to
265 setspecific/getspecific. Note that this does NOT call destructor
266 functions for any live values for this key. */
268 __gthread_key_delete (__gthread_key_t key)
273 __gthread_once (&tls_init_guard, tls_init);
275 if (__gthread_mutex_lock (&tls_lock) == ERROR)
278 if (!KEY_VALID_P (key))
280 __gthread_mutex_unlock (&tls_lock);
284 tls_keys.generation[key]++; /* making it odd */
285 tls_keys.dtor[key] = 0;
287 __gthread_mutex_unlock (&tls_lock);
291 /* Retrieve the thread-specific value for KEY. If it has never been
292 set in this thread, or KEY is invalid, returns NULL.
294 It does not matter if this function races with key_create or
295 key_delete; the worst that can happen is you get a value other than
296 the one that a serialized implementation would have provided. */
299 __gthread_getspecific (__gthread_key_t key)
301 struct tls_data *data;
306 data = __gthread_get_tls_data ();
311 if (data->generation[key] != tls_keys.generation[key])
314 return data->values[key];
317 /* Set the thread-specific value for KEY. If KEY is invalid, or
318 memory allocation fails, returns -1, otherwise 0.
320 The generation count protects this function against races with
321 key_create/key_delete; the worst thing that can happen is that a
322 value is successfully stored into a dead generation (and then
323 immediately becomes invalid). However, we do have to make sure
324 to read tls_keys.generation[key] atomically. */
327 __gthread_setspecific (__gthread_key_t key, void *value)
329 struct tls_data *data;
330 unsigned int generation;
335 data = __gthread_get_tls_data ();
338 if (!delete_hook_installed)
340 /* Install the delete hook. */
341 if (__gthread_mutex_lock (&tls_lock) == ERROR)
343 if (!delete_hook_installed)
345 taskDeleteHookAdd ((FUNCPTR)tls_delete_hook);
346 delete_hook_installed = 1;
348 __gthread_mutex_unlock (&tls_lock);
351 data = malloc (sizeof (struct tls_data));
355 memset (data, 0, sizeof (struct tls_data));
356 data->owner = &self_owner;
357 __gthread_set_tls_data (data);
360 generation = tls_keys.generation[key];
365 data->generation[key] = generation;
366 data->values[key] = value;
370 #endif /* __GTHREADS */