亚洲综合图片区自拍_思思91精品国产综合在线观看_一区二区三区欧美_欧美黑人又粗又大_亚洲人成精品久久久久桥本

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

2023-06-28 13:21:28 來源:Linux大陸

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

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


(資料圖片僅供參考)

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

1.1 多級時間輪對象

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

每一級時間輪對象

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

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

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

定義多級時間輪,首先需要明確的便是級聯(lián)的層數(shù),也就是說需要確定有幾個時間輪。

輪子上指針位置,就是當前時間輪運行到的位置,它與真實時間的差便是后續(xù)時間輪需要調(diào)度執(zhí)行,它們的差值是時間輪運作起來的驅(qū)動力。

多級時間輪對象的定義

//實現(xiàn)5級時間輪范圍為0~(2^8*2^6*2^6*2^6*2^6)=2^32structtvec_base{unsignedlongcurrent_index;pthread_tthincrejiffies;pthread_tthreadID;structtvec_roottv1;/*第一個輪*/structtvectv2;/*第二個輪*/structtvectv3;/*第三個輪*/structtvectv4;/*第四個輪*/structtvectv5;/*第五個輪*/};

1.2 時間輪對象

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

#defineTVN_BITS6#defineTVR_BITS8#defineTVN_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ù)對象的定義

typedefvoid(*timeouthandle)(unsignedlong);structtimer_list{structlist_headentry;//將時間連接成鏈表unsignedlongexpires;//超時時間void(*function)(unsignedlong);//超時后的處理函數(shù)unsignedlongdata;//處理函數(shù)的參數(shù)structtvec_base*base;//指向時間輪};

在時間輪上的效果圖:

1.4 雙向鏈表

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

struct list可以說是一個萬能的雙向鏈表操作框架,我們只需要在自定義的結(jié)構(gòu)中定義一個struct list對象即可使用它的標準操作接口。同時它還提供了一個類似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ù)量和功能上稍有差別。可以說這個list.h文件是學(xué)習(xí)操作雙向鏈表的不二選擇,它幾乎實現(xiàn)了所有的操作:增、刪、改、查、遍歷、替換、清空等等。這里我拼湊了一個源碼中的log.h函數(shù),終于湊夠了多級時間輪中使用到的接口。

