1 /* Copyright (C) 2002, 2003, 2004, 2006, 2007, 2008
2 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4 Contributed by Ulrich Drepper <drepper@redhat.com>, 2002.
5 OpenSolaris bits contributed by David Bartley
6 <dtbartle@csclub.uwaterloo.ca>, 2008.
8 The GNU C Library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Lesser General Public
10 License as published by the Free Software Foundation; either
11 version 2.1 of the License, or (at your option) any later version.
13 The GNU C Library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public
19 License along with the GNU C Library; if not, write to the Free
20 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
25 #include <sys/segments.h>
26 #include <sys/stack.h>
27 #include <sys/stack.h>
28 #include <sys/regset.h>
29 #include <sys/segments.h>
30 #include <inline-syscall.h>
31 #include <sched_priv.h>
32 #include <createthread_arch.c>
34 DECLARE_INLINE_SYSCALL (int, lwp_create, ucontext_t *ucp, int flags,
36 DECLARE_INLINE_SYSCALL (int, lwp_continue, pthread_t lwpid);
37 DECLARE_INLINE_SYSCALL (int, lwp_kill, pthread_t lwpid, int sig);
39 #ifndef TLS_MULTIPLE_THREADS_IN_TCB
40 /* Pointer to the corresponding variable in libc. */
41 int *__libc_multiple_threads_ptr attribute_hidden;
45 create_thread (struct pthread *pd, const struct pthread_attr *attr,
46 STACK_VARIABLES_PARMS)
49 assert (pd->header.tcb != NULL);
52 /* Do arch-specific creation. */
54 int errval = create_thread_arch (&ctx, pd, attr, STACK_VARIABLES_ARGS);
58 /* One more thread. We cannot have the thread do this itself, since it
59 might exist but not have been scheduled yet by the time we've returned
60 and need to check the value to behave correctly. We must do it before
61 creating the thread, in case it does get scheduled first and then
62 might mistakenly think it was the only thread. In the failure case,
63 we momentarily store a false value; this doesn't matter because there
64 is no kosher thing a signal handler interrupting us right here can do
65 that cares whether the thread count is correct. */
66 atomic_increment (&__nptl_nthreads);
68 /* We set the thread to be initially suspended so that we can set
71 ((attr->flags & ATTR_FLAG_DAEMON) ? THR_DAEMON : 0) |
72 ((attr->flags & ATTR_FLAG_DETACHSTATE) ? THR_DETACHED : 0);
73 if (attr->flags & ATTR_FLAG_THR_CREATE == 0)
74 lwp_flags |= THR_SUSPENDED;
75 errval = INLINE_SYSCALL (lwp_create, 3, &ctx, lwp_flags, &pd->tid);
76 if (errval == 0 && (attr->flags & ATTR_FLAG_THR_CREATE) == 0)
79 if (attr->flags & (ATTR_FLAG_SCHED_SET | ATTR_FLAG_POLICY_SET))
82 errval = __sched_getscheduler_id (P_LWPID, pd->tid,
86 if (attr->flags & ATTR_FLAG_SCHED_SET)
87 priority = attr->schedparam.__sched_priority;
88 if (attr->flags & ATTR_FLAG_POLICY_SET)
89 policy = attr->schedpolicy;
91 errval = __sched_setscheduler_id (P_LWPID, pd->tid,
99 /* Resume thread if requested. */
102 if (!(attr->flags & ATTR_FLAG_SUSPENDED))
103 errval = INLINE_SYSCALL (lwp_continue, 1, pd->tid);
106 /* Kill the thread if we didn't succeed above. */
108 INLINE_SYSCALL (lwp_kill, 2, pd->tid, SIGKILL);
113 atomic_decrement (&__nptl_nthreads); /* Oops, we lied for a second. */
115 /* Failed. If the thread is detached, remove the TCB here since
116 the caller cannot do this. The caller remembered the thread
117 as detached and cannot reverify that it is not since it must
118 not access the thread descriptor again. */
119 if (IS_DETACHED (pd))
120 __deallocate_stack (pd);