Deleted Added
full compact
thr_create.c (113661) thr_create.c (113786)
1/*
2 * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
3 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

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

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
31 * SUCH DAMAGE.
32 *
1/*
2 * Copyright (c) 2003 Daniel M. Eischen <deischen@gdeb.com>
3 * Copyright (c) 1995-1998 John Birrell <jb@cimlogic.com.au>
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:

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

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
31 * SUCH DAMAGE.
32 *
33 * $FreeBSD: head/lib/libkse/thread/thr_create.c 113661 2003-04-18 07:09:43Z deischen $
33 * $FreeBSD: head/lib/libkse/thread/thr_create.c 113786 2003-04-21 04:02:56Z deischen $
34 */
35#include <errno.h>
36#include <stdlib.h>
37#include <string.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <stddef.h>
41#include <sys/time.h>

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

98 struct kse_group *kseg = NULL;
99 kse_critical_t crit;
100 int i;
101 int ret = 0;
102
103 if (_thr_initial == NULL)
104 _libpthread_init(NULL);
105
34 */
35#include <errno.h>
36#include <stdlib.h>
37#include <string.h>
38#include <fcntl.h>
39#include <unistd.h>
40#include <stddef.h>
41#include <sys/time.h>

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

98 struct kse_group *kseg = NULL;
99 kse_critical_t crit;
100 int i;
101 int ret = 0;
102
103 if (_thr_initial == NULL)
104 _libpthread_init(NULL);
105
106 crit = _kse_critical_enter();
106 /*
107 * Turn on threaded mode, if failed, it is unnecessary to
108 * do further work.
109 */
110 if (_kse_isthreaded() == 0 && _kse_setthreaded(1)) {
111 return (EAGAIN);
112 }
107 curthread = _get_curthread();
113 curthread = _get_curthread();
108 curkse = curthread->kse;
109
114
110 /* Allocate memory for the thread structure: */
115 /*
116 * Allocate memory for the thread structure.
117 * Some functions use malloc, so don't put it
118 * in a critical region.
119 */
111 if ((new_thread = _thr_alloc(curthread)) == NULL) {
112 /* Insufficient memory to create a thread: */
113 ret = EAGAIN;
114 } else {
115 /* Initialize the thread structure: */
116 memset(new_thread, 0, sizeof(struct pthread));
117
118 /* Check if default thread attributes are required: */

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

130 else if (((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) &&
131 (((kse = _kse_alloc(curthread)) == NULL)
132 || ((kseg = _kseg_alloc(curthread)) == NULL))) {
133 /* Insufficient memory to create a new KSE/KSEG: */
134 ret = EAGAIN;
135 if (kse != NULL)
136 _kse_free(curthread, kse);
137 if ((new_thread->attr.flags & THR_STACK_USER) == 0) {
120 if ((new_thread = _thr_alloc(curthread)) == NULL) {
121 /* Insufficient memory to create a thread: */
122 ret = EAGAIN;
123 } else {
124 /* Initialize the thread structure: */
125 memset(new_thread, 0, sizeof(struct pthread));
126
127 /* Check if default thread attributes are required: */

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

139 else if (((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) != 0) &&
140 (((kse = _kse_alloc(curthread)) == NULL)
141 || ((kseg = _kseg_alloc(curthread)) == NULL))) {
142 /* Insufficient memory to create a new KSE/KSEG: */
143 ret = EAGAIN;
144 if (kse != NULL)
145 _kse_free(curthread, kse);
146 if ((new_thread->attr.flags & THR_STACK_USER) == 0) {
147 crit = _kse_critical_enter();
148 curkse = _get_curkse();
138 KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
149 KSE_LOCK_ACQUIRE(curkse, &_thread_list_lock);
150 /* Stack routines don't use malloc/free. */
139 _thr_stack_free(&new_thread->attr);
140 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
151 _thr_stack_free(&new_thread->attr);
152 KSE_LOCK_RELEASE(curkse, &_thread_list_lock);
153 _kse_critical_leave(crit);
141 }
142 _thr_free(curthread, new_thread);
143 }
144 else {
145 if (kseg != NULL) {
146 /* Add the KSE to the KSEG's list of KSEs. */
147 TAILQ_INSERT_HEAD(&kseg->kg_kseq, kse, k_qe);
148 kse->k_kseg = kseg;

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

164 new_thread->sigmask = curthread->sigmask;
165
166 /* No thread is wanting to join to this one: */
167 new_thread->joiner = NULL;
168
169 /* Initialize the signal frame: */
170 new_thread->curframe = NULL;
171
154 }
155 _thr_free(curthread, new_thread);
156 }
157 else {
158 if (kseg != NULL) {
159 /* Add the KSE to the KSEG's list of KSEs. */
160 TAILQ_INSERT_HEAD(&kseg->kg_kseq, kse, k_qe);
161 kse->k_kseg = kseg;

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

177 new_thread->sigmask = curthread->sigmask;
178
179 /* No thread is wanting to join to this one: */
180 new_thread->joiner = NULL;
181
182 /* Initialize the signal frame: */
183 new_thread->curframe = NULL;
184
172 /* Initialize the machine context: */
185 /*
186 * Initialize the machine context.
187 * Enter a critical region to get consistent context.
188 */
189 crit = _kse_critical_enter();
173 THR_GETCONTEXT(&new_thread->tmbx.tm_context);
190 THR_GETCONTEXT(&new_thread->tmbx.tm_context);
191 _kse_critical_leave(crit);
174 new_thread->tmbx.tm_udata = new_thread;
175 new_thread->tmbx.tm_context.uc_sigmask =
176 new_thread->sigmask;
177 new_thread->tmbx.tm_context.uc_stack.ss_size =
178 new_thread->attr.stacksize_attr;
179 new_thread->tmbx.tm_context.uc_stack.ss_sp =
180 new_thread->attr.stackaddr_attr;
192 new_thread->tmbx.tm_udata = new_thread;
193 new_thread->tmbx.tm_context.uc_sigmask =
194 new_thread->sigmask;
195 new_thread->tmbx.tm_context.uc_stack.ss_size =
196 new_thread->attr.stacksize_attr;
197 new_thread->tmbx.tm_context.uc_stack.ss_sp =
198 new_thread->attr.stackaddr_attr;
181
182 makecontext(&new_thread->tmbx.tm_context,
183 (void (*)(void))thread_start, 4, new_thread,
184 start_routine, arg);
199 makecontext(&new_thread->tmbx.tm_context,
200 (void (*)(void))thread_start, 4, new_thread,
201 start_routine, arg);
185
186 /*
187 * Check if this thread is to inherit the scheduling
188 * attributes from its parent:
189 */
190 if ((new_thread->attr.flags & PTHREAD_INHERIT_SCHED) != 0) {
202 /*
203 * Check if this thread is to inherit the scheduling
204 * attributes from its parent:
205 */
206 if ((new_thread->attr.flags & PTHREAD_INHERIT_SCHED) != 0) {
191 /* Copy the scheduling attributes: */
207 /*
208 * Copy the scheduling attributes.
209 * Lock the scheduling lock to get consistent
210 * scheduling parameters.
211 */
212 THR_SCHED_LOCK(curthread, curthread);
192 new_thread->base_priority =
193 curthread->base_priority &
194 ~THR_SIGNAL_PRIORITY;
195 new_thread->attr.prio =
196 curthread->base_priority &
197 ~THR_SIGNAL_PRIORITY;
198 new_thread->attr.sched_policy =
199 curthread->attr.sched_policy;
213 new_thread->base_priority =
214 curthread->base_priority &
215 ~THR_SIGNAL_PRIORITY;
216 new_thread->attr.prio =
217 curthread->base_priority &
218 ~THR_SIGNAL_PRIORITY;
219 new_thread->attr.sched_policy =
220 curthread->attr.sched_policy;
221 THR_SCHED_UNLOCK(curthread, curthread);
200 } else {
201 /*
202 * Use just the thread priority, leaving the
203 * other scheduling attributes as their
204 * default values:
205 */
206 new_thread->base_priority =
207 new_thread->attr.prio;
208 }
209 new_thread->active_priority = new_thread->base_priority;
210 new_thread->inherited_priority = 0;
211
212 /* Initialize the mutex queue: */
213 TAILQ_INIT(&new_thread->mutexq);
214
222 } else {
223 /*
224 * Use just the thread priority, leaving the
225 * other scheduling attributes as their
226 * default values:
227 */
228 new_thread->base_priority =
229 new_thread->attr.prio;
230 }
231 new_thread->active_priority = new_thread->base_priority;
232 new_thread->inherited_priority = 0;
233
234 /* Initialize the mutex queue: */
235 TAILQ_INIT(&new_thread->mutexq);
236
215 /* Initialize thread locking. */
237 /*
238 * Initialize thread locking.
239 * Lock initializing needs malloc, so don't
240 * enter critical region before doing this!
241 */
216 if (_lock_init(&new_thread->lock, LCK_ADAPTIVE,
217 _thr_lock_wait, _thr_lock_wakeup) != 0)
218 PANIC("Cannot initialize thread lock");
219 for (i = 0; i < MAX_THR_LOCKLEVEL; i++) {
220 _lockuser_init(&new_thread->lockusers[i],
221 (void *)new_thread);
222 _LCK_SET_PRIVATE2(&new_thread->lockusers[i],
223 (void *)new_thread);

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

240 * off the main process kseg.
241 */
242 if ((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) == 0) {
243 new_thread->kseg = _kse_initial->k_kseg;
244 new_thread->kse = _kse_initial;
245 }
246 else {
247 kse->k_curthread = NULL;
242 if (_lock_init(&new_thread->lock, LCK_ADAPTIVE,
243 _thr_lock_wait, _thr_lock_wakeup) != 0)
244 PANIC("Cannot initialize thread lock");
245 for (i = 0; i < MAX_THR_LOCKLEVEL; i++) {
246 _lockuser_init(&new_thread->lockusers[i],
247 (void *)new_thread);
248 _LCK_SET_PRIVATE2(&new_thread->lockusers[i],
249 (void *)new_thread);

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

266 * off the main process kseg.
267 */
268 if ((new_thread->attr.flags & PTHREAD_SCOPE_SYSTEM) == 0) {
269 new_thread->kseg = _kse_initial->k_kseg;
270 new_thread->kse = _kse_initial;
271 }
272 else {
273 kse->k_curthread = NULL;
274#ifdef NOT_YET
248 kse->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
275 kse->k_kseg->kg_flags |= KGF_SINGLE_THREAD;
276#endif
249 new_thread->kse = kse;
250 new_thread->kseg = kse->k_kseg;
251 kse->k_mbx.km_udata = kse;
252 kse->k_mbx.km_curthread = NULL;
253 }
277 new_thread->kse = kse;
278 new_thread->kseg = kse->k_kseg;
279 kse->k_mbx.km_udata = kse;
280 kse->k_mbx.km_curthread = NULL;
281 }
254 KSE_LOCK_ACQUIRE(curthread->kse, &_thread_list_lock);
255
282
283 crit = _kse_critical_enter();
284 KSE_LOCK_ACQUIRE(curthread->kse, &_thread_list_lock);
256 /*
257 * Initialise the unique id which GDB uses to
258 * track threads.
259 */
260 new_thread->uniqueid = next_uniqueid++;
285 /*
286 * Initialise the unique id which GDB uses to
287 * track threads.
288 */
289 new_thread->uniqueid = next_uniqueid++;
261
262 /* Add the thread to the linked list of all threads: */
263 THR_LIST_ADD(new_thread);
290 /* Add the thread to the linked list of all threads: */
291 THR_LIST_ADD(new_thread);
264
265 KSE_LOCK_RELEASE(curthread->kse, &_thread_list_lock);
266
267 /*
268 * Schedule the new thread starting a new KSEG/KSE
269 * pair if necessary.
270 */
271 _thr_schedule_add(curthread, new_thread);
292 KSE_LOCK_RELEASE(curthread->kse, &_thread_list_lock);
293
294 /*
295 * Schedule the new thread starting a new KSEG/KSE
296 * pair if necessary.
297 */
298 _thr_schedule_add(curthread, new_thread);
299 _kse_critical_leave(crit);
272
273 /* Return a pointer to the thread structure: */
274 (*thread) = new_thread;
275 }
276 }
300
301 /* Return a pointer to the thread structure: */
302 (*thread) = new_thread;
303 }
304 }
277 _kse_critical_leave(crit);
278
305
279 if ((ret == 0) && (_kse_isthreaded() == 0))
280 _kse_setthreaded(1);
281
282 /* Return the status: */
283 return (ret);
284}
285
286static int
287create_stack(struct pthread_attr *pattr)
288{
289 int ret;

--- 23 unchanged lines hidden ---
306 /* Return the status: */
307 return (ret);
308}
309
310static int
311create_stack(struct pthread_attr *pattr)
312{
313 int ret;

--- 23 unchanged lines hidden ---