]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/pt-alloc.c
Update copyright dates with scripts/update-copyrights
[thirdparty/glibc.git] / htl / pt-alloc.c
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.
4
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.
9
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.
14
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/>. */
18
19 #include <assert.h>
20 #include <errno.h>
21 #include <pthread.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include <pt-internal.h>
26
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." */
30
31 /* Thread ID lookup table. */
32 struct __pthread **__pthread_threads;
33
34 /* The size of the thread ID lookup table. */
35 int __pthread_max_threads;
36
37 /* The total number of thread IDs currently in use, or on the list of
38 available thread IDs. */
39 int __pthread_num_threads;
40
41 /* A lock for the table, and the other variables above. */
42 pthread_rwlock_t __pthread_threads_lock;
43
44 /* List of thread structures corresponding to free thread IDs. */
45 struct __pthread *__pthread_free_threads;
46 pthread_mutex_t __pthread_free_threads_lock;
47
48 static inline error_t
49 initialize_pthread (struct __pthread *new)
50 {
51 error_t err;
52
53 err = __pthread_init_specific (new);
54 if (err)
55 return err;
56
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;
64
65 new->state_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
66 new->state_cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
67
68 memset (&new->res_state, '\0', sizeof (new->res_state));
69
70 new->tcb = NULL;
71
72 new->next = 0;
73 new->prevp = 0;
74
75 return 0;
76 }
77
78
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)
83 {
84 error_t err;
85
86 struct __pthread *new;
87 struct __pthread **threads;
88 struct __pthread **old_threads;
89 int max_threads;
90 int new_max_threads;
91
92 __pthread_mutex_lock (&__pthread_free_threads_lock);
93 for (new = __pthread_free_threads; new; new = new->next)
94 {
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)
101 {
102 __pthread_dequeue (new);
103 break;
104 }
105 }
106 __pthread_mutex_unlock (&__pthread_free_threads_lock);
107
108 if (new)
109 {
110 if (new->tcb)
111 {
112 /* Drop old values */
113 _dl_deallocate_tls (new->tcb, 1);
114 }
115
116 err = initialize_pthread (new);
117 if (!err)
118 *pthread = new;
119 return err;
120 }
121
122 /* Allocate a new thread structure. */
123 new = malloc (sizeof (struct __pthread));
124 if (new == NULL)
125 return ENOMEM;
126
127 err = initialize_pthread (new);
128 if (err)
129 {
130 free (new);
131 return err;
132 }
133
134 retry:
135 __pthread_rwlock_wrlock (&__pthread_threads_lock);
136
137 if (__pthread_num_threads < __pthread_max_threads)
138 {
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;
143
144 __pthread_rwlock_unlock (&__pthread_threads_lock);
145
146 *pthread = new;
147 return 0;
148 }
149 #ifdef PTHREAD_THREADS_MAX
150 else if (__pthread_num_threads >= PTHREAD_THREADS_MAX)
151 {
152 /* We have reached the limit on the number of threads per process. */
153 __pthread_rwlock_unlock (&__pthread_threads_lock);
154
155 free (new);
156 return EAGAIN;
157 }
158 #endif
159
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;
164
165 __pthread_rwlock_unlock (&__pthread_threads_lock);
166
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)
172 {
173 free (new);
174 return ENOMEM;
175 }
176
177 __pthread_rwlock_wrlock (&__pthread_threads_lock);
178
179 /* Check if nobody else has already enlarged the table. */
180 if (max_threads != __pthread_max_threads)
181 {
182 /* Yep, they did. */
183 __pthread_rwlock_unlock (&__pthread_threads_lock);
184
185 /* Free the newly allocated table and try again to allocate a slot. */
186 free (threads);
187 goto retry;
188 }
189
190 /* Copy over the contents of the old table. */
191 memcpy (threads, __pthread_threads,
192 __pthread_max_threads * sizeof (struct __pthread *));
193
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;
197
198 /* Replace the table with the new one. */
199 __pthread_max_threads = new_max_threads;
200 __pthread_threads = threads;
201
202 /* And allocate ourselves one of the newly created slots. */
203 new->thread = 1 + __pthread_num_threads++;
204 __pthread_threads[new->thread - 1] = NULL;
205
206 __pthread_rwlock_unlock (&__pthread_threads_lock);
207
208 free (old_threads);
209
210 *pthread = new;
211 return 0;
212 }
213
214 void
215 attribute_hidden
216 __pthread_init_static_tls (struct link_map *map)
217 {
218 int i;
219
220 __pthread_rwlock_wrlock (&__pthread_threads_lock);
221 for (i = 0; i < __pthread_num_threads; ++i)
222 {
223 struct __pthread *t = __pthread_threads[i];
224
225 if (t == NULL)
226 continue;
227
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
235
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);
239 }
240 __pthread_rwlock_unlock (&__pthread_threads_lock);
241 }