Add schedule node midway

This commit is contained in:
TXuian
2024-12-24 01:01:54 +08:00
parent 7639937678
commit 21304531a5
21 changed files with 269 additions and 237 deletions
@@ -41,7 +41,7 @@ struct ksemaphore {
sem_id_t id;
sem_val_t val;
/* list of waiting threads */
struct double_list_node wait_list_guard;
RbtTree wait_thd_tree;
/* list to manage semaphores */
/// @todo Use RB-Tree to manage all semaphores
struct double_list_node sem_list_node;
@@ -5,6 +5,13 @@
#include "actracer.h"
#define RBTTREE_INSERT_SECC 0
#define RBTTREE_INSERT_FAILED -1
#define RBTTREE_INSERT_EXISTED -2
#define RBTTREE_DELETE_SUCC 0
#define RBTTREE_DELETE_FAILED -1
// CLRS
// Insertion and Deletion in a Red Black Tree
enum rbt_type {
@@ -26,10 +33,13 @@ typedef struct RbtTree {
int nr_ele;
} RbtTree;
typedef void(rbt_traverse_fn)(RbtNode* node);
void rbtree_init(RbtTree* tree);
int rbt_insert(RbtTree* tree, uintptr_t key, void* data);
RbtNode* rbt_search(RbtTree* tree, uintptr_t key);
int rbt_delete(RbtTree* tree, uintptr_t key);
void rbt_traverse(RbtTree* tree, rbt_traverse_fn fn);
void module_rbt_factory_init(TraceTag* _softkernel_tag);
@@ -2,20 +2,52 @@
#pragma once
#include "actracer.h"
#include "ksemaphore.h"
#include "rbtree.h"
#define TASK_MAX_PRIORITY 32
#define UNINIT_SNODE_ID 0
typedef uintptr_t snode_id_t;
enum ThreadState {
INIT = 0,
READY,
RUNNING,
DEAD,
BLOCKED,
SLEEPING,
NEVER_RUN,
NR_STATE,
};
typedef struct ScheduleContext {
intptr_t remain_tick;
} ScheduleContext;
typedef struct TaskSleepContext {
int64_t remain_ms;
} TaskSleepContext;
struct ScheduleNode {
TraceTag task_ref;
struct double_list_node list_node;
struct Thread* pthd;
snode_id_t snode_id;
enum ThreadState state;
ScheduleContext sched_context;
TaskSleepContext sleep_context;
};
struct Scheduler {
TraceTag tag;
struct double_list_node task_list_head[TASK_MAX_PRIORITY]; /* list of task control blocks that are allocated */
struct double_list_node task_running_list_head;
struct double_list_node task_blocked_list_head;
struct double_list_node task_sleep_list_head;
RbtTree snode_state_pool[NR_STATE];
struct XiziSemaphorePool semaphore_pool;
};
};
extern struct Scheduler g_scheduler;
bool init_schedule_node(struct ScheduleNode* snode, struct Thread* bind_thd);
bool task_trans_sched_state(struct ScheduleNode* snode, RbtTree* from_pool, RbtTree* to_pool, enum ThreadState target_state);
void task_block(struct Thread* thd);
void task_dead(struct Thread* thd);
void task_yield(struct Thread* thd);
void task_into_ready(struct Thread* thd);
+9 -26
View File
@@ -41,22 +41,14 @@ Modification:
#include "share_page.h"
#include "spinlock.h"
#include "scheduler.h"
#define TASK_CLOCK_TICK 50
#define TASK_MAX_PRIORITY 32
#define TASK_DEFAULT_PRIORITY 2
#define TASK_NAME_MAX_LEN 16
#define SLEEP_MONITOR_CORE 0
enum ProcState {
INIT = 0,
READY,
RUNNING,
DEAD,
BLOCKED,
SLEEPING,
NEVER_RUN,
};
/* Thread Control Block */
struct ThreadContext {
struct Thread* task; // process of current thread
@@ -75,10 +67,6 @@ struct ThreadContext {
struct trapframe* trapframe;
};
struct TaskSleepContext {
int64_t remain_ms;
};
/* Process Control Block */
struct Thread {
/* task name */
@@ -107,12 +95,13 @@ struct Thread {
bool advance_unblock; // @todo abandon
/* task schedule attributes */
struct double_list_node node;
struct TaskSleepContext sleep_context;
enum ProcState state;
int priority; // priority
int remain_tick;
int maxium_tick;
// struct double_list_node node;
// struct TaskSleepContext sleep_context;
// enum ThreadState state;
// int priority; // priority
// int remain_tick;
// int maxium_tick;
struct ScheduleNode snode;
};
struct SchedulerRightGroup {
@@ -157,9 +146,6 @@ struct XiziTaskManager {
/* init task manager */
void (*init)();
/* init a task control block, set name, remain_tick, state, cwd, priority, etc. */
void (*task_set_default_schedule_attr)(struct Thread*);
/* use by task_scheduler, find next READY task, should be in locked */
struct Thread* (*next_runnable_task)(void);
/* function that's runing by kernel thread context, schedule use tasks */
@@ -168,9 +154,6 @@ struct XiziTaskManager {
/* handle task state */
/* call to yield current use task */
void (*task_yield_noschedule)(struct Thread* task, bool is_blocking);
/* block and unblock task */
void (*task_block)(struct double_list_node* head, struct Thread* task);
void (*task_unblock)(struct Thread* task);
/* set task priority */
void (*set_cur_task_priority)(int priority);
};