1 /* Copyright (C) 2002-2020 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. */
35 #if defined(__GTHREADS)
43 #include <taskHookLib.h>
48 #include <_vxworks-versions.h>
50 /* Thread-local storage.
52 A gthread TLS key is simply an offset in an array, the address of which
53 we store in a single pointer field associated with the current task.
55 On VxWorks 7, we have direct support for __thread variables and use
56 such a variable as the pointer "field". On other versions, we resort
57 to __gthread_get_tls_data and __gthread_set_tls_data functions provided
60 There is also a global array which records which keys are valid and
61 which have destructors.
63 A task delete hook is installed to execute key destructors. The routines
64 __gthread_enter_tls_dtor_context and __gthread_leave_tls_dtor_context,
65 which are also provided by the kernel, ensure that it is safe to call
66 free() on memory allocated by the task being deleted. This is a no-op on
67 VxWorks 5, but a major undertaking on AE.
69 The task delete hook is only installed when at least one thread
70 has TLS data. This is a necessary precaution, to allow this module
71 to be unloaded - a module with a hook can not be removed.
73 Since this interface is used to allocate only a small number of
74 keys, the table size is small and static, which simplifies the
75 code quite a bit. Revisit this if and when it becomes necessary. */
79 /* This is the structure pointed to by the pointer returned
80 by __gthread_get_tls_data. */
84 void *values
[MAX_KEYS
];
85 unsigned int generation
[MAX_KEYS
];
88 /* To make sure we only delete TLS data associated with this object,
89 include a pointer to a local variable in the TLS data object. */
90 static int self_owner
;
92 /* Flag to check whether the delete hook is installed. Once installed
93 it is only removed when unloading this module. */
94 static volatile int delete_hook_installed
;
96 /* TLS data access internal API. A straight __thread variable starting with
97 VxWorks 7, a pointer returned by kernel provided routines otherwise. */
99 #if _VXWORKS_MAJOR_GE(7)
101 static __thread
struct tls_data
*__gthread_tls_data
;
103 #define VX_GET_TLS_DATA() __gthread_tls_data
104 #define VX_SET_TLS_DATA(x) __gthread_tls_data = (x)
106 #define VX_ENTER_TLS_DTOR()
107 #define VX_LEAVE_TLS_DTOR()
111 extern void *__gthread_get_tls_data (void);
112 extern void __gthread_set_tls_data (void *data
);
114 extern void __gthread_enter_tls_dtor_context (void);
115 extern void __gthread_leave_tls_dtor_context (void);
117 #define VX_GET_TLS_DATA() __gthread_get_tls_data()
118 #define VX_SET_TLS_DATA(x) __gthread_set_tls_data(x)
120 #define VX_ENTER_TLS_DTOR() __gthread_enter_tls_dtor_context ()
121 #define VX_LEAVE_TLS_DTOR() __gthread_leave_tls_dtor_context ()
125 /* This is a global structure which records all of the active keys.
127 A key is potentially valid (i.e. has been handed out by
128 __gthread_key_create) iff its generation count in this structure is
129 even. In that case, the matching entry in the dtors array is a
130 routine to be called when a thread terminates with a valid,
131 non-NULL specific value for that key.
133 A key is actually valid in a thread T iff the generation count
134 stored in this structure is equal to the generation count stored in
135 T's specific-value structure. */
137 typedef void (*tls_dtor
) (void *);
141 tls_dtor dtor
[MAX_KEYS
];
142 unsigned int generation
[MAX_KEYS
];
145 #define KEY_VALID_P(key) !(tls_keys.generation[key] & 1)
147 /* Note: if MAX_KEYS is increased, this initializer must be updated
148 to match. All the generation counts begin at 1, which means no
150 static struct tls_keys tls_keys
=
152 { NULL
, NULL
, NULL
, NULL
},
156 /* This lock protects the tls_keys structure. */
157 static __gthread_mutex_t tls_lock
;
159 static __gthread_once_t tls_init_guard
= __GTHREAD_ONCE_INIT
;
161 /* Internal routines. */
163 /* The task TCB has just been deleted. Call the destructor
164 function for each TLS key that has both a destructor and
165 a non-NULL specific value in this thread.
167 This routine does not need to take tls_lock; the generation
168 count protects us from calling a stale destructor. It does
169 need to read tls_keys.dtor[key] atomically. */
172 tls_delete_hook (void *tcb ATTRIBUTE_UNUSED
)
174 struct tls_data
*data
;
177 data
= VX_GET_TLS_DATA();
179 if (data
&& data
->owner
== &self_owner
)
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
]);
195 VX_SET_TLS_DATA(NULL
);
199 /* Initialize global data used by the TLS system. */
203 __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock
);
206 static void tls_destructor (void) __attribute__ ((destructor
));
208 tls_destructor (void)
211 /* All threads but this one should have exited by now. */
212 tls_delete_hook (NULL
);
214 /* Unregister the hook. */
215 if (delete_hook_installed
)
216 taskDeleteHookDelete ((FUNCPTR
)tls_delete_hook
);
218 if (tls_init_guard
.done
&& __gthread_mutex_lock (&tls_lock
) != ERROR
)
219 semDelete (tls_lock
);
222 /* External interface */
224 /* Store in KEYP a value which can be passed to __gthread_setspecific/
225 __gthread_getspecific to store and retrieve a value which is
226 specific to each calling thread. If DTOR is not NULL, it will be
227 called when a thread terminates with a non-NULL specific value for
228 this key, with the value as its sole argument. */
231 __gthread_key_create (__gthread_key_t
*keyp
, tls_dtor dtor
)
235 __gthread_once (&tls_init_guard
, tls_init
);
237 if (__gthread_mutex_lock (&tls_lock
) == ERROR
)
240 for (key
= 0; key
< MAX_KEYS
; key
++)
241 if (!KEY_VALID_P (key
))
245 __gthread_mutex_unlock (&tls_lock
);
249 tls_keys
.generation
[key
]++; /* making it even */
250 tls_keys
.dtor
[key
] = dtor
;
252 __gthread_mutex_unlock (&tls_lock
);
256 /* Invalidate KEY; it can no longer be used as an argument to
257 setspecific/getspecific. Note that this does NOT call destructor
258 functions for any live values for this key. */
260 __gthread_key_delete (__gthread_key_t key
)
265 __gthread_once (&tls_init_guard
, tls_init
);
267 if (__gthread_mutex_lock (&tls_lock
) == ERROR
)
270 if (!KEY_VALID_P (key
))
272 __gthread_mutex_unlock (&tls_lock
);
276 tls_keys
.generation
[key
]++; /* making it odd */
277 tls_keys
.dtor
[key
] = 0;
279 __gthread_mutex_unlock (&tls_lock
);
283 /* Retrieve the thread-specific value for KEY. If it has never been
284 set in this thread, or KEY is invalid, returns NULL.
286 It does not matter if this function races with key_create or
287 key_delete; the worst that can happen is you get a value other than
288 the one that a serialized implementation would have provided. */
291 __gthread_getspecific (__gthread_key_t key
)
293 struct tls_data
*data
;
298 data
= VX_GET_TLS_DATA();
303 if (data
->generation
[key
] != tls_keys
.generation
[key
])
306 return data
->values
[key
];
309 /* Set the thread-specific value for KEY. If KEY is invalid, or
310 memory allocation fails, returns -1, otherwise 0.
312 The generation count protects this function against races with
313 key_create/key_delete; the worst thing that can happen is that a
314 value is successfully stored into a dead generation (and then
315 immediately becomes invalid). However, we do have to make sure
316 to read tls_keys.generation[key] atomically. */
319 __gthread_setspecific (__gthread_key_t key
, void *value
)
321 struct tls_data
*data
;
322 unsigned int generation
;
327 data
= VX_GET_TLS_DATA();
331 if (!delete_hook_installed
)
333 /* Install the delete hook. */
334 if (__gthread_mutex_lock (&tls_lock
) == ERROR
)
336 if (!delete_hook_installed
)
338 taskDeleteHookAdd ((FUNCPTR
)tls_delete_hook
);
339 delete_hook_installed
= 1;
341 __gthread_mutex_unlock (&tls_lock
);
344 data
= malloc (sizeof (struct tls_data
));
348 memset (data
, 0, sizeof (struct tls_data
));
349 data
->owner
= &self_owner
;
351 VX_SET_TLS_DATA(data
);
354 generation
= tls_keys
.generation
[key
];
359 data
->generation
[key
] = generation
;
360 data
->values
[key
] = value
;
364 #endif /* __GTHREADS */