0
  • 聊天消息
  • 系統(tǒng)消息
  • 評論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會員中心
創(chuàng)作中心

完善資料讓更多小伙伴認識你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

多級時間輪實現(xiàn)框架

科技綠洲 ? 來源:Linux開發(fā)架構(gòu)之路 ? 作者:Linux開發(fā)架構(gòu)之路 ? 2023-11-11 15:49 ? 次閱讀

一. 多級時間輪實現(xiàn)框架

圖片

上圖是5個時間輪級聯(lián)的效果圖。中間的大輪是工作輪,只有在它上的任務(wù)才會被執(zhí)行;其他輪上的任務(wù)時間到后遷移到下一級輪上,他們最終都會遷移到工作輪上而被調(diào)度執(zhí)行。

多級時間輪的原理也容易理解:就拿時鐘做說明,秒針轉(zhuǎn)動一圈分針轉(zhuǎn)動一格;分針轉(zhuǎn)動一圈時針轉(zhuǎn)動一格;同理時間輪也是如此:當(dāng)?shù)图壿嗈D(zhuǎn)動一圈時,高一級輪轉(zhuǎn)動一格,同時會將高一級輪上的任務(wù)重新分配到低級輪上。從而實現(xiàn)了多級輪級聯(lián)的效果。

1.1 多級時間輪對象

圖片

多級時間輪應(yīng)該至少包括以下內(nèi)容:

  • 每一級時間輪對象
  • 輪子上指針的位置

關(guān)于輪子上指針的位置有一個比較巧妙的辦法:那就是位運算。比如定義一個無符號整型的數(shù):

圖片

通過獲取當(dāng)前的系統(tǒng)時間便可以通過位操作轉(zhuǎn)換為時間輪上的時間,通過與實際時間輪上的時間作比較,從而確定時間輪要前進調(diào)度的時間,進而操作對應(yīng)時間輪槽位對應(yīng)的任務(wù)。

為什么至少需要這兩個成員呢?

  • 定義多級時間輪,首先需要明確的便是級聯(lián)的層數(shù),也就是說需要確定有幾個時間輪。
  • 輪子上指針位置,就是當(dāng)前時間輪運行到的位置,它與真實時間的差便是后續(xù)時間輪需要調(diào)度執(zhí)行,它們的差值是時間輪運作起來的驅(qū)動力。

多級時間輪對象的定義

//實現(xiàn)5級時間輪 范圍為0~ (2^8 * 2^6 * 2^6 * 2^6 *2^6)=2^32
struct tvec_base
{
    unsigned long 		current_index;   
    pthread_t  			thincrejiffies;
    pthread_t  			threadID;
    struct tvec_root 	tv1;	/*第一個輪*/
    struct tvec      	tv2;	/*第二個輪*/
    struct tvec      	tv3;	/*第三個輪*/
    struct tvec      	tv4;	/*第四個輪*/
    struct tvec      	tv5;	/*第五個輪*/
};

1.2 時間輪對象

圖片

我們知道每一個輪子實際上都是一個哈希表,上面我們只是實例化了五個輪子的對象,但是五個輪子具體包含什么,有幾個槽位等等沒有明確(即struct tvec和struct tvec_root)。

#define TVN_BITS 		6
#define TVR_BITS 		8
#define TVN_SIZE 		(1

此外,每一個時間輪都是哈希表,因此它的類型應(yīng)該至少包含兩個指針域來實現(xiàn)雙向鏈表的功能。這里我們?yōu)榱朔奖闶褂猛ㄓ玫膕truct list_head的雙向鏈表結(jié)構(gòu)。

1.3 定時任務(wù)對象

圖片

定時器的主要工作是為了在未來的特定時間完成某項任務(wù),而這個任務(wù)經(jīng)常包含以下內(nèi)容:

  • 任務(wù)的處理邏輯(回調(diào)函數(shù))
  • 任務(wù)的參數(shù)
  • 雙向鏈表節(jié)點
  • 到時時間

定時任務(wù)對象的定義

typedef void (*timeouthandle)(unsigned long );

struct timer_list{
    struct list_head entry;          //將時間連接成鏈表
    unsigned long expires;           //超時時間
    void (*function)(unsigned long); //超時后的處理函數(shù)
    unsigned long data;              //處理函數(shù)的參數(shù)
    struct tvec_base *base;          //指向時間輪
};

在時間輪上的效果圖:

圖片

