Deleted Added
full compact
thr_create.c (58094) thr_create.c (67097)
1/*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
1/*
2 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright

--- 15 unchanged lines hidden (view full) ---

24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * SUCH DAMAGE.
31 *
32 * $FreeBSD: head/lib/libkse/thread/thr_create.c 58094 2000-03-15 13:59:27Z deischen $
32 * $FreeBSD: head/lib/libkse/thread/thr_create.c 67097 2000-10-13 22:12:32Z deischen $
33 */
34#include <errno.h>
35#include <stdlib.h>
36#include <string.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <stddef.h>
40#include <sys/time.h>
41#include <sys/param.h>
42#include <sys/mman.h>
43#ifdef _THREAD_SAFE
44#include <machine/reg.h>
45#include <pthread.h>
46#include "pthread_private.h"
47#include "libc_private.h"
48
49static u_int64_t next_uniqueid = 1;
50
51#define OFF(f) offsetof(struct pthread, f)
33 */
34#include <errno.h>
35#include <stdlib.h>
36#include <string.h>
37#include <fcntl.h>
38#include <unistd.h>
39#include <stddef.h>
40#include <sys/time.h>
41#include <sys/param.h>
42#include <sys/mman.h>
43#ifdef _THREAD_SAFE
44#include <machine/reg.h>
45#include <pthread.h>
46#include "pthread_private.h"
47#include "libc_private.h"
48
49static u_int64_t next_uniqueid = 1;
50
51#define OFF(f) offsetof(struct pthread, f)
52#define SIGFRAME_OFF(f) offsetof(struct pthread_signal_frame, f)
52int _thread_next_offset = OFF(tle.tqe_next);
53int _thread_uniqueid_offset = OFF(uniqueid);
54int _thread_state_offset = OFF(state);
55int _thread_name_offset = OFF(name);
53int _thread_next_offset = OFF(tle.tqe_next);
54int _thread_uniqueid_offset = OFF(uniqueid);
55int _thread_state_offset = OFF(state);
56int _thread_name_offset = OFF(name);
56int _thread_sig_saved_offset = OFF(sig_saved);
57int _thread_saved_sigcontext_offset = OFF(saved_sigcontext);
58int _thread_saved_jmp_buf_offset = OFF(saved_jmp_buf);
57int _thread_curframe_offset = OFF(curframe);
58int _thread_sigframe_ctx_offset = SIGFRAME_OFF(ctx);
59int _thread_sigframe_ctxtype_offset = SIGFRAME_OFF(ctxtype);
59#undef OFF
60#undef OFF
61#undef SIGFRAME_OFF
60
61int _thread_PS_RUNNING_value = PS_RUNNING;
62int _thread_PS_DEAD_value = PS_DEAD;
62
63int _thread_PS_RUNNING_value = PS_RUNNING;
64int _thread_PS_DEAD_value = PS_DEAD;
65int _thread_CTX_JB_NOSIG_value = CTX_JB_NOSIG;
66int _thread_CTX_JB_value = CTX_JB;
67int _thread_CTX_SJB_value = CTX_SJB;
68int _thread_CTX_UC_value = CTX_UC;
69int _thread_sigframe_size_value = sizeof(struct pthread_signal_frame);
63
64int
65pthread_create(pthread_t * thread, const pthread_attr_t * attr,
66 void *(*start_routine) (void *), void *arg)
67{
68 int f_gc = 0;
69 int ret = 0;
70 pthread_t gc_thread;

--- 86 unchanged lines hidden (view full) ---

157 }
158
159 /* Check for errors: */
160 if (ret != 0) {
161 } else {
162 /* Initialise the thread structure: */
163 memset(new_thread, 0, sizeof(struct pthread));
164 new_thread->slice_usec = -1;
70
71int
72pthread_create(pthread_t * thread, const pthread_attr_t * attr,
73 void *(*start_routine) (void *), void *arg)
74{
75 int f_gc = 0;
76 int ret = 0;
77 pthread_t gc_thread;

--- 86 unchanged lines hidden (view full) ---

164 }
165
166 /* Check for errors: */
167 if (ret != 0) {
168 } else {
169 /* Initialise the thread structure: */
170 memset(new_thread, 0, sizeof(struct pthread));
171 new_thread->slice_usec = -1;
165 new_thread->sig_saved = 0;
166 new_thread->stack = stack;
167 new_thread->start_routine = start_routine;
168 new_thread->arg = arg;
169
170 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
171 PTHREAD_CANCEL_DEFERRED;
172
173 /*
174 * Write a magic value to the thread structure
175 * to help identify valid ones:
176 */
177 new_thread->magic = PTHREAD_MAGIC;
178
179 /* Initialise the thread for signals: */
180 new_thread->sigmask = _thread_run->sigmask;
181
172 new_thread->stack = stack;
173 new_thread->start_routine = start_routine;
174 new_thread->arg = arg;
175
176 new_thread->cancelflags = PTHREAD_CANCEL_ENABLE |
177 PTHREAD_CANCEL_DEFERRED;
178
179 /*
180 * Write a magic value to the thread structure
181 * to help identify valid ones:
182 */
183 new_thread->magic = PTHREAD_MAGIC;
184
185 /* Initialise the thread for signals: */
186 new_thread->sigmask = _thread_run->sigmask;
187
188 /* Initialize the first signal frame: */
189 new_thread->sigframes[0] = &new_thread->sigframe0;
190 new_thread->curframe = &new_thread->sigframe0;
191
182 /* Initialise the jump buffer: */
192 /* Initialise the jump buffer: */
183 setjmp(new_thread->saved_jmp_buf);
193 _setjmp(new_thread->curframe->ctx.jb);
184
185 /*
186 * Set up new stack frame so that it looks like it
187 * returned from a longjmp() to the beginning of
188 * _thread_start().
189 */
194
195 /*
196 * Set up new stack frame so that it looks like it
197 * returned from a longjmp() to the beginning of
198 * _thread_start().
199 */
190#if defined(__FreeBSD__)
191#if defined(__alpha__)
192 new_thread->saved_jmp_buf[0]._jb[2] =
193 (long)_thread_start;
194 new_thread->saved_jmp_buf[0]._jb[4 + R_RA] =
195 0;
196 new_thread->saved_jmp_buf[0]._jb[4 + R_T12] =
197 (long)_thread_start;
198#else
199 new_thread->saved_jmp_buf[0]._jb[0] =
200 (long)_thread_start;
201#endif
202#elif defined(__NetBSD__)
203#if defined(__alpha__)
204 new_thread->saved_jmp_buf[2] = (long)_thread_start;
205 new_thread->saved_jmp_buf[4 + R_RA] = 0;
206 new_thread->saved_jmp_buf[4 + R_T12] =
207 (long)_thread_start;
208#else
209 new_thread->saved_jmp_buf[0] = (long)_thread_start;
210#endif
211#else
212#error "Don't recognize this operating system!"
213#endif
200 SET_RETURN_ADDR_JB(new_thread->curframe->ctx.jb,
201 _thread_start);
214
215 /* The stack starts high and builds down: */
202
203 /* The stack starts high and builds down: */
216#if defined(__FreeBSD__)
217#if defined(__alpha__)
218 new_thread->saved_jmp_buf[0]._jb[4 + R_SP] =
219 (long)new_thread->stack + pattr->stacksize_attr
220 - sizeof(double);
221#else
222 new_thread->saved_jmp_buf[0]._jb[2] =
223 (int)(new_thread->stack + pattr->stacksize_attr -
224 sizeof(double));
225#endif
226#elif defined(__NetBSD__)
227#if defined(__alpha__)
228 new_thread->saved_jmp_buf[4 + R_SP] =
229 (long)new_thread->stack + pattr->stacksize_attr -
230 sizeof(double);
231#else
232 new_thread->saved_jmp_buf[2] = (long)new_thread->stack
233 + pattr->stacksize_attr - sizeof(double);
234#endif
235#else
236#error "Don't recognize this operating system!"
237#endif
204 SET_STACK_JB(new_thread->curframe->ctx.jb,
205 (long)new_thread->stack + pattr->stacksize_attr
206 - sizeof(double));
238
207
208 /* Initialize the rest of the frame: */
209 new_thread->curframe->ctxtype = CTX_JB_NOSIG;
210 /* Set the base of the stack: */
211 new_thread->curframe->stackp =
212 GET_STACK_JB(new_thread->curframe->ctx.jb);
213 new_thread->sigframe_count = 0;
214
239 /* Copy the thread attributes: */
240 memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr));
241
242 /*
243 * Check if this thread is to inherit the scheduling
244 * attributes from its parent:
245 */
246 if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) {
247 /* Copy the scheduling attributes: */
215 /* Copy the thread attributes: */
216 memcpy(&new_thread->attr, pattr, sizeof(struct pthread_attr));
217
218 /*
219 * Check if this thread is to inherit the scheduling
220 * attributes from its parent:
221 */
222 if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) {
223 /* Copy the scheduling attributes: */
248 new_thread->base_priority
249 = _thread_run->base_priority;
250 new_thread->attr.prio
251 = _thread_run->base_priority;
252 new_thread->attr.sched_policy
253 = _thread_run->attr.sched_policy;
224 new_thread->base_priority =
225 _thread_run->base_priority &
226 ~PTHREAD_SIGNAL_PRIORITY;
227 new_thread->attr.prio =
228 _thread_run->base_priority &
229 ~PTHREAD_SIGNAL_PRIORITY;
230 new_thread->attr.sched_policy =
231 _thread_run->attr.sched_policy;
254 } else {
255 /*
256 * Use just the thread priority, leaving the
257 * other scheduling attributes as their
258 * default values:
259 */
232 } else {
233 /*
234 * Use just the thread priority, leaving the
235 * other scheduling attributes as their
236 * default values:
237 */
260 new_thread->base_priority
261 = new_thread->attr.prio;
238 new_thread->base_priority =
239 new_thread->attr.prio;
262 }
263 new_thread->active_priority = new_thread->base_priority;
264 new_thread->inherited_priority = 0;
265
266 /* Initialise the join queue for the new thread: */
267 TAILQ_INIT(&(new_thread->join_queue));
268
269 /* Initialize the mutex queue: */
270 TAILQ_INIT(&new_thread->mutexq);
271
272 /* Initialise hooks in the thread structure: */
273 new_thread->specific_data = NULL;
274 new_thread->cleanup = NULL;
275 new_thread->flags = 0;
276 new_thread->poll_data.nfds = 0;
277 new_thread->poll_data.fds = NULL;
240 }
241 new_thread->active_priority = new_thread->base_priority;
242 new_thread->inherited_priority = 0;
243
244 /* Initialise the join queue for the new thread: */
245 TAILQ_INIT(&(new_thread->join_queue));
246
247 /* Initialize the mutex queue: */
248 TAILQ_INIT(&new_thread->mutexq);
249
250 /* Initialise hooks in the thread structure: */
251 new_thread->specific_data = NULL;
252 new_thread->cleanup = NULL;
253 new_thread->flags = 0;
254 new_thread->poll_data.nfds = 0;
255 new_thread->poll_data.fds = NULL;
278 new_thread->jmpflags = 0;
279 new_thread->continuation = NULL;
280
281 /*
282 * Defer signals to protect the scheduling queues
283 * from access by the signal handler:
284 */
285 _thread_kern_sig_defer();
286

