core: thread: have start_thread() allocate memory dynamically
authorH. Peter Anvin <hpa@zytor.com>
Wed, 9 Sep 2009 16:07:58 +0000 (09:07 -0700)
committerEric W. Biederman <ebiederm@xmission.com>
Fri, 8 Apr 2011 21:41:15 +0000 (14:41 -0700)
Have start_thread() allocate memory dynamically, using malloc().
XXX: should probably free that memory in __exit_thread()... could be
"interesting".

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
core/include/thread.h
core/thread/idle_thread.c
core/thread/start_thread.c

index 917c36a..514c30b 100644 (file)
@@ -73,8 +73,8 @@ static inline void irq_restore(irq_state_t __st)
     asm volatile("pushl %0 ; popfl" : : "rm" (__st));
 }
 
-void start_thread(struct thread *t, void *stack, size_t stack_size, int prio,
-                 void (*start_func)(void *), void *func_arg);
+struct thread *start_thread(size_t stack_size, int prio,
+                           void (*start_func)(void *), void *func_arg);
 void __exit_thread(void);
 void kill_thread(struct thread *);
 
index 8a319ff..5c79e31 100644 (file)
@@ -2,10 +2,6 @@
 #include <limits.h>
 #include <sys/cpu.h>
 
-static struct thread idle_thread;
-
-static char idle_thread_stack[4096];
-
 static void idle_thread_func(void *dummy)
 {
     (void)dummy;
@@ -19,7 +15,6 @@ static void idle_thread_func(void *dummy)
 
 void start_idle_thread(void)
 {
-    start_thread(&idle_thread, idle_thread_stack, sizeof idle_thread_stack,
-                INT_MAX, idle_thread_func, NULL);
+    start_thread(4096, INT_MAX, idle_thread_func, NULL);
 }
 
index f07984f..afe7ecf 100644 (file)
@@ -1,13 +1,23 @@
 #include <string.h>
+#include <stdlib.h>
 #include "thread.h"
 
 extern void (*__start_thread)(void);
 
-void start_thread(struct thread *t, void *stack, size_t stack_size, int prio,
-                 void (*start_func)(void *), void *func_arg)
+struct thread *start_thread(size_t stack_size, int prio,
+                           void (*start_func)(void *), void *func_arg)
 {
     irq_state_t irq;
-    struct thread *curr;
+    struct thread *curr, *t;
+    char *stack;
+    const size_t thread_mask = __alignof__(struct thread)-1;
+
+    stack_size = (stack_size + thread_mask) & ~thread_mask;
+    stack = malloc(stack_size + sizeof(struct thread));
+    if (!stack)
+       return NULL;
+
+    t = (struct thread *)(stack + stack_size);
 
     memset(t, 0, sizeof *t);
 
@@ -30,4 +40,5 @@ void start_thread(struct thread *t, void *stack, size_t stack_size, int prio,
     __schedule();
 
     irq_restore(irq);
+    return t;
 }