1.4 雙向鏈表

在時間輪上我們采用雙向鏈表的數(shù)據(jù)類型。采用雙向鏈表的除了操作上比單鏈表復(fù)雜,多占一個指針域外沒有其他不可接收的問題。而多占一個指針域在今天大內(nèi)存的時代明顯不是什么問題。至于雙向鏈表操作的復(fù)雜性,我們可以通過使用通用的struct list結(jié)構(gòu)來解決,因為雙向鏈表有眾多的標(biāo)準(zhǔn)操作函數(shù),我們可以通過直接引用list.h頭文件來使用他們提供的接口

struct list可以說是一個萬能的雙向鏈表操作框架,我們只需要在自定義的結(jié)構(gòu)中定義一個struct list對象即可使用它的標(biāo)準(zhǔn)操作接口。同時它還提供了一個類似container_of的接口,在應(yīng)用層一般叫做list_entry,因此我們可以很方便的通過struct list成員找到自定義的結(jié)構(gòu)體的起始地址。

關(guān)于應(yīng)用層的log.h, 我將在下面的代碼中附上該文件。如果需要內(nèi)核層的實現(xiàn),可以直接從linux源碼中獲取。

1.5 聯(lián)結(jié)方式

多級時間輪效果圖:

圖片

二. 多級時間輪C語言實現(xiàn)

2.1 雙向鏈表頭文件: list.h

提到雙向鏈表,很多的源碼工程中都會實現(xiàn)一系列的統(tǒng)一的雙向鏈表操作函數(shù)。它們?yōu)殡p向鏈表封裝了統(tǒng)計的接口,使用者只需要在自定義的結(jié)構(gòu)中添加一個struct list_head結(jié)構(gòu),然后調(diào)用它們提供的接口,便可以完成雙向鏈表的所有操作。這些操作一般都在list.h的頭文件中實現(xiàn)。Linux源碼中也有實現(xiàn)(內(nèi)核態(tài)的實現(xiàn))。他們實現(xiàn)的方式基本完全一樣,只是實現(xiàn)的接口數(shù)量和功能上稍有差別??梢哉f這個list.h文件是學(xué)習(xí)操作雙向鏈表的不二選擇,它幾乎實現(xiàn)了所有的操作:增、刪、改、查、遍歷、替換、清空等等。這里我拼湊了一個源碼中的log.h函數(shù),終于湊夠了多級時間輪中使用到的接口。

圖片

#if !defined(_BLKID_LIST_H) && !defined(LIST_HEAD)
#define _BLKID_LIST_H
#ifdef __cplusplus 
extern "C" {
#endif
/*
 * Simple doubly linked list implementation.
 *
 * Some of the internal functions ("__xxx") are useful when
 * manipulating whole lists rather than single entries, as
 * sometimes we already know the next/prev entries and we can
 * generate better code by using them directly rather than
 * using the generic single-entry routines.
 */
struct list_head {
	struct list_head *next, *prev;
};
#define LIST_HEAD_INIT(name) { &(name), &(name) }
#define LIST_HEAD(name) 
	struct list_head name = LIST_HEAD_INIT(name)
#define INIT_LIST_HEAD(ptr) do { 
	(ptr)- >next = (ptr); (ptr)- >prev = (ptr); 
} while (0)
static inline void
__list_add(struct list_head *entry,
                struct list_head *prev, struct list_head *next)
{
    next- >prev = entry;
    entry- >next = next;
    entry- >prev = prev;
    prev- >next = entry;
}
/**
 * Insert a new element after the given list head. The new element does not
 * need to be initialised as empty list.
 * The list changes from:
 *      head → some element → ...
 * to
 *      head → new element → older element → ...
 *
 * Example:
 * struct foo *newfoo = malloc(...);
 * list_add(&newfoo- >entry, &bar- >list_of_foos);
 *
 * @param entry The new element to prepend to the list.
 * @param head The existing list.
 */
static inline void
list_add(struct list_head *entry, struct list_head *head)
{
    __list_add(entry, head, head- >next);
}
/**
 * Append a new element to the end of the list given with this list head.
 *
 * The list changes from:
 *      head → some element → ... → lastelement
 * to
 *      head → some element → ... → lastelement → new element
 *
 * Example:
 * struct foo *newfoo = malloc(...);
 * list_add_tail(&newfoo- >entry, &bar- >list_of_foos);
 *
 * @param entry The new element to prepend to the list.
 * @param head The existing list.
 */
