2 * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
3 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by John Birrell.
17 * 4. Neither the name of the author nor the names of any co-contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * $FreeBSD: src/lib/libpthread/thread/thr_init.c,v 1.66 2004/08/21 11:49:19 davidxu Exp $
34 * $DragonFly: src/lib/libthread_xu/thread/thr_init.c,v 1.6 2006/04/05 00:48:50 davidxu Exp $
37 /* Allocate space for global thread variables here: */
38 #define GLOBAL_PTHREAD_PRIVATE
40 #include "namespace.h"
41 #include <sys/param.h>
42 #include <sys/types.h>
43 #include <sys/signalvar.h>
44 #include <machine/reg.h>
45 #include <machine/tls.h>
47 #include <sys/ioctl.h>
48 #include <sys/mount.h>
50 #include <sys/socket.h>
51 #include <sys/event.h>
53 #include <sys/sysctl.h>
55 #include <sys/ttycom.h>
64 #include <pthread_np.h>
70 #include "un-namespace.h"
72 #include "libc_private.h"
73 #include "thr_private.h"
75 extern int _thread_state_running;
77 int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
78 int __pthread_mutex_lock(pthread_mutex_t *);
79 int __pthread_mutex_trylock(pthread_mutex_t *);
81 void _thread_init(void) __attribute__ ((constructor));
83 static void init_private(void);
84 static void init_main_thread(struct pthread *thread);
86 static int init_once = 0;
89 STATIC_LIB_REQUIRE(_pthread_attr_init);
91 STATIC_LIB_REQUIRE(_pthread_barrier_init);
92 /* thr_barrierattr.c */
93 STATIC_LIB_REQUIRE(_pthread_barrierattr_init);
95 STATIC_LIB_REQUIRE(_pthread_cancel);
97 STATIC_LIB_REQUIRE(_pthread_cleanup_push);
98 /* thr_concurrency.c */
99 STATIC_LIB_REQUIRE(_pthread_get_concurrency);
101 STATIC_LIB_REQUIRE(_pthread_cond_init);
103 STATIC_LIB_REQUIRE(_pthread_condattr_init);
105 STATIC_LIB_REQUIRE(_pthread_create);
107 STATIC_LIB_REQUIRE(_pthread_detach);
109 STATIC_LIB_REQUIRE(_pthread_equal);
111 STATIC_LIB_REQUIRE(_pthread_exit);
113 STATIC_LIB_REQUIRE(_pthread_atfork);
114 STATIC_LIB_REQUIRE(_fork);
116 STATIC_LIB_REQUIRE(_pthread_getprio);
117 /* thr_getschedparam.c */
118 STATIC_LIB_REQUIRE(_pthread_getschedparam);
120 STATIC_LIB_REQUIRE(_pthread_set_name_np);
122 STATIC_LIB_REQUIRE(_pthread_join);
124 STATIC_LIB_REQUIRE(_pthread_kill);
126 STATIC_LIB_REQUIRE(_pthread_main_np);
128 STATIC_LIB_REQUIRE(_pthread_multi_np);
130 STATIC_LIB_REQUIRE(_pthread_mutex_init);
131 /* thr_mutex_prioceiling.c */
132 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprioceiling);
133 /* thr_mutex_protocol.c */
134 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprotocol);
135 /* thr_mutexattr.c */
136 STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
138 STATIC_LIB_REQUIRE(_pthread_once);
139 /* thr_pspinlock.c */
140 STATIC_LIB_REQUIRE(_pthread_spin_init);
141 /* thr_resume_np.c */
142 STATIC_LIB_REQUIRE(_pthread_resume_np);
144 STATIC_LIB_REQUIRE(_pthread_rwlock_init);
145 /* thr_rwlockattr.c */
146 STATIC_LIB_REQUIRE(_pthread_rwlockattr_init);
148 STATIC_LIB_REQUIRE(_pthread_self);
150 STATIC_LIB_REQUIRE(_sem_init);
152 STATIC_LIB_REQUIRE(_pthread_setprio);
153 /* thr_setschedparam.c */
154 STATIC_LIB_REQUIRE(_pthread_setschedparam);
156 STATIC_LIB_REQUIRE(_sigwait);
157 /* thr_single_np.c */
158 STATIC_LIB_REQUIRE(_pthread_single_np);
160 STATIC_LIB_REQUIRE(_pthread_key_create);
162 STATIC_LIB_REQUIRE(_spinlock);
163 /* thr_suspend_np.c */
164 STATIC_LIB_REQUIRE(_pthread_suspend_np);
165 /* thr_switch_np.c */
166 STATIC_LIB_REQUIRE(_pthread_switch_add_np);
168 STATIC_LIB_REQUIRE(_thread_state_running);
170 STATIC_LIB_REQUIRE(__wait4);
172 STATIC_LIB_REQUIRE(_pthread_yield);
175 * Threaded process initialization.
177 * This is only called under two conditions:
179 * 1) Some thread routines have detected that the library hasn't yet
180 * been initialized (_thr_initial == NULL && curthread == NULL), or
182 * 2) An explicit call to reinitialize after a fork (indicated
183 * by curthread != NULL)
186 _libpthread_init(struct pthread *curthread)
189 sigset_t sigset, oldset;
191 /* Check if this function has already been called: */
192 if ((_thr_initial != NULL) && (curthread == NULL))
193 /* Only initialize the threaded application once. */
197 * Check for the special case of this process running as
198 * or in place of init as pid = 1:
200 if ((_thr_pid = getpid()) == 1) {
202 * Setup a new session for this process which is
203 * assumed to be running as root.
206 PANIC("Can't set session ID");
207 if (revoke(_PATH_CONSOLE) != 0)
208 PANIC("Can't revoke console");
209 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
210 PANIC("Can't open console");
211 if (setlogin("root") == -1)
212 PANIC("Can't set login to root");
213 if (__sys_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
214 PANIC("Can't set controlling terminal");
217 /* Initialize pthread private data. */
220 /* Set the initial thread. */
221 if (curthread == NULL) {
223 /* Create and initialize the initial thread. */
224 curthread = _thr_alloc(NULL);
225 if (curthread == NULL)
226 PANIC("Can't allocate initial thread");
227 init_main_thread(curthread);
230 * Add the thread to the thread list queue.
232 THR_LIST_ADD(curthread);
233 _thread_active_threads = 1;
235 /* Setup the thread specific data */
236 tls_set_tcb(curthread->tcb);
240 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
242 _thr_initial = curthread;
243 SIGDELSET(oldset, SIGCANCEL);
244 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
245 if (td_eventismember(&_thread_event_mask, TD_CREATE))
246 _thr_report_creation(curthread, curthread);
251 * This function and pthread_create() do a lot of the same things.
252 * It'd be nice to consolidate the common stuff in one place.
255 init_main_thread(struct pthread *thread)
257 /* Setup the thread attributes. */
258 thread->tid = _thr_get_tid();
259 thread->attr = _pthread_attr_default;
261 * Set up the thread stack.
263 * Create a red zone below the main stack. All other stacks
264 * are constrained to a maximum size by the parameters
265 * passed to mmap(), but this stack is only limited by
266 * resource limits, so this stack needs an explicitly mapped
267 * red zone to protect the thread stack that is just beyond.
269 if (mmap((void *)_usrstack - _thr_stack_initial -
270 _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
271 -1, 0) == MAP_FAILED)
272 PANIC("Cannot allocate red zone for initial thread");
275 * Mark the stack as an application supplied stack so that it
278 * XXX - I'm not sure it would hurt anything to deallocate
279 * the main thread stack because deallocation doesn't
280 * actually free() it; it just puts it in the free
281 * stack queue for later reuse.
283 thread->attr.stackaddr_attr = (void *)_usrstack - _thr_stack_initial;
284 thread->attr.stacksize_attr = _thr_stack_initial;
285 thread->attr.guardsize_attr = _thr_guard_default;
286 thread->attr.flags |= THR_STACK_USER;
289 * Write a magic value to the thread structure
290 * to help identify valid ones:
292 thread->magic = THR_MAGIC;
294 thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
295 thread->name = strdup("initial thread");
297 /* Default the priority of the initial thread: */
298 thread->base_priority = THR_DEFAULT_PRIORITY;
299 thread->active_priority = THR_DEFAULT_PRIORITY;
300 thread->inherited_priority = 0;
302 /* Initialize the mutex queue: */
303 TAILQ_INIT(&thread->mutexq);
304 TAILQ_INIT(&thread->pri_mutexq);
306 thread->state = PS_RUNNING;
307 thread->uniqueid = 0;
309 /* Others cleared to zero by thr_alloc() */
318 _thr_umtx_init(&_mutex_static_lock);
319 _thr_umtx_init(&_cond_static_lock);
320 _thr_umtx_init(&_rwlock_static_lock);
321 _thr_umtx_init(&_keytable_lock);
322 _thr_umtx_init(&_thr_atfork_lock);
323 _thr_umtx_init(&_thr_event_lock);
324 _thr_spinlock_init();
328 * Avoid reinitializing some things if they don't need to be,
329 * e.g. after a fork().
331 if (init_once == 0) {
332 /* Find the stack top */
334 mib[1] = KERN_USRSTACK;
335 len = sizeof (_usrstack);
336 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
337 PANIC("Cannot get kern.usrstack from sysctl");
338 _thr_page_size = getpagesize();
339 _thr_guard_default = _thr_page_size;
340 _pthread_attr_default.guardsize_attr = _thr_guard_default;
342 TAILQ_INIT(&_thr_atfork_list);
343 #ifdef SYSTEM_SCOPE_ONLY
344 _thread_scope_system = 1;
346 if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
347 _thread_scope_system = 1;
348 else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
349 _thread_scope_system = -1;
358 _libpthread_init(NULL);
361 extern int _thread_autoinit_dummy_decl;
362 int _thread_autoinit_dummy_decl = 0;