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