static inline void
list_add_tail(struct list_head *entry, struct list_head *head)
{
    __list_add(entry, head- >prev, head);
}
static inline void
__list_del(struct list_head *prev, struct list_head *next)
{
    next- >prev = prev;
    prev- >next = next;
}
/**
 * Remove the element from the list it is in. Using this function will reset
 * the pointers to/from this element so it is removed from the list. It does
 * NOT free the element itself or manipulate it otherwise.
 *
 * Using list_del on a pure list head (like in the example at the top of
 * this file) will NOT remove the first element from
 * the list but rather reset the list as empty list.
 *
 * Example:
 * list_del(&foo- >entry);
 *
 * @param entry The element to remove.
 */
static inline void
list_del(struct list_head *entry)
{
    __list_del(entry- >prev, entry- >next);
}
static inline void
list_del_init(struct list_head *entry)
{
    __list_del(entry- >prev, entry- >next);
    INIT_LIST_HEAD(entry);
}
static inline void list_move_tail(struct list_head *list,
				  struct list_head *head)
{
	__list_del(list- >prev, list- >next);
	list_add_tail(list, head);
}
/**
 * Check if the list is empty.
 *
 * Example:
 * list_empty(&bar- >list_of_foos);
 *
 * @return True if the list contains one or more elements or False otherwise.
 */
static inline int
list_empty(struct list_head *head)
{
    return head- >next == head;
}
/**
 * list_replace - replace old entry by new one
 * @old : the element to be replaced
 * @new : the new element to insert
 *
 * If @old was empty, it will be overwritten.
 */
static inline void list_replace(struct list_head *old,
				struct list_head *new)
{
	new- >next = old- >next;
	new- >next- >prev = new;
	new- >prev = old- >prev;
	new- >prev- >next = new;
}
/**
 * Retrieve the first list entry for the given list pointer.
 *
 * Example:
 * struct foo *first;
 * first = list_first_entry(&bar- >list_of_foos, struct foo, list_of_foos);
 *
 * @param ptr The list head
 * @param type Data type of the list element to retrieve
 * @param member Member name of the struct list_head field in the list element.
 * @return A pointer to the first list element.
 */
#define list_first_entry(ptr, type, member) 
    list_entry((ptr)- >next, type, member)
static inline void list_replace_init(struct list_head *old,
					struct list_head *new)
{
	list_replace(old, new);
	INIT_LIST_HEAD(old);
}
/**
 * list_entry - get the struct for this entry
 * @ptr:	the &struct list_head pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_struct within the struct.
 */
#define list_entry(ptr, type, member) 
	((type *)((char *)(ptr)-(unsigned long)(&((type *)0)- >member)))
/**
 * list_for_each - iterate over elements in a list
 * @pos:	the &struct list_head to use as a loop counter.
 * @head:	the head for your list.
 */
#define list_for_each(pos, head) 
	for (pos = (head)- >next; pos != (head); pos = pos- >next)
/**
 * list_for_each_safe - iterate over elements in a list, but don't dereference
 *                      pos after the body is done (in case it is freed)
 * @pos:	the &struct list_head to use as a loop counter.
 * @pnext:	the &struct list_head to use as a pointer to the next item.
 * @head:	the head for your list (not included in iteration).
 */
#define list_for_each_safe(pos, pnext, head) 
	for (pos = (head)- >next, pnext = pos- >next; pos != (head); 
	     pos = pnext, pnext = pos- >next)
#ifdef __cplusplus
}
#endif
#endif /* _BLKID_LIST_H */

這里面一般會用到一個重要實現(xiàn):container_of, 它的原理這里不敘述

2.2 調(diào)試信息頭文件: log.h

這個頭文件實際上不是必須的,我只是用它來添加調(diào)試信息(代碼中的errlog(), log()都是log.h中的宏函數(shù))。它的效果是給打印的信息加上顏色,效果如下:

圖片

log.h的代碼如下:

#ifndef _LOG_h_
#define _LOG_h_
#include < stdio.h >
#define COL(x)  "?33[;" #x "m"
#define RED     COL(31)
#define GREEN   COL(32)
#define YELLOW  COL(33)
#define BLUE    COL(34)
#define MAGENTA COL(35)
#define CYAN    COL(36)
#define WHITE   COL(0)
#define GRAY    "?33[0m"
#define errlog(fmt, arg...) do{     
    printf(RED"[#ERROR: Toeny Sun:"GRAY YELLOW" %s:%d]:"GRAY WHITE fmt GRAY, __func__, __LINE__, ##arg);
}while(0)
#define log(fmt, arg...) do{     
    printf(WHITE"[#DEBUG: Toeny Sun: "GRAY YELLOW"%s:%d]:"GRAY WHITE fmt GRAY, __func__, __LINE__, ##arg);
}while(0)
#endif

