此部分为.h文件
/*
* AbsTimer.h
*
* Created on: 2016骞?鏈?5鏃? * Author: yyy
*/
#ifndef INCLUDE_TIMER_ABSTIMER_H_
#define INCLUDE_TIMER_ABSTIMER_H_
#include "base/BaseTypes.h"
#include "timer/TimerContext.h"
U32 setAbsTimer(U32 workerId, U8 timerNo, U32 timerLen, UserContext userCtxt = InvalidCtxt);
void scanAbsTimer();
void killAbsTimer(U32 timerNo, U32 workerId);
#endif /* INCLUDE_TIMER_ABSTIMER_H_ */
/*
* Clock.h
*
* Created on: 2016骞?鏈?7鏃? * Author: yyy
*/
#ifndef INCLUDE_TIMER_CLOCK_H_
#define INCLUDE_TIMER_CLOCK_H_
#include "base/BaseTypes.h"
struct SysSoftClock
{
U8 second;
U8 minute;
U8 hour;
U8 day;
U8 month;
U8 rsv[3];
U16 year;
U8 week;
U8 count10ms;
};
void getCurrentSysSoftClock(SysSoftClock& clock);
ErrCode clockToSeconds(SysSoftClock& clock, U32& seconds);
ErrCode secondsToClock(U32 seconds, SysSoftClock& clock);
#endif /* INCLUDE_TIMER_CLOCK_H_ */
/*
* Clock.h
*
* Created on: 2016骞?鏈?7鏃? * Author: yyy
*/
#ifndef INCLUDE_TIMER_CLOCK_H_
#define INCLUDE_TIMER_CLOCK_H_
#include "base/BaseTypes.h"
struct SysSoftClock
{
U8 second;
U8 minute;
U8 hour;
U8 day;
U8 month;
U8 rsv[3];
U16 year;
U8 week;
U8 count10ms;
};
void getCurrentSysSoftClock(SysSoftClock& clock);
ErrCode clockToSeconds(SysSoftClock& clock, U32& seconds);
ErrCode secondsToClock(U32 seconds, SysSoftClock& clock);
#endif /* INCLUDE_TIMER_CLOCK_H_ */
//ErrCode.h
#ifndef H9FBFA506_0CD9_40C3_A2FC_222EFFD398A3
#define H9FBFA506_0CD9_40C3_A2FC_222EFFD398A3
#include "infra/base/BaseTypes.h"
const ErrCode ERR_COMMON_SUCC = 0; //成功;common,公共的
const ErrCode ERR_COMMON_FAIL = 1;//失败
const ErrCode ERR_TIMER_SUCC = ERR_COMMON_SUCC;
const ErrCode ERR_TIMER_FAIL = ERR_COMMON_FAIL;
const ErrCode ERR_TIMER_START = ERR_COMMON_FAIL + 1;
//start是2
const ErrCode ERR_TIMER_INVALID_TIMER_NO = ERR_TIMER_START + 0;
const ErrCode ERR_TIMER_ALLOC_TIMER_ID_FAILED = ERR_TIMER_START + 1;//3
const ErrCode ERR_TIMER_INVALID_WORKER_ID = ERR_TIMER_START + 2;//4
const ErrCode ERR_TIMER_CALL_MKTIME_FAIL = ERR_TIMER_START + 3;//5
const ErrCode ERR_TIMER_CALL_LOCALTIME_FAIL = ERR_TIMER_START + 4;//6
const ErrCode ERR_TIMER_INPUT_CLOCK_TOO_EARLY = ERR_TIMER_START + 5;//7
const ErrCode ERR_TIMER_INVALID_CTXT = ERR_TIMER_START + 6;//8
const ErrCode ERR_TIMER_ALLOC_TIMER_NO_FAILED = ERR_TIMER_START + 7;//9
#endif
/*
* Init.h
*
* Created on: 2016骞?鏈?7鏃? * Author: yyy
*/
#ifndef INCLUDE_TIMER_INIT_H_
#define INCLUDE_TIMER_INIT_H_
void initTimer();
struct InitializationCtrl
{
static InitializationCtrl& getInstance();
bool hasDone();
void setFlag();
private:
bool flag{false};
};
#endif /* INCLUDE_TIMER_INIT_H_ */
//RelativeTimer.h
#ifndef HE7CBB704_9C6C_4730_BB82_85DBAB5331CD
#define HE7CBB704_9C6C_4730_BB82_85DBAB5331CD
#include "base/BaseTypes.h"
#include "lock-free/DoubleLinkedList.h"
#include "timer/TimerConst.h"
#include "timer/TimerContext.h"
typedef struct RelativeTimerQueue
{
U32 elemCount;
U32 cursorForTick;
U32 cursorForTid;
List* tick[MAX_TMCB_NUM];
U8 tid[MAX_TMCB_NUM];
} RelativeTimerQueue;
void initRelativeTimer();
U32 setRelativeTimer(U32 workerId, U8 timerNo, U32 duration, U32 para, UserContext userCtxt = InvalidCtxt);
void killRelativeTimer(U32 workerId, U8 timerNo, U32 para);
void scanRelativeTimer();
#endif
ScanTimer.h
#ifndef HF5F87F13_FE4A_4211_9D98_B7351F3636B9
#define HF5F87F13_FE4A_4211_9D98_B7351F3636B9
#include "base/BaseTypes.h"
#include <pthread.h>
void initScanTimer();
pthread_attr_t getThreadAttr();
void* notifyRoutine(void* para);
void* waitRoutine(void* para);
void scanTimer();
#endif
//TimerAgt.h
#ifndef HDA4CF299_B61B_4972_A803_89A5BE802EA5
#define HDA4CF299_B61B_4972_A803_89A5BE802EA5
#include "base/BaseTypes.h"
#include "timer/TimerConst.h"
struct SysSoftClock;
ErrCode startTimer(U8 timerNo, U32 timerLen, U32 para = INVALID_PARA);
ErrCode startTimer(U8 timerNo, SysSoftClock& clock);
void stopTimer(U8 timerNo, U32 para = INVALID_PARA);
#endif
TimerCommRes.h
#ifndef HEBA645AC_8FC2_4259_B4F8_02584AA45B68
#define HEBA645AC_8FC2_4259_B4F8_02584AA45B68
#include "base/BaseTypes.h"
#include "timer/TimerConst.h"
#include "lock-free/DoubleLinkedList.h"
#include "timer/TimerContext.h"
#include "timer/Clock.h"
#include <map>
#include <vector>
typedef enum TimerType
{
RELATIVE_TYPE,
ABS_TYPE
} TimerType;
typedef struct TimerCtrlBlock
{
U8 timerNo;
U32 para;
TimerType timerType;
U32 timerId;
U32 workerId;
BOOL hasTimeOut;
UserContext userCtxt;
SysSoftClock timeoutClock;
} TimerCtrlBlock;
struct TimerGroup
{
// U32 id[MAX_WORKER_TIMER_NUM];
std::map<std::string, U32> timersInWorker;
};
struct TimerCommRes
{
static TimerCommRes& getInstance();
~TimerCommRes();
U32 allocTimerCtrlBlock(U32 workerId, U8 timerNo, U32 para);
TimerCtrlBlock* getTimerCtrlBlock(U32 timerId);
TimerCtrlBlock* getTimerCtrlBlock(U32 workerId, U8 timerNo, U32 para);
std::vector<TimerCtrlBlock*> getTimersInfo(U32 workerId);
std::vector<TimerCtrlBlock*> getRunningTimerInfo();
void freeTimerCtrlBlock(U32 timerId);
U32 getFreeBlockNum();
private:
TimerCommRes();
private:
TimerCtrlBlock tmcb[MAX_TMCB_NUM];
TimerGroup timerGroup[MAX_TMCB_NUM];
void* queue;
};
void sendTimeoutMsgToWorker(U32 msgId, U32 workerId, U32 para, UserContext userCtxt);
void deadlyChaseTimer(U32 workerId, U8 timerNo, U32 para, UserContext userCtxt);
#endif
//TimerConst.h
#ifndef H8D266168_EEF0_4844_ADD7_7584F0922828
#define H8D266168_EEF0_4844_ADD7_7584F0922828
#include "base/BaseTypes.h"
const U32 INVALID_PARA = 0xFFFFFFFF;//invalid无效的,para
const U32 MAX_TMCB_NUM = 20000;
const U8 MAX_WORKER_TIMER_NUM = 32;
const U32 INVALID_TIMER_ID = 0xFFFFFFFF;
const U8 TIMER_IN_TID_LIST = 1;
const U32 INVALID_TIMER_RECEIVER_ID = 0xFFFFFFFF;//RECEIVER,接收器
const U32 MAX_DYN_TIMER_RECEIVER_ID = 5000;
const U32 INVALID_EVENT = 0xFFFFFFFF;//EVENT项目
#endif
/*
* TimerContext.h
*
* Created on: 2016骞?鏈?5鏃? * Author: zxl
*/
#ifndef H1C0B83AB_FE58_4CCF_807F_DC32023E9908
#define H1C0B83AB_FE58_4CCF_807F_DC32023E9908
#include "infra/message/CallBackFuncDef.h"
#include "infra/base/BaseTypes.h"
enum UserContext
{
InvalidCtxt,
SaturnCtxt,
PubSubCtxt
};
struct TimerContext
{
static TimerContext& getInstance();
void inject(UserContext cxt,
RECEIVER_CALLBACK timerOutFunc,
RECEIVER_CALLBACK deadlyChaseFunc,
GET_RECEIVE_ID_CALLBACK idFunc);
U8 getContext();
RECEIVER_CALLBACK getTimerOutFunc(UserContext ctxt);
RECEIVER_CALLBACK getDeadlyChaseFunc(UserContext ctxt);
GET_RECEIVE_ID_CALLBACK getIdFunc(UserContext ctxt);
private:
U8 ctxt{0};
RECEIVER_CALLBACK timerOutFuncs[3]{nullptr};
RECEIVER_CALLBACK deadlyChaseFuncs[3]{nullptr};
GET_RECEIVE_ID_CALLBACK idFuncs[3]{nullptr};
};
UserContext getUserContext(U32& receiverId);
#endif
TimerNo.h
#ifndef H6F4A03BC_A18A_4239_9A61_052EAFFE5227
#define H6F4A03BC_A18A_4239_9A61_052EAFFE5227
#include "base/BaseTypes.h"
const U8 TIMER_NO_BEGIN = 1;
const U8 TIMER_NO_END = 64;
const U8 TIMER_NO_1 = 1;
const U8 TIMER_NO_2 = 2;
const U8 TIMER_NO_3 = 3;
const U8 TIMER_NO_4 = 4;
const U8 TIMER_NO_5 = 5;
const U8 TIMER_NO_6 = 6;
const U8 TIMER_NO_7 = 7;
const U8 TIMER_NO_8 = 8;
const U8 TIMER_NO_9 = 9;
const U8 TIMER_NO_10 = 10;
const U8 TIMER_NO_11 = 11;
const U8 TIMER_NO_12 = 12;
const U8 TIMER_NO_13 = 13;
const U8 TIMER_NO_14 = 14;
const U8 TIMER_NO_15 = 15;
const U8 TIMER_NO_16 = 16;
const U8 TIMER_NO_17 = 17;
const U8 TIMER_NO_18 = 18;
const U8 TIMER_NO_19 = 19;
const U8 TIMER_NO_20 = 20;
const U8 TIMER_NO_21 = 21;
const U8 TIMER_NO_22 = 22;
const U8 TIMER_NO_23 = 23;
const U8 TIMER_NO_24 = 24;
const U8 TIMER_NO_25 = 25;
const U8 TIMER_NO_26 = 26;
const U8 TIMER_NO_27 = 27;
const U8 TIMER_NO_28 = 28;
const U8 TIMER_NO_29 = 29;
const U8 TIMER_NO_30 = 30;
const U8 TIMER_NO_31 = 31;
const U8 TIMER_NO_32 = 32;
const U8 DYN_TIMER_NO_BEGIN = 33;
const U8 DYN_TIMER_NO_1 = DYN_TIMER_NO_BEGIN + 0;
const U8 DYN_TIMER_NO_2 = DYN_TIMER_NO_BEGIN + 1;
const U8 DYN_TIMER_NO_3 = DYN_TIMER_NO_BEGIN + 2;
const U8 DYN_TIMER_NO_4 = DYN_TIMER_NO_BEGIN + 3;
const U8 DYN_TIMER_NO_5 = DYN_TIMER_NO_BEGIN + 4;
const U8 DYN_TIMER_NO_6 = DYN_TIMER_NO_BEGIN + 5;
const U8 DYN_TIMER_NO_7 = DYN_TIMER_NO_BEGIN + 6;
const U8 DYN_TIMER_NO_8 = DYN_TIMER_NO_BEGIN + 7;
const U8 DYN_TIMER_NO_9 = DYN_TIMER_NO_BEGIN + 8;
const U8 DYN_TIMER_NO_10 = DYN_TIMER_NO_BEGIN + 9;
const U8 DYN_TIMER_NO_11 = DYN_TIMER_NO_BEGIN + 10;
const U8 DYN_TIMER_NO_12 = DYN_TIMER_NO_BEGIN + 11;
const U8 DYN_TIMER_NO_13 = DYN_TIMER_NO_BEGIN + 12;
const U8 DYN_TIMER_NO_14 = DYN_TIMER_NO_BEGIN + 13;
const U8 DYN_TIMER_NO_15 = DYN_TIMER_NO_BEGIN + 14;
const U8 DYN_TIMER_NO_16 = DYN_TIMER_NO_BEGIN + 15;
const U8 DYN_TIMER_NO_17 = DYN_TIMER_NO_BEGIN + 16;
const U8 DYN_TIMER_NO_18 = DYN_TIMER_NO_BEGIN + 17;
const U8 DYN_TIMER_NO_19 = DYN_TIMER_NO_BEGIN + 18;
const U8 DYN_TIMER_NO_20 = DYN_TIMER_NO_BEGIN + 19;
const U8 DYN_TIMER_NO_21 = DYN_TIMER_NO_BEGIN + 20;
const U8 DYN_TIMER_NO_22 = DYN_TIMER_NO_BEGIN + 21;
const U8 DYN_TIMER_NO_23 = DYN_TIMER_NO_BEGIN + 22;
const U8 DYN_TIMER_NO_24 = DYN_TIMER_NO_BEGIN + 23;
const U8 DYN_TIMER_NO_25 = DYN_TIMER_NO_BEGIN + 24;
const U8 DYN_TIMER_NO_26 = DYN_TIMER_NO_BEGIN + 25;
const U8 DYN_TIMER_NO_27 = DYN_TIMER_NO_BEGIN + 26;
const U8 DYN_TIMER_NO_28 = DYN_TIMER_NO_BEGIN + 27;
const U8 DYN_TIMER_NO_29 = DYN_TIMER_NO_BEGIN + 28;
const U8 DYN_TIMER_NO_30 = DYN_TIMER_NO_BEGIN + 29;
const U8 DYN_TIMER_NO_31 = DYN_TIMER_NO_BEGIN + 30;
const U8 DYN_TIMER_NO_32 = DYN_TIMER_NO_BEGIN + 31; //64
#endif
TimerNoAllocator.h
#ifndef HEEAC7FD9_6F8E_4EB4_9593_97B432FD12BA
#define HEEAC7FD9_6F8E_4EB4_9593_97B432FD12BA
#include "infra/base/BaseTypes.h"
struct TimerNoAllocator
{
//分配函数,三个参数,接收id,有没有para,timerNo的引用
static ErrCode alloc(U32 receiverId, bool hasPara, U8& timerNo);
static void free(U32 receiverId, U8 timerNo);
};
#endiNo