powerpc64le: bump binutils version requirement to >= 2.26
[glibc.git] / htl / pt-create.c
blobf501a12017c275b3831785f696fa306103ae0d97
1 /* Thread creation.
2 Copyright (C) 2000-2020 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 <signal.h>
23 #include <resolv.h>
25 #include <atomic.h>
26 #include <hurd/resource.h>
28 #include <pt-internal.h>
29 #include <pthreadP.h>
31 #if IS_IN (libpthread)
32 # include <ctype.h>
33 #endif
34 #ifdef HAVE_USELOCALE
35 # include <locale.h>
36 #endif
38 /* The total number of pthreads currently active. This is defined
39 here since it would be really stupid to have a threads-using
40 program that doesn't call `pthread_create'. */
41 unsigned int __pthread_total;
44 /* The entry-point for new threads. */
45 static void
46 entry_point (struct __pthread *self, void *(*start_routine) (void *), void *arg)
48 ___pthread_self = self;
49 __resp = &self->res_state;
51 #if IS_IN (libpthread)
52 /* Initialize pointers to locale data. */
53 __ctype_init ();
54 #endif
55 #ifdef HAVE_USELOCALE
56 /* A fresh thread needs to be bound to the global locale. */
57 uselocale (LC_GLOBAL_LOCALE);
58 #endif
60 __pthread_startup ();
62 if (self->c11)
64 /* The function pointer of the c11 thread start is cast to an incorrect
65 type on __pthread_create call, however it is casted back to correct
66 one so the call behavior is well-defined (it is assumed that pointers
67 to void are able to represent all values of int). */
68 int (*start)(void*) = (int (*) (void*)) start_routine;
69 __pthread_exit ((void*) (uintptr_t) start (arg));
71 else
72 __pthread_exit (start_routine (arg));
75 /* Create a thread with attributes given by ATTR, executing
76 START_ROUTINE with argument ARG. */
77 int
78 __pthread_create (pthread_t * thread, const pthread_attr_t * attr,
79 void *(*start_routine) (void *), void *arg)
81 int err;
82 struct __pthread *pthread;
84 err = __pthread_create_internal (&pthread, attr, start_routine, arg);
85 if (!err)
86 *thread = pthread->thread;
87 else if (err == ENOMEM)
88 err = EAGAIN;
90 return err;
92 weak_alias (__pthread_create, pthread_create)
94 /* Internal version of pthread_create. See comment in
95 pt-internal.h. */
96 int
97 __pthread_create_internal (struct __pthread **thread,
98 const pthread_attr_t * attr,
99 void *(*start_routine) (void *), void *arg)
101 int err;
102 struct __pthread *pthread;
103 const struct __pthread_attr *setup;
104 sigset_t sigset;
105 size_t stacksize;
107 /* Allocate a new thread structure. */
108 err = __pthread_alloc (&pthread);
109 if (err)
110 goto failed;
112 if (attr == ATTR_C11_THREAD)
114 attr = NULL;
115 pthread->c11 = true;
117 else
118 pthread->c11 = false;
120 /* Use the default attributes if ATTR is NULL. */
121 setup = attr ? attr : &__pthread_default_attr;
123 stacksize = setup->__stacksize;
124 if (stacksize == 0)
126 struct rlimit rlim;
127 __getrlimit (RLIMIT_STACK, &rlim);
128 if (rlim.rlim_cur != RLIM_INFINITY)
129 stacksize = rlim.rlim_cur;
130 if (stacksize == 0)
131 stacksize = PTHREAD_STACK_DEFAULT;
134 /* Initialize the thread state. */
135 pthread->state = (setup->__detachstate == PTHREAD_CREATE_DETACHED
136 ? PTHREAD_DETACHED : PTHREAD_JOINABLE);
138 if (setup->__stackaddr)
140 pthread->stackaddr = setup->__stackaddr;
142 /* If the user supplied a stack, it is not our responsibility to
143 setup a stack guard. */
144 pthread->guardsize = 0;
145 pthread->stack = 0;
147 else
149 /* Allocate a stack. */
150 err = __pthread_stack_alloc (&pthread->stackaddr,
151 ((setup->__guardsize + __vm_page_size - 1)
152 / __vm_page_size) * __vm_page_size
153 + stacksize);
154 if (err)
155 goto failed_stack_alloc;
157 pthread->guardsize = setup->__guardsize;
158 pthread->stack = 1;
161 pthread->stacksize = stacksize;
163 /* Allocate the kernel thread and other required resources. */
164 err = __pthread_thread_alloc (pthread);
165 if (err)
166 goto failed_thread_alloc;
168 pthread->tcb = _dl_allocate_tls (NULL);
169 if (pthread->tcb == NULL)
171 err = ENOMEM;
172 goto failed_thread_tls_alloc;
174 pthread->tcb->tcb = pthread->tcb;
176 /* And initialize the rest of the machine context. This may include
177 additional machine- and system-specific initializations that
178 prove convenient. */
179 err = __pthread_setup (pthread, entry_point, start_routine, arg);
180 if (err)
181 goto failed_setup;
183 /* Initialize the system-specific signal state for the new
184 thread. */
185 err = __pthread_sigstate_init (pthread);
186 if (err)
187 goto failed_sigstate;
189 /* If the new thread is joinable, add a reference for the caller. */
190 if (pthread->state == PTHREAD_JOINABLE)
191 pthread->nr_refs++;
193 /* Set the new thread's signal mask and set the pending signals to
194 empty. POSIX says: "The signal mask shall be inherited from the
195 creating thread. The set of signals pending for the new thread
196 shall be empty." If the currnet thread is not a pthread then we
197 just inherit the process' sigmask. */
198 if (__pthread_num_threads == 1)
199 err = __sigprocmask (0, 0, &sigset);
200 else
201 err = __pthread_sigstate (_pthread_self (), 0, 0, &sigset, 0);
202 assert_perror (err);
204 err = __pthread_sigstate (pthread, SIG_SETMASK, &sigset, 0, 1);
205 assert_perror (err);
207 /* Increase the total number of threads. We do this before actually
208 starting the new thread, since the new thread might immediately
209 call `pthread_exit' which decreases the number of threads and
210 calls `exit' if the number of threads reaches zero. Increasing
211 the number of threads from within the new thread isn't an option
212 since this thread might return and call `pthread_exit' before the
213 new thread runs. */
214 atomic_increment (&__pthread_total);
216 /* Store a pointer to this thread in the thread ID lookup table. We
217 could use __thread_setid, however, we only lock for reading as no
218 other thread should be using this entry (we also assume that the
219 store is atomic). */
220 __pthread_rwlock_rdlock (&__pthread_threads_lock);
221 __pthread_threads[pthread->thread - 1] = pthread;
222 __pthread_rwlock_unlock (&__pthread_threads_lock);
224 /* At this point it is possible to guess our pthread ID. We have to
225 make sure that all functions taking a pthread_t argument can
226 handle the fact that this thread isn't really running yet. Since
227 the new thread might be passed its ID through pthread_create (to
228 avoid calling pthread_self), read it before starting the thread. */
229 *thread = pthread;
231 /* Schedule the new thread. */
232 err = __pthread_thread_start (pthread);
233 if (err)
234 goto failed_starting;
237 return 0;
239 failed_starting:
240 /* If joinable, a reference was added for the caller. */
241 if (pthread->state == PTHREAD_JOINABLE)
242 __pthread_dealloc (pthread);
244 __pthread_setid (pthread->thread, NULL);
245 atomic_decrement (&__pthread_total);
246 failed_sigstate:
247 __pthread_sigstate_destroy (pthread);
248 failed_setup:
249 _dl_deallocate_tls (pthread->tcb, 1);
250 pthread->tcb = NULL;
251 failed_thread_tls_alloc:
252 __pthread_thread_terminate (pthread);
254 /* __pthread_thread_terminate has taken care of deallocating the stack and
255 the thread structure. */
256 goto failed;
257 failed_thread_alloc:
258 if (pthread->stack)
259 __pthread_stack_dealloc (pthread->stackaddr,
260 ((setup->__guardsize + __vm_page_size - 1)
261 / __vm_page_size) * __vm_page_size + stacksize);
262 failed_stack_alloc:
263 __pthread_dealloc (pthread);
264 failed:
265 return err;