summaryrefslogtreecommitdiffstats
path: root/lib/libkse/thread/thr_create.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/libkse/thread/thr_create.c')
-rw-r--r--lib/libkse/thread/thr_create.c91
1 files changed, 56 insertions, 35 deletions
diff --git a/lib/libkse/thread/thr_create.c b/lib/libkse/thread/thr_create.c
index f5631a5..1641ce4 100644
--- a/lib/libkse/thread/thr_create.c
+++ b/lib/libkse/thread/thr_create.c
@@ -29,7 +29,7 @@
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
- * $Id: uthread_create.c,v 1.14 1999/07/05 00:35:17 jasone Exp $
+ * $Id: uthread_create.c,v 1.15 1999/07/06 00:25:36 jasone Exp $
*/
#include <errno.h>
#include <stdlib.h>
@@ -79,64 +79,82 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr,
/* Check if a stack was specified in the thread attributes: */
if ((stack = pattr->stackaddr_attr) != NULL) {
}
-#ifdef __i386__
/* Allocate memory for a default-size stack: */
else if (pattr->stacksize_attr == PTHREAD_STACK_DEFAULT) {
struct stack * spare_stack;
/* Allocate or re-use a default-size stack. */
- /* Use the garbage collector mutex for synchronization
+ /*
+ * Use the garbage collector mutex for synchronization
* of the spare stack list.
- *
- * XXX This may not be ideal. */
+ */
if (pthread_mutex_lock(&_gc_mutex) != 0)
PANIC("Cannot lock gc mutex");
- if (NULL != (spare_stack = SLIST_FIRST(&_stackq))) {
+ if ((spare_stack = SLIST_FIRST(&_stackq)) != NULL) {
/* Use the spare stack. */
SLIST_REMOVE_HEAD(&_stackq, qe);
- stack = sizeof(struct stack) + (void *) spare_stack - PTHREAD_STACK_DEFAULT;
+
+ /* Unlock the garbage collector mutex. */
+ if (pthread_mutex_unlock(&_gc_mutex) != 0)
+ PANIC("Cannot unlock gc mutex");
+
+ stack = sizeof(struct stack)
+ + (void *) spare_stack
+ - PTHREAD_STACK_DEFAULT;
} else {
+ /* Unlock the garbage collector mutex. */
+ if (pthread_mutex_unlock(&_gc_mutex) != 0)
+ PANIC("Cannot unlock gc mutex");
+
/* Allocate a new stack. */
stack = _next_stack + PTHREAD_STACK_GUARD;
- /* Even if stack allocation fails, we don't want to try to use this location again, so unconditionally
- * decrement _next_stack. Under normal operating conditions, the most likely reason for an mmap()
- * error is a stack overflow of the adjacent thread stack. */
- _next_stack -= (PTHREAD_STACK_DEFAULT + PTHREAD_STACK_GUARD);
+ /*
+ * Even if stack allocation fails, we don't want
+ * to try to use this location again, so
+ * unconditionally decrement _next_stack. Under
+ * normal operating conditions, the most likely
+ * reason for an mmap() error is a stack
+ * overflow of the adjacent thread stack.
+ */
+ _next_stack -= (PTHREAD_STACK_DEFAULT
+ + PTHREAD_STACK_GUARD);
/* Red zone: */
- if (MAP_FAILED == mmap(_next_stack, PTHREAD_STACK_GUARD, 0, MAP_ANON, -1, 0)) {
+ if (mmap(_next_stack, PTHREAD_STACK_GUARD, 0,
+ MAP_ANON, -1, 0) == MAP_FAILED) {
ret = EAGAIN;
free(new_thread);
}
/* Stack: */
- else if (MAP_FAILED == mmap(stack, PTHREAD_STACK_DEFAULT, PROT_READ | PROT_WRITE, MAP_STACK, -1, 0)) {
+ else if (mmap(stack,
+ PTHREAD_STACK_DEFAULT,
+ PROT_READ | PROT_WRITE,
+#ifdef __i386__
+ MAP_STACK,
+#else
+ MAP_ANON,
+#endif
+ -1, 0) == MAP_FAILED) {
ret = EAGAIN;
- munmap(_next_stack, PTHREAD_STACK_GUARD);
+ munmap(_next_stack,
+ PTHREAD_STACK_GUARD);
free(new_thread);
}
}
-
- /* Unlock the garbage collector mutex. */
- if (pthread_mutex_unlock(&_gc_mutex) != 0)
- PANIC("Cannot unlock gc mutex");
}
- /* The user wants a stack of a particular size. Lets hope they really know what they want, and simply malloc the
- * stack. */
- else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) {
+ /*
+ * The user wants a stack of a particular size. Lets hope they
+ * really know what they want, and simply malloc the stack.
+ */
+ else if ((stack = (void *) malloc(pattr->stacksize_attr))
+ == NULL) {
/* Insufficient memory to create a thread: */
ret = EAGAIN;
free(new_thread);
}
-#else
- /* Allocate memory for the stack: */
- else if ((stack = (void *) malloc(pattr->stacksize_attr)) == NULL) {
- /* Insufficient memory to create a thread: */
- ret = EAGAIN;
- free(new_thread);
- }
-#endif
+
/* Check for errors: */
if (ret != 0) {
} else {
@@ -211,16 +229,20 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr,
*/
if (new_thread->attr.flags & PTHREAD_INHERIT_SCHED) {
/* Copy the scheduling attributes: */
- new_thread->base_priority = _thread_run->base_priority;
- new_thread->attr.prio = _thread_run->base_priority;
- new_thread->attr.sched_policy = _thread_run->attr.sched_policy;
+ new_thread->base_priority
+ = _thread_run->base_priority;
+ new_thread->attr.prio
+ = _thread_run->base_priority;
+ new_thread->attr.sched_policy
+ = _thread_run->attr.sched_policy;
} else {
/*
* Use just the thread priority, leaving the
* other scheduling attributes as their
* default values:
*/
- new_thread->base_priority = new_thread->attr.prio;
+ new_thread->base_priority
+ = new_thread->attr.prio;
}
new_thread->active_priority = new_thread->base_priority;
new_thread->inherited_priority = 0;
@@ -256,8 +278,7 @@ pthread_create(pthread_t * thread, const pthread_attr_t * attr,
if (pattr->suspend == PTHREAD_CREATE_SUSPENDED) {
new_thread->state = PS_SUSPENDED;
PTHREAD_WAITQ_INSERT(new_thread);
- }
- else {
+ } else {
new_thread->state = PS_RUNNING;
PTHREAD_PRIOQ_INSERT_TAIL(new_thread);
}
OpenPOWER on IntegriCloud