specific.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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. /* 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. /* Delete a key */
  51. int pthread_key_delete(pthread_key_t key)
  52. {
  53. pthread_descr self = thread_self();
  54. pthread_mutex_lock(&pthread_keys_mutex);
  55. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use) {
  56. pthread_mutex_unlock(&pthread_keys_mutex);
  57. return EINVAL;
  58. }
  59. pthread_keys[key].in_use = 0;
  60. pthread_keys[key].destr = NULL;
  61. /* Set the value of the key to NULL in all running threads, so
  62. that if the key is reallocated later by pthread_key_create, its
  63. associated values will be NULL in all threads.
  64. Do nothing if no threads have been created yet. */
  65. if (__pthread_manager_request != -1)
  66. {
  67. pthread_descr th;
  68. unsigned int idx1st, idx2nd;
  69. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  70. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  71. th = self;
  72. do {
  73. /* If the thread already is terminated don't modify the memory. */
  74. if (!th->p_terminated && th->p_specific[idx1st] != NULL)
  75. th->p_specific[idx1st][idx2nd] = NULL;
  76. th = th->p_nextlive;
  77. } while (th != self);
  78. }
  79. pthread_mutex_unlock(&pthread_keys_mutex);
  80. return 0;
  81. }
  82. /* Set the value of a key */
  83. int pthread_setspecific(pthread_key_t key, const void * pointer)
  84. {
  85. pthread_descr self = thread_self();
  86. unsigned int idx1st, idx2nd;
  87. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
  88. return EINVAL;
  89. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  90. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  91. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
  92. void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
  93. if (newp == NULL)
  94. return ENOMEM;
  95. THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
  96. }
  97. THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
  98. return 0;
  99. }
  100. /* Get the value of a key */
  101. void * pthread_getspecific(pthread_key_t key)
  102. {
  103. pthread_descr self = thread_self();
  104. unsigned int idx1st, idx2nd;
  105. if (key >= PTHREAD_KEYS_MAX)
  106. return NULL;
  107. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  108. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  109. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
  110. || !pthread_keys[key].in_use)
  111. return NULL;
  112. return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
  113. }
  114. /* Call the destruction routines on all keys */
  115. void __pthread_destroy_specifics()
  116. {
  117. pthread_descr self = thread_self();
  118. int i, j, round, found_nonzero;
  119. destr_function destr;
  120. void * data;
  121. for (round = 0, found_nonzero = 1;
  122. found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
  123. round++) {
  124. found_nonzero = 0;
  125. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
  126. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
  127. for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
  128. destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
  129. data = THREAD_GETMEM_NC(self, p_specific[i])[j];
  130. if (destr != NULL && data != NULL) {
  131. THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
  132. destr(data);
  133. found_nonzero = 1;
  134. }
  135. }
  136. }
  137. __pthread_lock(THREAD_GETMEM(self, p_lock), self);
  138. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
  139. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL) {
  140. free(THREAD_GETMEM_NC(self, p_specific[i]));
  141. THREAD_SETMEM_NC(self, p_specific[i], NULL);
  142. }
  143. }
  144. __pthread_unlock(THREAD_GETMEM(self, p_lock));
  145. }
  146. /* Thread-specific data for libc. */
  147. #if !(USE_TLS && HAVE___THREAD)
  148. static int
  149. libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
  150. {
  151. pthread_descr self = thread_self();
  152. THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
  153. return 0;
  154. }
  155. int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
  156. = libc_internal_tsd_set;
  157. static void *
  158. libc_internal_tsd_get(enum __libc_tsd_key_t key)
  159. {
  160. pthread_descr self = thread_self();
  161. return THREAD_GETMEM_NC(self, p_libc_specific[key]);
  162. }
  163. void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
  164. = libc_internal_tsd_get;
  165. static void ** __attribute__ ((__const__))
  166. libc_internal_tsd_address (enum __libc_tsd_key_t key)
  167. {
  168. pthread_descr self = thread_self();
  169. return &self->p_libc_specific[key];
  170. }
  171. void **(*const __libc_internal_tsd_address) (enum __libc_tsd_key_t key)
  172. __attribute__ ((__const__)) = libc_internal_tsd_address;
  173. #endif