完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
#include
#include #include #include #include "timeslice.h" //5个任务 TimesilceTaskObj task_1, task_2, task_3, task_4, task_5; //具体操作 void task1_hdl() { printf(">> task 1 is running ...n"); } void task2_hdl() { printf(">> task 2 is running ...n"); } void task3_hdl() { printf(">> task 3 is running ...n"); } void task4_hdl() { printf(">> task 4 is running ...n"); } void task5_hdl() { printf(">> task 5 is running ...n"); } // 初始化任务对象,并且将任务添加到时间片轮询调度中 void task_init() { timeslice_task_init(&task_1, task1_hdl, 1, 10); timeslice_task_init(&task_2, task2_hdl, 2, 20); timeslice_task_init(&task_3, task3_hdl, 3, 30); timeslice_task_init(&task_4, task4_hdl, 4, 40); timeslice_task_init(&task_5, task5_hdl, 5, 50); timeslice_task_add(&task_1); timeslice_task_add(&task_2); timeslice_task_add(&task_3); timeslice_task_add(&task_4); timeslice_task_add(&task_5); } // 开两个线程模拟在单片机上的运行过程 void timeslice_exec_thread() { while (true) { timeslice_exec(); } } void timeslice_tick_thread() { while (true) { timeslice_tick(); Sleep(10); } } int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); task_init(); printf(">> task num: %dn", timeslice_get_task_num()); printf(">> task len: %dn", timeslice_get_task_timeslice_len(&task_3)); timeslice_task_del(&task_2); printf(">> delet task 2n"); printf(">> task 2 is exist: %dn", timeslice_task_isexist(&task_2)); printf(">> task num: %dn", timeslice_get_task_num()); timeslice_task_del(&task_5); printf(">> delet task 5n"); printf(">> task num: %dn", timeslice_get_task_num()); printf(">> task 3 is exist: %dn", timeslice_task_isexist(&task_3)); timeslice_task_add(&task_2); printf(">> add task 2n"); printf(">> task 2 is exist: %dn", timeslice_task_isexist(&task_2)); timeslice_task_add(&task_5); printf(">> add task 5n"); printf(">> task num: %dn", timeslice_get_task_num()); printf("nn========timeslice running===========n"); std::thread thread_1(timeslice_exec_thread); std::thread thread_2(timeslice_tick_thread); thread_1.join(); thread_2.join(); return a.exec(); } #include "list.h" //初始化节点,将list节点的前继节点和后继节点都指向list本身 void list_init(ListObj* list) { list->next = list->prev = list; } void list_replace(ListObj*old,ListObj*newadd) { newadd->next = old->next; newadd->next->prev = newadd; newadd->prev = old->prev; newadd->prev->next = newadd; } void list_insert_after(ListObj* list, ListObj* node) { list->next->prev = node; node->next = list->next; list->next = node; node->prev = list; } void list_insert_before(ListObj* list, ListObj* node) { list->prev->next = node; node->prev = list->prev; list->prev = node; node->next = list; } void list_remove(ListObj* node) { node->next->prev = node->prev; node->prev->next = node->next; node->next = node->prev = node; } //判断是否为空 表头的后继节点是否是表头本身 int list_isempty(const ListObj* list) { return list->next == list; } unsigned int list_len(const ListObj* list) { unsigned int len = 0; const ListObj* p = list; while (p->next != list) { p = p->next; len++; } return len; } #ifndef _LIST_H #define _LIST_H // 获得结构体(TYPE)的变量成员(MEMBER)在此结构体中的偏移量。 #define offset_of(type, member) (unsigned long) &((type*)0)->member // 根据"结构体(type)变量"中的"域成员变量(member)的指针(ptr)"来获取指向整个结构体变量的指针 #define container_of(ptr, type, member) ((type *)((char *)(ptr) - offset_of(type, member))) //双向链表的节点定义,是双向链表的表头,也代表双向链表的节点 typedef struct list_structure { struct list_structure* next; struct list_structure* prev; } ListObj; //定义节点,设置name的前继节点和后继节点都指向name本身 #define LIST_HEAD_INIT(name) {&(name), &(name)} #define LIST_HEAD(name) ListObj name = LIST_HEAD_INIT(name) void list_init(ListObj* list); void list_insert_after(ListObj* list, ListObj* node); void list_insert_before(ListObj* list, ListObj* node); void list_remove(ListObj* node); int list_isempty(const ListObj* list); unsigned int list_len(const ListObj* list);//get list len #define list_entry(node, type, member) container_of(node, type, member) //遍历链表用于获取节点 #define list_for_each(pos, head) for (pos = (head)->next; pos != (head); pos = pos->next) //遍历链表御用删除节点 #define list_for_each_safe(pos, n, head) for (pos = (head)->next, n = pos->next; pos != (head); pos = n, n = pos->next) #endif #include "./timeslice.h" static LIST_HEAD(timeslice_task_list); void timeslice_exec() { ListObj* node; TimesilceTaskObj* task; list_for_each(node, ×lice_task_list) { task = list_entry(node, TimesilceTaskObj, timeslice_task_list); if (task->is_run == TASK_RUN) { task->task_hdl();//task run callback task->is_run = TASK_STOP; } } } void timeslice_tick() { ListObj* node; TimesilceTaskObj* task; list_for_each(node, ×lice_task_list) { task = list_entry(node, TimesilceTaskObj, timeslice_task_list); if (task->timer != 0) { task->timer--; if (task->timer == 0) { task->is_run = TASK_RUN; task->timer = task->timeslice_len; } } } } unsigned int timeslice_get_task_num() { return list_len(×lice_task_list); } void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len) { obj->id = id; obj->is_run = TASK_STOP; obj->task_hdl = task_hdl; obj->timer = timeslice_len; obj->timeslice_len = timeslice_len; } void timeslice_task_add(TimesilceTaskObj* obj) { //add to list list_insert_before(×lice_task_list, &obj->timeslice_task_list); } void timeslice_task_del(TimesilceTaskObj* obj) { if (timeslice_task_isexist(obj)) list_remove(&obj->timeslice_task_list); else return; } unsigned char timeslice_task_isexist(TimesilceTaskObj* obj) { unsigned char isexist = 0; ListObj* node; TimesilceTaskObj* task; list_for_each(node, ×lice_task_list) { task = list_entry(node, TimesilceTaskObj, timeslice_task_list); if (obj->id == task->id) isexist = 1; } return isexist; } unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj) { return obj->timeslice_len; } #ifndef _TIMESLICE_H #define _TIMESLICE_H #include "list.h" typedef enum { TASK_STOP, TASK_RUN } IsTaskRun; typedef struct timesilce { unsigned int id;//任务id void (*task_hdl)(void);//任务回调函数 IsTaskRun is_run;//任务状态 unsigned int timer; unsigned int timeslice_len;//时长 ListObj timeslice_task_list;//双向链表 } TimesilceTaskObj; void timeslice_exec(void); void timeslice_tick(void); void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len); void timeslice_task_add(TimesilceTaskObj* obj); void timeslice_task_del(TimesilceTaskObj* obj); unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj); unsigned int timeslice_get_task_num(void); unsigned char timeslice_task_isexist(TimesilceTaskObj* obj); #endif |
|
|
|
只有小组成员才能发言,加入小组>>
3284 浏览 9 评论
2960 浏览 16 评论
3464 浏览 1 评论
9008 浏览 16 评论
4054 浏览 18 评论
1123浏览 3评论
577浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
572浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2305浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1862浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 21:02 , Processed in 1.052046 second(s), Total 78, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号