specific.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 <features.h>
  16. #define __USE_GNU
  17. #include <errno.h>
  18. #include <stddef.h>
  19. #include <stdlib.h>
  20. #include "pthread.h"
  21. #include "internals.h"
  22. #include "spinlock.h"
  23. #include "restart.h"
  24. #include <bits/libc-lock.h>
  25. /* Table of keys. */
  26. static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
  27. { { 0, NULL } };
  28. /* For debugging purposes put the maximum number of keys in a variable. */
  29. const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX;
  30. const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE;
  31. /* Mutex to protect access to pthread_keys */
  32. static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
  33. /* Create a new key */
  34. int pthread_key_create(pthread_key_t * key, destr_function destr)
  35. {
  36. int i;
  37. pthread_mutex_lock(&pthread_keys_mutex);
  38. for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
  39. if (! pthread_keys[i].in_use) {
  40. /* Mark key in use */
  41. pthread_keys[i].in_use = 1;
  42. pthread_keys[i].destr = destr;
  43. pthread_mutex_unlock(&pthread_keys_mutex);
  44. *key = i;
  45. return 0;
  46. }
  47. }
  48. pthread_mutex_unlock(&pthread_keys_mutex);
  49. return EAGAIN;
  50. }
  51. /* Delete a key */
  52. int pthread_key_delete(pthread_key_t key)
  53. {
  54. pthread_descr self = thread_self();
  55. pthread_mutex_lock(&pthread_keys_mutex);
  56. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
  57. pthread_mutex_unlock(&pthread_keys_mutex);
  58. return EINVAL;
  59. }
  60. pthread_keys[key].in_use = 0;
  61. pthread_keys[key].destr = NULL;
  62. /* Set the value of the key to NULL in all running threads, so
  63. that if the key is reallocated later by pthread_key_create, its
  64. associated values will be NULL in all threads.
  65. Do nothing if no threads have been created yet. */
  66. if (__pthread_manager_request != -1)
  67. {
  68. pthread_descr th;
  69. unsigned int idx1st, idx2nd;
  70. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  71. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  72. th = self;
  73. do {
  74. /* If the thread already is terminated don't modify the memory. */
  75. if (!th->p_terminated && th->p_specific[idx1st] != NULL)
  76. th->p_specific[idx1st][idx2nd] = NULL;
  77. th = th->p_nextlive;
  78. } while (th != self);
  79. }
  80. pthread_mutex_unlock(&pthread_keys_mutex);
  81. return 0;
  82. }
  83. /* Set the value of a key */
  84. int pthread_setspecific(pthread_key_t key, const void * pointer)
  85. {
  86. pthread_descr self = thread_self();
  87. unsigned int idx1st, idx2nd;
  88. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
  89. return EINVAL;
  90. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  91. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  92. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
  93. void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
  94. if (newp == NULL)
  95. return ENOMEM;
  96. THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
  97. }
  98. THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
  99. return 0;
  100. }
  101. /* Get the value of a key */
  102. void * pthread_getspecific(pthread_key_t key)
  103. {
  104. pthread_descr self = thread_self();
  105. unsigned int idx1st, idx2nd;
  106. if (key >= PTHREAD_KEYS_MAX)
  107. return NULL;
  108. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  109. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  110. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
  111. || !pthread_keys[key].in_use)
  112. return NULL;
  113. return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
  114. }
  115. /* Call the destruction routines on all keys */
  116. void __pthread_destroy_specifics()
  117. {
  118. pthread_descr self = thread_self();
  119. int i, j, round, found_nonzero;
  120. destr_function destr;
  121. void * data;
  122. for (round = 0, found_nonzero = 1;
  123. found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
  124. round++) {
  125. found_nonzero = 0;
  126. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
  127. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
  128. for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
  129. destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
  130. data = THREAD_GETMEM_NC(self, p_specific[i])[j];
  131. if (destr != NULL && data != NULL) {
  132. THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
  133. destr(data);
  134. found_nonzero = 1;
  135. }
  136. }
  137. }
  138. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  139. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
  140. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
  141. free(THREAD_GETMEM_NC(self, p_specific[i]));
  142. THREAD_SETMEM_NC(self, p_specific[i], NULL);
  143. }
  144. }
  145. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  146. }
  147. /* Thread-specific data for libc. */
  148. #if !(USE_TLS && HAVE___THREAD)
  149. static int
  150. libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
  151. {
  152. pthread_descr self = thread_self();
  153. THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
  154. return 0;
  155. }
  156. int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
  157. = libc_internal_tsd_set;
  158. static void *
  159. libc_internal_tsd_get(enum __libc_tsd_key_t key)
  160. {
  161. pthread_descr self = thread_self();
  162. return THREAD_GETMEM_NC(self, p_libc_specific[key]);
  163. }
  164. void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
  165. = libc_internal_tsd_get;
  166. static void ** __attribute__ ((__const__))
  167. libc_internal_tsd_address (enum __libc_tsd_key_t key)
  168. {
  169. pthread_descr self = thread_self();
  170. return &self->p_libc_specific[key];
  171. }
  172. void **(*const __libc_internal_tsd_address) (enum __libc_tsd_key_t key)
  173. __THROW __attribute__ ((__const__)) = libc_internal_tsd_address;
  174. #endif