]> git.ipfire.org Git - thirdparty/glibc.git/blob - htl/pt-alloc.c
x86: In ld.so, diagnose missing APX support in APX-only builds
[thirdparty/glibc.git] / htl / pt-alloc.c
1 /* Allocate a new thread structure.
2 Copyright (C) 2000-2019 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 <http://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 new->cancelation_handlers = 0;
69
70 memset (&new->res_state, '\0', sizeof (new->res_state));
71
72 new->tcb = NULL;
73
74 new->next = 0;
75 new->prevp = 0;
76
77 return 0;
78 }
79
80
81 /* Allocate a new thread structure and its pthread thread ID (but not
82 a kernel thread). */
83 int
84 __pthread_alloc (struct __pthread **pthread)
85 {
86 error_t err;
87
88 struct __pthread *new;
89 struct __pthread **threads;
90 struct __pthread **old_threads;
91 int max_threads;
92 int new_max_threads;
93
94 __pthread_mutex_lock (&__pthread_free_threads_lock);
95 for (new = __pthread_free_threads; new; new = new->next)
96 {
97 /* There is no need to take NEW->STATE_LOCK: if NEW is on this
98 list, then it is protected by __PTHREAD_FREE_THREADS_LOCK
99 except in __pthread_dealloc where after it is added to the
100 list (with the lock held), it drops the lock and then sets
101 NEW->STATE and immediately stops using NEW. */
102 if (new->state == PTHREAD_TERMINATED)
103 {
104 __pthread_dequeue (new);
105 break;
106 }
107 }
108 __pthread_mutex_unlock (&__pthread_free_threads_lock);
109
110 if (new)
111 {
112 if (new->tcb)
113 {
114 /* Drop old values */
115 _dl_deallocate_tls (new->tcb, 1);
116 }
117
118 err = initialize_pthread (new);
119 if (!err)
120 *pthread = new;
121 return err;
122 }
123
124 /* Allocate a new thread structure. */
125 new = malloc (sizeof (struct __pthread));
126 if (new == NULL)
127 return ENOMEM;
128
129 err = initialize_pthread (new);
130 if (err)
131 {
132 free (new);
133 return err;
134 }
135
136 retry:
137 __pthread_rwlock_wrlock (&__pthread_threads_lock);
138
139 if (__pthread_num_threads < __pthread_max_threads)
140 {
141 /* We have a free slot. Use the slot number plus one as the
142 thread ID for the new thread. */
143 new->thread = 1 + __pthread_num_threads++;
144 __pthread_threads[new->thread - 1] = NULL;
145
146 __pthread_rwlock_unlock (&__pthread_threads_lock);
147
148 *pthread = new;
149 return 0;
150 }
151 #ifdef PTHREAD_THREADS_MAX
152 else if (__pthread_num_threads >= PTHREAD_THREADS_MAX)
153 {
154 /* We have reached the limit on the number of threads per process. */
155 __pthread_rwlock_unlock (&__pthread_threads_lock);
156
157 free (new);
158 return EAGAIN;
159 }
160 #endif
161
162 /* We are going to enlarge the threads table. Save its current
163 size. We're going to release the lock before doing the necessary
164 memory allocation, since that's a potentially blocking operation. */
165 max_threads = __pthread_max_threads;
166
167 __pthread_rwlock_unlock (&__pthread_threads_lock);
168
169 /* Allocate a new lookup table that's twice as large. */
170 new_max_threads
171 = max_threads > 0 ? max_threads * 2 : _POSIX_THREAD_THREADS_MAX;
172 threads = malloc (new_max_threads * sizeof (struct __pthread *));
173 if (threads == NULL)
174 {
175 free (new);
176 return ENOMEM;
177 }
178
179 __pthread_rwlock_wrlock (&__pthread_threads_lock);
180
181 /* Check if nobody else has already enlarged the table. */
182 if (max_threads != __pthread_max_threads)
183 {
184 /* Yep, they did. */
185 __pthread_rwlock_unlock (&__pthread_threads_lock);
186
187 /* Free the newly allocated table and try again to allocate a slot. */
188 free (threads);
189 goto retry;
190 }
191
192 /* Copy over the contents of the old table. */
193 memcpy (threads, __pthread_threads,
194 __pthread_max_threads * sizeof (struct __pthread *));
195
196 /* Save the location of the old table. We want to deallocate its
197 storage after we released the lock. */
198 old_threads = __pthread_threads;
199
200 /* Replace the table with the new one. */
201 __pthread_max_threads = new_max_threads;
202 __pthread_threads = threads;
203
204 /* And allocate ourselves one of the newly created slots. */
205 new->thread = 1 + __pthread_num_threads++;
206 __pthread_threads[new->thread - 1] = NULL;
207
208 __pthread_rwlock_unlock (&__pthread_threads_lock);
209
210 free (old_threads);
211
212 *pthread = new;
213 return 0;
214 }