specific.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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, clear it just in the
  89. 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. {
  95. struct pthread_request request;
  96. args.self = 0;
  97. request.req_thread = self;
  98. request.req_kind = REQ_FOR_EACH_THREAD;
  99. request.req_args.for_each.arg = &args;
  100. request.req_args.for_each.fn = pthread_key_delete_helper;
  101. TEMP_FAILURE_RETRY(write_not_cancel(__pthread_manager_request,
  102. (char *) &request, sizeof(request)));
  103. suspend(self);
  104. }
  105. else
  106. {
  107. if (self->p_specific[args.idx1st] != NULL)
  108. self->p_specific[args.idx1st][args.idx2nd] = NULL;
  109. }
  110. pthread_mutex_unlock(&pthread_keys_mutex);
  111. return 0;
  112. }
  113. /* Set the value of a key */
  114. int __pthread_setspecific(pthread_key_t key, const void * pointer)
  115. {
  116. pthread_descr self = thread_self();
  117. unsigned int idx1st, idx2nd;
  118. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
  119. return EINVAL;
  120. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  121. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  122. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
  123. void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
  124. if (newp == NULL)
  125. return ENOMEM;
  126. THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
  127. }
  128. THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
  129. return 0;
  130. }
  131. strong_alias (__pthread_setspecific, pthread_setspecific)
  132. /* Get the value of a key */
  133. void * __pthread_getspecific(pthread_key_t key)
  134. {
  135. pthread_descr self = thread_self();
  136. unsigned int idx1st, idx2nd;
  137. if (key >= PTHREAD_KEYS_MAX)
  138. return NULL;
  139. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  140. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  141. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
  142. || !pthread_keys[key].in_use)
  143. return NULL;
  144. return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
  145. }
  146. strong_alias (__pthread_getspecific, pthread_getspecific)
  147. /* Call the destruction routines on all keys */
  148. void __pthread_destroy_specifics()
  149. {
  150. pthread_descr self = thread_self();
  151. int i, j, round, found_nonzero;
  152. destr_function destr;
  153. void * data;
  154. for (round = 0, found_nonzero = 1;
  155. found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
  156. round++) {
  157. found_nonzero = 0;
  158. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
  159. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
  160. for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
  161. destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
  162. data = THREAD_GETMEM_NC(self, p_specific[i])[j];
  163. if (destr != NULL && data != NULL) {
  164. THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
  165. destr(data);
  166. found_nonzero = 1;
  167. }
  168. }
  169. }
  170. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  171. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
  172. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
  173. free(THREAD_GETMEM_NC(self, p_specific[i]));
  174. THREAD_SETMEM_NC(self, p_specific[i], NULL);
  175. }
  176. }
  177. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  178. }
  179. #if !(USE_TLS && HAVE___THREAD)
  180. /* Thread-specific data for libc. */
  181. int
  182. __pthread_internal_tsd_set (int key, const void * pointer)
  183. {
  184. pthread_descr self = thread_self();
  185. THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
  186. return 0;
  187. }
  188. void *
  189. __pthread_internal_tsd_get (int key)
  190. {
  191. pthread_descr self = thread_self();
  192. return THREAD_GETMEM_NC(self, p_libc_specific[key]);
  193. }
  194. void ** __attribute__ ((__const__))
  195. __pthread_internal_tsd_address (int key)
  196. {
  197. pthread_descr self = thread_self();
  198. return &self->p_libc_specific[key];
  199. }
  200. #endif