Deleted Added
full compact
thr_init.c (213153) thr_init.c (213241)
1/*
2 * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
3 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
1/*
2 * Copyright (c) 2003 Daniel M. Eischen <deischen@freebsd.org>
3 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
20 *
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
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/lib/libthr/thread/thr_init.c 213153 2010-09-25 01:57:47Z davidxu $
33 * $FreeBSD: head/lib/libthr/thread/thr_init.c 213241 2010-09-28 04:57:56Z davidxu $
34 */
35
36#include "namespace.h"
37#include <sys/types.h>
38#include <sys/signalvar.h>
39#include <sys/ioctl.h>
40#include <sys/sysctl.h>
41#include <sys/ttycom.h>
42#include <sys/mman.h>
43#include <sys/rtprio.h>
44#include <errno.h>
45#include <fcntl.h>
46#include <paths.h>
47#include <pthread.h>
48#include <pthread_np.h>
49#include <signal.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include <unistd.h>
54#include "un-namespace.h"
55
56#include "libc_private.h"
57#include "thr_private.h"
58
59char *_usrstack;
60struct pthread *_thr_initial;
61int _libthr_debug;
62int _thread_event_mask;
63struct pthread *_thread_last_event;
64pthreadlist _thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
65pthreadlist _thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
66int _thread_active_threads = 1;
67atfork_head _thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
68struct urwlock _thr_atfork_lock = DEFAULT_URWLOCK;
69
70struct pthread_prio _thr_priorities[3] = {
71 {RTP_PRIO_MIN, RTP_PRIO_MAX, 0}, /* FIFO */
72 {0, 0, 63}, /* OTHER */
73 {RTP_PRIO_MIN, RTP_PRIO_MAX, 0} /* RR */
74};
75
76struct pthread_attr _pthread_attr_default = {
77 .sched_policy = SCHED_OTHER,
78 .sched_inherit = PTHREAD_INHERIT_SCHED,
79 .prio = 0,
80 .suspend = THR_CREATE_RUNNING,
81 .flags = PTHREAD_SCOPE_SYSTEM,
82 .stackaddr_attr = NULL,
83 .stacksize_attr = THR_STACK_DEFAULT,
84 .guardsize_attr = 0,
85 .cpusetsize = 0,
86 .cpuset = NULL
87};
88
89struct pthread_mutex_attr _pthread_mutexattr_default = {
90 .m_type = PTHREAD_MUTEX_DEFAULT,
91 .m_protocol = PTHREAD_PRIO_NONE,
92 .m_ceiling = 0
93};
94
34 */
35
36#include "namespace.h"
37#include <sys/types.h>
38#include <sys/signalvar.h>
39#include <sys/ioctl.h>
40#include <sys/sysctl.h>
41#include <sys/ttycom.h>
42#include <sys/mman.h>
43#include <sys/rtprio.h>
44#include <errno.h>
45#include <fcntl.h>
46#include <paths.h>
47#include <pthread.h>
48#include <pthread_np.h>
49#include <signal.h>
50#include <stdlib.h>
51#include <string.h>
52#include <time.h>
53#include <unistd.h>
54#include "un-namespace.h"
55
56#include "libc_private.h"
57#include "thr_private.h"
58
59char *_usrstack;
60struct pthread *_thr_initial;
61int _libthr_debug;
62int _thread_event_mask;
63struct pthread *_thread_last_event;
64pthreadlist _thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
65pthreadlist _thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
66int _thread_active_threads = 1;
67atfork_head _thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
68struct urwlock _thr_atfork_lock = DEFAULT_URWLOCK;
69
70struct pthread_prio _thr_priorities[3] = {
71 {RTP_PRIO_MIN, RTP_PRIO_MAX, 0}, /* FIFO */
72 {0, 0, 63}, /* OTHER */
73 {RTP_PRIO_MIN, RTP_PRIO_MAX, 0} /* RR */
74};
75
76struct pthread_attr _pthread_attr_default = {
77 .sched_policy = SCHED_OTHER,
78 .sched_inherit = PTHREAD_INHERIT_SCHED,
79 .prio = 0,
80 .suspend = THR_CREATE_RUNNING,
81 .flags = PTHREAD_SCOPE_SYSTEM,
82 .stackaddr_attr = NULL,
83 .stacksize_attr = THR_STACK_DEFAULT,
84 .guardsize_attr = 0,
85 .cpusetsize = 0,
86 .cpuset = NULL
87};
88
89struct pthread_mutex_attr _pthread_mutexattr_default = {
90 .m_type = PTHREAD_MUTEX_DEFAULT,
91 .m_protocol = PTHREAD_PRIO_NONE,
92 .m_ceiling = 0
93};
94
95struct pthread_mutex_attr _pthread_mutexattr_adaptive_default = {
96 .m_type = PTHREAD_MUTEX_ADAPTIVE_NP,
97 .m_protocol = PTHREAD_PRIO_NONE,
98 .m_ceiling = 0
99};
100
95/* Default condition variable attributes: */
96struct pthread_cond_attr _pthread_condattr_default = {
97 .c_pshared = PTHREAD_PROCESS_PRIVATE,
98 .c_clockid = CLOCK_REALTIME
99};
100
101pid_t _thr_pid;
102int _thr_is_smp = 0;
103size_t _thr_guard_default;
104size_t _thr_stack_default = THR_STACK_DEFAULT;
105size_t _thr_stack_initial = THR_STACK_INITIAL;
106int _thr_page_size;
107int _thr_spinloops;
108int _thr_yieldloops;
109int _gc_count;
110struct umutex _mutex_static_lock = DEFAULT_UMUTEX;
111struct umutex _cond_static_lock = DEFAULT_UMUTEX;
112struct umutex _rwlock_static_lock = DEFAULT_UMUTEX;
113struct umutex _keytable_lock = DEFAULT_UMUTEX;
114struct urwlock _thr_list_lock = DEFAULT_URWLOCK;
115struct umutex _thr_event_lock = DEFAULT_UMUTEX;
116
117int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
118int __pthread_mutex_lock(pthread_mutex_t *);
119int __pthread_mutex_trylock(pthread_mutex_t *);
120void _thread_init_hack(void) __attribute__ ((constructor));
121
122static void init_private(void);
123static void init_main_thread(struct pthread *thread);
124
125/*
126 * All weak references used within libc should be in this table.
127 * This is so that static libraries will work.
128 */
129
130STATIC_LIB_REQUIRE(_fork);
131STATIC_LIB_REQUIRE(_pthread_getspecific);
132STATIC_LIB_REQUIRE(_pthread_key_create);
133STATIC_LIB_REQUIRE(_pthread_key_delete);
134STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
135STATIC_LIB_REQUIRE(_pthread_mutex_init);
136STATIC_LIB_REQUIRE(_pthread_mutex_lock);
137STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
138STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
139STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
140STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
141STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
142STATIC_LIB_REQUIRE(_pthread_once);
143STATIC_LIB_REQUIRE(_pthread_setspecific);
144STATIC_LIB_REQUIRE(_raise);
145STATIC_LIB_REQUIRE(_sem_destroy);
146STATIC_LIB_REQUIRE(_sem_getvalue);
147STATIC_LIB_REQUIRE(_sem_init);
148STATIC_LIB_REQUIRE(_sem_post);
149STATIC_LIB_REQUIRE(_sem_timedwait);
150STATIC_LIB_REQUIRE(_sem_trywait);
151STATIC_LIB_REQUIRE(_sem_wait);
152STATIC_LIB_REQUIRE(_sigaction);
153STATIC_LIB_REQUIRE(_sigprocmask);
154STATIC_LIB_REQUIRE(_sigsuspend);
155STATIC_LIB_REQUIRE(_sigtimedwait);
156STATIC_LIB_REQUIRE(_sigwait);
157STATIC_LIB_REQUIRE(_sigwaitinfo);
158STATIC_LIB_REQUIRE(_spinlock);
159STATIC_LIB_REQUIRE(_spinlock_debug);
160STATIC_LIB_REQUIRE(_spinunlock);
161STATIC_LIB_REQUIRE(_thread_init_hack);
162
163/*
164 * These are needed when linking statically. All references within
165 * libgcc (and in the future libc) to these routines are weak, but
166 * if they are not (strongly) referenced by the application or other
167 * libraries, then the actual functions will not be loaded.
168 */
169STATIC_LIB_REQUIRE(_pthread_once);
170STATIC_LIB_REQUIRE(_pthread_key_create);
171STATIC_LIB_REQUIRE(_pthread_key_delete);
172STATIC_LIB_REQUIRE(_pthread_getspecific);
173STATIC_LIB_REQUIRE(_pthread_setspecific);
174STATIC_LIB_REQUIRE(_pthread_mutex_init);
175STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
176STATIC_LIB_REQUIRE(_pthread_mutex_lock);
177STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
178STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
179STATIC_LIB_REQUIRE(_pthread_create);
180
181/* Pull in all symbols required by libthread_db */
182STATIC_LIB_REQUIRE(_thread_state_running);
183
184#define DUAL_ENTRY(entry) \
185 (pthread_func_t)entry, (pthread_func_t)entry
186
187static pthread_func_t jmp_table[][2] = {
188 {DUAL_ENTRY(_pthread_atfork)}, /* PJT_ATFORK */
189 {DUAL_ENTRY(_pthread_attr_destroy)}, /* PJT_ATTR_DESTROY */
190 {DUAL_ENTRY(_pthread_attr_getdetachstate)}, /* PJT_ATTR_GETDETACHSTATE */
191 {DUAL_ENTRY(_pthread_attr_getguardsize)}, /* PJT_ATTR_GETGUARDSIZE */
192 {DUAL_ENTRY(_pthread_attr_getinheritsched)}, /* PJT_ATTR_GETINHERITSCHED */
193 {DUAL_ENTRY(_pthread_attr_getschedparam)}, /* PJT_ATTR_GETSCHEDPARAM */
194 {DUAL_ENTRY(_pthread_attr_getschedpolicy)}, /* PJT_ATTR_GETSCHEDPOLICY */
195 {DUAL_ENTRY(_pthread_attr_getscope)}, /* PJT_ATTR_GETSCOPE */
196 {DUAL_ENTRY(_pthread_attr_getstackaddr)}, /* PJT_ATTR_GETSTACKADDR */
197 {DUAL_ENTRY(_pthread_attr_getstacksize)}, /* PJT_ATTR_GETSTACKSIZE */
198 {DUAL_ENTRY(_pthread_attr_init)}, /* PJT_ATTR_INIT */
199 {DUAL_ENTRY(_pthread_attr_setdetachstate)}, /* PJT_ATTR_SETDETACHSTATE */
200 {DUAL_ENTRY(_pthread_attr_setguardsize)}, /* PJT_ATTR_SETGUARDSIZE */
201 {DUAL_ENTRY(_pthread_attr_setinheritsched)}, /* PJT_ATTR_SETINHERITSCHED */
202 {DUAL_ENTRY(_pthread_attr_setschedparam)}, /* PJT_ATTR_SETSCHEDPARAM */
203 {DUAL_ENTRY(_pthread_attr_setschedpolicy)}, /* PJT_ATTR_SETSCHEDPOLICY */
204 {DUAL_ENTRY(_pthread_attr_setscope)}, /* PJT_ATTR_SETSCOPE */
205 {DUAL_ENTRY(_pthread_attr_setstackaddr)}, /* PJT_ATTR_SETSTACKADDR */
206 {DUAL_ENTRY(_pthread_attr_setstacksize)}, /* PJT_ATTR_SETSTACKSIZE */
207 {DUAL_ENTRY(_pthread_cancel)}, /* PJT_CANCEL */
208 {DUAL_ENTRY(_pthread_cleanup_pop)}, /* PJT_CLEANUP_POP */
209 {DUAL_ENTRY(_pthread_cleanup_push)}, /* PJT_CLEANUP_PUSH */
210 {DUAL_ENTRY(_pthread_cond_broadcast)}, /* PJT_COND_BROADCAST */
211 {DUAL_ENTRY(_pthread_cond_destroy)}, /* PJT_COND_DESTROY */
212 {DUAL_ENTRY(_pthread_cond_init)}, /* PJT_COND_INIT */
213 {DUAL_ENTRY(_pthread_cond_signal)}, /* PJT_COND_SIGNAL */
214 {DUAL_ENTRY(_pthread_cond_timedwait)}, /* PJT_COND_TIMEDWAIT */
215 {(pthread_func_t)__pthread_cond_wait,
216 (pthread_func_t)_pthread_cond_wait}, /* PJT_COND_WAIT */
217 {DUAL_ENTRY(_pthread_detach)}, /* PJT_DETACH */
218 {DUAL_ENTRY(_pthread_equal)}, /* PJT_EQUAL */
219 {DUAL_ENTRY(_pthread_exit)}, /* PJT_EXIT */
220 {DUAL_ENTRY(_pthread_getspecific)}, /* PJT_GETSPECIFIC */
221 {DUAL_ENTRY(_pthread_join)}, /* PJT_JOIN */
222 {DUAL_ENTRY(_pthread_key_create)}, /* PJT_KEY_CREATE */
223 {DUAL_ENTRY(_pthread_key_delete)}, /* PJT_KEY_DELETE*/
224 {DUAL_ENTRY(_pthread_kill)}, /* PJT_KILL */
225 {DUAL_ENTRY(_pthread_main_np)}, /* PJT_MAIN_NP */
226 {DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
227 {DUAL_ENTRY(_pthread_mutexattr_init)}, /* PJT_MUTEXATTR_INIT */
228 {DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
229 {DUAL_ENTRY(_pthread_mutex_destroy)}, /* PJT_MUTEX_DESTROY */
230 {DUAL_ENTRY(_pthread_mutex_init)}, /* PJT_MUTEX_INIT */
231 {(pthread_func_t)__pthread_mutex_lock,
232 (pthread_func_t)_pthread_mutex_lock}, /* PJT_MUTEX_LOCK */
233 {(pthread_func_t)__pthread_mutex_trylock,
234 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
235 {DUAL_ENTRY(_pthread_mutex_unlock)}, /* PJT_MUTEX_UNLOCK */
236 {DUAL_ENTRY(_pthread_once)}, /* PJT_ONCE */
237 {DUAL_ENTRY(_pthread_rwlock_destroy)}, /* PJT_RWLOCK_DESTROY */
238 {DUAL_ENTRY(_pthread_rwlock_init)}, /* PJT_RWLOCK_INIT */
239 {DUAL_ENTRY(_pthread_rwlock_rdlock)}, /* PJT_RWLOCK_RDLOCK */
240 {DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
241 {DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
242 {DUAL_ENTRY(_pthread_rwlock_unlock)}, /* PJT_RWLOCK_UNLOCK */
243 {DUAL_ENTRY(_pthread_rwlock_wrlock)}, /* PJT_RWLOCK_WRLOCK */
244 {DUAL_ENTRY(_pthread_self)}, /* PJT_SELF */
245 {DUAL_ENTRY(_pthread_setcancelstate)}, /* PJT_SETCANCELSTATE */
246 {DUAL_ENTRY(_pthread_setcanceltype)}, /* PJT_SETCANCELTYPE */
247 {DUAL_ENTRY(_pthread_setspecific)}, /* PJT_SETSPECIFIC */
248 {DUAL_ENTRY(_pthread_sigmask)}, /* PJT_SIGMASK */
249 {DUAL_ENTRY(_pthread_testcancel)}, /* PJT_TESTCANCEL */
250 {DUAL_ENTRY(__pthread_cleanup_pop_imp)},/* PJT_CLEANUP_POP_IMP */
251 {DUAL_ENTRY(__pthread_cleanup_push_imp)},/* PJT_CLEANUP_PUSH_IMP */
252 {DUAL_ENTRY(_pthread_cancel_enter)}, /* PJT_CANCEL_ENTER */
253 {DUAL_ENTRY(_pthread_cancel_leave)} /* PJT_CANCEL_LEAVE */
254};
255
256static int init_once = 0;
257
258/*
259 * For the shared version of the threads library, the above is sufficient.
260 * But for the archive version of the library, we need a little bit more.
261 * Namely, we must arrange for this particular module to be pulled in from
262 * the archive library at link time. To accomplish that, we define and
263 * initialize a variable, "_thread_autoinit_dummy_decl". This variable is
264 * referenced (as an extern) from libc/stdlib/exit.c. This will always
265 * create a need for this module, ensuring that it is present in the
266 * executable.
267 */
268extern int _thread_autoinit_dummy_decl;
269int _thread_autoinit_dummy_decl = 0;
270
271void
272_thread_init_hack(void)
273{
274
275 _libpthread_init(NULL);
276}
277
278
279/*
280 * Threaded process initialization.
281 *
282 * This is only called under two conditions:
283 *
284 * 1) Some thread routines have detected that the library hasn't yet
285 * been initialized (_thr_initial == NULL && curthread == NULL), or
286 *
287 * 2) An explicit call to reinitialize after a fork (indicated
288 * by curthread != NULL)
289 */
290void
291_libpthread_init(struct pthread *curthread)
292{
293 int fd, first = 0;
294
295 /* Check if this function has already been called: */
296 if ((_thr_initial != NULL) && (curthread == NULL))
297 /* Only initialize the threaded application once. */
298 return;
299
300 /*
301 * Check the size of the jump table to make sure it is preset
302 * with the correct number of entries.
303 */
304 if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
305 PANIC("Thread jump table not properly initialized");
306 memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
307
308 /*
309 * Check for the special case of this process running as
310 * or in place of init as pid = 1:
311 */
312 if ((_thr_pid = getpid()) == 1) {
313 /*
314 * Setup a new session for this process which is
315 * assumed to be running as root.
316 */
317 if (setsid() == -1)
318 PANIC("Can't set session ID");
319 if (revoke(_PATH_CONSOLE) != 0)
320 PANIC("Can't revoke console");
321 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
322 PANIC("Can't open console");
323 if (setlogin("root") == -1)
324 PANIC("Can't set login to root");
325 if (_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
326 PANIC("Can't set controlling terminal");
327 }
328
329 /* Initialize pthread private data. */
330 init_private();
331
332 /* Set the initial thread. */
333 if (curthread == NULL) {
334 first = 1;
335 /* Create and initialize the initial thread. */
336 curthread = _thr_alloc(NULL);
337 if (curthread == NULL)
338 PANIC("Can't allocate initial thread");
339 init_main_thread(curthread);
340 }
341 /*
342 * Add the thread to the thread list queue.
343 */
344 THR_LIST_ADD(curthread);
345 _thread_active_threads = 1;
346
347 /* Setup the thread specific data */
348 _tcb_set(curthread->tcb);
349
350 if (first) {
351 _thr_initial = curthread;
352 _thr_signal_init();
353 if (_thread_event_mask & TD_CREATE)
354 _thr_report_creation(curthread, curthread);
355 }
356}
357
358/*
359 * This function and pthread_create() do a lot of the same things.
360 * It'd be nice to consolidate the common stuff in one place.
361 */
362static void
363init_main_thread(struct pthread *thread)
364{
365 struct sched_param sched_param;
366
367 /* Setup the thread attributes. */
368 thr_self(&thread->tid);
369 thread->attr = _pthread_attr_default;
370 /*
371 * Set up the thread stack.
372 *
373 * Create a red zone below the main stack. All other stacks
374 * are constrained to a maximum size by the parameters
375 * passed to mmap(), but this stack is only limited by
376 * resource limits, so this stack needs an explicitly mapped
377 * red zone to protect the thread stack that is just beyond.
378 */
379 if (mmap(_usrstack - _thr_stack_initial -
380 _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
381 -1, 0) == MAP_FAILED)
382 PANIC("Cannot allocate red zone for initial thread");
383
384 /*
385 * Mark the stack as an application supplied stack so that it
386 * isn't deallocated.
387 *
388 * XXX - I'm not sure it would hurt anything to deallocate
389 * the main thread stack because deallocation doesn't
390 * actually free() it; it just puts it in the free
391 * stack queue for later reuse.
392 */
393 thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
394 thread->attr.stacksize_attr = _thr_stack_initial;
395 thread->attr.guardsize_attr = _thr_guard_default;
396 thread->attr.flags |= THR_STACK_USER;
397
398 /*
399 * Write a magic value to the thread structure
400 * to help identify valid ones:
401 */
402 thread->magic = THR_MAGIC;
403
404 thread->cancel_enable = 1;
405 thread->cancel_async = 0;
406 thr_set_name(thread->tid, "initial thread");
407
408 /* Initialize the mutex queue: */
409 TAILQ_INIT(&thread->mutexq);
410 TAILQ_INIT(&thread->pp_mutexq);
411
412 thread->state = PS_RUNNING;
413
414 _thr_getscheduler(thread->tid, &thread->attr.sched_policy,
415 &sched_param);
416 thread->attr.prio = sched_param.sched_priority;
417
418#ifdef _PTHREAD_FORCED_UNWIND
419 thread->unwind_stackend = _usrstack;
420#endif
421
422 /* Others cleared to zero by thr_alloc() */
423}
424
425static void
426init_private(void)
427{
428 size_t len;
429 int mib[2];
430 char *env;
431
432 _thr_umutex_init(&_mutex_static_lock);
433 _thr_umutex_init(&_cond_static_lock);
434 _thr_umutex_init(&_rwlock_static_lock);
435 _thr_umutex_init(&_keytable_lock);
436 _thr_urwlock_init(&_thr_atfork_lock);
437 _thr_umutex_init(&_thr_event_lock);
438 _thr_once_init();
439 _thr_spinlock_init();
440 _thr_list_init();
441
442 /*
443 * Avoid reinitializing some things if they don't need to be,
444 * e.g. after a fork().
445 */
446 if (init_once == 0) {
447 /* Find the stack top */
448 mib[0] = CTL_KERN;
449 mib[1] = KERN_USRSTACK;
450 len = sizeof (_usrstack);
451 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
452 PANIC("Cannot get kern.usrstack from sysctl");
453 len = sizeof(_thr_is_smp);
454 sysctlbyname("kern.smp.cpus", &_thr_is_smp, &len, NULL, 0);
455 _thr_is_smp = (_thr_is_smp > 1);
456 _thr_page_size = getpagesize();
457 _thr_guard_default = _thr_page_size;
458 _pthread_attr_default.guardsize_attr = _thr_guard_default;
459 _pthread_attr_default.stacksize_attr = _thr_stack_default;
460 env = getenv("LIBPTHREAD_SPINLOOPS");
461 if (env)
462 _thr_spinloops = atoi(env);
463 env = getenv("LIBPTHREAD_YIELDLOOPS");
464 if (env)
465 _thr_yieldloops = atoi(env);
466 TAILQ_INIT(&_thr_atfork_list);
467 }
468 init_once = 1;
469}
101/* Default condition variable attributes: */
102struct pthread_cond_attr _pthread_condattr_default = {
103 .c_pshared = PTHREAD_PROCESS_PRIVATE,
104 .c_clockid = CLOCK_REALTIME
105};
106
107pid_t _thr_pid;
108int _thr_is_smp = 0;
109size_t _thr_guard_default;
110size_t _thr_stack_default = THR_STACK_DEFAULT;
111size_t _thr_stack_initial = THR_STACK_INITIAL;
112int _thr_page_size;
113int _thr_spinloops;
114int _thr_yieldloops;
115int _gc_count;
116struct umutex _mutex_static_lock = DEFAULT_UMUTEX;
117struct umutex _cond_static_lock = DEFAULT_UMUTEX;
118struct umutex _rwlock_static_lock = DEFAULT_UMUTEX;
119struct umutex _keytable_lock = DEFAULT_UMUTEX;
120struct urwlock _thr_list_lock = DEFAULT_URWLOCK;
121struct umutex _thr_event_lock = DEFAULT_UMUTEX;
122
123int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
124int __pthread_mutex_lock(pthread_mutex_t *);
125int __pthread_mutex_trylock(pthread_mutex_t *);
126void _thread_init_hack(void) __attribute__ ((constructor));
127
128static void init_private(void);
129static void init_main_thread(struct pthread *thread);
130
131/*
132 * All weak references used within libc should be in this table.
133 * This is so that static libraries will work.
134 */
135
136STATIC_LIB_REQUIRE(_fork);
137STATIC_LIB_REQUIRE(_pthread_getspecific);
138STATIC_LIB_REQUIRE(_pthread_key_create);
139STATIC_LIB_REQUIRE(_pthread_key_delete);
140STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
141STATIC_LIB_REQUIRE(_pthread_mutex_init);
142STATIC_LIB_REQUIRE(_pthread_mutex_lock);
143STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
144STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
145STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
146STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
147STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
148STATIC_LIB_REQUIRE(_pthread_once);
149STATIC_LIB_REQUIRE(_pthread_setspecific);
150STATIC_LIB_REQUIRE(_raise);
151STATIC_LIB_REQUIRE(_sem_destroy);
152STATIC_LIB_REQUIRE(_sem_getvalue);
153STATIC_LIB_REQUIRE(_sem_init);
154STATIC_LIB_REQUIRE(_sem_post);
155STATIC_LIB_REQUIRE(_sem_timedwait);
156STATIC_LIB_REQUIRE(_sem_trywait);
157STATIC_LIB_REQUIRE(_sem_wait);
158STATIC_LIB_REQUIRE(_sigaction);
159STATIC_LIB_REQUIRE(_sigprocmask);
160STATIC_LIB_REQUIRE(_sigsuspend);
161STATIC_LIB_REQUIRE(_sigtimedwait);
162STATIC_LIB_REQUIRE(_sigwait);
163STATIC_LIB_REQUIRE(_sigwaitinfo);
164STATIC_LIB_REQUIRE(_spinlock);
165STATIC_LIB_REQUIRE(_spinlock_debug);
166STATIC_LIB_REQUIRE(_spinunlock);
167STATIC_LIB_REQUIRE(_thread_init_hack);
168
169/*
170 * These are needed when linking statically. All references within
171 * libgcc (and in the future libc) to these routines are weak, but
172 * if they are not (strongly) referenced by the application or other
173 * libraries, then the actual functions will not be loaded.
174 */
175STATIC_LIB_REQUIRE(_pthread_once);
176STATIC_LIB_REQUIRE(_pthread_key_create);
177STATIC_LIB_REQUIRE(_pthread_key_delete);
178STATIC_LIB_REQUIRE(_pthread_getspecific);
179STATIC_LIB_REQUIRE(_pthread_setspecific);
180STATIC_LIB_REQUIRE(_pthread_mutex_init);
181STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
182STATIC_LIB_REQUIRE(_pthread_mutex_lock);
183STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
184STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
185STATIC_LIB_REQUIRE(_pthread_create);
186
187/* Pull in all symbols required by libthread_db */
188STATIC_LIB_REQUIRE(_thread_state_running);
189
190#define DUAL_ENTRY(entry) \
191 (pthread_func_t)entry, (pthread_func_t)entry
192
193static pthread_func_t jmp_table[][2] = {
194 {DUAL_ENTRY(_pthread_atfork)}, /* PJT_ATFORK */
195 {DUAL_ENTRY(_pthread_attr_destroy)}, /* PJT_ATTR_DESTROY */
196 {DUAL_ENTRY(_pthread_attr_getdetachstate)}, /* PJT_ATTR_GETDETACHSTATE */
197 {DUAL_ENTRY(_pthread_attr_getguardsize)}, /* PJT_ATTR_GETGUARDSIZE */
198 {DUAL_ENTRY(_pthread_attr_getinheritsched)}, /* PJT_ATTR_GETINHERITSCHED */
199 {DUAL_ENTRY(_pthread_attr_getschedparam)}, /* PJT_ATTR_GETSCHEDPARAM */
200 {DUAL_ENTRY(_pthread_attr_getschedpolicy)}, /* PJT_ATTR_GETSCHEDPOLICY */
201 {DUAL_ENTRY(_pthread_attr_getscope)}, /* PJT_ATTR_GETSCOPE */
202 {DUAL_ENTRY(_pthread_attr_getstackaddr)}, /* PJT_ATTR_GETSTACKADDR */
203 {DUAL_ENTRY(_pthread_attr_getstacksize)}, /* PJT_ATTR_GETSTACKSIZE */
204 {DUAL_ENTRY(_pthread_attr_init)}, /* PJT_ATTR_INIT */
205 {DUAL_ENTRY(_pthread_attr_setdetachstate)}, /* PJT_ATTR_SETDETACHSTATE */
206 {DUAL_ENTRY(_pthread_attr_setguardsize)}, /* PJT_ATTR_SETGUARDSIZE */
207 {DUAL_ENTRY(_pthread_attr_setinheritsched)}, /* PJT_ATTR_SETINHERITSCHED */
208 {DUAL_ENTRY(_pthread_attr_setschedparam)}, /* PJT_ATTR_SETSCHEDPARAM */
209 {DUAL_ENTRY(_pthread_attr_setschedpolicy)}, /* PJT_ATTR_SETSCHEDPOLICY */
210 {DUAL_ENTRY(_pthread_attr_setscope)}, /* PJT_ATTR_SETSCOPE */
211 {DUAL_ENTRY(_pthread_attr_setstackaddr)}, /* PJT_ATTR_SETSTACKADDR */
212 {DUAL_ENTRY(_pthread_attr_setstacksize)}, /* PJT_ATTR_SETSTACKSIZE */
213 {DUAL_ENTRY(_pthread_cancel)}, /* PJT_CANCEL */
214 {DUAL_ENTRY(_pthread_cleanup_pop)}, /* PJT_CLEANUP_POP */
215 {DUAL_ENTRY(_pthread_cleanup_push)}, /* PJT_CLEANUP_PUSH */
216 {DUAL_ENTRY(_pthread_cond_broadcast)}, /* PJT_COND_BROADCAST */
217 {DUAL_ENTRY(_pthread_cond_destroy)}, /* PJT_COND_DESTROY */
218 {DUAL_ENTRY(_pthread_cond_init)}, /* PJT_COND_INIT */
219 {DUAL_ENTRY(_pthread_cond_signal)}, /* PJT_COND_SIGNAL */
220 {DUAL_ENTRY(_pthread_cond_timedwait)}, /* PJT_COND_TIMEDWAIT */
221 {(pthread_func_t)__pthread_cond_wait,
222 (pthread_func_t)_pthread_cond_wait}, /* PJT_COND_WAIT */
223 {DUAL_ENTRY(_pthread_detach)}, /* PJT_DETACH */
224 {DUAL_ENTRY(_pthread_equal)}, /* PJT_EQUAL */
225 {DUAL_ENTRY(_pthread_exit)}, /* PJT_EXIT */
226 {DUAL_ENTRY(_pthread_getspecific)}, /* PJT_GETSPECIFIC */
227 {DUAL_ENTRY(_pthread_join)}, /* PJT_JOIN */
228 {DUAL_ENTRY(_pthread_key_create)}, /* PJT_KEY_CREATE */
229 {DUAL_ENTRY(_pthread_key_delete)}, /* PJT_KEY_DELETE*/
230 {DUAL_ENTRY(_pthread_kill)}, /* PJT_KILL */
231 {DUAL_ENTRY(_pthread_main_np)}, /* PJT_MAIN_NP */
232 {DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
233 {DUAL_ENTRY(_pthread_mutexattr_init)}, /* PJT_MUTEXATTR_INIT */
234 {DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
235 {DUAL_ENTRY(_pthread_mutex_destroy)}, /* PJT_MUTEX_DESTROY */
236 {DUAL_ENTRY(_pthread_mutex_init)}, /* PJT_MUTEX_INIT */
237 {(pthread_func_t)__pthread_mutex_lock,
238 (pthread_func_t)_pthread_mutex_lock}, /* PJT_MUTEX_LOCK */
239 {(pthread_func_t)__pthread_mutex_trylock,
240 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
241 {DUAL_ENTRY(_pthread_mutex_unlock)}, /* PJT_MUTEX_UNLOCK */
242 {DUAL_ENTRY(_pthread_once)}, /* PJT_ONCE */
243 {DUAL_ENTRY(_pthread_rwlock_destroy)}, /* PJT_RWLOCK_DESTROY */
244 {DUAL_ENTRY(_pthread_rwlock_init)}, /* PJT_RWLOCK_INIT */
245 {DUAL_ENTRY(_pthread_rwlock_rdlock)}, /* PJT_RWLOCK_RDLOCK */
246 {DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
247 {DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
248 {DUAL_ENTRY(_pthread_rwlock_unlock)}, /* PJT_RWLOCK_UNLOCK */
249 {DUAL_ENTRY(_pthread_rwlock_wrlock)}, /* PJT_RWLOCK_WRLOCK */
250 {DUAL_ENTRY(_pthread_self)}, /* PJT_SELF */
251 {DUAL_ENTRY(_pthread_setcancelstate)}, /* PJT_SETCANCELSTATE */
252 {DUAL_ENTRY(_pthread_setcanceltype)}, /* PJT_SETCANCELTYPE */
253 {DUAL_ENTRY(_pthread_setspecific)}, /* PJT_SETSPECIFIC */
254 {DUAL_ENTRY(_pthread_sigmask)}, /* PJT_SIGMASK */
255 {DUAL_ENTRY(_pthread_testcancel)}, /* PJT_TESTCANCEL */
256 {DUAL_ENTRY(__pthread_cleanup_pop_imp)},/* PJT_CLEANUP_POP_IMP */
257 {DUAL_ENTRY(__pthread_cleanup_push_imp)},/* PJT_CLEANUP_PUSH_IMP */
258 {DUAL_ENTRY(_pthread_cancel_enter)}, /* PJT_CANCEL_ENTER */
259 {DUAL_ENTRY(_pthread_cancel_leave)} /* PJT_CANCEL_LEAVE */
260};
261
262static int init_once = 0;
263
264/*
265 * For the shared version of the threads library, the above is sufficient.
266 * But for the archive version of the library, we need a little bit more.
267 * Namely, we must arrange for this particular module to be pulled in from
268 * the archive library at link time. To accomplish that, we define and
269 * initialize a variable, "_thread_autoinit_dummy_decl". This variable is
270 * referenced (as an extern) from libc/stdlib/exit.c. This will always
271 * create a need for this module, ensuring that it is present in the
272 * executable.
273 */
274extern int _thread_autoinit_dummy_decl;
275int _thread_autoinit_dummy_decl = 0;
276
277void
278_thread_init_hack(void)
279{
280
281 _libpthread_init(NULL);
282}
283
284
285/*
286 * Threaded process initialization.
287 *
288 * This is only called under two conditions:
289 *
290 * 1) Some thread routines have detected that the library hasn't yet
291 * been initialized (_thr_initial == NULL && curthread == NULL), or
292 *
293 * 2) An explicit call to reinitialize after a fork (indicated
294 * by curthread != NULL)
295 */
296void
297_libpthread_init(struct pthread *curthread)
298{
299 int fd, first = 0;
300
301 /* Check if this function has already been called: */
302 if ((_thr_initial != NULL) && (curthread == NULL))
303 /* Only initialize the threaded application once. */
304 return;
305
306 /*
307 * Check the size of the jump table to make sure it is preset
308 * with the correct number of entries.
309 */
310 if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
311 PANIC("Thread jump table not properly initialized");
312 memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
313
314 /*
315 * Check for the special case of this process running as
316 * or in place of init as pid = 1:
317 */
318 if ((_thr_pid = getpid()) == 1) {
319 /*
320 * Setup a new session for this process which is
321 * assumed to be running as root.
322 */
323 if (setsid() == -1)
324 PANIC("Can't set session ID");
325 if (revoke(_PATH_CONSOLE) != 0)
326 PANIC("Can't revoke console");
327 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
328 PANIC("Can't open console");
329 if (setlogin("root") == -1)
330 PANIC("Can't set login to root");
331 if (_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
332 PANIC("Can't set controlling terminal");
333 }
334
335 /* Initialize pthread private data. */
336 init_private();
337
338 /* Set the initial thread. */
339 if (curthread == NULL) {
340 first = 1;
341 /* Create and initialize the initial thread. */
342 curthread = _thr_alloc(NULL);
343 if (curthread == NULL)
344 PANIC("Can't allocate initial thread");
345 init_main_thread(curthread);
346 }
347 /*
348 * Add the thread to the thread list queue.
349 */
350 THR_LIST_ADD(curthread);
351 _thread_active_threads = 1;
352
353 /* Setup the thread specific data */
354 _tcb_set(curthread->tcb);
355
356 if (first) {
357 _thr_initial = curthread;
358 _thr_signal_init();
359 if (_thread_event_mask & TD_CREATE)
360 _thr_report_creation(curthread, curthread);
361 }
362}
363
364/*
365 * This function and pthread_create() do a lot of the same things.
366 * It'd be nice to consolidate the common stuff in one place.
367 */
368static void
369init_main_thread(struct pthread *thread)
370{
371 struct sched_param sched_param;
372
373 /* Setup the thread attributes. */
374 thr_self(&thread->tid);
375 thread->attr = _pthread_attr_default;
376 /*
377 * Set up the thread stack.
378 *
379 * Create a red zone below the main stack. All other stacks
380 * are constrained to a maximum size by the parameters
381 * passed to mmap(), but this stack is only limited by
382 * resource limits, so this stack needs an explicitly mapped
383 * red zone to protect the thread stack that is just beyond.
384 */
385 if (mmap(_usrstack - _thr_stack_initial -
386 _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
387 -1, 0) == MAP_FAILED)
388 PANIC("Cannot allocate red zone for initial thread");
389
390 /*
391 * Mark the stack as an application supplied stack so that it
392 * isn't deallocated.
393 *
394 * XXX - I'm not sure it would hurt anything to deallocate
395 * the main thread stack because deallocation doesn't
396 * actually free() it; it just puts it in the free
397 * stack queue for later reuse.
398 */
399 thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
400 thread->attr.stacksize_attr = _thr_stack_initial;
401 thread->attr.guardsize_attr = _thr_guard_default;
402 thread->attr.flags |= THR_STACK_USER;
403
404 /*
405 * Write a magic value to the thread structure
406 * to help identify valid ones:
407 */
408 thread->magic = THR_MAGIC;
409
410 thread->cancel_enable = 1;
411 thread->cancel_async = 0;
412 thr_set_name(thread->tid, "initial thread");
413
414 /* Initialize the mutex queue: */
415 TAILQ_INIT(&thread->mutexq);
416 TAILQ_INIT(&thread->pp_mutexq);
417
418 thread->state = PS_RUNNING;
419
420 _thr_getscheduler(thread->tid, &thread->attr.sched_policy,
421 &sched_param);
422 thread->attr.prio = sched_param.sched_priority;
423
424#ifdef _PTHREAD_FORCED_UNWIND
425 thread->unwind_stackend = _usrstack;
426#endif
427
428 /* Others cleared to zero by thr_alloc() */
429}
430
431static void
432init_private(void)
433{
434 size_t len;
435 int mib[2];
436 char *env;
437
438 _thr_umutex_init(&_mutex_static_lock);
439 _thr_umutex_init(&_cond_static_lock);
440 _thr_umutex_init(&_rwlock_static_lock);
441 _thr_umutex_init(&_keytable_lock);
442 _thr_urwlock_init(&_thr_atfork_lock);
443 _thr_umutex_init(&_thr_event_lock);
444 _thr_once_init();
445 _thr_spinlock_init();
446 _thr_list_init();
447
448 /*
449 * Avoid reinitializing some things if they don't need to be,
450 * e.g. after a fork().
451 */
452 if (init_once == 0) {
453 /* Find the stack top */
454 mib[0] = CTL_KERN;
455 mib[1] = KERN_USRSTACK;
456 len = sizeof (_usrstack);
457 if (sysctl(mib, 2, &_usrstack, &len, NULL, 0) == -1)
458 PANIC("Cannot get kern.usrstack from sysctl");
459 len = sizeof(_thr_is_smp);
460 sysctlbyname("kern.smp.cpus", &_thr_is_smp, &len, NULL, 0);
461 _thr_is_smp = (_thr_is_smp > 1);
462 _thr_page_size = getpagesize();
463 _thr_guard_default = _thr_page_size;
464 _pthread_attr_default.guardsize_attr = _thr_guard_default;
465 _pthread_attr_default.stacksize_attr = _thr_stack_default;
466 env = getenv("LIBPTHREAD_SPINLOOPS");
467 if (env)
468 _thr_spinloops = atoi(env);
469 env = getenv("LIBPTHREAD_YIELDLOOPS");
470 if (env)
471 _thr_yieldloops = atoi(env);
472 TAILQ_INIT(&_thr_atfork_list);
473 }
474 init_once = 1;
475}