#if!defined(_BLKID_LIST_H)&&!defined(LIST_HEAD)#define_BLKID_LIST_H#ifdef__cplusplusextern"C"{#endif/**Simpledoublylinkedlistimplementation.**Someoftheinternalfunctions("__xxx")areusefulwhen*manipulatingwholelistsratherthansingleentries,as*sometimeswealreadyknowthenext/preventriesandwecan*generatebettercodebyusingthemdirectlyratherthan*usingthegenericsingle-entryroutines.*/structlist_head{structlist_head*next,*prev;};#defineLIST_HEAD_INIT(name){&(name),&(name)}#defineLIST_HEAD(name)structlist_headname=LIST_HEAD_INIT(name)#defineINIT_LIST_HEAD(ptr)do{(ptr)->next=(ptr);(ptr)->prev=(ptr);}while(0)staticinlinevoid__list_add(structlist_head*entry,structlist_head*prev,structlist_head*next){next->prev=entry;entry->next=next;entry->prev=prev;prev->next=entry;}/***Insertanewelementafterthegivenlisthead.Thenewelementdoesnot*needtobeinitialisedasemptylist.*Thelistchangesfrom:*head→someelement→...*to*head→newelement→olderelement→...**Example:*structfoo*newfoo=malloc(...);*list_add(&newfoo->entry,&bar->list_of_foos);**@paramentryThenewelementtoprependtothelist.*@paramheadTheexistinglist.*/staticinlinevoidlist_add(structlist_head*entry,structlist_head*head){__list_add(entry,head,head->next);}/***Appendanewelementtotheendofthelistgivenwiththislisthead.**Thelistchangesfrom:*head→someelement→...→lastelement*to*head→someelement→...→lastelement→newelement**Example:*structfoo*newfoo=malloc(...);*list_add_tail(&newfoo->entry,&bar->list_of_foos);**@paramentryThenewelementtoprependtothelist.*@paramheadTheexistinglist.*/staticinlinevoidlist_add_tail(structlist_head*entry,structlist_head*head){__list_add(entry,head->prev,head);}staticinlinevoid__list_del(structlist_head*prev,structlist_head*next){next->prev=prev;prev->next=next;}/***Removetheelementfromthelistitisin.Usingthisfunctionwillreset*thepointersto/fromthiselementsoitisremovedfromthelist.Itdoes*NOTfreetheelementitselformanipulateitotherwise.**Usinglist_delonapurelisthead(likeintheexampleatthetopof*thisfile)willNOTremovethefirstelementfrom*thelistbutratherresetthelistasemptylist.**Example:*list_del(&foo->entry);**@paramentryTheelementtoremove.*/staticinlinevoidlist_del(structlist_head*entry){__list_del(entry->prev,entry->next);}staticinlinevoidlist_del_init(structlist_head*entry){__list_del(entry->prev,entry->next);INIT_LIST_HEAD(entry);}staticinlinevoidlist_move_tail(structlist_head*list,structlist_head*head){__list_del(list->prev,list->next);list_add_tail(list,head);}/***Checkifthelistisempty.**Example:*list_empty(&bar->list_of_foos);**@returnTrueifthelistcontainsoneormoreelementsorFalseotherwise.*/staticinlineintlist_empty(structlist_head*head){returnhead->next==head;}/***list_replace-replaceoldentrybynewone*@old:theelementtobereplaced*@new:thenewelementtoinsert**If@oldwasempty,itwillbeoverwritten.*/staticinlinevoidlist_replace(structlist_head*old,structlist_head*new){new->next=old->next;new->next->prev=new;new->prev=old->prev;new->prev->next=new;}/***Retrievethefirstlistentryforthegivenlistpointer.**Example:*structfoo*first;*first=list_first_entry(&bar->list_of_foos,structfoo,list_of_foos);**@paramptrThelisthead*@paramtypeDatatypeofthelistelementtoretrieve*@parammemberMembernameofthestructlist_headfieldinthelistelement.*@returnApointertothefirstlistelement.*/#definelist_first_entry(ptr,type,member)list_entry((ptr)->next,type,member)staticinlinevoidlist_replace_init(structlist_head*old,structlist_head*new){list_replace(old,new);INIT_LIST_HEAD(old);}/***list_entry-getthestructforthisentry*@ptr:the&structlist_headpointer.*@type:thetypeofthestructthisisembeddedin.*@member:thenameofthelist_structwithinthestruct.*/#definelist_entry(ptr,type,member)((type*)((char*)(ptr)-(unsignedlong)(&((type*)0)->member)))/***list_for_each-iterateoverelementsinalist*@pos:the&structlist_headtouseasaloopcounter.*@head:theheadforyourlist.*/#definelist_for_each(pos,head)for(pos=(head)->next;pos!=(head);pos=pos->next)/***list_for_each_safe-iterateoverelementsinalist,butdon"tdereference*posafterthebodyisdone(incaseitisfreed)*@pos:the&structlist_headtouseasaloopcounter.*@pnext:the&structlist_headtouseasapointertothenextitem.*@head:theheadforyourlist(notincludediniteration).*/#definelist_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#defineCOL(x)"33[;"#x"m"#defineREDCOL(31)#defineGREENCOL(32)#defineYELLOWCOL(33)#defineBLUECOL(34)#defineMAGENTACOL(35)#defineCYANCOL(36)#defineWHITECOL(0)#defineGRAY"33[0m"#defineerrlog(fmt,arg...)do{printf(RED"[#ERROR:ToenySun:"GRAYYELLOW"%s:%d]:"GRAYWHITEfmtGRAY,__func__,__LINE__,##arg);}while(0)#definelog(fmt,arg...)do{printf(WHITE"[#DEBUG:ToenySun:"GRAYYELLOW"%s:%d]:"GRAYWHITEfmtGRAY,__func__,__LINE__,##arg);}while(0)#endif

2.3 時間輪代碼: timewheel.c

