]>
git.ipfire.org Git - thirdparty/glibc.git/blob - htl/pt-create.c
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/>. */
26 #include <hurd/resource.h>
27 #include <sys/single_threaded.h>
29 #include <pt-internal.h>
32 #if IS_IN (libpthread)
39 /* The total number of pthreads currently active. This is defined
40 here since it would be really stupid to have a threads-using
41 program that doesn't call `pthread_create'. */
42 unsigned int __pthread_total
;
45 /* The entry-point for new threads. */
47 entry_point (struct __pthread
*self
, void *(*start_routine
) (void *), void *arg
)
51 ___pthread_self
= self
;
52 __resp
= &self
->res_state
;
54 #if IS_IN (libpthread)
55 /* Initialize pointers to locale data. */
59 /* A fresh thread needs to be bound to the global locale. */
60 uselocale (LC_GLOBAL_LOCALE
);
65 /* We can now unleash signals. */
66 err
= __pthread_sigstate (self
, SIG_SETMASK
, &self
->init_sigset
, 0, 0);
71 /* The function pointer of the c11 thread start is cast to an incorrect
72 type on __pthread_create call, however it is casted back to correct
73 one so the call behavior is well-defined (it is assumed that pointers
74 to void are able to represent all values of int). */
75 int (*start
)(void*) = (int (*) (void*)) start_routine
;
76 __pthread_exit ((void*) (uintptr_t) start (arg
));
79 __pthread_exit (start_routine (arg
));
82 /* Create a thread with attributes given by ATTR, executing
83 START_ROUTINE with argument ARG. */
85 __pthread_create (pthread_t
* thread
, const pthread_attr_t
* attr
,
86 void *(*start_routine
) (void *), void *arg
)
89 struct __pthread
*pthread
;
91 err
= __pthread_create_internal (&pthread
, attr
, start_routine
, arg
);
93 *thread
= pthread
->thread
;
94 else if (err
== ENOMEM
)
99 weak_alias (__pthread_create
, pthread_create
)
101 /* Internal version of pthread_create. See comment in
104 __pthread_create_internal (struct __pthread
**thread
,
105 const pthread_attr_t
* attr
,
106 void *(*start_routine
) (void *), void *arg
)
109 struct __pthread
*pthread
;
110 const struct __pthread_attr
*setup
;
114 /* Avoid a data race in the multi-threaded case. */
115 if (__libc_single_threaded
)
116 __libc_single_threaded
= 0;
118 /* Allocate a new thread structure. */
119 err
= __pthread_alloc (&pthread
);
123 if (attr
== ATTR_C11_THREAD
)
129 pthread
->c11
= false;
131 /* Use the default attributes if ATTR is NULL. */
132 setup
= attr
? attr
: &__pthread_default_attr
;
134 stacksize
= setup
->__stacksize
;
138 __getrlimit (RLIMIT_STACK
, &rlim
);
139 if (rlim
.rlim_cur
!= RLIM_INFINITY
)
140 stacksize
= rlim
.rlim_cur
;
142 stacksize
= PTHREAD_STACK_DEFAULT
;
145 /* Initialize the thread state. */
146 pthread
->state
= (setup
->__detachstate
== PTHREAD_CREATE_DETACHED
147 ? PTHREAD_DETACHED
: PTHREAD_JOINABLE
);
149 if (setup
->__stackaddr
)
151 pthread
->stackaddr
= setup
->__stackaddr
;
153 /* If the user supplied a stack, it is not our responsibility to
154 setup a stack guard. */
155 pthread
->guardsize
= 0;
160 /* Allocate a stack. */
161 err
= __pthread_stack_alloc (&pthread
->stackaddr
,
162 ((setup
->__guardsize
+ __vm_page_size
- 1)
163 / __vm_page_size
) * __vm_page_size
166 goto failed_stack_alloc
;
168 pthread
->guardsize
= setup
->__guardsize
;
172 pthread
->stacksize
= stacksize
;
174 /* Allocate the kernel thread and other required resources. */
175 err
= __pthread_thread_alloc (pthread
);
177 goto failed_thread_alloc
;
179 pthread
->tcb
= _dl_allocate_tls (NULL
);
180 if (pthread
->tcb
== NULL
)
183 goto failed_thread_tls_alloc
;
185 pthread
->tcb
->tcb
= pthread
->tcb
;
187 /* And initialize the rest of the machine context. This may include
188 additional machine- and system-specific initializations that
190 err
= __pthread_setup (pthread
, entry_point
, start_routine
, arg
);
194 /* Initialize the system-specific signal state for the new
196 err
= __pthread_sigstate_init (pthread
);
198 goto failed_sigstate
;
200 /* If the new thread is joinable, add a reference for the caller. */
201 if (pthread
->state
== PTHREAD_JOINABLE
)
204 /* Set the new thread's signal mask and set the pending signals to
205 empty. POSIX says: "The signal mask shall be inherited from the
206 creating thread. The set of signals pending for the new thread
207 shall be empty." If the currnet thread is not a pthread then we
208 just inherit the process' sigmask. */
209 if (__pthread_num_threads
== 1)
210 err
= __sigprocmask (0, 0, &pthread
->init_sigset
);
212 err
= __pthread_sigstate (_pthread_self (), 0, 0, &pthread
->init_sigset
, 0);
215 /* But block the signals for now, until the thread is fully initialized. */
216 __sigfillset (&sigset
);
217 err
= __pthread_sigstate (pthread
, SIG_SETMASK
, &sigset
, 0, 1);
220 /* Increase the total number of threads. We do this before actually
221 starting the new thread, since the new thread might immediately
222 call `pthread_exit' which decreases the number of threads and
223 calls `exit' if the number of threads reaches zero. Increasing
224 the number of threads from within the new thread isn't an option
225 since this thread might return and call `pthread_exit' before the
227 atomic_increment (&__pthread_total
);
229 /* Store a pointer to this thread in the thread ID lookup table. We
230 could use __thread_setid, however, we only lock for reading as no
231 other thread should be using this entry (we also assume that the
233 __pthread_rwlock_rdlock (&__pthread_threads_lock
);
234 __pthread_threads
[pthread
->thread
- 1] = pthread
;
235 __pthread_rwlock_unlock (&__pthread_threads_lock
);
237 /* At this point it is possible to guess our pthread ID. We have to
238 make sure that all functions taking a pthread_t argument can
239 handle the fact that this thread isn't really running yet. Since
240 the new thread might be passed its ID through pthread_create (to
241 avoid calling pthread_self), read it before starting the thread. */
244 /* Schedule the new thread. */
245 err
= __pthread_thread_start (pthread
);
247 goto failed_starting
;
253 /* If joinable, a reference was added for the caller. */
254 if (pthread
->state
== PTHREAD_JOINABLE
)
255 __pthread_dealloc (pthread
);
257 __pthread_setid (pthread
->thread
, NULL
);
258 atomic_decrement (&__pthread_total
);
260 __pthread_sigstate_destroy (pthread
);
262 _dl_deallocate_tls (pthread
->tcb
, 1);
264 failed_thread_tls_alloc
:
265 __pthread_thread_terminate (pthread
);
267 /* __pthread_thread_terminate has taken care of deallocating the stack and
268 the thread structure. */
272 __pthread_stack_dealloc (pthread
->stackaddr
,
273 ((setup
->__guardsize
+ __vm_page_size
- 1)
274 / __vm_page_size
) * __vm_page_size
+ stacksize
);
276 __pthread_dealloc (pthread
);