specific.c 5.7 KB

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