specific.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. /* mods for uClibc: removed strong_alias'es */
  15. /* Thread-specific data */
  16. #include <errno.h>
  17. #include <stddef.h>
  18. #include <stdlib.h>
  19. #include "pthread.h"
  20. #include "internals.h"
  21. /* Table of keys. */
  22. static struct pthread_key_struct pthread_keys[PTHREAD_KEYS_MAX] =
  23. { { 0, NULL } };
  24. /* For debugging purposes put the maximum number of keys in a variable. */
  25. const int __linuxthreads_pthread_keys_max = PTHREAD_KEYS_MAX;
  26. const int __linuxthreads_pthread_key_2ndlevel_size = PTHREAD_KEY_2NDLEVEL_SIZE;
  27. /* Mutex to protect access to pthread_keys */
  28. static pthread_mutex_t pthread_keys_mutex = PTHREAD_MUTEX_INITIALIZER;
  29. /* Create a new key */
  30. int pthread_key_create(pthread_key_t * key, destr_function destr)
  31. {
  32. int i;
  33. pthread_mutex_lock(&pthread_keys_mutex);
  34. for (i = 0; i < PTHREAD_KEYS_MAX; i++) {
  35. if (! pthread_keys[i].in_use) {
  36. /* Mark key in use */
  37. pthread_keys[i].in_use = 1;
  38. pthread_keys[i].destr = destr;
  39. pthread_mutex_unlock(&pthread_keys_mutex);
  40. *key = i;
  41. return 0;
  42. }
  43. }
  44. pthread_mutex_unlock(&pthread_keys_mutex);
  45. return EAGAIN;
  46. }
  47. //strong_alias (__pthread_key_create, pthread_key_create)
  48. /* Delete a key */
  49. int pthread_key_delete(pthread_key_t key)
  50. {
  51. pthread_descr self = thread_self();
  52. pthread_descr th;
  53. unsigned int idx1st, idx2nd;
  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. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  65. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  66. th = self;
  67. do {
  68. /* If the thread already is terminated don't modify the memory. */
  69. if (!th->p_terminated && th->p_specific[idx1st] != NULL)
  70. th->p_specific[idx1st][idx2nd] = NULL;
  71. th = th->p_nextlive;
  72. } while (th != self);
  73. pthread_mutex_unlock(&pthread_keys_mutex);
  74. return 0;
  75. }
  76. /* Set the value of a key */
  77. int pthread_setspecific(pthread_key_t key, const void * pointer)
  78. {
  79. pthread_descr self = thread_self();
  80. unsigned int idx1st, idx2nd;
  81. if (key >= PTHREAD_KEYS_MAX || !pthread_keys[key].in_use)
  82. return EINVAL;
  83. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  84. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  85. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL) {
  86. void *newp = calloc(PTHREAD_KEY_2NDLEVEL_SIZE, sizeof (void *));
  87. if (newp == NULL)
  88. return ENOMEM;
  89. THREAD_SETMEM_NC(self, p_specific[idx1st], newp);
  90. }
  91. THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd] = (void *) pointer;
  92. return 0;
  93. }
  94. //strong_alias (__pthread_setspecific, pthread_setspecific)
  95. /* Get the value of a key */
  96. void * pthread_getspecific(pthread_key_t key)
  97. {
  98. pthread_descr self = thread_self();
  99. unsigned int idx1st, idx2nd;
  100. if (key >= PTHREAD_KEYS_MAX)
  101. return NULL;
  102. idx1st = key / PTHREAD_KEY_2NDLEVEL_SIZE;
  103. idx2nd = key % PTHREAD_KEY_2NDLEVEL_SIZE;
  104. if (THREAD_GETMEM_NC(self, p_specific[idx1st]) == NULL
  105. || !pthread_keys[key].in_use)
  106. return NULL;
  107. return THREAD_GETMEM_NC(self, p_specific[idx1st])[idx2nd];
  108. }
  109. //strong_alias (__pthread_getspecific, pthread_getspecific)
  110. /* Call the destruction routines on all keys */
  111. void __pthread_destroy_specifics()
  112. {
  113. pthread_descr self = thread_self();
  114. int i, j, round, found_nonzero;
  115. destr_function destr;
  116. void * data;
  117. for (round = 0, found_nonzero = 1;
  118. found_nonzero && round < PTHREAD_DESTRUCTOR_ITERATIONS;
  119. round++) {
  120. found_nonzero = 0;
  121. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++)
  122. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
  123. for (j = 0; j < PTHREAD_KEY_2NDLEVEL_SIZE; j++) {
  124. destr = pthread_keys[i * PTHREAD_KEY_2NDLEVEL_SIZE + j].destr;
  125. data = THREAD_GETMEM_NC(self, p_specific[i])[j];
  126. if (destr != NULL && data != NULL) {
  127. THREAD_GETMEM_NC(self, p_specific[i])[j] = NULL;
  128. destr(data);
  129. found_nonzero = 1;
  130. }
  131. }
  132. }
  133. for (i = 0; i < PTHREAD_KEY_1STLEVEL_SIZE; i++) {
  134. if (THREAD_GETMEM_NC(self, p_specific[i]) != NULL)
  135. free(THREAD_GETMEM_NC(self, p_specific[i]));
  136. }
  137. }
  138. /* Thread-specific data for libc. */
  139. static int
  140. libc_internal_tsd_set(enum __libc_tsd_key_t key, const void * pointer)
  141. {
  142. pthread_descr self = thread_self();
  143. THREAD_SETMEM_NC(self, p_libc_specific[key], (void *) pointer);
  144. return 0;
  145. }
  146. int (*__libc_internal_tsd_set)(enum __libc_tsd_key_t key, const void * pointer)
  147. = libc_internal_tsd_set;
  148. static void *
  149. libc_internal_tsd_get(enum __libc_tsd_key_t key)
  150. {
  151. pthread_descr self = thread_self();
  152. return THREAD_GETMEM_NC(self, p_libc_specific[key]);
  153. }
  154. void * (*__libc_internal_tsd_get)(enum __libc_tsd_key_t key)
  155. = libc_internal_tsd_get;