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
34 #include "namespace.h"
35 #include <sys/param.h>
36 #include <sys/signalvar.h>
37 #include <sys/ioctl.h>
38 #include <sys/sysctl.h>
40 #include <sys/ttycom.h>
49 #include "un-namespace.h"
51 #include "libc_private.h"
52 #include "thr_private.h"
54 /* Default thread attributes: */
55 struct pthread_attr _pthread_attr_default
= {
56 .sched_policy
= SCHED_OTHER
,
58 .prio
= THR_DEFAULT_PRIORITY
,
59 .suspend
= THR_CREATE_RUNNING
,
60 .flags
= PTHREAD_SCOPE_SYSTEM
,
61 .stackaddr_attr
= NULL
,
62 .stacksize_attr
= THR_STACK_DEFAULT
,
66 /* Default mutex attributes: */
67 struct pthread_mutex_attr _pthread_mutexattr_default
= {
68 .m_type
= PTHREAD_MUTEX_DEFAULT
,
69 .m_protocol
= PTHREAD_PRIO_NONE
,
74 /* Default condition variable attributes: */
75 struct pthread_cond_attr _pthread_condattr_default
= {
76 .c_pshared
= PTHREAD_PROCESS_PRIVATE
,
77 .c_clockid
= CLOCK_REALTIME
81 STATIC_LIB_REQUIRE(_pthread_attr_init
);
83 STATIC_LIB_REQUIRE(_pthread_barrier_init
);
84 /* thr_barrierattr.c */
85 STATIC_LIB_REQUIRE(_pthread_barrierattr_init
);
87 STATIC_LIB_REQUIRE(_pthread_cancel
);
89 STATIC_LIB_REQUIRE(_pthread_cleanup_push
);
90 /* thr_concurrency.c */
91 STATIC_LIB_REQUIRE(_pthread_getconcurrency
);
92 STATIC_LIB_REQUIRE(_pthread_setconcurrency
);
94 STATIC_LIB_REQUIRE(_pthread_cond_init
);
96 STATIC_LIB_REQUIRE(_pthread_condattr_init
);
98 STATIC_LIB_REQUIRE(_pthread_create
);
100 STATIC_LIB_REQUIRE(_pthread_detach
);
102 STATIC_LIB_REQUIRE(_pthread_equal
);
104 STATIC_LIB_REQUIRE(_pthread_exit
);
106 STATIC_LIB_REQUIRE(_pthread_atfork
);
107 STATIC_LIB_REQUIRE(_fork
);
109 STATIC_LIB_REQUIRE(_pthread_getaffinity_np
);
111 STATIC_LIB_REQUIRE(_pthread_getprio
);
112 /* thr_getschedparam.c */
113 STATIC_LIB_REQUIRE(_pthread_getschedparam
);
115 STATIC_LIB_REQUIRE(_pthread_set_name_np
);
117 STATIC_LIB_REQUIRE(_pthread_init_early
);
119 STATIC_LIB_REQUIRE(_pthread_join
);
121 STATIC_LIB_REQUIRE(_pthread_kill
);
123 STATIC_LIB_REQUIRE(_pthread_main_np
);
125 STATIC_LIB_REQUIRE(_pthread_multi_np
);
127 STATIC_LIB_REQUIRE(_pthread_mutex_init
);
128 /* thr_mutex_prioceiling.c */
129 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprioceiling
);
130 /* thr_mutex_protocol.c */
131 STATIC_LIB_REQUIRE(_pthread_mutexattr_getprotocol
);
132 /* thr_mutexattr.c */
133 STATIC_LIB_REQUIRE(_pthread_mutexattr_init
);
135 STATIC_LIB_REQUIRE(_pthread_once
);
136 /* thr_pspinlock.c */
137 STATIC_LIB_REQUIRE(_pthread_spin_init
);
138 /* thr_resume_np.c */
139 STATIC_LIB_REQUIRE(_pthread_resume_np
);
141 STATIC_LIB_REQUIRE(_pthread_rwlock_init
);
142 /* thr_rwlockattr.c */
143 STATIC_LIB_REQUIRE(_pthread_rwlockattr_init
);
145 STATIC_LIB_REQUIRE(_pthread_self
);
147 STATIC_LIB_REQUIRE(_sem_init
);
149 STATIC_LIB_REQUIRE(_pthread_setaffinity_np
);
151 STATIC_LIB_REQUIRE(_pthread_setprio
);
152 /* thr_setschedparam.c */
153 STATIC_LIB_REQUIRE(_pthread_setschedparam
);
155 STATIC_LIB_REQUIRE(_sigwait
);
156 /* thr_single_np.c */
157 STATIC_LIB_REQUIRE(_pthread_single_np
);
159 STATIC_LIB_REQUIRE(_pthread_key_create
);
161 STATIC_LIB_REQUIRE(_spinlock
);
162 /* thr_suspend_np.c */
163 STATIC_LIB_REQUIRE(_pthread_suspend_np
);
164 /* thr_switch_np.c */
165 STATIC_LIB_REQUIRE(_pthread_switch_add_np
);
167 STATIC_LIB_REQUIRE(_thread_state_running
);
169 STATIC_LIB_REQUIRE(__wait4
);
171 STATIC_LIB_REQUIRE(_pthread_yield
);
174 struct pthread
*_thr_initial
;
175 static void *_thr_main_redzone
;
178 size_t _thr_guard_default
;
179 size_t _thr_stack_default
= THR_STACK_DEFAULT
;
180 size_t _thr_stack_initial
= THR_STACK_INITIAL
;
183 static void init_private(void);
184 static void _libpthread_uninit(void);
185 static void init_main_thread(struct pthread
*thread
);
186 static int init_once
= 0;
188 void _thread_init(void) __attribute__ ((constructor
));
192 _libpthread_init(NULL
);
195 void _thread_uninit(void) __attribute__ ((destructor
));
199 _libpthread_uninit();
203 * This function is used by libc to initialise libpthread
204 * early during dynamic linking.
206 void _pthread_init_early(void);
208 _pthread_init_early(void)
210 _libpthread_init(NULL
);
214 * Threaded process initialization.
216 * This is only called under two conditions:
218 * 1) Some thread routines have detected that the library hasn't yet
219 * been initialized (_thr_initial == NULL && curthread == NULL), or
221 * 2) An explicit call to reinitialize after a fork (indicated
222 * by curthread != NULL)
225 _libpthread_init(struct pthread
*curthread
)
228 sigset_t sigset
, oldset
;
230 /* Check if this function has already been called: */
231 if ((_thr_initial
!= NULL
) && (curthread
== NULL
))
232 /* Only initialize the threaded application once. */
236 * Check for the special case of this process running as
237 * or in place of init as pid = 1:
239 if ((_thr_pid
= getpid()) == 1) {
241 * Setup a new session for this process which is
242 * assumed to be running as root.
245 PANIC("Can't set session ID");
246 if (revoke(_PATH_CONSOLE
) != 0)
247 PANIC("Can't revoke console");
248 if ((fd
= __sys_open(_PATH_CONSOLE
, O_RDWR
)) < 0)
249 PANIC("Can't open console");
250 if (setlogin("root") == -1)
251 PANIC("Can't set login to root");
252 if (__sys_ioctl(fd
, TIOCSCTTY
, NULL
) == -1)
253 PANIC("Can't set controlling terminal");
256 /* Initialize pthread private data. */
259 /* Set the initial thread. */
260 if (curthread
== NULL
) {
262 /* Create and initialize the initial thread. */
263 curthread
= _thr_alloc(NULL
);
264 if (curthread
== NULL
)
265 PANIC("Can't allocate initial thread");
266 init_main_thread(curthread
);
269 * Add the thread to the thread list queue.
271 THR_LIST_ADD(curthread
);
272 _thread_active_threads
= 1;
274 /* Setup the thread specific data */
275 tls_set_tcb(curthread
->tcb
);
279 __sys_sigprocmask(SIG_SETMASK
, &sigset
, &oldset
);
281 _thr_initial
= curthread
;
282 SIGDELSET(oldset
, SIGCANCEL
);
283 __sys_sigprocmask(SIG_SETMASK
, &oldset
, NULL
);
284 if (td_eventismember(&_thread_event_mask
, TD_CREATE
))
285 _thr_report_creation(curthread
, curthread
);
290 _libpthread_uninit(void)
292 if (_thr_initial
== NULL
)
294 if (_thr_main_redzone
&& _thr_main_redzone
!= MAP_FAILED
) {
295 munmap(_thr_main_redzone
, _thr_guard_default
);
296 _thr_main_redzone
= NULL
;
298 _thr_stack_cleanup();
302 * This function and pthread_create() do a lot of the same things.
303 * It'd be nice to consolidate the common stuff in one place.
306 init_main_thread(struct pthread
*thread
)
308 /* Setup the thread attributes. */
309 thread
->tid
= _thr_get_tid();
310 thread
->attr
= _pthread_attr_default
;
312 * Set up the thread stack.
314 * Create a red zone below the main stack. All other stacks
315 * are constrained to a maximum size by the parameters
316 * passed to mmap(), but this stack is only limited by
317 * resource limits, so this stack needs an explicitly mapped
318 * red zone to protect the thread stack that is just beyond.
320 _thr_main_redzone
= mmap(_usrstack
- _thr_stack_initial
-
321 _thr_guard_default
, _thr_guard_default
,
322 0, MAP_ANON
| MAP_TRYFIXED
, -1, 0);
323 if (_thr_main_redzone
== MAP_FAILED
) {
324 PANIC("Cannot allocate red zone for initial thread");
328 * Mark the stack as an application supplied stack so that it
331 * XXX - I'm not sure it would hurt anything to deallocate
332 * the main thread stack because deallocation doesn't
333 * actually free() it; it just puts it in the free
334 * stack queue for later reuse.
336 thread
->attr
.stackaddr_attr
= _usrstack
- _thr_stack_initial
;
337 thread
->attr
.stacksize_attr
= _thr_stack_initial
;
338 thread
->attr
.guardsize_attr
= _thr_guard_default
;
339 thread
->attr
.flags
|= THR_STACK_USER
;
342 * Write a magic value to the thread structure
343 * to help identify valid ones:
345 thread
->magic
= THR_MAGIC
;
347 thread
->cancelflags
= PTHREAD_CANCEL_ENABLE
| PTHREAD_CANCEL_DEFERRED
;
348 thread
->name
= strdup("initial thread");
350 /* Default the priority of the initial thread: */
351 thread
->base_priority
= THR_DEFAULT_PRIORITY
;
352 thread
->active_priority
= THR_DEFAULT_PRIORITY
;
353 thread
->inherited_priority
= 0;
355 /* Initialize the mutex queue: */
356 TAILQ_INIT(&thread
->mutexq
);
358 thread
->state
= PS_RUNNING
;
359 thread
->uniqueid
= 0;
361 /* Others cleared to zero by thr_alloc() */
370 _thr_umtx_init(&_mutex_static_lock
);
371 _thr_umtx_init(&_cond_static_lock
);
372 _thr_umtx_init(&_rwlock_static_lock
);
373 _thr_umtx_init(&_keytable_lock
);
374 _thr_umtx_init(&_thr_atfork_lock
);
375 _thr_umtx_init(&_thr_event_lock
);
376 _thr_spinlock_init();
380 * Avoid reinitializing some things if they don't need to be,
381 * e.g. after a fork().
383 if (init_once
== 0) {
384 /* Find the stack top */
386 mib
[1] = KERN_USRSTACK
;
387 len
= sizeof (_usrstack
);
388 if (sysctl(mib
, 2, &_usrstack
, &len
, NULL
, 0) == -1)
389 PANIC("Cannot get kern.usrstack from sysctl");
390 _thr_page_size
= getpagesize();
391 _thr_guard_default
= _thr_page_size
;
392 _pthread_attr_default
.guardsize_attr
= _thr_guard_default
;
393 TAILQ_INIT(&_thr_atfork_list
);
398 __strong_reference(_pthread_init_early
, pthread_init_early
);