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>
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 *);
#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;
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);
}
#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);
__schedule();
irq_restore(irq);
+ return t;
}