specific.c 5.6 KB

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