sched: allocate and assign ids to each thread
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
#ifndef SOCKS_SCHED_H_
|
||||
#define SOCKS_SCHED_H_
|
||||
|
||||
#include <socks/pmap.h>
|
||||
#include <socks/locks.h>
|
||||
#include <socks/queue.h>
|
||||
#include <socks/object.h>
|
||||
#include <socks/btree.h>
|
||||
#include <socks/locks.h>
|
||||
#include <socks/object.h>
|
||||
#include <socks/pmap.h>
|
||||
#include <socks/queue.h>
|
||||
#include <socks/status.h>
|
||||
|
||||
#define TASK_NAME_MAX 64
|
||||
#define PRIO_MAX 32
|
||||
#define THREAD_KSTACK_ORDER VM_PAGE_4K
|
||||
#define THREAD_MAX 65536
|
||||
|
||||
#define wait_event(wq, cond) \
|
||||
({ \
|
||||
@@ -123,7 +124,7 @@ struct timer {
|
||||
struct cpu_data *t_cpu;
|
||||
struct thread *t_owner;
|
||||
unsigned long t_expiry;
|
||||
void(*t_callback)(struct timer *);
|
||||
void (*t_callback)(struct timer *);
|
||||
};
|
||||
|
||||
struct wait_item {
|
||||
@@ -136,7 +137,7 @@ struct waitqueue {
|
||||
spin_lock_t wq_lock;
|
||||
};
|
||||
|
||||
typedef void(*work_func_t)(struct work_item *);
|
||||
typedef void (*work_func_t)(struct work_item *);
|
||||
|
||||
struct work_item {
|
||||
void *w_data;
|
||||
@@ -174,8 +175,14 @@ extern void rq_remove_thread(struct runqueue *rq, struct thread *thr);
|
||||
extern struct runqueue *cpu_rq(unsigned int cpu);
|
||||
|
||||
extern struct task *task_alloc(void);
|
||||
static inline struct task *task_ref(struct task *task) { return OBJECT_CAST(struct task, t_base, object_ref(&task->t_base)); }
|
||||
static inline void task_deref(struct task *task) { object_deref(&task->t_base); }
|
||||
static inline struct task *task_ref(struct task *task)
|
||||
{
|
||||
return OBJECT_CAST(struct task, t_base, object_ref(&task->t_base));
|
||||
}
|
||||
static inline void task_deref(struct task *task)
|
||||
{
|
||||
object_deref(&task->t_base);
|
||||
}
|
||||
extern struct task *task_from_pid(unsigned int pid);
|
||||
extern struct task *kernel_task(void);
|
||||
extern struct task *idle_task(void);
|
||||
@@ -196,7 +203,9 @@ static inline void task_lock_irqsave(struct task *task, unsigned long *flags)
|
||||
object_lock_irqsave(&task->t_base, flags);
|
||||
}
|
||||
|
||||
static inline void task_unlock_irqrestore(struct task *task, unsigned long flags)
|
||||
static inline void task_unlock_irqrestore(
|
||||
struct task *task,
|
||||
unsigned long flags)
|
||||
{
|
||||
object_unlock_irqrestore(&task->t_base, flags);
|
||||
}
|
||||
@@ -205,7 +214,7 @@ extern struct thread *thread_alloc(void);
|
||||
extern kern_status_t thread_init(struct thread *thr, uintptr_t ip);
|
||||
extern int thread_priority(struct thread *thr);
|
||||
extern void idle(void);
|
||||
extern struct thread *create_kernel_thread(void(*fn)(void));
|
||||
extern struct thread *create_kernel_thread(void (*fn)(void));
|
||||
extern struct thread *create_idle_thread(void);
|
||||
|
||||
extern void add_timer(struct timer *timer);
|
||||
|
||||
@@ -1,10 +1,13 @@
|
||||
#include <socks/sched.h>
|
||||
#include <socks/object.h>
|
||||
#include <socks/bitmap.h>
|
||||
#include <socks/cpu.h>
|
||||
#include <socks/machine/thread.h>
|
||||
#include <socks/object.h>
|
||||
#include <socks/sched.h>
|
||||
|
||||
#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),
|
||||
@@ -30,6 +33,9 @@ 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_prio = PRIO_NORMAL;
|
||||
thr->tr_state = THREAD_READY;
|
||||
thr->tr_quantum_target = default_quantum();
|
||||
@@ -39,7 +45,8 @@ kern_status_t thread_init(struct thread *thr, uintptr_t ip)
|
||||
return KERN_NO_MEMORY;
|
||||
}
|
||||
|
||||
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack) + vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
|
||||
thr->tr_sp = (uintptr_t)vm_page_get_vaddr(thr->tr_kstack)
|
||||
+ vm_page_order_to_bytes(THREAD_KSTACK_ORDER);
|
||||
thr->tr_bp = thr->tr_sp;
|
||||
|
||||
prepare_stack(ip, &thr->tr_sp);
|
||||
@@ -49,7 +56,6 @@ kern_status_t thread_init(struct thread *thr, uintptr_t ip)
|
||||
|
||||
void thread_free(struct thread *thr)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
struct thread *current_thread(void)
|
||||
@@ -74,11 +80,14 @@ int thread_priority(struct thread *thr)
|
||||
return thr->tr_prio;
|
||||
}
|
||||
|
||||
struct thread *create_kernel_thread(void(*fn)(void))
|
||||
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_parent = kernel;
|
||||
thr->tr_prio = PRIO_NORMAL;
|
||||
thr->tr_state = THREAD_READY;
|
||||
@@ -101,6 +110,9 @@ 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_parent = idle;
|
||||
thr->tr_prio = PRIO_NORMAL;
|
||||
thr->tr_state = THREAD_READY;
|
||||
|
||||
Reference in New Issue
Block a user