specific.c 5.3 KB

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