【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(一)

简介: 【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构

简介

timeslice是一个时间片轮询框架,他是一个完全解耦的时间片轮询框架,他的使用非常方便,该项目一共有四个文件分别是tieslice的头文件和源文件以及list的头文件和源文件,tieslice是负责轮询任务,list是一个双向链表负责任务的管理,在Linux内核中使用非常广泛也很经典,该框架是参考rtt实时操作系统的侵入式链表实现的,本章文章是将该框架移植到stm32单片机上实验,使用也非常容易,单片机只需要启用一个定时器作为时钟即可;

本章使用环境:

stm32f407vet6

代码工程使用cubemx创建

项目代码

该项目的代码是我在微信公众号上看到的一个文章,代码并没有上传在github上,这里直接贴上源代码;

timeslice.h

#ifndef _TIMESLICE_H
#define _TIMESLICE_H
#include "list.h"
typedef enum {
    TASK_STOP,
    TASK_RUN
} IsTaskRun;
typedef struct timesilce
{
    unsigned int 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

timeslice.c

#include "timeslice.h"
static LIST_HEAD(timeslice_task_list);
void timeslice_exec()
{
    ListObj* node;
    TimesilceTaskObj* task;
    list_for_each(node, &timeslice_task_list)
    {
        task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
        if (task->is_run == TASK_RUN)
        {
            task->task_hdl();
            task->is_run = TASK_STOP;
        }
    }
}
void timeslice_tick()
{
    ListObj* node;
    TimesilceTaskObj* task;
    list_for_each(node, &timeslice_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(&timeslice_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)
{
    list_insert_before(&timeslice_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, &timeslice_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;
}

list.h

#ifndef _LIST_H
#define _LIST_H
#define offset_of(type, member)             (unsigned long) &((type*)0)->member
#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;
#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);
#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

list.c

#include "list.h"
void list_init(ListObj* list)
{
    list->next = list->prev = list;
}
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;
}


【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(二)https://developer.aliyun.com/article/1472622

相关文章
|
13天前
|
存储 Java 容器
Web后端开发中对三层架构解耦之控制反转与依赖注入
Web后端开发中对三层架构解耦之控制反转与依赖注入
12 0
|
13天前
|
XML 前端开发 JavaScript
后端请求响应和分层解耦web开发的三层架构
后端请求响应和分层解耦web开发的三层架构
17 0
|
2月前
|
关系型数据库 分布式数据库 数据库
【PolarDB开源】PolarDB与微服务架构的融合:灵活扩展与高效管理
【5月更文挑战第23天】阿里云PolarDB是适用于微服务的高性能分布式数据库,提供数据分片、水平扩展及高可用性解决方案。通过SQL或API实现弹性扩展,内置故障转移保障服务连续性,且兼容MySQL协议,易于集成微服务生态。通过Spring Boot示例展示了PolarDB的配置与集成过程,强调其在现代云原生应用中的重要角色。
75 1
|
2月前
|
存储 关系型数据库 分布式数据库
【PolarDB开源】PolarDB高可用架构解析:确保业务连续性的关键设计
【5月更文挑战第22天】阿里云PolarDB是一款高可用、高性能的云原生数据库,采用分布式共享存储架构实现计算与存储分离。通过主从复制保证数据实时同步,当主节点故障时,从节点能快速接管。此外,PolarDB提供自动故障转移和数据备份恢复功能,确保业务连续性和数据安全性。一个简单的Python SDK使用示例展示了查询数据的过程。总之,PolarDB通过多种机制保障了企业在异常情况下的服务稳定和数据完整性。
224 5
|
2月前
|
架构师 数据挖掘 Python
最全pandas库(Python),2024年最新阿里云架构师面试
最全pandas库(Python),2024年最新阿里云架构师面试
最全pandas库(Python),2024年最新阿里云架构师面试
|
2月前
|
存储 关系型数据库 分布式数据库
【PolarDB开源】深入PolarDB内核:探究存储计算分离架构的设计哲学
【5月更文挑战第20天】PolarDB是阿里巴巴的云原生分布式数据库,以其存储计算分离架构为核心,解决了传统数据库的扩展性和资源灵活性问题。该架构将数据存储和计算处理分开,实现高性能(通过RDMA加速数据传输)、高可用性(多副本冗余保证数据可靠性)和灵活扩展(计算资源独立扩展)。通过动态添加计算节点以应对业务流量变化,PolarDB展示了其在云时代应对复杂业务场景的能力。随着开源项目的进展,PolarDB将持续推动数据库技术发展。
91 6
|
2月前
|
人工智能 自然语言处理 开发者
首个基于SSM-Transformer混合架构,开源商业大模型Jamba
【4月更文挑战第13天】AI模型部署与优化迎来新解决方案,ai21labs推出的SSM-Transformer混合架构大模型Jamba结合英伟达NVIDIA NIM服务。Jamba模型在自然语言处理上表现出色,开源特性促进AI技术普及,而NIM提供跨平台、高性能的部署支持。不过,技术门槛、资源需求及优化挑战仍需考虑。
62 6
首个基于SSM-Transformer混合架构,开源商业大模型Jamba
|
2月前
|
Cloud Native 安全 微服务
云原生开源沙龙北京站火热报名中丨微服务安全零信任架构
云原生开源沙龙北京站火热报名中丨微服务安全零信任架构。
|
2月前
|
程序员 Linux
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(三)
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构
|
2月前
|
调度
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(二)
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构