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 * $DragonFly: src/lib/libthread_xu/thread/thr_init.c,v 1.8 2006/04/06 13:03:09 davidxu Exp $
36 #include "namespace.h"
37 #include <sys/param.h>
38 #include <sys/signalvar.h>
40 #include <sys/ioctl.h>
41 #include <sys/sysctl.h>
43 #include <sys/ttycom.h>
53 #include "un-namespace.h"
55 #include "libc_private.h"
56 #include "thr_private.h"
59 STATIC_LIB_REQUIRE(_pthread_attr_init);
61 STATIC_LIB_REQUIRE(_pthread_barrier_init);
62 /* thr_barrierattr.c */
63 STATIC_LIB_REQUIRE(_pthread_barrierattr_init);
65 STATIC_LIB_REQUIRE(_pthread_cancel);
67 STATIC_LIB_REQUIRE(_pthread_cleanup_push);
68 /* thr_concurrency.c */
69 STATIC_LIB_REQUIRE(_pthread_get_concurrency);
71 STATIC_LIB_REQUIRE(_pthread_cond_init);
73 STATIC_LIB_REQUIRE(_pthread_condattr_init);
75 STATIC_LIB_REQUIRE(_pthread_create);
77 STATIC_LIB_REQUIRE(_pthread_detach);
79 STATIC_LIB_REQUIRE(_pthread_equal);
81 STATIC_LIB_REQUIRE(_pthread_exit);
83 STATIC_LIB_REQUIRE(_pthread_atfork);
84 STATIC_LIB_REQUIRE(_fork);
86 STATIC_LIB_REQUIRE(_pthread_getprio);
87 /* thr_getschedparam.c */
88 STATIC_LIB_REQUIRE(_pthread_getschedparam);
90 STATIC_LIB_REQUIRE(_pthread_set_name_np);
92 STATIC_LIB_REQUIRE(_pthread_join);
94 STATIC_LIB_REQUIRE(_pthread_kill);
96 STATIC_LIB_REQUIRE(_pthread_main_np);
98 STATIC_LIB_REQUIRE(_pthread_multi_np);
100 STATIC_LIB_REQUIRE(_pthread_mutex_init);
101 /* thr_mutex_prioceiling.c */
102 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprioceiling);
103 /* thr_mutex_protocol.c */
104 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprotocol);
105 /* thr_mutexattr.c */
106 STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
108 STATIC_LIB_REQUIRE(_pthread_once);
109 /* thr_pspinlock.c */
110 STATIC_LIB_REQUIRE(_pthread_spin_init);
111 /* thr_resume_np.c */
112 STATIC_LIB_REQUIRE(_pthread_resume_np);
114 STATIC_LIB_REQUIRE(_pthread_rwlock_init);
115 /* thr_rwlockattr.c */
116 STATIC_LIB_REQUIRE(_pthread_rwlockattr_init);
118 STATIC_LIB_REQUIRE(_pthread_self);
120 STATIC_LIB_REQUIRE(_sem_init);
122 STATIC_LIB_REQUIRE(_pthread_setprio);
123 /* thr_setschedparam.c */
124 STATIC_LIB_REQUIRE(_pthread_setschedparam);
126 STATIC_LIB_REQUIRE(_sigwait);
127 /* thr_single_np.c */
128 STATIC_LIB_REQUIRE(_pthread_single_np);
130 STATIC_LIB_REQUIRE(_pthread_key_create);
132 STATIC_LIB_REQUIRE(_spinlock);
133 /* thr_suspend_np.c */
134 STATIC_LIB_REQUIRE(_pthread_suspend_np);
135 /* thr_switch_np.c */
136 STATIC_LIB_REQUIRE(_pthread_switch_add_np);
138 STATIC_LIB_REQUIRE(_thread_state_running);
140 STATIC_LIB_REQUIRE(__wait4);
142 STATIC_LIB_REQUIRE(_pthread_yield);
145 struct pthread *_thr_initial;
146 int _thread_scope_system;
149 size_t _thr_guard_default;
150 size_t _thr_stack_default = THR_STACK_DEFAULT;
151 size_t _thr_stack_initial = THR_STACK_INITIAL;
154 static void init_private(void);
155 static void init_main_thread(struct pthread *thread);
156 static int init_once = 0;
158 void _thread_init(void) __attribute__ ((constructor));
162 _libpthread_init(NULL);
166 * Threaded process initialization.
168 * This is only called under two conditions:
170 * 1) Some thread routines have detected that the library hasn't yet
171 * been initialized (_thr_initial == NULL && curthread == NULL), or
173 * 2) An explicit call to reinitialize after a fork (indicated
174 * by curthread != NULL)
177 _libpthread_init(struct pthread *curthread)
180 sigset_t sigset, oldset;
182 /* Check if this function has already been called: */
183 if ((_thr_initial != NULL) && (curthread == NULL))
184 /* Only initialize the threaded application once. */
188 * Check for the special case of this process running as
189 * or in place of init as pid = 1:
191 if ((_thr_pid = getpid()) == 1) {
193 * Setup a new session for this process which is
194 * assumed to be running as root.
197 PANIC("Can't set session ID");
198 if (revoke(_PATH_CONSOLE) != 0)
199 PANIC("Can't revoke console");
200 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
201 PANIC("Can't open console");
202 if (setlogin("root") == -1)
203 PANIC("Can't set login to root");
204 if (__sys_ioctl(fd, TIOCSCTTY, NULL) == -1)
205 PANIC("Can't set controlling terminal");
208 /* Initialize pthread private data. */
211 /* Set the initial thread. */
212 if (curthread == NULL) {
214 /* Create and initialize the initial thread. */
215 curthread = _thr_alloc(NULL);
216 if (curthread == NULL)
217 PANIC("Can't allocate initial thread");
218 init_main_thread(curthread);
221 * Add the thread to the thread list queue.
223 THR_LIST_ADD(curthread);
224 _thread_active_threads = 1;
226 /* Setup the thread specific data */
227 tls_set_tcb(curthread->tcb);
231 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
233 _thr_initial = curthread;
234 SIGDELSET(oldset, SIGCANCEL);
235 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
236 if (td_eventismember(&_thread_event_mask, TD_CREATE))
237 _thr_report_creation(curthread, curthread);
242 * This function and pthread_create() do a lot of the same things.
243 * It'd be nice to consolidate the common stuff in one place.
246 init_main_thread(struct pthread *thread)
248 /* Setup the thread attributes. */
249 thread->tid = _thr_get_tid();
250 thread->attr = _pthread_attr_default;
252 * Set up the thread stack.
254 * Create a red zone below the main stack. All other stacks
255 * are constrained to a maximum size by the parameters
256 * passed to mmap(), but this stack is only limited by
257 * resource limits, so this stack needs an explicitly mapped
258 * red zone to protect the thread stack that is just beyond.
260 if (mmap(_usrstack - _thr_stack_initial -
261 _thr_guard_default, _thr_guard_default,
262 0, MAP_ANON | MAP_TRYFIXED, -1, 0) == MAP_FAILED) {
263 PANIC("Cannot allocate red zone for initial thread");
267 * Mark the stack as an application supplied stack so that it
270 * XXX - I'm not sure it would hurt anything to deallocate
271 * the main thread stack because deallocation doesn't
272 * actually free() it; it just puts it in the free
273 * stack queue for later reuse.
275 thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
276 thread->attr.stacksize_attr = _thr_stack_initial;
277 thread->attr.guardsize_attr = _thr_guard_default;
278 thread->attr.flags |= THR_STACK_USER;
281 * Write a magic value to the thread structure
282 * to help identify valid ones:
284 thread->magic = THR_MAGIC;
286 thread->cancelflags = PTHREAD_CANCEL_ENABLE | PTHREAD_CANCEL_DEFERRED;
287 thread->name = strdup("initial thread");
289 /* Default the priority of the initial thread: */
290 thread->base_priority = THR_DEFAULT_PRIORITY;
291 thread->active_priority = THR_DEFAULT_PRIORITY;
292 thread->inherited_priority = 0;
294 /* Initialize the mutex queue: */
295 TAILQ_INIT(&thread->mutexq);
297 thread->state = PS_RUNNING;
298 thread->uniqueid = 0;
300 /* Others cleared to zero by thr_alloc() */
309 _thr_umtx_init(&_mutex_static_lock);
310 _thr_umtx_init(&_cond_static_lock);
311 _thr_umtx_init(&_rwlock_static_lock);
312 _thr_umtx_init(&_keytable_lock);
313 _thr_umtx_init(&_thr_atfork_lock);
314 _thr_umtx_init(&_thr_event_lock);
315 _thr_spinlock_init();
319 * Avoid reinitializing some things if they don't need to be,
320 * e.g. after a fork().
322 if (init_once == 0) {
323 /* Find the stack top */
325 mib[1] = KERN_USRSTACK;
326 len = sizeof (_usrstack);
327 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
328 PANIC("Cannot get kern.usrstack from sysctl");
329 _thr_page_size = getpagesize();
330 _thr_guard_default = _thr_page_size;
331 _pthread_attr_default.guardsize_attr = _thr_guard_default;
333 TAILQ_INIT(&_thr_atfork_list);
334 #ifdef SYSTEM_SCOPE_ONLY
335 _thread_scope_system = 1;
337 if (getenv("LIBPTHREAD_SYSTEM_SCOPE") != NULL)
338 _thread_scope_system = 1;
339 else if (getenv("LIBPTHREAD_PROCESS_SCOPE") != NULL)
340 _thread_scope_system = -1;