Update ChangeLog.old/ChangeLog.23.
[glibc.git] / htl / pt-alloc.c
blobacc67f27119b3a1940bb2995879d9ffb2a89373d
1 /* Allocate a new thread structure.
2 Copyright (C) 2000-2021 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library 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 GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 #include <assert.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <string.h>
25 #include <pt-internal.h>
27 /* This braindamage is necessary because the standard says that some
28 of the threads functions "shall fail" if "No thread could be found
29 corresponding to that specified by the given thread ID." */
31 /* Thread ID lookup table. */
32 struct __pthread **__pthread_threads;
34 /* The size of the thread ID lookup table. */
35 int __pthread_max_threads;
37 /* The total number of thread IDs currently in use, or on the list of
38 available thread IDs. */
39 int __pthread_num_threads;
41 /* A lock for the table, and the other variables above. */
42 pthread_rwlock_t __pthread_threads_lock;
44 /* List of thread structures corresponding to free thread IDs. */
45 struct __pthread *__pthread_free_threads;
46 pthread_mutex_t __pthread_free_threads_lock;
48 static inline error_t
49 initialize_pthread (struct __pthread *new)
51 error_t err;
53 err = __pthread_init_specific (new);
54 if (err)
55 return err;
57 new->nr_refs = 1;
58 new->cancel_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
59 new->cancel_hook = NULL;
60 new->cancel_hook_arg = NULL;
61 new->cancel_state = PTHREAD_CANCEL_ENABLE;
62 new->cancel_type = PTHREAD_CANCEL_DEFERRED;
63 new->cancel_pending = 0;
65 new->state_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
66 new->state_cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
68 memset (&new->res_state, '\0', sizeof (new->res_state));
70 new->tcb = NULL;
72 new->next = 0;
73 new->prevp = 0;
75 return 0;
79 /* Allocate a new thread structure and its pthread thread ID (but not
80 a kernel thread). */
81 int
82 __pthread_alloc (struct __pthread **pthread)
84 error_t err;
86 struct __pthread *new;
87 struct __pthread **threads;
88 struct __pthread **old_threads;
89 int max_threads;
90 int new_max_threads;
92 __pthread_mutex_lock (&__pthread_free_threads_lock);
93 for (new = __pthread_free_threads; new; new = new->next)
95 /* There is no need to take NEW->STATE_LOCK: if NEW is on this
96 list, then it is protected by __PTHREAD_FREE_THREADS_LOCK
97 except in __pthread_dealloc where after it is added to the
98 list (with the lock held), it drops the lock and then sets
99 NEW->STATE and immediately stops using NEW. */
100 if (new->state == PTHREAD_TERMINATED)
102 __pthread_dequeue (new);
103 break;
106 __pthread_mutex_unlock (&__pthread_free_threads_lock);
108 if (new)
110 if (new->tcb)
112 /* Drop old values */
113 _dl_deallocate_tls (new->tcb, 1);
116 err = initialize_pthread (new);
117 if (!err)
118 *pthread = new;
119 return err;
122 /* Allocate a new thread structure. */
123 new = malloc (sizeof (struct __pthread));
124 if (new == NULL)
125 return ENOMEM;
127 err = initialize_pthread (new);
128 if (err)
130 free (new);
131 return err;
134 retry:
135 __pthread_rwlock_wrlock (&__pthread_threads_lock);
137 if (__pthread_num_threads < __pthread_max_threads)
139 /* We have a free slot. Use the slot number plus one as the
140 thread ID for the new thread. */
141 new->thread = 1 + __pthread_num_threads++;
142 __pthread_threads[new->thread - 1] = NULL;
144 __pthread_rwlock_unlock (&__pthread_threads_lock);
146 *pthread = new;
147 return 0;
149 #ifdef PTHREAD_THREADS_MAX
150 else if (__pthread_num_threads >= PTHREAD_THREADS_MAX)
152 /* We have reached the limit on the number of threads per process. */
153 __pthread_rwlock_unlock (&__pthread_threads_lock);
155 free (new);
156 return EAGAIN;
158 #endif
160 /* We are going to enlarge the threads table. Save its current
161 size. We're going to release the lock before doing the necessary
162 memory allocation, since that's a potentially blocking operation. */
163 max_threads = __pthread_max_threads;
165 __pthread_rwlock_unlock (&__pthread_threads_lock);
167 /* Allocate a new lookup table that's twice as large. */
168 new_max_threads
169 = max_threads > 0 ? max_threads * 2 : _POSIX_THREAD_THREADS_MAX;
170 threads = malloc (new_max_threads * sizeof (struct __pthread *));
171 if (threads == NULL)
173 free (new);
174 return ENOMEM;
177 __pthread_rwlock_wrlock (&__pthread_threads_lock);
179 /* Check if nobody else has already enlarged the table. */
180 if (max_threads != __pthread_max_threads)
182 /* Yep, they did. */
183 __pthread_rwlock_unlock (&__pthread_threads_lock);
185 /* Free the newly allocated table and try again to allocate a slot. */
186 free (threads);
187 goto retry;
190 /* Copy over the contents of the old table. */
191 memcpy (threads, __pthread_threads,
192 __pthread_max_threads * sizeof (struct __pthread *));
194 /* Save the location of the old table. We want to deallocate its
195 storage after we released the lock. */
196 old_threads = __pthread_threads;
198 /* Replace the table with the new one. */
199 __pthread_max_threads = new_max_threads;
200 __pthread_threads = threads;
202 /* And allocate ourselves one of the newly created slots. */
203 new->thread = 1 + __pthread_num_threads++;
204 __pthread_threads[new->thread - 1] = NULL;
206 __pthread_rwlock_unlock (&__pthread_threads_lock);
208 free (old_threads);
210 *pthread = new;
211 return 0;
214 void
215 attribute_hidden
216 __pthread_init_static_tls (struct link_map *map)
218 int i;
220 __pthread_rwlock_wrlock (&__pthread_threads_lock);
221 for (i = 0; i < __pthread_num_threads; ++i)
223 struct __pthread *t = __pthread_threads[i];
225 if (t == NULL)
226 continue;
228 # if TLS_TCB_AT_TP
229 void *dest = (char *) t->tcb - map->l_tls_offset;
230 # elif TLS_DTV_AT_TP
231 void *dest = (char *) t->tcb + map->l_tls_offset + TLS_PRE_TCB_SIZE;
232 # else
233 # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
234 # endif
236 /* Initialize the memory. */
237 memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
238 '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
240 __pthread_rwlock_unlock (&__pthread_threads_lock);