specific.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /* Linuxthreads - a simple clone()-based implementation of Posix */
  2. /* threads for Linux. */
  3. /* Copyright (C) 1996 Xavier Leroy (Xavier.Leroy@inria.fr) */
  4. /* */
  5. /* This program is free software; you can redistribute it and/or */
  6. /* modify it under the terms of the GNU Library General Public License */
  7. /* as published by the Free Software Foundation; either version 2 */
  8. /* of the License, or (at your option) any later version. */
  9. /* */
  10. /* This program is distributed in the hope that it will be useful, */
  11. /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
  12. /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
  13. /* GNU Library General Public License for more details. */
  14. /* Thread-specific data */
  15. #include <errno.h>
  16. #include <stddef.h>
  17. #include <stdlib.h>
  18. #include "pthread.h"
  19. #include "internals.h"
  20. #include "spinlock.h"
  21. #include "restart.h"
  22. #include <bits/libc-lock.h>
  23. #include <not-cancel.h>
  24. /* Table of keys. */
  25. static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
  26. { { 0, NULL } };
  27. /* For debugging purposes put the maximum number of keys in a variable. */
  28. const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX;
  29. const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE;
  30. /* Mutex to protect access to pthread_keys */
  31. static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
  32. /* Create a new key */
  33. int __pthread_key_create(pthread_key_t * key, destr_function destr)
  34. {
  35. int i;
  36. pthread_mutex_lock(&pthread_keys_mutex);
  37. for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
  38. if (! pthread_keys[i].in_use) {
  39. /* Mark key in use */
  40. pthread_keys[i].in_use = 1;
  41. pthread_keys[i].destr = destr;
  42. pthread_mutex_unlock(&pthread_keys_mutex);
  43. *key = i;
  44. return 0;
  45. }
  46. }
  47. pthread_mutex_unlock(&pthread_keys_mutex);
  48. return EAGAIN;
  49. }
  50. strong_alias (__pthread_key_create, pthread_key_create)
  51. /* Reset deleted key's value to NULL in each live thread.
  52. * NOTE: this executes in the context of the thread manager! */
  53. struct pthread_key_delete_helper_args {
  54. /* Damn, we need lexical closures in C! ;) */
  55. unsigned int idx1st, idx2nd;
  56. pthread_descr self;
  57. };
  58. static void pthread_key_delete_helper(void *arg, pthread_descr th)
  59. {
  60. struct pthread_key_delete_helper_args *args = arg;
  61. unsigned int idx1st = args->idx1st;
  62. unsigned int idx2nd = args->idx2nd;
  63. pthread_descr self = args->self;
  64. if (self == 0)
  65. self = args->self = thread_self();
  66. if (!th->p_terminated) {
  67. /* pthread_exit() may try to free th->p_specific[idx1st] concurrently. */
  68. __pthread_lock(th->p_lock, self);
  69. if (th->p_specific[idx1st] != NULL)
  70. th->p_specific[idx1st][idx2nd] = NULL;
  71. __pthread_unlock(th->p_lock);
  72. }
  73. }
  74. /* Delete a key */
  75. int pthread_key_delete(pthread_key_t key)
  76. {
  77. pthread_descr self = thread_self();
  78. pthread_mutex_lock(&pthread_keys_mutex);
  79. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
  80. pthread_mutex_unlock(&pthread_keys_mutex);
  81. return EINVAL;
  82. }
  83. pthread_keys[key].in_use = 0;
  84. pthread_keys[key].destr = NULL;
  85. /* Set the value of the key to NULL in all running threads, so
  86. that if the key is reallocated later by pthread_key_create, its
  87. associated values will be NULL in all threads.
  88. If no threads have been created yet, or if we are exiting, clear
  89. it just in the current thread. */
  90. struct pthread_key_delete_helper_args args;
  91. args.idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  92. args.idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  93. if (__pthread_manager_request != -1
  94. && !(__builtin_expect (__pthread_exit_requested, 0)))
  95. {
  96. struct pthread_request request;
  97. args.self = 0;
  98. request.req_thread = self;
  99. request.req_kind = REQ_FOR_EACH_THREAD;
  100. request.req_args.for_each.arg = &args;
  101. request.req_args.for_each.fn = pthread_key_delete_helper;
  102. TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
  103. (char *) &request, sizeof(request)));
  104. suspend(self);
  105. }
  106. else
  107. {
  108. if (self->p_specific[args.idx1st] != NULL)
  109. self->p_specific[args.idx1st][args.idx2nd] = NULL;
  110. }
  111. pthread_mutex_unlock(&pthread_keys_mutex);
  112. return 0;
  113. }
  114. /* Set the value of a key */
  115. int __pthread_setspecific(pthread_key_t key, const void * pointer)
  116. {
  117. pthread_descr self = thread_self();
  118. unsigned int idx1st, idx2nd;
  119. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
  120. return EINVAL;
  121. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  122. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  123. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
  124. void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
  125. if (newp == NULL)
  126. return ENOMEM;
  127. THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
  128. }
  129. THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
  130. return 0;
  131. }
  132. strong_alias (__pthread_setspecific, pthread_setspecific)
  133. /* Get the value of a key */
  134. void * __pthread_getspecific(pthread_key_t key)
  135. {
  136. pthread_descr self = thread_self();
  137. unsigned int idx1st, idx2nd;
  138. if (key >= PTHREAD_KEYS_MAX)
  139. return NULL;
  140. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  141. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  142. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
  143. || !pthread_keys[key].in_use)
  144. return NULL;
  145. return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
  146. }
  147. strong_alias (__pthread_getspecific, pthread_getspecific)
  148. /* Call the destruction routines on all keys */
  149. void __pthread_destroy_specifics()
  150. {
  151. pthread_descr self = thread_self();
  152. int i, j, round, found_nonzero;
  153. destr_function destr;
  154. void * data;
  155. for (round = 0, found_nonzero = 1;
  156. found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
  157. round++) {
  158. found_nonzero = 0;
  159. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
  160. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
  161. for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
  162. destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
  163. data = THREAD_GETMEM_NC(self, p_specific[i])[j];
  164. if (destr != NULL && data != NULL) {
  165. THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
  166. destr(data);
  167. found_nonzero = 1;
  168. }
  169. }
  170. }
  171. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  172. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
  173. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
  174. void *p = THREAD_GETMEM_NC(self, p_specific[i]);
  175. THREAD_SETMEM_NC(self, p_specific[i], NULL);
  176. free(p);
  177. }
  178. }
  179. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  180. }
  181. #if !defined __UCLIBC_HAS_TLS__ && defined __UCLIBC_HAS_RPC__
  182. /* Thread-specific data for libc. */
  183. int
  184. __pthread_internal_tsd_set (int key, const void * pointer)
  185. {
  186. pthread_descr self = thread_self();
  187. THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
  188. return 0;
  189. }
  190. void *
  191. __pthread_internal_tsd_get (int key)
  192. {
  193. pthread_descr self = thread_self();
  194. return THREAD_GETMEM_NC(self, p_libc_specific[key]);
  195. }
  196. void ** __attribute__ ((__const__))
  197. __pthread_internal_tsd_address (int key)
  198. {
  199. pthread_descr self = thread_self();
  200. return &self->p_libc_specific[key];
  201. }
  202. #endif