Replace the nologin shell script with a tiny c version. Just for the sake
[dragonfly/netmp.git] / lib / libthread_xu / thread / thr_create.c
blob53752c5fc79e5ebaf25ec6074c571a159c5c2301
1 /*
2 * Copyright (c) 2005 David Xu <davidxu@freebsd.org>
3 * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
4 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by John Birrell.
18 * 4. Neither the name of the author nor the names of any co-contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $FreeBSD: src/lib/libpthread/thread/thr_create.c,v 1.58 2004/10/23 23:28:36 davidxu Exp $
35 * $DragonFly: src/lib/libthread_xu/thread/thr_create.c,v 1.11 2007/03/13 00:19:29 corecode Exp $
38 #include "namespace.h"
39 #include <errno.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <fcntl.h>
43 #include <unistd.h>
44 #include <stddef.h>
45 #include <sys/time.h>
46 #include <machine/reg.h>
47 #include <machine/tls.h>
49 #include <pthread.h>
50 #include <sys/signalvar.h>
51 #include "un-namespace.h"
53 #include "thr_private.h"
54 #include "libc_private.h"
56 static int create_stack(struct pthread_attr *pattr);
57 static void thread_start(void *);
59 int
60 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
61 void *(*start_routine) (void *), void *arg)
63 struct lwp_params create_params;
64 void *stack;
65 sigset_t sigmask, oldsigmask;
66 struct pthread *curthread, *new_thread;
67 int ret = 0, locked;
69 _thr_check_init();
72 * Tell libc and others now they need lock to protect their data.
74 if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
75 return (EAGAIN);
77 curthread = tls_get_curthread();
78 if ((new_thread = _thr_alloc(curthread)) == NULL)
79 return (EAGAIN);
81 if (attr == NULL || *attr == NULL)
82 /* Use the default thread attributes: */
83 new_thread->attr = _pthread_attr_default;
84 else
85 new_thread->attr = *(*attr);
86 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
87 /* inherit scheduling contention scope */
88 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
89 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
90 else
91 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
93 * scheduling policy and scheduling parameters will be
94 * inherited in following code.
98 if (_thread_scope_system > 0)
99 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
100 else if (_thread_scope_system < 0)
101 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
103 if (create_stack(&new_thread->attr) != 0) {
104 /* Insufficient memory to create a stack: */
105 new_thread->terminated = 1;
106 _thr_free(curthread, new_thread);
107 return (EAGAIN);
110 * Write a magic value to the thread structure
111 * to help identify valid ones:
113 new_thread->magic = THR_MAGIC;
114 new_thread->start_routine = start_routine;
115 new_thread->arg = arg;
116 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
117 PTHREAD_CANCEL_DEFERRED;
119 * Check if this thread is to inherit the scheduling
120 * attributes from its parent:
122 if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
124 * Copy the scheduling attributes. Lock the scheduling
125 * lock to get consistent scheduling parameters.
127 THR_LOCK(curthread);
128 new_thread->base_priority = curthread->base_priority;
129 new_thread->attr.prio = curthread->base_priority;
130 new_thread->attr.sched_policy = curthread->attr.sched_policy;
131 THR_UNLOCK(curthread);
132 } else {
134 * Use just the thread priority, leaving the
135 * other scheduling attributes as their
136 * default values:
138 new_thread->base_priority = new_thread->attr.prio;
140 new_thread->active_priority = new_thread->base_priority;
142 /* Initialize the mutex queue: */
143 TAILQ_INIT(&new_thread->mutexq);
145 /* Initialise hooks in the thread structure: */
146 if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
147 new_thread->flags = THR_FLAGS_NEED_SUSPEND;
149 new_thread->state = PS_RUNNING;
151 if (new_thread->attr.flags & PTHREAD_CREATE_DETACHED)
152 new_thread->tlflags |= TLFLAGS_DETACHED;
155 * Thread created by thr_create() inherits currrent thread
156 * sigmask, however, before new thread setup itself correctly,
157 * it can not handle signal, so we should masks all signals here.
159 SIGFILLSET(sigmask);
160 __sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
161 new_thread->sigmask = oldsigmask;
162 /* Add the new thread. */
163 new_thread->refcount = 1;
164 _thr_link(curthread, new_thread);
165 /* Return thread pointer eariler so that new thread can use it. */
166 (*thread) = new_thread;
167 if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
168 THR_THREAD_LOCK(curthread, new_thread);
169 locked = 1;
170 } else
171 locked = 0;
172 /* Schedule the new thread. */
173 stack = (char *)new_thread->attr.stackaddr_attr +
174 new_thread->attr.stacksize_attr;
175 bzero(&create_params, sizeof(create_params));
176 create_params.func = thread_start;
177 create_params.arg = new_thread;
178 create_params.stack = stack;
179 create_params.tid1 = &new_thread->tid;
180 ret = lwp_create(&create_params);
181 __sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
182 if (ret != 0) {
183 if (!locked)
184 THR_THREAD_LOCK(curthread, new_thread);
185 new_thread->state = PS_DEAD;
186 new_thread->terminated = 1;
187 if (new_thread->flags & THR_FLAGS_NEED_SUSPEND) {
188 new_thread->cycle++;
189 _thr_umtx_wake(&new_thread->cycle, INT_MAX);
191 THR_THREAD_UNLOCK(curthread, new_thread);
192 THREAD_LIST_LOCK(curthread);
193 _thread_active_threads--;
194 new_thread->tlflags |= TLFLAGS_DETACHED;
195 _thr_ref_delete_unlocked(curthread, new_thread);
196 THREAD_LIST_UNLOCK(curthread);
197 (*thread) = 0;
198 ret = EAGAIN;
199 } else if (locked) {
200 _thr_report_creation(curthread, new_thread);
201 THR_THREAD_UNLOCK(curthread, new_thread);
203 return (ret);
206 static int
207 create_stack(struct pthread_attr *pattr)
209 int ret;
211 /* Check if a stack was specified in the thread attributes: */
212 if ((pattr->stackaddr_attr) != NULL) {
213 pattr->guardsize_attr = 0;
214 pattr->flags |= THR_STACK_USER;
215 ret = 0;
217 else
218 ret = _thr_stack_alloc(pattr);
219 return (ret);
222 static void
223 thread_start(void *arg)
225 struct pthread *curthread = (struct pthread *)arg;
227 tls_set_tcb(curthread->tcb);
229 /* Thread was created with all signals blocked, unblock them. */
230 __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
232 THR_LOCK(curthread);
233 THR_UNLOCK(curthread);
235 if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
236 _thr_suspend_check(curthread);
238 /* Run the current thread's start routine with argument: */
239 _pthread_exit(curthread->start_routine(curthread->arg));
241 /* This point should never be reached. */
242 PANIC("Thread has resumed after exit");
245 __strong_reference(_pthread_create, pthread_create);