diff --git a/sched/task.c b/sched/task.c index 92011ad..4cd26a5 100644 --- a/sched/task.c +++ b/sched/task.c @@ -17,7 +17,10 @@ static struct object_type task_type = { static struct task *__kernel_task; static struct task *__idle_task; -static spin_lock_t task_list_lock; +static spin_lock_t pid_map_lock = SPIN_LOCK_INIT; +static DECLARE_BITMAP(pid_map, PID_MAX); + +static spin_lock_t task_list_lock = SPIN_LOCK_INIT; static struct btree task_list; BTREE_DEFINE_SIMPLE_GET( @@ -45,6 +48,31 @@ void idle(void) } } +static unsigned int pid_alloc(void) +{ + unsigned long flags; + spin_lock_irqsave(&pid_map_lock, &flags); + + unsigned int pid = bitmap_lowest_clear(pid_map, PID_MAX); + if (pid != BITMAP_NPOS) { + bitmap_set(pid_map, pid); + } + + spin_unlock_irqrestore(&pid_map_lock, flags); + + return pid; +} + +static void pid_free(unsigned int pid) +{ + unsigned long flags; + spin_lock_irqsave(&pid_map_lock, &flags); + + bitmap_clear(pid_map, pid); + + spin_unlock_irqrestore(&pid_map_lock, flags); +} + kern_status_t setup_kernel_task(void) { __kernel_task = task_alloc(); diff --git a/sched/thread.c b/sched/thread.c index accddc7..251423f 100644 --- a/sched/thread.c +++ b/sched/thread.c @@ -6,8 +6,6 @@ #define THREAD_CAST(p) OBJECT_C_CAST(struct thread, thr_base, &thread_type, p) -static DECLARE_BITMAP(thread_ids, THREAD_MAX) = {0}; - static struct object_type thread_type = { .ob_name = "thread", .ob_size = sizeof(struct thread), @@ -33,8 +31,7 @@ struct thread *thread_alloc(void) kern_status_t thread_init(struct thread *thr, uintptr_t ip) { - thr->tr_id = bitmap_lowest_clear(thread_ids, THREAD_MAX); - bitmap_set(thread_ids, thr->tr_id); + thr->tr_id = thr->tr_parent->t_next_thread_id++; thr->tr_prio = PRIO_NORMAL; thr->tr_state = THREAD_READY; @@ -85,8 +82,7 @@ struct thread *create_kernel_thread(void (*fn)(void)) struct task *kernel = kernel_task(); struct thread *thr = thread_alloc(); - thr->tr_id = 1; - bitmap_set(thread_ids, 1); + thr->tr_id = kernel->t_next_thread_id++; thr->tr_parent = kernel; thr->tr_prio = PRIO_NORMAL; @@ -110,8 +106,7 @@ struct thread *create_idle_thread(void) struct task *idle = idle_task(); struct thread *thr = thread_alloc(); - thr->tr_id = 0; - bitmap_set(thread_ids, 0); + thr->tr_id = idle->t_next_thread_id++; thr->tr_parent = idle; thr->tr_prio = PRIO_NORMAL;