gthr-vxworks-tls.c 10 KB

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