--- 25 unchanged lines hidden (view full) ---

312 */
313 _thread_kern_sig_undefer();
314
315 /* Return a pointer to the thread structure: */
316 (*thread) = new_thread;
317
318 /* Schedule the new user thread: */
319 _thread_kern_sched(NULL);
256 new_thread->continuation = NULL;
257
258 /*
259 * Defer signals to protect the scheduling queues
260 * from access by the signal handler:
261 */
262 _thread_kern_sig_defer();
263

--- 25 unchanged lines hidden (view full) ---

289 */
290 _thread_kern_sig_undefer();
291
292 /* Return a pointer to the thread structure: */
293 (*thread) = new_thread;
294
295 /* Schedule the new user thread: */
296 _thread_kern_sched(NULL);
320
321 /*
322 * Start a garbage collector thread
323 * if necessary.
324 */
325 if (f_gc && pthread_create(&gc_thread,NULL,
326 _thread_gc,NULL) != 0)
327 PANIC("Can't create gc thread");
297 /*
298 * Start a garbage collector thread
299 * if necessary.
300 */
301 if (f_gc && pthread_create(&gc_thread,NULL,
302 _thread_gc,NULL) != 0)
303 PANIC("Can't create gc thread");
304
328 }
329 }
330
331 /* Return the status: */
332 return (ret);
333}
334
335void

--- 12 unchanged lines hidden ---
305 }
306 }
307
308 /* Return the status: */
309 return (ret);
310}
311
312void

--- 12 unchanged lines hidden ---