2.3 時間輪代碼: timewheel.c

/*
 *毫秒定時器  采用多級時間輪方式  借鑒linux內(nèi)核中的實現(xiàn)
 *支持的范圍為1 ~  2^32 毫秒(大約有49天)
 *若設(shè)置的定時器超過最大值 則按最大值設(shè)置定時器
 **/
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
#include < unistd.h >
#include < pthread.h >
#include < sys/time.h >
#include "list.h"
#include "log.h" 
#define TVN_BITS 		6
#define TVR_BITS 		8
#define TVN_SIZE 		(1

2.4 編譯運行

toney@ubantu:/mnt/hgfs/em嵌入式學(xué)習(xí)記錄/4. timerwheel/2. 多級時間輪$ ls
a.out  list.h  log.h  mutiTimeWheel.c
toney@ubantu:/mnt/hgfs/em嵌入式學(xué)習(xí)記錄/4. timerwheel/2. 多級時間輪$ gcc mutiTimeWheel.c -lpthread
toney@ubantu:/mnt/hgfs/em嵌入式學(xué)習(xí)記錄/4. timerwheel/2. 多級時間輪$ ./a.out 
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100
[#DEBUG: Toeny Sun: mytimer:370]:100

從結(jié)果可以看出:如果添加的定時任務(wù)是比較耗時的操作,那么后續(xù)的任務(wù)也會被阻塞,可能一直到超時,甚至一直阻塞下去,這個取決于當(dāng)前任務(wù)是否耗時。這個理論上是絕不能接受的:一個任務(wù)不應(yīng)該也不能去影響其他的任務(wù)吧。但是目前沒有對此問題進行改進和完善,以后有機會再繼續(xù)完善吧。

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請聯(lián)系本站處理。 舉報投訴
  • 參數(shù)
    +關(guān)注

    關(guān)注

    11

    文章

    1661

    瀏覽量

    31877
  • 函數(shù)
    +關(guān)注

    關(guān)注

    3

    文章

    4237

    瀏覽量

    61967
  • 代碼
    +關(guān)注

    關(guān)注

    30

    文章

    4671

    瀏覽量

    67765
收藏 人收藏

    評論

    相關(guān)推薦

    使用cola_os軟件定時器實現(xiàn)時間片輪詢框架

    如果使用RTOS顯得太浪費,這時候可以嘗試使用使用cola_os這類基于軟件定時器實現(xiàn)時間片輪詢框架
    的頭像 發(fā)表于 09-22 09:03 ?1282次閱讀
    使用cola_os軟件定時器<b class='flag-5'>實現(xiàn)時間</b>片輪詢<b class='flag-5'>框架</b>

    Linux編程之經(jīng)典多級時間定時器(C語言版)

    上圖是5個時間級聯(lián)的效果圖。中間的大是工作,只有在它上的任務(wù)才會被執(zhí)行;其他輪上的任務(wù)時間到后遷移到下一級輪上,他們最終都會遷移到工作
    發(fā)表于 11-08 14:06 ?802次閱讀

    多級時間定時器的原理及編程實現(xiàn)方案

    struct list可以說是一個萬能的雙向鏈表操作框架,我們只需要在自定義的結(jié)構(gòu)中定義一個struct list對象即可使用它的標(biāo)準(zhǔn)操作接口。
    發(fā)表于 06-28 12:50 ?582次閱讀
    <b class='flag-5'>多級</b><b class='flag-5'>時間</b><b class='flag-5'>輪</b>定時器的原理及編程<b class='flag-5'>實現(xiàn)</b>方案

    基于J2EE的數(shù)據(jù)通用性操作框架的研究與實現(xiàn)

    基于J2EE的數(shù)據(jù)通用性操作框架的研究與實現(xiàn)數(shù)據(jù)操作是基于J2EE的多級應(yīng)用開發(fā)中常見而又具有通用性的重要一部分,而框架技術(shù)的研究可以實現(xiàn)
    發(fā)表于 06-17 09:35

    制作庫時如何分多級菜單

    使用 LabView有一段時間了,一直在想如何可以做好更好的使用及模塊移植。最近一段時間做了不少的庫、庫進行多層繼承敢制作過。但是發(fā)現(xiàn)有個問題就是庫調(diào)用的屬性只有一級菜單,不能進行分類。然而調(diào)用其內(nèi)部的屬性時卻可以有多級,不知如
    發(fā)表于 01-20 16:31

    時間的種類有哪些?

    什么是單層時間?什么是多層時間?
    發(fā)表于 11-06 06:15

    基于RT-Thread+RA6M4的麥結(jié)構(gòu)的底盤運動控制系統(tǒng)設(shè)計案例

    ,使其驅(qū)動4個麥的電機。其地盤可實現(xiàn)全向移動,即平面的縱向,橫向移動和原地的旋轉(zhuǎn)移動。應(yīng)用背景在目前移動機器人開發(fā)中,除了仿生結(jié)構(gòu)的機器人之外,麥結(jié)構(gòu)的移動機器人和萬向結(jié)構(gòu)的移動
    發(fā)表于 08-17 14:50

    LCD多級菜單具體實現(xiàn)

    LCD多級菜單具體實現(xiàn) //Last Modify Time:03/11/07 01:22 //ReadMe //屏寬:112 //屏高:64 #include reg51.h #include
    發(fā)表于 07-02 15:23 ?133次下載

    單片機程序時間片法框架

    來源 | ERYUESANHI 編排 | strongerHuang 今天分享一篇單片機程序框架的文章。 程序架構(gòu)重要性 很多人尤其是初學(xué)者在寫代碼的時候往往都是想一點寫一點,最開始沒有一個整體
    的頭像 發(fā)表于 08-26 11:07 ?3575次閱讀

    簡單時間算法詳解

    時間算法是通過一個時間去維護定時任務(wù),按照一定的時間單位對時間
    的頭像 發(fā)表于 08-22 11:45 ?2320次閱讀

    定時器實現(xiàn)原理——時間

    時間算法中,輪詢線程遍歷到某一個時間刻度后,總是執(zhí)行對應(yīng)刻度上任務(wù)隊列中的所有任務(wù)(通常是將任務(wù)扔給異步線程池來處理),而不再需要遍歷檢查所有任務(wù)的時間戳是否達到要求(不用每次從小頂
    的頭像 發(fā)表于 08-22 11:47 ?1248次閱讀

    使用風(fēng)扇實現(xiàn)多級通風(fēng)

    電子發(fā)燒友網(wǎng)站提供《使用風(fēng)扇實現(xiàn)多級通風(fēng).zip》資料免費下載
    發(fā)表于 11-23 10:21 ?0次下載
    使用風(fēng)扇<b class='flag-5'>實現(xiàn)</b><b class='flag-5'>多級</b>通風(fēng)

    Linux 編程之經(jīng)典多級時間定時器(上)

    多級時間的原理也容易理解:就拿時鐘做說明,秒針轉(zhuǎn)動一圈分針轉(zhuǎn)動一格;分針轉(zhuǎn)動一圈時針轉(zhuǎn)動一格;同理時間也是如此:當(dāng)?shù)图壿嗈D(zhuǎn)動一圈時,高一
    的頭像 發(fā)表于 04-21 14:45 ?558次閱讀
    Linux 編程之經(jīng)典<b class='flag-5'>多級</b><b class='flag-5'>時間</b><b class='flag-5'>輪</b>定時器(上)

    Linux 編程之經(jīng)典多級時間定時器(下)

    多級時間的原理也容易理解:就拿時鐘做說明,秒針轉(zhuǎn)動一圈分針轉(zhuǎn)動一格;分針轉(zhuǎn)動一圈時針轉(zhuǎn)動一格;同理時間也是如此:當(dāng)?shù)图壿嗈D(zhuǎn)動一圈時,高一
    的頭像 發(fā)表于 04-21 14:45 ?657次閱讀

    多級時間實現(xiàn)框架

    一. 多級時間實現(xiàn)框架 上圖是5個時間級聯(lián)的效果
    的頭像 發(fā)表于 06-22 14:57 ?620次閱讀
    <b class='flag-5'>多級</b><b class='flag-5'>時間</b><b class='flag-5'>輪</b><b class='flag-5'>實現(xiàn)</b><b class='flag-5'>框架</b>