sched: allocate and assign ids to each thread

This commit is contained in:
2024-09-17 17:49:05 +01:00
parent d29b955ee8
commit ef05233dcf
2 changed files with 61 additions and 40 deletions

View File

@@ -1,30 +1,31 @@
#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 TASK_NAME_MAX 64
#define PRIO_MAX 32
#define THREAD_KSTACK_ORDER VM_PAGE_4K
#define THREAD_MAX 65536
#define wait_event(wq, cond) \
({ \
struct thread *self = current_thread(); \
struct wait_item waiter; \
wait_item_init(&waiter, self); \
for (;;) { \
thread_wait_begin(&waiter, wq); \
if (cond) { \
break; \
} \
schedule(SCHED_NORMAL); \
} \
thread_wait_end(&waiter, wq); \
#define wait_event(wq, cond) \
({ \
struct thread *self = current_thread(); \
struct wait_item waiter; \
wait_item_init(&waiter, self); \
for (;;) { \
thread_wait_begin(&waiter, wq); \
if (cond) { \
break; \
} \
schedule(SCHED_NORMAL); \
} \
thread_wait_end(&waiter, wq); \
})
#ifdef __cplusplus
@@ -40,34 +41,34 @@ enum task_state {
};
enum thread_state {
THREAD_READY = 1,
THREAD_READY = 1,
THREAD_SLEEPING = 2,
THREAD_STOPPED = 3,
THREAD_STOPPED = 3,
};
enum thread_flags {
THREAD_F_NEED_RESCHED = 0x01u,
THREAD_F_NO_PREEMPT = 0x02u,
THREAD_F_NO_PREEMPT = 0x02u,
};
enum sched_priority {
PRIO_IDLE = 4,
PRIO_SUBNORMAL = 6,
PRIO_NORMAL = 10,
PRIO_IDLE = 4,
PRIO_SUBNORMAL = 6,
PRIO_NORMAL = 10,
PRIO_SUPERNORMAL = 14,
PRIO_HIGH = 18,
PRIO_REALTIME = 24,
PRIO_HIGH = 18,
PRIO_REALTIME = 24,
};
enum sched_mode {
/* used when calling from non-interrupt context.
threads that aren't in state THREAD_READY are
removed from the runqueue. */
SCHED_NORMAL = 0,
SCHED_NORMAL = 0,
/* used when calling from interrupt context.
threads that aren't in state THREAD_READY are
still added to the runqueue. */
SCHED_IRQ = 1,
SCHED_IRQ = 1,
};
struct task {
@@ -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);