Use common bits/shm.h for more architectures.
[glibc.git] / htl / pt-alloc.c
blobf87829fe7807862ea052662cb5f585f76674e98b
1 /* Allocate a new thread structure.
2 Copyright (C) 2000-2018 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 <http://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 new->cancelation_handlers = 0;
70 memset (&new->res_state, '\0', sizeof (new->res_state));
72 new->tcb = NULL;
74 new->next = 0;
75 new->prevp = 0;
77 return 0;
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)
86 error_t err;
88 struct __pthread *new;
89 struct __pthread **threads;
90 struct __pthread **old_threads;
91 int max_threads;
92 int new_max_threads;
94 __pthread_mutex_lock (&__pthread_free_threads_lock);
95 for (new = __pthread_free_threads; new; new = new->next)
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)
104 __pthread_dequeue (new);
105 break;
108 __pthread_mutex_unlock (&__pthread_free_threads_lock);
110 if (new)
112 if (new->tcb)
114 /* Drop old values */
115 _dl_deallocate_tls (new->tcb, 1);
118 err = initialize_pthread (new);
119 if (!err)
120 *pthread = new;
121 return err;
124 /* Allocate a new thread structure. */
125 new = malloc (sizeof (struct __pthread));
126 if (new == NULL)
127 return ENOMEM;
129 err = initialize_pthread (new);
130 if (err)
132 free (new);
133 return err;
136 retry:
137 __pthread_rwlock_wrlock (&__pthread_threads_lock);
139 if (__pthread_num_threads < __pthread_max_threads)
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;
146 __pthread_rwlock_unlock (&__pthread_threads_lock);
148 *pthread = new;
149 return 0;
151 #ifdef PTHREAD_THREADS_MAX
152 else if (__pthread_num_threads >= PTHREAD_THREADS_MAX)
154 /* We have reached the limit on the number of threads per process. */
155 __pthread_rwlock_unlock (&__pthread_threads_lock);
157 free (new);
158 return EAGAIN;
160 #endif
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;
167 __pthread_rwlock_unlock (&__pthread_threads_lock);
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)
175 free (new);
176 return ENOMEM;
179 __pthread_rwlock_wrlock (&__pthread_threads_lock);
181 /* Check if nobody else has already enlarged the table. */
182 if (max_threads != __pthread_max_threads)
184 /* Yep, they did. */
185 __pthread_rwlock_unlock (&__pthread_threads_lock);
187 /* Free the newly allocated table and try again to allocate a slot. */
188 free (threads);
189 goto retry;
192 /* Copy over the contents of the old table. */
193 memcpy (threads, __pthread_threads,
194 __pthread_max_threads * sizeof (struct __pthread *));
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;
200 /* Replace the table with the new one. */
201 __pthread_max_threads = new_max_threads;
202 __pthread_threads = threads;
204 /* And allocate ourselves one of the newly created slots. */
205 new->thread = 1 + __pthread_num_threads++;
206 __pthread_threads[new->thread - 1] = NULL;
208 __pthread_rwlock_unlock (&__pthread_threads_lock);
210 free (old_threads);
212 *pthread = new;
213 return 0;