📄 timermanager.cc
字号:
#include "TimerManager.h"#include "TimerCache.h"#include "TimerHandler.h"#include "oamTask.h"#include <time.h>#include <signal.h>#include <pthread.h>#include <unistd.h>#include <stdio.h>#define SIGMYTIMER (SIGRTMAX)static void Timeout(int signo, siginfo_t* info, void* context){ int endTime = time(0); CommonData data; data.setData((char *)&endTime); data.setLength(sizeof(int)); TimerHandler::instance()->putq(data);}TimerManager::TimerManager():pCache_m(0){}TimerManager::~TimerManager(){ idMap_m.clear();}int TimerManager::initialize(TimerCache * pCache){ pCache_m = pCache; struct sigaction sysact; sigemptyset(&sysact.sa_mask); sysact.sa_flags = SA_SIGINFO; sysact.sa_sigaction = Timeout; return sigaction(SIGMYTIMER, &sysact, 0);}timer_t TimerManager::createTimer(ITimer * timer , int interval , bool periodical, void * args){ int currentTime = time(0); int timerId = pCache_m->insert(timer , currentTime , interval , periodical , args); struct sigevent evp; timer_t nTimerID; evp.sigev_notify = SIGEV_SIGNAL; evp.sigev_signo = SIGMYTIMER; evp.sigev_value.sival_ptr = &nTimerID; int nCreate = timer_create(CLOCK_REALTIME, &evp, &nTimerID); if (nCreate == 0) { struct itimerspec value; struct itimerspec ovalue; value.it_value.tv_sec = interval; value.it_value.tv_nsec = 0; if (periodical) { value.it_interval.tv_sec = value.it_value.tv_sec; value.it_interval.tv_nsec = value.it_value.tv_nsec; } else { value.it_interval.tv_sec = 0; value.it_interval.tv_nsec = 0; } timer_settime(nTimerID, 0, &value, &ovalue); } else { perror("create timer failed."); return (timer_t)-1; } idMap_m[nTimerID] = timerId; return nTimerID; } int TimerManager::deleteTimer(timer_t timerId){ int ret = timer_delete(timerId); if (ret == -1) { perror("delete timer failed."); return -1; } ret = pCache_m->remove(idMap_m[timerId]); if (ret == -1) { perror("remove the timer from the cache failed."); return -1; } TimerIdMap::iterator it = idMap_m.find(timerId); if (it != idMap_m.end()) { idMap_m.erase(it); } else { perror("can not find the timerId from the id map."); return -1; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -