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
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>
43 #include <sys/rtprio.h>
48 #include <pthread_np.h>
54 #include "un-namespace.h"
56 #include "libc_private.h"
57 #include "thr_private.h"
60 struct pthread *_thr_initial;
62 int _thread_event_mask;
63 struct pthread *_thread_last_event;
64 pthreadlist _thread_list = TAILQ_HEAD_INITIALIZER(_thread_list);
65 pthreadlist _thread_gc_list = TAILQ_HEAD_INITIALIZER(_thread_gc_list);
66 int _thread_active_threads = 1;
67 atfork_head _thr_atfork_list = TAILQ_HEAD_INITIALIZER(_thr_atfork_list);
68 struct umutex _thr_atfork_lock = DEFAULT_UMUTEX;
70 struct 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 */
76 struct pthread_attr _pthread_attr_default = {
77 .sched_policy = SCHED_OTHER,
78 .sched_inherit = PTHREAD_INHERIT_SCHED,
80 .suspend = THR_CREATE_RUNNING,
81 .flags = PTHREAD_SCOPE_SYSTEM,
82 .stackaddr_attr = NULL,
83 .stacksize_attr = THR_STACK_DEFAULT,
89 struct pthread_mutex_attr _pthread_mutexattr_default = {
90 .m_type = PTHREAD_MUTEX_DEFAULT,
91 .m_protocol = PTHREAD_PRIO_NONE,
96 /* Default condition variable attributes: */
97 struct pthread_cond_attr _pthread_condattr_default = {
98 .c_pshared = PTHREAD_PROCESS_PRIVATE,
99 .c_clockid = CLOCK_REALTIME
104 size_t _thr_guard_default;
105 size_t _thr_stack_default = THR_STACK_DEFAULT;
106 size_t _thr_stack_initial = THR_STACK_INITIAL;
111 struct umutex _mutex_static_lock = DEFAULT_UMUTEX;
112 struct umutex _cond_static_lock = DEFAULT_UMUTEX;
113 struct umutex _rwlock_static_lock = DEFAULT_UMUTEX;
114 struct umutex _keytable_lock = DEFAULT_UMUTEX;
115 struct umutex _thr_list_lock = DEFAULT_UMUTEX;
116 struct umutex _thr_event_lock = DEFAULT_UMUTEX;
118 int __pthread_cond_wait(pthread_cond_t *, pthread_mutex_t *);
119 int __pthread_mutex_lock(pthread_mutex_t *);
120 int __pthread_mutex_trylock(pthread_mutex_t *);
121 void _thread_init_hack(void) __attribute__ ((constructor));
123 static void init_private(void);
124 static void init_main_thread(struct pthread *thread);
127 * All weak references used within libc should be in this table.
128 * This is so that static libraries will work.
131 STATIC_LIB_REQUIRE(_fork);
132 STATIC_LIB_REQUIRE(_pthread_getspecific);
133 STATIC_LIB_REQUIRE(_pthread_key_create);
134 STATIC_LIB_REQUIRE(_pthread_key_delete);
135 STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
136 STATIC_LIB_REQUIRE(_pthread_mutex_init);
137 STATIC_LIB_REQUIRE(_pthread_mutex_lock);
138 STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
139 STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
140 STATIC_LIB_REQUIRE(_pthread_mutexattr_init);
141 STATIC_LIB_REQUIRE(_pthread_mutexattr_destroy);
142 STATIC_LIB_REQUIRE(_pthread_mutexattr_settype);
143 STATIC_LIB_REQUIRE(_pthread_once);
144 STATIC_LIB_REQUIRE(_pthread_setspecific);
145 STATIC_LIB_REQUIRE(_raise);
146 STATIC_LIB_REQUIRE(_sem_destroy);
147 STATIC_LIB_REQUIRE(_sem_getvalue);
148 STATIC_LIB_REQUIRE(_sem_init);
149 STATIC_LIB_REQUIRE(_sem_post);
150 STATIC_LIB_REQUIRE(_sem_timedwait);
151 STATIC_LIB_REQUIRE(_sem_trywait);
152 STATIC_LIB_REQUIRE(_sem_wait);
153 STATIC_LIB_REQUIRE(_sigaction);
154 STATIC_LIB_REQUIRE(_sigprocmask);
155 STATIC_LIB_REQUIRE(_sigsuspend);
156 STATIC_LIB_REQUIRE(_sigtimedwait);
157 STATIC_LIB_REQUIRE(_sigwait);
158 STATIC_LIB_REQUIRE(_sigwaitinfo);
159 STATIC_LIB_REQUIRE(_spinlock);
160 STATIC_LIB_REQUIRE(_spinlock_debug);
161 STATIC_LIB_REQUIRE(_spinunlock);
162 STATIC_LIB_REQUIRE(_thread_init_hack);
163 STATIC_LIB_REQUIRE(_vfork);
166 * These are needed when linking statically. All references within
167 * libgcc (and in the future libc) to these routines are weak, but
168 * if they are not (strongly) referenced by the application or other
169 * libraries, then the actual functions will not be loaded.
171 STATIC_LIB_REQUIRE(_pthread_once);
172 STATIC_LIB_REQUIRE(_pthread_key_create);
173 STATIC_LIB_REQUIRE(_pthread_key_delete);
174 STATIC_LIB_REQUIRE(_pthread_getspecific);
175 STATIC_LIB_REQUIRE(_pthread_setspecific);
176 STATIC_LIB_REQUIRE(_pthread_mutex_init);
177 STATIC_LIB_REQUIRE(_pthread_mutex_destroy);
178 STATIC_LIB_REQUIRE(_pthread_mutex_lock);
179 STATIC_LIB_REQUIRE(_pthread_mutex_trylock);
180 STATIC_LIB_REQUIRE(_pthread_mutex_unlock);
181 STATIC_LIB_REQUIRE(_pthread_create);
183 /* Pull in all symbols required by libthread_db */
184 STATIC_LIB_REQUIRE(_thread_state_running);
186 #define DUAL_ENTRY(entry) \
187 (pthread_func_t)entry, (pthread_func_t)entry
189 static pthread_func_t jmp_table[][2] = {
190 {DUAL_ENTRY(_pthread_atfork)}, /* PJT_ATFORK */
191 {DUAL_ENTRY(_pthread_attr_destroy)}, /* PJT_ATTR_DESTROY */
192 {DUAL_ENTRY(_pthread_attr_getdetachstate)}, /* PJT_ATTR_GETDETACHSTATE */
193 {DUAL_ENTRY(_pthread_attr_getguardsize)}, /* PJT_ATTR_GETGUARDSIZE */
194 {DUAL_ENTRY(_pthread_attr_getinheritsched)}, /* PJT_ATTR_GETINHERITSCHED */
195 {DUAL_ENTRY(_pthread_attr_getschedparam)}, /* PJT_ATTR_GETSCHEDPARAM */
196 {DUAL_ENTRY(_pthread_attr_getschedpolicy)}, /* PJT_ATTR_GETSCHEDPOLICY */
197 {DUAL_ENTRY(_pthread_attr_getscope)}, /* PJT_ATTR_GETSCOPE */
198 {DUAL_ENTRY(_pthread_attr_getstackaddr)}, /* PJT_ATTR_GETSTACKADDR */
199 {DUAL_ENTRY(_pthread_attr_getstacksize)}, /* PJT_ATTR_GETSTACKSIZE */
200 {DUAL_ENTRY(_pthread_attr_init)}, /* PJT_ATTR_INIT */
201 {DUAL_ENTRY(_pthread_attr_setdetachstate)}, /* PJT_ATTR_SETDETACHSTATE */
202 {DUAL_ENTRY(_pthread_attr_setguardsize)}, /* PJT_ATTR_SETGUARDSIZE */
203 {DUAL_ENTRY(_pthread_attr_setinheritsched)}, /* PJT_ATTR_SETINHERITSCHED */
204 {DUAL_ENTRY(_pthread_attr_setschedparam)}, /* PJT_ATTR_SETSCHEDPARAM */
205 {DUAL_ENTRY(_pthread_attr_setschedpolicy)}, /* PJT_ATTR_SETSCHEDPOLICY */
206 {DUAL_ENTRY(_pthread_attr_setscope)}, /* PJT_ATTR_SETSCOPE */
207 {DUAL_ENTRY(_pthread_attr_setstackaddr)}, /* PJT_ATTR_SETSTACKADDR */
208 {DUAL_ENTRY(_pthread_attr_setstacksize)}, /* PJT_ATTR_SETSTACKSIZE */
209 {DUAL_ENTRY(_pthread_cancel)}, /* PJT_CANCEL */
210 {DUAL_ENTRY(_pthread_cleanup_pop)}, /* PJT_CLEANUP_POP */
211 {DUAL_ENTRY(_pthread_cleanup_push)}, /* PJT_CLEANUP_PUSH */
212 {DUAL_ENTRY(_pthread_cond_broadcast)}, /* PJT_COND_BROADCAST */
213 {DUAL_ENTRY(_pthread_cond_destroy)}, /* PJT_COND_DESTROY */
214 {DUAL_ENTRY(_pthread_cond_init)}, /* PJT_COND_INIT */
215 {DUAL_ENTRY(_pthread_cond_signal)}, /* PJT_COND_SIGNAL */
216 {DUAL_ENTRY(_pthread_cond_timedwait)}, /* PJT_COND_TIMEDWAIT */
217 {(pthread_func_t)__pthread_cond_wait,
218 (pthread_func_t)_pthread_cond_wait}, /* PJT_COND_WAIT */
219 {DUAL_ENTRY(_pthread_detach)}, /* PJT_DETACH */
220 {DUAL_ENTRY(_pthread_equal)}, /* PJT_EQUAL */
221 {DUAL_ENTRY(_pthread_exit)}, /* PJT_EXIT */
222 {DUAL_ENTRY(_pthread_getspecific)}, /* PJT_GETSPECIFIC */
223 {DUAL_ENTRY(_pthread_join)}, /* PJT_JOIN */
224 {DUAL_ENTRY(_pthread_key_create)}, /* PJT_KEY_CREATE */
225 {DUAL_ENTRY(_pthread_key_delete)}, /* PJT_KEY_DELETE*/
226 {DUAL_ENTRY(_pthread_kill)}, /* PJT_KILL */
227 {DUAL_ENTRY(_pthread_main_np)}, /* PJT_MAIN_NP */
228 {DUAL_ENTRY(_pthread_mutexattr_destroy)}, /* PJT_MUTEXATTR_DESTROY */
229 {DUAL_ENTRY(_pthread_mutexattr_init)}, /* PJT_MUTEXATTR_INIT */
230 {DUAL_ENTRY(_pthread_mutexattr_settype)}, /* PJT_MUTEXATTR_SETTYPE */
231 {DUAL_ENTRY(_pthread_mutex_destroy)}, /* PJT_MUTEX_DESTROY */
232 {DUAL_ENTRY(_pthread_mutex_init)}, /* PJT_MUTEX_INIT */
233 {(pthread_func_t)__pthread_mutex_lock,
234 (pthread_func_t)_pthread_mutex_lock}, /* PJT_MUTEX_LOCK */
235 {(pthread_func_t)__pthread_mutex_trylock,
236 (pthread_func_t)_pthread_mutex_trylock},/* PJT_MUTEX_TRYLOCK */
237 {DUAL_ENTRY(_pthread_mutex_unlock)}, /* PJT_MUTEX_UNLOCK */
238 {DUAL_ENTRY(_pthread_once)}, /* PJT_ONCE */
239 {DUAL_ENTRY(_pthread_rwlock_destroy)}, /* PJT_RWLOCK_DESTROY */
240 {DUAL_ENTRY(_pthread_rwlock_init)}, /* PJT_RWLOCK_INIT */
241 {DUAL_ENTRY(_pthread_rwlock_rdlock)}, /* PJT_RWLOCK_RDLOCK */
242 {DUAL_ENTRY(_pthread_rwlock_tryrdlock)},/* PJT_RWLOCK_TRYRDLOCK */
243 {DUAL_ENTRY(_pthread_rwlock_trywrlock)},/* PJT_RWLOCK_TRYWRLOCK */
244 {DUAL_ENTRY(_pthread_rwlock_unlock)}, /* PJT_RWLOCK_UNLOCK */
245 {DUAL_ENTRY(_pthread_rwlock_wrlock)}, /* PJT_RWLOCK_WRLOCK */
246 {DUAL_ENTRY(_pthread_self)}, /* PJT_SELF */
247 {DUAL_ENTRY(_pthread_setcancelstate)}, /* PJT_SETCANCELSTATE */
248 {DUAL_ENTRY(_pthread_setcanceltype)}, /* PJT_SETCANCELTYPE */
249 {DUAL_ENTRY(_pthread_setspecific)}, /* PJT_SETSPECIFIC */
250 {DUAL_ENTRY(_pthread_sigmask)}, /* PJT_SIGMASK */
251 {DUAL_ENTRY(_pthread_testcancel)} /* PJT_TESTCANCEL */
254 static int init_once = 0;
257 * For the shared version of the threads library, the above is sufficient.
258 * But for the archive version of the library, we need a little bit more.
259 * Namely, we must arrange for this particular module to be pulled in from
260 * the archive library at link time. To accomplish that, we define and
261 * initialize a variable, "_thread_autoinit_dummy_decl". This variable is
262 * referenced (as an extern) from libc/stdlib/exit.c. This will always
263 * create a need for this module, ensuring that it is present in the
266 extern int _thread_autoinit_dummy_decl;
267 int _thread_autoinit_dummy_decl = 0;
270 _thread_init_hack(void)
273 _libpthread_init(NULL);
278 * Threaded process initialization.
280 * This is only called under two conditions:
282 * 1) Some thread routines have detected that the library hasn't yet
283 * been initialized (_thr_initial == NULL && curthread == NULL), or
285 * 2) An explicit call to reinitialize after a fork (indicated
286 * by curthread != NULL)
289 _libpthread_init(struct pthread *curthread)
292 sigset_t sigset, oldset;
294 /* Check if this function has already been called: */
295 if ((_thr_initial != NULL) && (curthread == NULL))
296 /* Only initialize the threaded application once. */
300 * Check the size of the jump table to make sure it is preset
301 * with the correct number of entries.
303 if (sizeof(jmp_table) != (sizeof(pthread_func_t) * PJT_MAX * 2))
304 PANIC("Thread jump table not properly initialized");
305 memcpy(__thr_jtable, jmp_table, sizeof(jmp_table));
308 * Check for the special case of this process running as
309 * or in place of init as pid = 1:
311 if ((_thr_pid = getpid()) == 1) {
313 * Setup a new session for this process which is
314 * assumed to be running as root.
317 PANIC("Can't set session ID");
318 if (revoke(_PATH_CONSOLE) != 0)
319 PANIC("Can't revoke console");
320 if ((fd = __sys_open(_PATH_CONSOLE, O_RDWR)) < 0)
321 PANIC("Can't open console");
322 if (setlogin("root") == -1)
323 PANIC("Can't set login to root");
324 if (_ioctl(fd, TIOCSCTTY, (char *) NULL) == -1)
325 PANIC("Can't set controlling terminal");
328 /* Initialize pthread private data. */
331 /* Set the initial thread. */
332 if (curthread == NULL) {
334 /* Create and initialize the initial thread. */
335 curthread = _thr_alloc(NULL);
336 if (curthread == NULL)
337 PANIC("Can't allocate initial thread");
338 init_main_thread(curthread);
341 * Add the thread to the thread list queue.
343 THR_LIST_ADD(curthread);
344 _thread_active_threads = 1;
346 /* Setup the thread specific data */
347 _tcb_set(curthread->tcb);
351 SIGDELSET(sigset, SIGTRAP);
352 __sys_sigprocmask(SIG_SETMASK, &sigset, &oldset);
354 _thr_initial = curthread;
355 SIGDELSET(oldset, SIGCANCEL);
356 __sys_sigprocmask(SIG_SETMASK, &oldset, NULL);
357 if (_thread_event_mask & TD_CREATE)
358 _thr_report_creation(curthread, curthread);
363 * This function and pthread_create() do a lot of the same things.
364 * It'd be nice to consolidate the common stuff in one place.
367 init_main_thread(struct pthread *thread)
369 struct sched_param sched_param;
371 /* Setup the thread attributes. */
372 thr_self(&thread->tid);
373 thread->attr = _pthread_attr_default;
375 * Set up the thread stack.
377 * Create a red zone below the main stack. All other stacks
378 * are constrained to a maximum size by the parameters
379 * passed to mmap(), but this stack is only limited by
380 * resource limits, so this stack needs an explicitly mapped
381 * red zone to protect the thread stack that is just beyond.
383 if (mmap(_usrstack - _thr_stack_initial -
384 _thr_guard_default, _thr_guard_default, 0, MAP_ANON,
385 -1, 0) == MAP_FAILED)
386 PANIC("Cannot allocate red zone for initial thread");
389 * Mark the stack as an application supplied stack so that it
392 * XXX - I'm not sure it would hurt anything to deallocate
393 * the main thread stack because deallocation doesn't
394 * actually free() it; it just puts it in the free
395 * stack queue for later reuse.
397 thread->attr.stackaddr_attr = _usrstack - _thr_stack_initial;
398 thread->attr.stacksize_attr = _thr_stack_initial;
399 thread->attr.guardsize_attr = _thr_guard_default;
400 thread->attr.flags |= THR_STACK_USER;
403 * Write a magic value to the thread structure
404 * to help identify valid ones:
406 thread->magic = THR_MAGIC;
408 thread->cancel_enable = 1;
409 thread->cancel_async = 0;
410 thr_set_name(thread->tid, "initial thread");
412 /* Initialize the mutex queue: */
413 TAILQ_INIT(&thread->mutexq);
414 TAILQ_INIT(&thread->pp_mutexq);
416 thread->state = PS_RUNNING;
418 _thr_getscheduler(thread->tid, &thread->attr.sched_policy,
420 thread->attr.prio = sched_param.sched_priority;
422 /* Others cleared to zero by thr_alloc() */
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_umutex_init(&_thr_atfork_lock);
437 _thr_umutex_init(&_thr_event_lock);
439 _thr_spinlock_init();
443 * Avoid reinitializing some things if they don't need to be,
444 * e.g. after a fork().
446 if (init_once == 0) {
447 /* Find the stack top */
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");
462 _thr_spinloops = atoi(env);
463 env = getenv("LIBPTHREAD_YIELDLOOPS");
465 _thr_yieldloops = atoi(env);
466 TAILQ_INIT(&_thr_atfork_list);