1*e4b17023SJohn Marino /* Copyright (C) 2002, 2003, 2004, 2005, 2009 Free Software Foundation, Inc.
2*e4b17023SJohn Marino Contributed by Zack Weinberg <zack@codesourcery.com>
3*e4b17023SJohn Marino
4*e4b17023SJohn Marino This file is part of GCC.
5*e4b17023SJohn Marino
6*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
7*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
8*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
9*e4b17023SJohn Marino version.
10*e4b17023SJohn Marino
11*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
13*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14*e4b17023SJohn Marino for more details.
15*e4b17023SJohn Marino
16*e4b17023SJohn Marino Under Section 7 of GPL version 3, you are granted additional
17*e4b17023SJohn Marino permissions described in the GCC Runtime Library Exception, version
18*e4b17023SJohn Marino 3.1, as published by the Free Software Foundation.
19*e4b17023SJohn Marino
20*e4b17023SJohn Marino You should have received a copy of the GNU General Public License and
21*e4b17023SJohn Marino a copy of the GCC Runtime Library Exception along with this program;
22*e4b17023SJohn Marino see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
24*e4b17023SJohn Marino
25*e4b17023SJohn Marino /* Threads compatibility routines for libgcc2 for VxWorks.
26*e4b17023SJohn Marino These are out-of-line routines called from gthr-vxworks.h.
27*e4b17023SJohn Marino
28*e4b17023SJohn Marino This file provides the TLS related support routines, calling specific
29*e4b17023SJohn Marino VxWorks kernel entry points for this purpose. The base VxWorks 5.x kernels
30*e4b17023SJohn Marino don't feature these entry points, and we provide gthr_supp_vxw_5x.c as an
31*e4b17023SJohn Marino option to fill this gap. Asking users to rebuild a kernel is not to be
32*e4b17023SJohn Marino taken lightly, still, so we have isolated these routines from the rest of
33*e4b17023SJohn Marino vxlib to ensure that the kernel dependencies are only dragged when really
34*e4b17023SJohn Marino necessary. */
35*e4b17023SJohn Marino
36*e4b17023SJohn Marino #include "tconfig.h"
37*e4b17023SJohn Marino #include "tsystem.h"
38*e4b17023SJohn Marino #include "gthr.h"
39*e4b17023SJohn Marino
40*e4b17023SJohn Marino #if defined(__GTHREADS)
41*e4b17023SJohn Marino #include <vxWorks.h>
42*e4b17023SJohn Marino #ifndef __RTP__
43*e4b17023SJohn Marino #include <vxLib.h>
44*e4b17023SJohn Marino #endif
45*e4b17023SJohn Marino #include <taskLib.h>
46*e4b17023SJohn Marino #ifndef __RTP__
47*e4b17023SJohn Marino #include <taskHookLib.h>
48*e4b17023SJohn Marino #else
49*e4b17023SJohn Marino # include <errno.h>
50*e4b17023SJohn Marino #endif
51*e4b17023SJohn Marino
52*e4b17023SJohn Marino /* Thread-local storage.
53*e4b17023SJohn Marino
54*e4b17023SJohn Marino We reserve a field in the TCB to point to a dynamically allocated
55*e4b17023SJohn Marino array which is used to store TLS values. A TLS key is simply an
56*e4b17023SJohn Marino offset in this array. The exact location of the TCB field is not
57*e4b17023SJohn Marino known to this code nor to vxlib.c -- all access to it indirects
58*e4b17023SJohn Marino through the routines __gthread_get_tls_data and
59*e4b17023SJohn Marino __gthread_set_tls_data, which are provided by the VxWorks kernel.
60*e4b17023SJohn Marino
61*e4b17023SJohn Marino There is also a global array which records which keys are valid and
62*e4b17023SJohn Marino which have destructors.
63*e4b17023SJohn Marino
64*e4b17023SJohn Marino A task delete hook is installed to execute key destructors. The
65*e4b17023SJohn Marino routines __gthread_enter_tls_dtor_context and
66*e4b17023SJohn Marino __gthread_leave_tls_dtor_context, which are also provided by the
67*e4b17023SJohn Marino kernel, ensure that it is safe to call free() on memory allocated
68*e4b17023SJohn Marino by the task being deleted. (This is a no-op on VxWorks 5, but
69*e4b17023SJohn Marino a major undertaking on AE.)
70*e4b17023SJohn Marino
71*e4b17023SJohn Marino The task delete hook is only installed when at least one thread
72*e4b17023SJohn Marino has TLS data. This is a necessary precaution, to allow this module
73*e4b17023SJohn Marino to be unloaded - a module with a hook can not be removed.
74*e4b17023SJohn Marino
75*e4b17023SJohn Marino Since this interface is used to allocate only a small number of
76*e4b17023SJohn Marino keys, the table size is small and static, which simplifies the
77*e4b17023SJohn Marino code quite a bit. Revisit this if and when it becomes necessary. */
78*e4b17023SJohn Marino
79*e4b17023SJohn Marino #define MAX_KEYS 4
80*e4b17023SJohn Marino
81*e4b17023SJohn Marino /* This is the structure pointed to by the pointer returned
82*e4b17023SJohn Marino by __gthread_get_tls_data. */
83*e4b17023SJohn Marino struct tls_data
84*e4b17023SJohn Marino {
85*e4b17023SJohn Marino int *owner;
86*e4b17023SJohn Marino void *values[MAX_KEYS];
87*e4b17023SJohn Marino unsigned int generation[MAX_KEYS];
88*e4b17023SJohn Marino };
89*e4b17023SJohn Marino
90*e4b17023SJohn Marino /* To make sure we only delete TLS data associated with this object,
91*e4b17023SJohn Marino include a pointer to a local variable in the TLS data object. */
92*e4b17023SJohn Marino static int self_owner;
93*e4b17023SJohn Marino
94*e4b17023SJohn Marino /* Flag to check whether the delete hook is installed. Once installed
95*e4b17023SJohn Marino it is only removed when unloading this module. */
96*e4b17023SJohn Marino static volatile int delete_hook_installed;
97*e4b17023SJohn Marino
98*e4b17023SJohn Marino /* kernel provided routines */
99*e4b17023SJohn Marino extern void *__gthread_get_tls_data (void);
100*e4b17023SJohn Marino extern void __gthread_set_tls_data (void *data);
101*e4b17023SJohn Marino
102*e4b17023SJohn Marino extern void __gthread_enter_tls_dtor_context (void);
103*e4b17023SJohn Marino extern void __gthread_leave_tls_dtor_context (void);
104*e4b17023SJohn Marino
105*e4b17023SJohn Marino
106*e4b17023SJohn Marino /* This is a global structure which records all of the active keys.
107*e4b17023SJohn Marino
108*e4b17023SJohn Marino A key is potentially valid (i.e. has been handed out by
109*e4b17023SJohn Marino __gthread_key_create) iff its generation count in this structure is
110*e4b17023SJohn Marino even. In that case, the matching entry in the dtors array is a
111*e4b17023SJohn Marino routine to be called when a thread terminates with a valid,
112*e4b17023SJohn Marino non-NULL specific value for that key.
113*e4b17023SJohn Marino
114*e4b17023SJohn Marino A key is actually valid in a thread T iff the generation count
115*e4b17023SJohn Marino stored in this structure is equal to the generation count stored in
116*e4b17023SJohn Marino T's specific-value structure. */
117*e4b17023SJohn Marino
118*e4b17023SJohn Marino typedef void (*tls_dtor) (void *);
119*e4b17023SJohn Marino
120*e4b17023SJohn Marino struct tls_keys
121*e4b17023SJohn Marino {
122*e4b17023SJohn Marino tls_dtor dtor[MAX_KEYS];
123*e4b17023SJohn Marino unsigned int generation[MAX_KEYS];
124*e4b17023SJohn Marino };
125*e4b17023SJohn Marino
126*e4b17023SJohn Marino #define KEY_VALID_P(key) !(tls_keys.generation[key] & 1)
127*e4b17023SJohn Marino
128*e4b17023SJohn Marino /* Note: if MAX_KEYS is increased, this initializer must be updated
129*e4b17023SJohn Marino to match. All the generation counts begin at 1, which means no
130*e4b17023SJohn Marino key is valid. */
131*e4b17023SJohn Marino static struct tls_keys tls_keys =
132*e4b17023SJohn Marino {
133*e4b17023SJohn Marino { 0, 0, 0, 0 },
134*e4b17023SJohn Marino { 1, 1, 1, 1 }
135*e4b17023SJohn Marino };
136*e4b17023SJohn Marino
137*e4b17023SJohn Marino /* This lock protects the tls_keys structure. */
138*e4b17023SJohn Marino static __gthread_mutex_t tls_lock;
139*e4b17023SJohn Marino
140*e4b17023SJohn Marino static __gthread_once_t tls_init_guard = __GTHREAD_ONCE_INIT;
141*e4b17023SJohn Marino
142*e4b17023SJohn Marino /* Internal routines. */
143*e4b17023SJohn Marino
144*e4b17023SJohn Marino /* The task TCB has just been deleted. Call the destructor
145*e4b17023SJohn Marino function for each TLS key that has both a destructor and
146*e4b17023SJohn Marino a non-NULL specific value in this thread.
147*e4b17023SJohn Marino
148*e4b17023SJohn Marino This routine does not need to take tls_lock; the generation
149*e4b17023SJohn Marino count protects us from calling a stale destructor. It does
150*e4b17023SJohn Marino need to read tls_keys.dtor[key] atomically. */
151*e4b17023SJohn Marino
152*e4b17023SJohn Marino static void
tls_delete_hook(void * tcb ATTRIBUTE_UNUSED)153*e4b17023SJohn Marino tls_delete_hook (void *tcb ATTRIBUTE_UNUSED)
154*e4b17023SJohn Marino {
155*e4b17023SJohn Marino struct tls_data *data;
156*e4b17023SJohn Marino __gthread_key_t key;
157*e4b17023SJohn Marino
158*e4b17023SJohn Marino #ifdef __RTP__
159*e4b17023SJohn Marino data = __gthread_get_tls_data ();
160*e4b17023SJohn Marino #else
161*e4b17023SJohn Marino /* In kernel mode, we can be called in the context of the thread
162*e4b17023SJohn Marino doing the killing, so must use the TCB to determine the data of
163*e4b17023SJohn Marino the thread being killed. */
164*e4b17023SJohn Marino data = __gthread_get_tsd_data (tcb);
165*e4b17023SJohn Marino #endif
166*e4b17023SJohn Marino
167*e4b17023SJohn Marino if (data && data->owner == &self_owner)
168*e4b17023SJohn Marino {
169*e4b17023SJohn Marino #ifdef __RTP__
170*e4b17023SJohn Marino __gthread_enter_tls_dtor_context ();
171*e4b17023SJohn Marino #else
172*e4b17023SJohn Marino __gthread_enter_tsd_dtor_context (tcb);
173*e4b17023SJohn Marino #endif
174*e4b17023SJohn Marino for (key = 0; key < MAX_KEYS; key++)
175*e4b17023SJohn Marino {
176*e4b17023SJohn Marino if (data->generation[key] == tls_keys.generation[key])
177*e4b17023SJohn Marino {
178*e4b17023SJohn Marino tls_dtor dtor = tls_keys.dtor[key];
179*e4b17023SJohn Marino
180*e4b17023SJohn Marino if (dtor)
181*e4b17023SJohn Marino dtor (data->values[key]);
182*e4b17023SJohn Marino }
183*e4b17023SJohn Marino }
184*e4b17023SJohn Marino free (data);
185*e4b17023SJohn Marino #ifdef __RTP__
186*e4b17023SJohn Marino __gthread_leave_tls_dtor_context ();
187*e4b17023SJohn Marino #else
188*e4b17023SJohn Marino __gthread_leave_tsd_dtor_context ();
189*e4b17023SJohn Marino #endif
190*e4b17023SJohn Marino
191*e4b17023SJohn Marino #ifdef __RTP__
192*e4b17023SJohn Marino __gthread_set_tls_data (0);
193*e4b17023SJohn Marino #else
194*e4b17023SJohn Marino __gthread_set_tsd_data (tcb, 0);
195*e4b17023SJohn Marino #endif
196*e4b17023SJohn Marino }
197*e4b17023SJohn Marino }
198*e4b17023SJohn Marino
199*e4b17023SJohn Marino /* Initialize global data used by the TLS system. */
200*e4b17023SJohn Marino static void
tls_init(void)201*e4b17023SJohn Marino tls_init (void)
202*e4b17023SJohn Marino {
203*e4b17023SJohn Marino __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock);
204*e4b17023SJohn Marino }
205*e4b17023SJohn Marino
206*e4b17023SJohn Marino static void tls_destructor (void) __attribute__ ((destructor));
207*e4b17023SJohn Marino static void
tls_destructor(void)208*e4b17023SJohn Marino tls_destructor (void)
209*e4b17023SJohn Marino {
210*e4b17023SJohn Marino #ifdef __RTP__
211*e4b17023SJohn Marino /* All threads but this one should have exited by now. */
212*e4b17023SJohn Marino tls_delete_hook (NULL);
213*e4b17023SJohn Marino #endif
214*e4b17023SJohn Marino /* Unregister the hook. */
215*e4b17023SJohn Marino if (delete_hook_installed)
216*e4b17023SJohn Marino taskDeleteHookDelete ((FUNCPTR)tls_delete_hook);
217*e4b17023SJohn Marino
218*e4b17023SJohn Marino if (tls_init_guard.done && __gthread_mutex_lock (&tls_lock) != ERROR)
219*e4b17023SJohn Marino semDelete (tls_lock);
220*e4b17023SJohn Marino }
221*e4b17023SJohn Marino
222*e4b17023SJohn Marino /* External interface */
223*e4b17023SJohn Marino
224*e4b17023SJohn Marino /* Store in KEYP a value which can be passed to __gthread_setspecific/
225*e4b17023SJohn Marino __gthread_getspecific to store and retrieve a value which is
226*e4b17023SJohn Marino specific to each calling thread. If DTOR is not NULL, it will be
227*e4b17023SJohn Marino called when a thread terminates with a non-NULL specific value for
228*e4b17023SJohn Marino this key, with the value as its sole argument. */
229*e4b17023SJohn Marino
230*e4b17023SJohn Marino int
__gthread_key_create(__gthread_key_t * keyp,tls_dtor dtor)231*e4b17023SJohn Marino __gthread_key_create (__gthread_key_t *keyp, tls_dtor dtor)
232*e4b17023SJohn Marino {
233*e4b17023SJohn Marino __gthread_key_t key;
234*e4b17023SJohn Marino
235*e4b17023SJohn Marino __gthread_once (&tls_init_guard, tls_init);
236*e4b17023SJohn Marino
237*e4b17023SJohn Marino if (__gthread_mutex_lock (&tls_lock) == ERROR)
238*e4b17023SJohn Marino return errno;
239*e4b17023SJohn Marino
240*e4b17023SJohn Marino for (key = 0; key < MAX_KEYS; key++)
241*e4b17023SJohn Marino if (!KEY_VALID_P (key))
242*e4b17023SJohn Marino goto found_slot;
243*e4b17023SJohn Marino
244*e4b17023SJohn Marino /* no room */
245*e4b17023SJohn Marino __gthread_mutex_unlock (&tls_lock);
246*e4b17023SJohn Marino return EAGAIN;
247*e4b17023SJohn Marino
248*e4b17023SJohn Marino found_slot:
249*e4b17023SJohn Marino tls_keys.generation[key]++; /* making it even */
250*e4b17023SJohn Marino tls_keys.dtor[key] = dtor;
251*e4b17023SJohn Marino *keyp = key;
252*e4b17023SJohn Marino __gthread_mutex_unlock (&tls_lock);
253*e4b17023SJohn Marino return 0;
254*e4b17023SJohn Marino }
255*e4b17023SJohn Marino
256*e4b17023SJohn Marino /* Invalidate KEY; it can no longer be used as an argument to
257*e4b17023SJohn Marino setspecific/getspecific. Note that this does NOT call destructor
258*e4b17023SJohn Marino functions for any live values for this key. */
259*e4b17023SJohn Marino int
__gthread_key_delete(__gthread_key_t key)260*e4b17023SJohn Marino __gthread_key_delete (__gthread_key_t key)
261*e4b17023SJohn Marino {
262*e4b17023SJohn Marino if (key >= MAX_KEYS)
263*e4b17023SJohn Marino return EINVAL;
264*e4b17023SJohn Marino
265*e4b17023SJohn Marino __gthread_once (&tls_init_guard, tls_init);
266*e4b17023SJohn Marino
267*e4b17023SJohn Marino if (__gthread_mutex_lock (&tls_lock) == ERROR)
268*e4b17023SJohn Marino return errno;
269*e4b17023SJohn Marino
270*e4b17023SJohn Marino if (!KEY_VALID_P (key))
271*e4b17023SJohn Marino {
272*e4b17023SJohn Marino __gthread_mutex_unlock (&tls_lock);
273*e4b17023SJohn Marino return EINVAL;
274*e4b17023SJohn Marino }
275*e4b17023SJohn Marino
276*e4b17023SJohn Marino tls_keys.generation[key]++; /* making it odd */
277*e4b17023SJohn Marino tls_keys.dtor[key] = 0;
278*e4b17023SJohn Marino
279*e4b17023SJohn Marino __gthread_mutex_unlock (&tls_lock);
280*e4b17023SJohn Marino return 0;
281*e4b17023SJohn Marino }
282*e4b17023SJohn Marino
283*e4b17023SJohn Marino /* Retrieve the thread-specific value for KEY. If it has never been
284*e4b17023SJohn Marino set in this thread, or KEY is invalid, returns NULL.
285*e4b17023SJohn Marino
286*e4b17023SJohn Marino It does not matter if this function races with key_create or
287*e4b17023SJohn Marino key_delete; the worst that can happen is you get a value other than
288*e4b17023SJohn Marino the one that a serialized implementation would have provided. */
289*e4b17023SJohn Marino
290*e4b17023SJohn Marino void *
__gthread_getspecific(__gthread_key_t key)291*e4b17023SJohn Marino __gthread_getspecific (__gthread_key_t key)
292*e4b17023SJohn Marino {
293*e4b17023SJohn Marino struct tls_data *data;
294*e4b17023SJohn Marino
295*e4b17023SJohn Marino if (key >= MAX_KEYS)
296*e4b17023SJohn Marino return 0;
297*e4b17023SJohn Marino
298*e4b17023SJohn Marino data = __gthread_get_tls_data ();
299*e4b17023SJohn Marino
300*e4b17023SJohn Marino if (!data)
301*e4b17023SJohn Marino return 0;
302*e4b17023SJohn Marino
303*e4b17023SJohn Marino if (data->generation[key] != tls_keys.generation[key])
304*e4b17023SJohn Marino return 0;
305*e4b17023SJohn Marino
306*e4b17023SJohn Marino return data->values[key];
307*e4b17023SJohn Marino }
308*e4b17023SJohn Marino
309*e4b17023SJohn Marino /* Set the thread-specific value for KEY. If KEY is invalid, or
310*e4b17023SJohn Marino memory allocation fails, returns -1, otherwise 0.
311*e4b17023SJohn Marino
312*e4b17023SJohn Marino The generation count protects this function against races with
313*e4b17023SJohn Marino key_create/key_delete; the worst thing that can happen is that a
314*e4b17023SJohn Marino value is successfully stored into a dead generation (and then
315*e4b17023SJohn Marino immediately becomes invalid). However, we do have to make sure
316*e4b17023SJohn Marino to read tls_keys.generation[key] atomically. */
317*e4b17023SJohn Marino
318*e4b17023SJohn Marino int
__gthread_setspecific(__gthread_key_t key,void * value)319*e4b17023SJohn Marino __gthread_setspecific (__gthread_key_t key, void *value)
320*e4b17023SJohn Marino {
321*e4b17023SJohn Marino struct tls_data *data;
322*e4b17023SJohn Marino unsigned int generation;
323*e4b17023SJohn Marino
324*e4b17023SJohn Marino if (key >= MAX_KEYS)
325*e4b17023SJohn Marino return EINVAL;
326*e4b17023SJohn Marino
327*e4b17023SJohn Marino data = __gthread_get_tls_data ();
328*e4b17023SJohn Marino if (!data)
329*e4b17023SJohn Marino {
330*e4b17023SJohn Marino if (!delete_hook_installed)
331*e4b17023SJohn Marino {
332*e4b17023SJohn Marino /* Install the delete hook. */
333*e4b17023SJohn Marino if (__gthread_mutex_lock (&tls_lock) == ERROR)
334*e4b17023SJohn Marino return ENOMEM;
335*e4b17023SJohn Marino if (!delete_hook_installed)
336*e4b17023SJohn Marino {
337*e4b17023SJohn Marino taskDeleteHookAdd ((FUNCPTR)tls_delete_hook);
338*e4b17023SJohn Marino delete_hook_installed = 1;
339*e4b17023SJohn Marino }
340*e4b17023SJohn Marino __gthread_mutex_unlock (&tls_lock);
341*e4b17023SJohn Marino }
342*e4b17023SJohn Marino
343*e4b17023SJohn Marino data = malloc (sizeof (struct tls_data));
344*e4b17023SJohn Marino if (!data)
345*e4b17023SJohn Marino return ENOMEM;
346*e4b17023SJohn Marino
347*e4b17023SJohn Marino memset (data, 0, sizeof (struct tls_data));
348*e4b17023SJohn Marino data->owner = &self_owner;
349*e4b17023SJohn Marino __gthread_set_tls_data (data);
350*e4b17023SJohn Marino }
351*e4b17023SJohn Marino
352*e4b17023SJohn Marino generation = tls_keys.generation[key];
353*e4b17023SJohn Marino
354*e4b17023SJohn Marino if (generation & 1)
355*e4b17023SJohn Marino return EINVAL;
356*e4b17023SJohn Marino
357*e4b17023SJohn Marino data->generation[key] = generation;
358*e4b17023SJohn Marino data->values[key] = value;
359*e4b17023SJohn Marino
360*e4b17023SJohn Marino return 0;
361*e4b17023SJohn Marino }
362*e4b17023SJohn Marino #endif /* __GTHREADS */
363