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 /* The size of the thread ID lookup table.  */
32 int __pthread_max_threads;
33 
34 /* List of thread structures corresponding to free thread IDs.  */
35 struct __pthread *__pthread_free_threads;
36 pthread_mutex_t __pthread_free_threads_lock;
37 
38 static inline error_t
initialize_pthread(struct __pthread * new)39 initialize_pthread (struct __pthread *new)
40 {
41   error_t err;
42 
43   err = __pthread_init_specific (new);
44   if (err)
45     return err;
46 
47   new->nr_refs = 1;
48   new->cancel_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
49   new->cancel_hook = NULL;
50   new->cancel_hook_arg = NULL;
51   new->cancel_state = PTHREAD_CANCEL_ENABLE;
52   new->cancel_type = PTHREAD_CANCEL_DEFERRED;
53   new->cancel_pending = 0;
54 
55   new->state_lock = (pthread_mutex_t) PTHREAD_MUTEX_INITIALIZER;
56   new->state_cond = (pthread_cond_t) PTHREAD_COND_INITIALIZER;
57 
58   memset (&new->res_state, '\0', sizeof (new->res_state));
59 
60   new->tcb = NULL;
61 
62   new->next = 0;
63   new->prevp = 0;
64 
65   return 0;
66 }
67 
68 
69 /* Allocate a new thread structure and its pthread thread ID (but not
70    a kernel thread).  */
71 int
__pthread_alloc(struct __pthread ** pthread)72 __pthread_alloc (struct __pthread **pthread)
73 {
74   error_t err;
75 
76   struct __pthread *new;
77   struct __pthread **threads;
78   struct __pthread **old_threads;
79   int max_threads;
80   int new_max_threads;
81 
82   __pthread_mutex_lock (&__pthread_free_threads_lock);
83   for (new = __pthread_free_threads; new; new = new->next)
84     {
85       /* There is no need to take NEW->STATE_LOCK: if NEW is on this
86          list, then it is protected by __PTHREAD_FREE_THREADS_LOCK
87          except in __pthread_dealloc where after it is added to the
88          list (with the lock held), it drops the lock and then sets
89          NEW->STATE and immediately stops using NEW.  */
90       if (new->state == PTHREAD_TERMINATED)
91 	{
92 	  __pthread_dequeue (new);
93 	  break;
94 	}
95     }
96   __pthread_mutex_unlock (&__pthread_free_threads_lock);
97 
98   if (new)
99     {
100       if (new->tcb)
101 	{
102 	  /* Drop old values */
103 	  _dl_deallocate_tls (new->tcb, 1);
104 	}
105 
106       err = initialize_pthread (new);
107       if (!err)
108 	*pthread = new;
109       return err;
110     }
111 
112   /* Allocate a new thread structure.  */
113   new = malloc (sizeof (struct __pthread));
114   if (new == NULL)
115     return ENOMEM;
116 
117   err = initialize_pthread (new);
118   if (err)
119     {
120       free (new);
121       return err;
122     }
123 
124 retry:
125   __libc_rwlock_wrlock (GL (dl_pthread_threads_lock));
126 
127   if (GL (dl_pthread_num_threads) < __pthread_max_threads)
128     {
129       /* We have a free slot.  Use the slot number plus one as the
130          thread ID for the new thread.  */
131       new->thread = 1 + GL (dl_pthread_num_threads)++;
132       GL (dl_pthread_threads)[new->thread - 1] = NULL;
133 
134       __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
135 
136       *pthread = new;
137       return 0;
138     }
139 #ifdef PTHREAD_THREADS_MAX
140   else if (GL (dl_pthread_num_threads) >= PTHREAD_THREADS_MAX)
141     {
142       /* We have reached the limit on the number of threads per process.  */
143       __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
144 
145       free (new);
146       return EAGAIN;
147     }
148 #endif
149 
150   /* We are going to enlarge the threads table.  Save its current
151      size.  We're going to release the lock before doing the necessary
152      memory allocation, since that's a potentially blocking operation.  */
153   max_threads = __pthread_max_threads;
154 
155   __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
156 
157   /* Allocate a new lookup table that's twice as large.  */
158   new_max_threads
159       = max_threads > 0 ? max_threads * 2 : _POSIX_THREAD_THREADS_MAX;
160   threads = malloc (new_max_threads * sizeof (struct __pthread *));
161   if (threads == NULL)
162     {
163       free (new);
164       return ENOMEM;
165     }
166 
167   __libc_rwlock_wrlock (GL (dl_pthread_threads_lock));
168 
169   /* Check if nobody else has already enlarged the table.  */
170   if (max_threads != __pthread_max_threads)
171     {
172       /* Yep, they did.  */
173       __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
174 
175       /* Free the newly allocated table and try again to allocate a slot.  */
176       free (threads);
177       goto retry;
178     }
179 
180   /* Copy over the contents of the old table.  */
181   memcpy (threads, GL (dl_pthread_threads),
182 	  __pthread_max_threads * sizeof (struct __pthread *));
183 
184   /* Save the location of the old table.  We want to deallocate its
185      storage after we released the lock.  */
186   old_threads = GL (dl_pthread_threads);
187 
188   /* Replace the table with the new one.  */
189   __pthread_max_threads = new_max_threads;
190   GL (dl_pthread_threads) = threads;
191 
192   /* And allocate ourselves one of the newly created slots.  */
193   new->thread = 1 + GL (dl_pthread_num_threads)++;
194   GL (dl_pthread_threads)[new->thread - 1] = NULL;
195 
196   __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
197 
198   free (old_threads);
199 
200   *pthread = new;
201   return 0;
202 }
203 
204 void
205 attribute_hidden
__pthread_init_static_tls(struct link_map * map)206 __pthread_init_static_tls (struct link_map *map)
207 {
208   int i;
209 
210   __libc_rwlock_wrlock (GL (dl_pthread_threads_lock));
211   for (i = 0; i < GL (dl_pthread_num_threads); ++i)
212     {
213       struct __pthread *t = GL (dl_pthread_threads)[i];
214 
215       if (t == NULL)
216 	continue;
217 
218 # if TLS_TCB_AT_TP
219       void *dest = (char *) t->tcb - map->l_tls_offset;
220 # elif TLS_DTV_AT_TP
221       void *dest = (char *) t->tcb + map->l_tls_offset + TLS_PRE_TCB_SIZE;
222 # else
223 #  error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
224 # endif
225 
226       /* Initialize the memory.  */
227       memset (__mempcpy (dest, map->l_tls_initimage, map->l_tls_initimage_size),
228 	      '\0', map->l_tls_blocksize - map->l_tls_initimage_size);
229     }
230   __libc_rwlock_unlock (GL (dl_pthread_threads_lock));
231 }
232