pt-alloc.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /* Allocate a new thread structure.
  2. Copyright (C) 2000-2026 Free Software Foundation, Inc.
  3. This file is part of the GNU C Library.
  4. The GNU C Library is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU Lesser General Public
  6. License as published by the Free Software Foundation; either
  7. version 2.1 of the License, or (at your option) any later version.
  8. The GNU C Library is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public
  13. License along with the GNU C Library; if not, see
  14. <https://www.gnu.org/licenses/>. */
  15. #include <assert.h>
  16. #include <errno.h>
  17. #include <pthread.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <pt-internal.h>
  21. #include <ldsodefs.h>
  22. /* This braindamage is necessary because the standard says that some
  23. of the threads functions "shall fail" if "No thread could be found
  24. corresponding to that specified by the given thread ID." */
  25. /* The size of the thread ID lookup table. */
  26. int __pthread_max_threads;
  27. libc_hidden_data_def (__pthread_max_threads)
  28. /* List of thread structures corresponding to free thread IDs. */
  29. struct __pthread *__pthread_free_threads;
  30. pthread_mutex_t __pthread_free_threads_lock;
  31. static inline error_t
  32. initialize_pthread (struct __pthread *new)
  33. {
  34. error_t err;
  35. err = __pthread_init_specific (new);
  36. if (err)
  37. return err;
  38. new->nr_refs = 1;
  39. new->cancel_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
  40. new->cancel_hook = NULL;
  41. new->cancel_hook_arg = NULL;
  42. new->cancel_state = PTHREAD_CANCEL_ENABLE;
  43. new->cancel_type = PTHREAD_CANCEL_DEFERRED;
  44. new->cancel_pending = 0;
  45. new->state_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
  46. new->state_cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
  47. new->terminated = FALSE;
  48. memset (&new->res_state, '\0', sizeof (new->res_state));
  49. new->tcb = NULL;
  50. new->next = 0;
  51. new->prevp = 0;
  52. return 0;
  53. }
  54. /* Allocate a new thread structure and its pthread thread ID (but not
  55. a kernel thread). */
  56. int
  57. __pthread_alloc (struct __pthread **pthread)
  58. {
  59. error_t err;
  60. struct __pthread *new;
  61. struct __pthread **threads;
  62. struct __pthread **old_threads;
  63. int max_threads;
  64. int new_max_threads;
  65. __pthread_mutex_lock (&__pthread_free_threads_lock);
  66. for (new = __pthread_free_threads; new; new = new->next)
  67. {
  68. /* There is no need to take NEW->STATE_LOCK: if NEW is on this
  69. list, then it is protected by __PTHREAD_FREE_THREADS_LOCK
  70. except in __pthread_dealloc_finish where after it is added to the
  71. list (with the lock held), it drops the lock and then sets
  72. NEW->STATE and immediately stops using NEW. */
  73. if (new->terminated)
  74. {
  75. __pthread_dequeue (new);
  76. break;
  77. }
  78. }
  79. __pthread_mutex_unlock (&__pthread_free_threads_lock);
  80. if (new)
  81. {
  82. if (new->tcb)
  83. {
  84. /* Drop old values */
  85. _dl_deallocate_tls (new->tcb, 1);
  86. }
  87. err = initialize_pthread (new);
  88. if (!err)
  89. *pthread = new;
  90. return err;
  91. }
  92. /* Allocate a new thread structure. */
  93. new = malloc (sizeof (struct __pthread));
  94. if (new == NULL)
  95. return ENOMEM;
  96. err = initialize_pthread (new);
  97. if (err)
  98. {
  99. free (new);
  100. return err;
  101. }
  102. retry:
  103. __libc_rwlock_wrlock (GL (dl_pthread_threads_lock));
  104. if (GL (dl_pthread_num_threads) < __pthread_max_threads)
  105. {
  106. /* We have a free slot. Use the slot number plus one as the
  107. thread ID for the new thread. */
  108. new->thread = 1 + GL (dl_pthread_num_threads)++;
  109. GL (dl_pthread_threads)[new->thread - 1] = NULL;
  110. __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
  111. *pthread = new;
  112. return 0;
  113. }
  114. #ifdef PTHREAD_THREADS_MAX
  115. else if (GL (dl_pthread_num_threads) >= PTHREAD_THREADS_MAX)
  116. {
  117. /* We have reached the limit on the number of threads per process. */
  118. __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
  119. free (new);
  120. return EAGAIN;
  121. }
  122. #endif
  123. /* We are going to enlarge the threads table. Save its current
  124. size. We're going to release the lock before doing the necessary
  125. memory allocation, since that's a potentially blocking operation. */
  126. max_threads = __pthread_max_threads;
  127. __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
  128. /* Allocate a new lookup table that's twice as large. */
  129. new_max_threads
  130. = max_threads > 0 ? max_threads * 2 : _POSIX_THREAD_THREADS_MAX;
  131. threads = malloc (new_max_threads * sizeof (struct __pthread *));
  132. if (threads == NULL)
  133. {
  134. free (new);
  135. return ENOMEM;
  136. }
  137. __libc_rwlock_wrlock (GL (dl_pthread_threads_lock));
  138. /* Check if nobody else has already enlarged the table. */
  139. if (max_threads != __pthread_max_threads)
  140. {
  141. /* Yep, they did. */
  142. __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
  143. /* Free the newly allocated table and try again to allocate a slot. */
  144. free (threads);
  145. goto retry;
  146. }
  147. /* Copy over the contents of the old table. */
  148. memcpy (threads, GL (dl_pthread_threads),
  149. __pthread_max_threads * sizeof (struct __pthread *));
  150. /* Save the location of the old table. We want to deallocate its
  151. storage after we released the lock. */
  152. old_threads = GL (dl_pthread_threads);
  153. /* Replace the table with the new one. */
  154. __pthread_max_threads = new_max_threads;
  155. GL (dl_pthread_threads) = threads;
  156. /* And allocate ourselves one of the newly created slots. */
  157. new->thread = 1 + GL (dl_pthread_num_threads)++;
  158. GL (dl_pthread_threads)[new->thread - 1] = NULL;
  159. __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
  160. free (old_threads);
  161. *pthread = new;
  162. return 0;
  163. }
  164. libc_hidden_def (__pthread_alloc)
  165. void
  166. attribute_hidden
  167. __pthread_init_static_tls (struct link_map *map)
  168. {
  169. int i;
  170. __libc_rwlock_wrlock (GL (dl_pthread_threads_lock));
  171. for (i = 0; i < GL (dl_pthread_num_threads); ++i)
  172. {
  173. struct __pthread *t = GL (dl_pthread_threads)[i];
  174. if (t == NULL)
  175. continue;
  176. # if TLS_TCB_AT_TP
  177. void *dest = (char *) t->tcb - map->l_tls_offset;
  178. # elif TLS_DTV_AT_TP
  179. void *dest = (char *) t->tcb + map->l_tls_offset + TLS_PRE_TCB_SIZE;
  180. # else
  181. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  182. # endif
  183. /* Initialize the memory. */
  184. memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
  185. '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
  186. }
  187. __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
  188. }
  189. libc_hidden_def (__pthread_init_static_tls)