/**毫秒定時器采用多級時間輪方式借鑒linux內(nèi)核中的實現(xiàn)*支持的范圍為1~2^32毫秒(大約有49天)*若設(shè)置的定時器超過最大值則按最大值設(shè)置定時器**/#include#include#include#include#include#include#include"list.h"#include"log.h"#defineTVN_BITS6#defineTVR_BITS8#defineTVN_SIZE(1<current_index>>(TVR_BITS+(N)*TVN_BITS))&TVN_MASK)typedefvoid(*timeouthandle)(unsignedlong);structtimer_list{structlist_headentry;//將時間連接成鏈表unsignedlongexpires;//超時時間void(*function)(unsignedlong);//超時后的處理函數(shù)unsignedlongdata;//處理函數(shù)的參數(shù)structtvec_base*base;//指向時間輪};structtvec{structlist_headvec[TVN_SIZE];};structtvec_root{structlist_headvec[TVR_SIZE];};//實現(xiàn)5級時間輪范圍為0~(2^8*2^6*2^6*2^6*2^6)=2^32structtvec_base{unsignedlongcurrent_index;pthread_tthincrejiffies;pthread_tthreadID;structtvec_roottv1;/*第一個輪*/structtvectv2;/*第二個輪*/structtvectv3;/*第三個輪*/structtvectv4;/*第四個輪*/structtvectv5;/*第五個輪*/};staticvoidinternal_add_timer(structtvec_base*base,structtimer_list*timer){structlist_head*vec;unsignedlongexpires=timer->expires;unsignedlongidx=expires-base->current_index;#if1if((signedlong)idx<0)/*這里是沒有辦法區(qū)分出是過時還是超長定時的吧?*/{vec=base->tv1.vec+(base->current_index&TVR_MASK);/*放到第一個輪的當前槽*/}elseif(idxtv1.vec+i;}elseif(idx<1<<(TVR_BITS+TVN_BITS))/*第二個輪*/{inti=(expires>>TVR_BITS)&TVN_MASK;vec=base->tv2.vec+i;}elseif(idx<1<<(TVR_BITS+2*TVN_BITS))/*第三個輪*/{inti=(expires>>(TVR_BITS+TVN_BITS))&TVN_MASK;vec=base->tv3.vec+i;}elseif(idx<1<<(TVR_BITS+3*TVN_BITS))/*第四個輪*/{inti=(expires>>(TVR_BITS+2*TVN_BITS))&TVN_MASK;vec=base->tv4.vec+i;}else/*第五個輪*/{inti;if(idx>0xffffffffUL){idx=0xffffffffUL;expires=idx+base->current_index;}i=(expires>>(TVR_BITS+3*TVN_BITS))&TVN_MASK;vec=base->tv5.vec+i;}#else/*上面可以優(yōu)化吧*/;#endiflist_add_tail(&timer->entry,vec);}staticinlinevoiddetach_timer(structtimer_list*timer){structlist_head*entry=&timer->entry;__list_del(entry->prev,entry->next);entry->next=NULL;entry->prev=NULL;}staticint__mod_timer(structtimer_list*timer,unsignedlongexpires){if(NULL!=timer->entry.next)detach_timer(timer);internal_add_timer(timer->base,timer);return0;}//修改定時器的超時時間外部接口intmod_timer(void*ptimer,unsignedlongexpires){structtimer_list*timer=(structtimer_list*)ptimer;structtvec_base*base;base=timer->base;if(NULL==base)return-1;expires=expires+base->current_index;if(timer->entry.next!=NULL&&timer->expires==expires)return0;if(NULL==timer->function){errlog("timer"stimeoutfunctionisnull");return-1;}timer->expires=expires;return__mod_timer(timer,expires);}//添加一個定時器staticvoid__ti_add_timer(structtimer_list*timer){if(NULL!=timer->entry.next){errlog("timerisalreadyexist");return;}mod_timer(timer,timer->expires);}/*添加一個定時器外部接口*返回定時器*/void*ti_add_timer(void*ptimewheel,unsignedlongexpires,timeouthandlephandle,unsignedlongarg){structtimer_list*ptimer;ptimer=(structtimer_list*)malloc(sizeof(structtimer_list));if(NULL==ptimer)returnNULL;bzero(ptimer,sizeof(structtimer_list));ptimer->entry.next=NULL;ptimer->base=(structtvec_base*)ptimewheel;ptimer->expires=expires;ptimer->function=phandle;ptimer->data=arg;__ti_add_timer(ptimer);returnptimer;}/**刪除一個定時器外部接口***/voidti_del_timer(void*p){structtimer_list*ptimer=(structtimer_list*)p;if(NULL==ptimer)return;if(NULL!=ptimer->entry.next)detach_timer(ptimer);free(ptimer);}/*時間輪級聯(lián)*/staticintcascade(structtvec_base*base,structtvec*tv,intindex){structlist_head*pos,*tmp;structtimer_list*timer;structlist_headtv_list;/*將tv[index]槽位上的所有任務(wù)轉(zhuǎn)移給tv_list,然后清空tv[index]*/list_replace_init(tv->vec+index,&tv_list);/*用tv_list替換tv->vec+index*/list_for_each_safe(pos,tmp,&tv_list)/*遍歷tv_list雙向鏈表,將任務(wù)重新添加到時間輪*/{timer=list_entry(pos,structtimer_list,entry);/*structtimer_list中成員entry的地址是pos,獲取structtimer_list的首地址*/internal_add_timer(base,timer);}returnindex;}staticvoid*deal_function_timeout(void*base){structtimer_list*timer;intret;structtimevaltv;structtvec_base*ba=(structtvec_base*)base;for(;;){gettimeofday(&tv,NULL);while(ba->current_index<=(tv.tv_sec*1000+tv.tv_usec/1000))/*單位:ms*/{structlist_headwork_list;intindex=ba->current_index&TVR_MASK;/*獲取第一個輪上的指針位置*/structlist_head*head=&work_list;/*指針指向0槽時,級聯(lián)輪需要更新任務(wù)列表*/if(!index&&(!cascade(ba,&ba->tv2,INDEX(0)))&&(!cascade(ba,&ba->tv3,INDEX(1)))&&(!cascade(ba,&ba->tv4,INDEX(2))))cascade(ba,&ba->tv5,INDEX(3));ba->current_index++;list_replace_init(ba->tv1.vec+index,&work_list);while(!list_empty(head)){void(*fn)(unsignedlong);unsignedlongdata;timer=list_first_entry(head,structtimer_list,entry);fn=timer->function;data=timer->data;detach_timer(timer);(*fn)(data);}}}}staticvoidinit_tvr_list(structtvec_root*tvr){inti;for(i=0;ivec[i]);}staticvoidinit_tvn_list(structtvec*tvn){inti;for(i=0;ivec[i]);}//創(chuàng)建時間輪外部接口void*ti_timewheel_create(void){structtvec_base*base;intret=0;structtimevaltv;base=(structtvec_base*)malloc(sizeof(structtvec_base));if(NULL==base)returnNULL;bzero(base,sizeof(structtvec_base));init_tvr_list(&base->tv1);init_tvn_list(&base->tv2);init_tvn_list(&base->tv3);init_tvn_list(&base->tv4);init_tvn_list(&base->tv5);gettimeofday(&tv,NULL);base->current_index=tv.tv_sec*1000+tv.tv_usec/1000;/*當前時間毫秒數(shù)*/if(0!=pthread_create(&base->threadID,NULL,deal_function_timeout,base)){free(base);returnNULL;}returnbase;}staticvoidti_release_tvr(structtvec_root*pvr){inti;structlist_head*pos,*tmp;structtimer_list*pen;for(i=0;ivec[i]){pen=list_entry(pos,structtimer_list,entry);list_del(pos);free(pen);}}}staticvoidti_release_tvn(structtvec*pvn){inti;structlist_head*pos,*tmp;structtimer_list*pen;for(i=0;ivec[i]){pen=list_entry(pos,structtimer_list,entry);list_del(pos);free(pen);}}}/**釋放時間輪外部接口**/voidti_timewheel_release(void*pwheel){structtvec_base*base=(structtvec_base*)pwheel;if(NULL==base)return;ti_release_tvr(&base->tv1);ti_release_tvn(&base->tv2);ti_release_tvn(&base->tv3);ti_release_tvn(&base->tv4);ti_release_tvn(&base->tv5);free(pwheel);}/************demo****************/structrequest_para{void*timer;intval;};voidmytimer(unsignedlongarg){structrequest_para*para=(structrequest_para*)arg;log("%d",para->val);mod_timer(para->timer,3000);//進行再次啟動定時器sleep(10);/*定時器依然被阻塞*///定時器資源的釋放是在這里完成的//ti_del_timer(para->timer);}intmain(intargc,char*argv[]){void*pwheel=NULL;void*timer=NULL;structrequest_para*para;para=(structrequest_para*)malloc(sizeof(structrequest_para));if(NULL==para)return0;bzero(para,sizeof(structrequest_para));//創(chuàng)建一個時間輪pwheel=ti_timewheel_create();if(NULL==pwheel)return-1;//添加一個定時器para->val=100;para->timer=ti_add_timer(pwheel,3000,&mytimer,(unsignedlong)para);while(1){sleep(2);}//釋放時間輪ti_timewheel_release(pwheel);return0;}

2.4 編譯運行

peng@ubuntu:/mnt/hgfs/timer/4.timerwheel/2.多級時間輪$lsa.outlist.hlog.hmutiTimeWheel.ctoney@ubantu:/mnt/hgfs/timer錄/4.timerwheel/2.多級時間輪$gccmutiTimeWheel.c-lpthreadtoney@ubantu:/mnt/hgfs/timer/4.timerwheel/2.多級時間輪$./a.out[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100[#DEBUG:ToenySun:mytimer:370]:100

從結(jié)果可以看出:如果添加的定時任務(wù)是比較耗時的操作,那么后續(xù)的任務(wù)也會被阻塞,可能一直到超時,甚至一直阻塞下去,這個取決于當前任務(wù)是否耗時。

這個理論上是絕不能接受的:一個任務(wù)不應(yīng)該也不能去影響其他的任務(wù)吧。但是目前沒有對此問題進行改進和完善,以后有機會再繼續(xù)完善吧。

編輯:黃飛

標簽:

上一篇:每日報道:單片機硬件消抖電路實現(xiàn)方法
下一篇:最后一頁