change task module for XiZi_AIoT
This commit is contained in:
@@ -1,91 +1,135 @@
|
||||
/*
|
||||
* Copyright (c) 2020 AIIT XUOS Lab
|
||||
* XiUOS is licensed under Mulan PSL v2.
|
||||
* You can use this software according to the terms and conditions of the Mulan PSL v2.
|
||||
* You may obtain a copy of Mulan PSL v2 at:
|
||||
* http://license.coscl.org.cn/MulanPSL2
|
||||
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
|
||||
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
|
||||
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
|
||||
* See the Mulan PSL v2 for more details.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file: task.h
|
||||
* @brief: file task.h
|
||||
* @version: 1.0
|
||||
* @author: AIIT XUOS Lab
|
||||
* @date: 2023/05/18
|
||||
*
|
||||
*/
|
||||
|
||||
#ifndef __TASK_H__
|
||||
#define __TASK_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
// 定义线程状态
|
||||
// 任务状态枚举类型
|
||||
typedef enum {
|
||||
THREAD_READY, // 就绪状态
|
||||
THREAD_RUNNING, // 运行状态
|
||||
THREAD_BLOCKED, // 阻塞状态
|
||||
THREAD_TERMINATED // 终止状态
|
||||
} task_state_t;
|
||||
TASK_CREATED, // 初始状态
|
||||
TASK_READY, // 就绪状态
|
||||
TASK_RUNNING, // 运行状态
|
||||
TASK_BLOCKED, // 阻塞状态
|
||||
} task_state_t;
|
||||
|
||||
|
||||
// 定义线程控制块(TCB)
|
||||
// 任务控制块结构体
|
||||
typedef struct tcb {
|
||||
uint32_t *stack_ptr; // 任务堆栈指针
|
||||
uint32_t *stack_bottom; // 任务堆栈底部指针
|
||||
uint32_t stack_size; // 任务堆栈大小
|
||||
task_state_t state; // 任务状态
|
||||
int priority; // 任务优先级
|
||||
struct tcb *next; // 指向下一个任务控制块的指针
|
||||
struct message_queue *message_queue; // 消息队列
|
||||
} tcb_t;
|
||||
|
||||
|
||||
// 消息结构体
|
||||
typedef struct message {
|
||||
int type; // 消息类型
|
||||
void *data; // 消息数据指针
|
||||
} message_t;
|
||||
|
||||
|
||||
// 消息队列结构体
|
||||
typedef struct message_queue {
|
||||
message_t *buffer; // 消息缓冲区
|
||||
int capacity; // 缓冲区容量
|
||||
int count; // 当前缓冲区中的消息数量
|
||||
int head; // 队头指针
|
||||
int tail; // 队尾指针
|
||||
semaphore_t *semaphore; // 信号量,用于实现消息队列的同步机制
|
||||
} message_queue_t;
|
||||
|
||||
|
||||
// 信号量结构体
|
||||
typedef struct semaphore {
|
||||
int count; // 计数器
|
||||
tcb_t *wait_list_head; // 指向等待信号量的任务控制块的指针
|
||||
mutex_t mutex; // 互斥锁,用于保护信号量和等待队列的访问
|
||||
} semaphore_t;
|
||||
|
||||
|
||||
// 互斥锁结构体
|
||||
typedef struct mutex {
|
||||
int lock; // 锁标志
|
||||
} mutex_t;
|
||||
|
||||
|
||||
// 上下文结构体
|
||||
typedef struct {
|
||||
int id; // 任务ID
|
||||
int priority; // 任务优先级
|
||||
task_state_t state; // 任务状态
|
||||
uint32_t* stack_ptr; // 任务堆栈指针
|
||||
size_t stack_size; // 任务堆栈大小
|
||||
uint32_t *stack_bottom; // 任务堆栈底部
|
||||
void (*entry_point)(void); // 任务入口函数
|
||||
void *arg; // 任务参数
|
||||
uint16_t cpu_id; // 任务所在的CPU ID
|
||||
} tcb;
|
||||
uint32_t cpsr; // 控制寄存器
|
||||
uint32_t pc; // 程序计数器
|
||||
uint32_t r0; // 寄存器 R0
|
||||
uint32_t r1; // 寄存器 R1
|
||||
uint32_t r2; // 寄存器 R2
|
||||
uint32_t r3; // 寄存器 R3
|
||||
uint32_t r4; // 寄存器 R4
|
||||
uint32_t r5; // 寄存器 R5
|
||||
uint32_t r6; // 寄存器 R6
|
||||
uint32_t r7; // 寄存器 R7
|
||||
uint32_t r8; // 寄存器 R8
|
||||
uint32_t r9; // 寄存器 R9
|
||||
uint32_t r10; // 寄存器 R10
|
||||
uint32_t r11; // 寄存器 R11
|
||||
uint32_t r12; // 寄存器 R12
|
||||
uint32_t sp; // 栈指针
|
||||
uint32_t lr; // 链接寄存器
|
||||
} context_t;
|
||||
|
||||
// 定义任务调度器
|
||||
typedef struct {
|
||||
int num_cpus; // CPU数量
|
||||
int num_tasks_per_cpu; // 每个CPU的任务数量
|
||||
tcb **ready_queue; // 就绪队列
|
||||
tcb *current_task; // 当前正在运行的任务
|
||||
uint16_t current_cpu_id; // 当前CPU的ID
|
||||
uint32_t *interrupt_stack_ptr; // 中断堆栈指针
|
||||
} scheduler_t;
|
||||
|
||||
// 初始化任务调度器
|
||||
scheduler_t *scheduler_init(int num_cpus, int num_tasks_per_cpu);
|
||||
|
||||
// 创建任务
|
||||
int create_task(void (*func)(void *), void *arg, int priority, size_t stack_size);
|
||||
|
||||
// 销毁任务
|
||||
int destroy_task(int task_id);
|
||||
|
||||
// 挂起任务
|
||||
int suspend_task(int task_id);
|
||||
|
||||
// 恢复任务
|
||||
int resume_task(int task_id);
|
||||
|
||||
// 调整任务优先级
|
||||
int set_task_priority(int task_id, int priority);
|
||||
|
||||
// 获取当前任务的ID
|
||||
int get_current_task_id();
|
||||
|
||||
// 获取当前CPU的ID
|
||||
uint16_t get_current_cpu_id;
|
||||
|
||||
// 获取任务控制块
|
||||
tcb *get_task_by_id(int task_id);
|
||||
|
||||
// 生成任务ID
|
||||
int generate_task_id();
|
||||
|
||||
// 切换到下一个任务
|
||||
void switch_to_next_task();
|
||||
|
||||
// 添加任务到就绪队列
|
||||
void add_tcbo_ready_queue(tcb *task);
|
||||
|
||||
// 从就绪队列中移除任务
|
||||
void remove_task_from_ready_queue(tcb *task);
|
||||
|
||||
// 初始化任务堆栈
|
||||
void init_stack(uint32_t *stack_ptr, size_t stack_size, void (*func)(void *), void *arg,tcb* task);
|
||||
|
||||
// 空闲任务的入口函数
|
||||
void idle_task(void *arg);
|
||||
|
||||
// 初始化空闲任务
|
||||
void init_idle_task();
|
||||
void tcb_init(tcb_t *tcb, int priority, int stack_size);
|
||||
void tcb_destroy(tcb_t *tcb);
|
||||
tcb_t *get_current_task(void);
|
||||
tcb_t *get_local_run_queue_head(int cpu_id);
|
||||
void add_to_global_run_queue(tcb_t *tcb);
|
||||
tcb_t *take_from_global_run_queue(void);
|
||||
void add_to_local_run_queue(tcb_t *tcb, int cpu_id);
|
||||
tcb_t *take_from_local_run_queue(int cpu_id);
|
||||
void move_current_task_to_global_run_queue(void);
|
||||
void switch_to_next_task(void);
|
||||
void task_init(tcb_t *tcb);
|
||||
tcb_t *create_task(int priority, int stack_size);
|
||||
void destroy_task(tcb_t *tcb);
|
||||
void send_message(tcb_t *dest_tcb, int type, void *data);
|
||||
void receive_message(int type, void *data);
|
||||
message_queue_t *message_queue_create(int capacity);
|
||||
void message_queue_destroy(message_queue_t *mq);
|
||||
int message_queue_push(message_queue_t *mq, message_t *message);
|
||||
int message_queue_pop(message_queue_t *mq, message_t *message);
|
||||
int message_queue_push(message_queue_t *mq, message_t *message);
|
||||
void context_init(context_t *context, void (*entry)(void), void *stack_ptr);
|
||||
void wait_for_interrupt(void);
|
||||
int get_cpu_id(void);
|
||||
void *get_stack_pointer(void);
|
||||
void set_stack_pointer(void *sp);
|
||||
void add_interrupt_handler(int irq, void (*handler)(void), int priority);
|
||||
void enter_critical(void);
|
||||
void leave_critical(void);
|
||||
void task_exit(void);
|
||||
void task_entry(void);
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user