Merge from vendor branch OPENSSH:
[dragonfly.git] / lib / libthread_xu / thread / thr_create.c
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.
6  *
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.
21  *
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.
33  *
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.5 2005/10/10 13:45:57 davidxu Exp $
36  */
37 #include <errno.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <stddef.h>
43 #include <sys/time.h>
44 #include <machine/reg.h>
45 #include <machine/tls.h>
46
47 #include <pthread.h>
48 #include <sys/signalvar.h>
49
50 #include "thr_private.h"
51 #include "libc_private.h"
52
53 struct start_arg {
54         struct pthread  *thread;
55         volatile umtx_t started;
56 };
57
58 static void free_thread(struct pthread *curthread, struct pthread *thread);
59 static int  create_stack(struct pthread_attr *pattr);
60 static void free_stack(struct pthread *curthread, struct pthread_attr *pattr);
61 static void thread_start(void *);
62
63 __weak_reference(_pthread_create, pthread_create);
64
65 int
66 _pthread_create(pthread_t * thread, const pthread_attr_t * attr,
67                void *(*start_routine) (void *), void *arg)
68 {
69         struct start_arg start_arg;
70         void *stack;
71         sigset_t sigmask, oldsigmask;
72         struct pthread *curthread, *new_thread;
73         int ret = 0, locked;
74
75         _thr_check_init();
76
77         /*
78          * Tell libc and others now they need lock to protect their data.
79          */
80         if (_thr_isthreaded() == 0 && _thr_setthreaded(1))
81                 return (EAGAIN);
82
83         curthread = tls_get_curthread();
84         if ((new_thread = _thr_alloc(curthread)) == NULL)
85                 return (EAGAIN);
86
87         if (attr == NULL || *attr == NULL)
88                 /* Use the default thread attributes: */
89                 new_thread->attr = _pthread_attr_default;
90         else
91                 new_thread->attr = *(*attr);
92         if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
93                 /* inherit scheduling contention scope */
94                 if (curthread->attr.flags & PTHREAD_SCOPE_SYSTEM)
95                         new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
96                 else
97                         new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
98                 /*
99                  * scheduling policy and scheduling parameters will be
100                  * inherited in following code.
101                  */
102         }
103
104         if (_thread_scope_system > 0)
105                 new_thread->attr.flags |= PTHREAD_SCOPE_SYSTEM;
106         else if (_thread_scope_system < 0)
107                 new_thread->attr.flags &= ~PTHREAD_SCOPE_SYSTEM;
108
109         if (create_stack(&new_thread->attr) != 0) {
110                 /* Insufficient memory to create a stack: */
111                 new_thread->terminated = 1;
112                 _thr_free(curthread, new_thread);
113                 return (EAGAIN);
114         }
115         /*
116          * Write a magic value to the thread structure
117          * to help identify valid ones:
118          */
119         new_thread->magic = THR_MAGIC;
120         new_thread->start_routine = start_routine;
121         new_thread->arg = arg;
122         new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
123             PTHREAD_CANCEL_DEFERRED;
124         /*
125          * Check if this thread is to inherit the scheduling
126          * attributes from its parent:
127          */
128         if (new_thread->attr.sched_inherit == PTHREAD_INHERIT_SCHED) {
129                 /*
130                  * Copy the scheduling attributes. Lock the scheduling
131                  * lock to get consistent scheduling parameters.
132                  */
133                 THR_LOCK(curthread);
134                 new_thread->base_priority = curthread->base_priority;
135                 new_thread->attr.prio = curthread->base_priority;
136                 new_thread->attr.sched_policy = curthread->attr.sched_policy;
137                 THR_UNLOCK(curthread);
138         } else {
139                 /*
140                  * Use just the thread priority, leaving the
141                  * other scheduling attributes as their
142                  * default values:
143                  */
144                 new_thread->base_priority = new_thread->attr.prio;
145         }
146         new_thread->active_priority = new_thread->base_priority;
147
148         /* Initialize the mutex queue: */
149         TAILQ_INIT(&new_thread->mutexq);
150         TAILQ_INIT(&new_thread->pri_mutexq);
151
152         /* Initialise hooks in the thread structure: */
153         if (new_thread->attr.suspend == THR_CREATE_SUSPENDED)
154                 new_thread->flags = THR_FLAGS_NEED_SUSPEND;
155         new_thread->state = PS_RUNNING;
156         /*
157          * Thread created by thr_create() inherits currrent thread
158          * sigmask, however, before new thread setup itself correctly,
159          * it can not handle signal, so we should masks all signals here.
160          */
161         SIGFILLSET(sigmask);
162         __sys_sigprocmask(SIG_SETMASK, &sigmask, &oldsigmask);
163         new_thread->sigmask = oldsigmask;
164         /* Add the new thread. */
165         _thr_link(curthread, new_thread);
166         /* Return thread pointer eariler so that new thread can use it. */
167         (*thread) = new_thread;
168         if (SHOULD_REPORT_EVENT(curthread, TD_CREATE)) {
169                 THR_THREAD_LOCK(curthread, new_thread);
170                 locked = 1;
171         } else
172                 locked = 0;
173         /* Schedule the new thread. */
174         stack = (char *)new_thread->attr.stackaddr_attr +
175                         new_thread->attr.stacksize_attr;
176         start_arg.thread  = new_thread;
177         start_arg.started = 0;
178         ret = rfork_thread(RFPROC | RFNOWAIT | RFMEM | RFSIGSHARE, stack,
179                            (int (*)(void *))thread_start, &start_arg);
180         __sys_sigprocmask(SIG_SETMASK, &oldsigmask, NULL);
181         if (ret >= 0) {
182                 /*
183                  * Wait until new thread get its thread id, so we can
184                  * send signal to it immediately after we return.
185                  */
186                 while (start_arg.started == 0)
187                         _thr_umtx_wait(&start_arg.started, 0, NULL, 0);
188                 ret = 0;
189         } else {
190                 ret = EAGAIN;
191         }
192         if (ret != 0) {
193                 if (locked)
194                         THR_THREAD_UNLOCK(curthread, new_thread);
195                 _thr_unlink(curthread, new_thread);
196                 free_thread(curthread, new_thread);
197                 (*thread) = 0;
198                 ret = EAGAIN;
199         } else if (locked) {
200                 _thr_report_creation(curthread, new_thread);
201                 THR_THREAD_UNLOCK(curthread, new_thread);
202         }
203         return (ret);
204 }
205
206 static void
207 free_thread(struct pthread *curthread, struct pthread *thread)
208 {
209         free_stack(curthread, &thread->attr);
210         curthread->terminated = 1;
211         _thr_free(curthread, thread);
212 }
213
214 static int
215 create_stack(struct pthread_attr *pattr)
216 {
217         int ret;
218
219         /* Check if a stack was specified in the thread attributes: */
220         if ((pattr->stackaddr_attr) != NULL) {
221                 pattr->guardsize_attr = 0;
222                 pattr->flags |= THR_STACK_USER;
223                 ret = 0;
224         }
225         else
226                 ret = _thr_stack_alloc(pattr);
227         return (ret);
228 }
229
230 static void
231 free_stack(struct pthread *curthread, struct pthread_attr *pattr)
232 {
233         if ((pattr->flags & THR_STACK_USER) == 0) {
234                 THREAD_LIST_LOCK(curthread);
235                 /* Stack routines don't use malloc/free. */
236                 _thr_stack_free(pattr);
237                 THREAD_LIST_UNLOCK(curthread);
238         }
239 }
240
241 static void
242 thread_start(void *arg)
243 {
244         struct pthread *curthread;
245         struct start_arg *start_arg = arg;
246
247         curthread = start_arg->thread;
248         curthread->tid = _thr_get_tid();
249         tls_set_tcb(curthread->tcb);
250         start_arg->started = 1;
251         _thr_umtx_wake(&start_arg->started, 1);
252
253         THR_LOCK(curthread);
254         THR_UNLOCK(curthread);
255
256         /* Thread was created with all signals blocked, unblock them. */
257         __sys_sigprocmask(SIG_SETMASK, &curthread->sigmask, NULL);
258
259         if (curthread->flags & THR_FLAGS_NEED_SUSPEND)
260                 _thr_suspend_check(curthread);
261
262         /* Run the current thread's start routine with argument: */
263         pthread_exit(curthread->start_routine(curthread->arg));
264
265         /* This point should never be reached. */
266         PANIC("Thread has resumed after exit");
267 }