hurd libpthread: add function missing in ABI list
[glibc.git] / htl / pt-create.c
blobf5c06ffdaff31689b967b26446fb9d2f55d4a01f
1 /* Thread creation.
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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If
17 not, see <http://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>
30 #if IS_IN (libpthread)
31 # include <ctype.h>
32 #endif
33 #ifdef HAVE_USELOCALE
34 # include <locale.h>
35 #endif
37 /* The total number of pthreads currently active. This is defined
38 here since it would be really stupid to have a threads-using
39 program that doesn't call `pthread_create'. */
40 unsigned int __pthread_total;
43 /* The entry-point for new threads. */
44 static void
45 entry_point (struct __pthread *self, void *(*start_routine) (void *), void *arg)
47 ___pthread_self = self;
48 __resp = &self->res_state;
50 #if IS_IN (libpthread)
51 /* Initialize pointers to locale data. */
52 __ctype_init ();
53 #endif
54 #ifdef HAVE_USELOCALE
55 /* A fresh thread needs to be bound to the global locale. */
56 uselocale (LC_GLOBAL_LOCALE);
57 #endif
59 __pthread_startup ();
61 pthread_exit (start_routine (arg));
64 /* Create a thread with attributes given by ATTR, executing
65 START_ROUTINE with argument ARG. */
66 int
67 pthread_create (pthread_t * thread, const pthread_attr_t * attr,
68 void *(*start_routine) (void *), void *arg)
70 int err;
71 struct __pthread *pthread;
73 err = __pthread_create_internal (&pthread, attr, start_routine, arg);
74 if (!err)
75 *thread = pthread->thread;
76 else if (err == ENOMEM)
77 err = EAGAIN;
79 return err;
82 /* Internal version of pthread_create. See comment in
83 pt-internal.h. */
84 int
85 __pthread_create_internal (struct __pthread **thread,
86 const pthread_attr_t * attr,
87 void *(*start_routine) (void *), void *arg)
89 int err;
90 struct __pthread *pthread;
91 const struct __pthread_attr *setup;
92 sigset_t sigset;
93 size_t stacksize;
95 /* Allocate a new thread structure. */
96 err = __pthread_alloc (&pthread);
97 if (err)
98 goto failed;
100 /* Use the default attributes if ATTR is NULL. */
101 setup = attr ? attr : &__pthread_default_attr;
103 stacksize = setup->__stacksize;
104 if (stacksize == 0)
106 struct rlimit rlim;
107 __getrlimit (RLIMIT_STACK, &rlim);
108 if (rlim.rlim_cur != RLIM_INFINITY)
109 stacksize = rlim.rlim_cur;
110 if (stacksize == 0)
111 stacksize = PTHREAD_STACK_DEFAULT;
114 /* Initialize the thread state. */
115 pthread->state = (setup->__detachstate == PTHREAD_CREATE_DETACHED
116 ? PTHREAD_DETACHED : PTHREAD_JOINABLE);
118 if (setup->__stackaddr)
120 pthread->stackaddr = setup->__stackaddr;
122 /* If the user supplied a stack, it is not our responsibility to
123 setup a stack guard. */
124 pthread->guardsize = 0;
125 pthread->stack = 0;
127 else
129 /* Allocate a stack. */
130 err = __pthread_stack_alloc (&pthread->stackaddr,
131 ((setup->__guardsize + __vm_page_size - 1)
132 / __vm_page_size) * __vm_page_size
133 + stacksize);
134 if (err)
135 goto failed_stack_alloc;
137 pthread->guardsize = setup->__guardsize;
138 pthread->stack = 1;
141 pthread->stacksize = stacksize;
143 /* Allocate the kernel thread and other required resources. */
144 err = __pthread_thread_alloc (pthread);
145 if (err)
146 goto failed_thread_alloc;
148 pthread->tcb = _dl_allocate_tls (NULL);
149 if (pthread->tcb == NULL)
151 err = ENOMEM;
152 goto failed_thread_tls_alloc;
154 pthread->tcb->tcb = pthread->tcb;
156 /* And initialize the rest of the machine context. This may include
157 additional machine- and system-specific initializations that
158 prove convenient. */
159 err = __pthread_setup (pthread, entry_point, start_routine, arg);
160 if (err)
161 goto failed_setup;
163 /* Initialize the system-specific signal state for the new
164 thread. */
165 err = __pthread_sigstate_init (pthread);
166 if (err)
167 goto failed_sigstate;
169 /* If the new thread is joinable, add a reference for the caller. */
170 if (pthread->state == PTHREAD_JOINABLE)
171 pthread->nr_refs++;
173 /* Set the new thread's signal mask and set the pending signals to
174 empty. POSIX says: "The signal mask shall be inherited from the
175 creating thread. The set of signals pending for the new thread
176 shall be empty." If the currnet thread is not a pthread then we
177 just inherit the process' sigmask. */
178 if (__pthread_num_threads == 1)
179 err = sigprocmask (0, 0, &sigset);
180 else
181 err = __pthread_sigstate (_pthread_self (), 0, 0, &sigset, 0);
182 assert_perror (err);
184 err = __pthread_sigstate (pthread, SIG_SETMASK, &sigset, 0, 1);
185 assert_perror (err);
187 /* Increase the total number of threads. We do this before actually
188 starting the new thread, since the new thread might immediately
189 call `pthread_exit' which decreases the number of threads and
190 calls `exit' if the number of threads reaches zero. Increasing
191 the number of threads from within the new thread isn't an option
192 since this thread might return and call `pthread_exit' before the
193 new thread runs. */
194 atomic_increment (&__pthread_total);
196 /* Store a pointer to this thread in the thread ID lookup table. We
197 could use __thread_setid, however, we only lock for reading as no
198 other thread should be using this entry (we also assume that the
199 store is atomic). */
200 __pthread_rwlock_rdlock (&__pthread_threads_lock);
201 __pthread_threads[pthread->thread - 1] = pthread;
202 __pthread_rwlock_unlock (&__pthread_threads_lock);
204 /* At this point it is possible to guess our pthread ID. We have to
205 make sure that all functions taking a pthread_t argument can
206 handle the fact that this thread isn't really running yet. Since
207 the new thread might be passed its ID through pthread_create (to
208 avoid calling pthread_self), read it before starting the thread. */
209 *thread = pthread;
211 /* Schedule the new thread. */
212 err = __pthread_thread_start (pthread);
213 if (err)
214 goto failed_starting;
217 return 0;
219 failed_starting:
220 /* If joinable, a reference was added for the caller. */
221 if (pthread->state == PTHREAD_JOINABLE)
222 __pthread_dealloc (pthread);
224 __pthread_setid (pthread->thread, NULL);
225 atomic_decrement (&__pthread_total);
226 failed_sigstate:
227 __pthread_sigstate_destroy (pthread);
228 failed_setup:
229 _dl_deallocate_tls (pthread->tcb, 1);
230 pthread->tcb = NULL;
231 failed_thread_tls_alloc:
232 __pthread_thread_terminate (pthread);
234 /* __pthread_thread_terminate has taken care of deallocating the stack and
235 the thread structure. */
236 goto failed;
237 failed_thread_alloc:
238 if (pthread->stack)
239 __pthread_stack_dealloc (pthread->stackaddr,
240 ((setup->__guardsize + __vm_page_size - 1)
241 / __vm_page_size) * __vm_page_size + stacksize);
242 failed_stack_alloc:
243 __pthread_dealloc (pthread);
244 failed:
245 return err;