1 /* Copyright (C) 2002-2018 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 /* Thread-local storage.
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.
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
58 There is also a global array which records which keys are valid and
59 which have destructors.
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.
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.
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. */
77 /* This is the structure pointed to by the pointer returned
78 by __gthread_get_tls_data. */
82 void *values
[MAX_KEYS
];
83 unsigned int generation
[MAX_KEYS
];
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
;
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
;
94 /* TLS data access internal API. A straight __thread variable on VxWorks 7,
95 a pointer returned by kernel provided routines otherwise. */
99 static __thread
struct tls_data
*__gthread_tls_data
;
101 #define VX_GET_TLS_DATA() __gthread_tls_data
102 #define VX_SET_TLS_DATA(x) __gthread_tls_data = (x)
104 #define VX_ENTER_TLS_DTOR()
105 #define VX_LEAVE_TLS_DTOR()
109 extern void *__gthread_get_tls_data (void);
110 extern void __gthread_set_tls_data (void *data
);
112 extern void __gthread_enter_tls_dtor_context (void);
113 extern void __gthread_leave_tls_dtor_context (void);
115 #define VX_GET_TLS_DATA() __gthread_get_tls_data()
116 #define VX_SET_TLS_DATA(x) __gthread_set_tls_data(x)
118 #define VX_ENTER_TLS_DTOR() __gthread_enter_tls_dtor_context ()
119 #define VX_LEAVE_TLS_DTOR() __gthread_leave_tls_dtor_context ()
121 #endif /* __VXWORKS7__ */
123 /* This is a global structure which records all of the active keys.
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.
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. */
135 typedef void (*tls_dtor
) (void *);
139 tls_dtor dtor
[MAX_KEYS
];
140 unsigned int generation
[MAX_KEYS
];
143 #define KEY_VALID_P(key) !(tls_keys.generation[key] & 1)
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
148 static struct tls_keys tls_keys
=
150 { NULL
, NULL
, NULL
, NULL
},
154 /* This lock protects the tls_keys structure. */
155 static __gthread_mutex_t tls_lock
;
157 static __gthread_once_t tls_init_guard
= __GTHREAD_ONCE_INIT
;
159 /* Internal routines. */
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.
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. */
170 tls_delete_hook (void *tcb ATTRIBUTE_UNUSED
)
172 struct tls_data
*data
;
175 data
= VX_GET_TLS_DATA();
177 if (data
&& data
->owner
== &self_owner
)
180 for (key
= 0; key
< MAX_KEYS
; key
++)
182 if (data
->generation
[key
] == tls_keys
.generation
[key
])
184 tls_dtor dtor
= tls_keys
.dtor
[key
];
187 dtor (data
->values
[key
]);
193 VX_SET_TLS_DATA(NULL
);
197 /* Initialize global data used by the TLS system. */
201 __GTHREAD_MUTEX_INIT_FUNCTION (&tls_lock
);
204 static void tls_destructor (void) __attribute__ ((destructor
));
206 tls_destructor (void)
209 /* All threads but this one should have exited by now. */
210 tls_delete_hook (NULL
);
212 /* Unregister the hook. */
213 if (delete_hook_installed
)
214 taskDeleteHookDelete ((FUNCPTR
)tls_delete_hook
);
216 if (tls_init_guard
.done
&& __gthread_mutex_lock (&tls_lock
) != ERROR
)
217 semDelete (tls_lock
);
220 /* External interface */
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. */
229 __gthread_key_create (__gthread_key_t
*keyp
, tls_dtor dtor
)
233 __gthread_once (&tls_init_guard
, tls_init
);
235 if (__gthread_mutex_lock (&tls_lock
) == ERROR
)
238 for (key
= 0; key
< MAX_KEYS
; key
++)
239 if (!KEY_VALID_P (key
))
243 __gthread_mutex_unlock (&tls_lock
);
247 tls_keys
.generation
[key
]++; /* making it even */
248 tls_keys
.dtor
[key
] = dtor
;
250 __gthread_mutex_unlock (&tls_lock
);
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. */
258 __gthread_key_delete (__gthread_key_t key
)
263 __gthread_once (&tls_init_guard
, tls_init
);
265 if (__gthread_mutex_lock (&tls_lock
) == ERROR
)
268 if (!KEY_VALID_P (key
))
270 __gthread_mutex_unlock (&tls_lock
);
274 tls_keys
.generation
[key
]++; /* making it odd */
275 tls_keys
.dtor
[key
] = 0;
277 __gthread_mutex_unlock (&tls_lock
);
281 /* Retrieve the thread-specific value for KEY. If it has never been
282 set in this thread, or KEY is invalid, returns NULL.
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. */
289 __gthread_getspecific (__gthread_key_t key
)
291 struct tls_data
*data
;
296 data
= GET_VX_TLS_DATA();
301 if (data
->generation
[key
] != tls_keys
.generation
[key
])
304 return data
->values
[key
];
307 /* Set the thread-specific value for KEY. If KEY is invalid, or
308 memory allocation fails, returns -1, otherwise 0.
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. */
317 __gthread_setspecific (__gthread_key_t key
, void *value
)
319 struct tls_data
*data
;
320 unsigned int generation
;
325 data
= VX_GET_TLS_DATA();
329 if (!delete_hook_installed
)
331 /* Install the delete hook. */
332 if (__gthread_mutex_lock (&tls_lock
) == ERROR
)
334 if (!delete_hook_installed
)
336 taskDeleteHookAdd ((FUNCPTR
)tls_delete_hook
);
337 delete_hook_installed
= 1;
339 __gthread_mutex_unlock (&tls_lock
);
342 data
= malloc (sizeof (struct tls_data
));
346 memset (data
, 0, sizeof (struct tls_data
));
347 data
->owner
= &self_owner
;
349 VX_SET_TLS_DATA(data
);
352 generation
= tls_keys
.generation
[key
];
357 data
->generation
[key
] = generation
;
358 data
->values
[key
] = value
;
362 #endif /* __GTHREADS */