This repository has been archived on 2024-01-25. You can view files and clone it, but cannot push or open issues or pull requests.
XF/src/simplified/xf/port/common/timeoutmanager.cpp

99 lines
3.0 KiB
C++
Raw Normal View History

2023-09-19 15:59:49 +02:00
2023-10-05 11:50:03 +02:00
#include "common/dispatcher.h"
2023-09-19 15:59:49 +02:00
#include <config/xf-config.h>
#if (USE_XF_COMMON_TIMEOUTMANAGER_CLASS != 0)
#include <cassert>
#include "xf/interface/behavior.h"
#include "xf/interface/mutex.h"
#include "timeoutmanager.h"
2023-10-17 14:18:38 +02:00
#if defined(XF_TRACE_EVENT_PUSH_POP) && (XF_TRACE_EVENT_PUSH_POP != 0)
#include "trace/trace.h"
#endif // XF_TRACE_EVENT_PUSH_POP
2023-09-19 15:59:49 +02:00
using Mutex = interface::XFMutex; // Rename XFMutex interface class to Mutex for easier use.
// Implementation of the getInstance() method of the 'interface::XFTimeoutManager' class.
//
// Note: The implementation is done here because only in this file the real XFTimeoutManager
// class is known (port specific class). An instance of the XFTimeoutManager class is
// returned by the 'interface::XFTimeoutManager' class.
2023-09-26 14:26:07 +02:00
interface::XFTimeoutManager * interface::XFTimeoutManager::getInstance() {
2023-09-19 15:59:49 +02:00
static ::XFTimeoutManager timeoutManager;
return &timeoutManager;
}
2023-10-05 11:50:03 +02:00
// TODO done: Implement code for XFTimeoutManager class
2023-09-19 15:59:49 +02:00
2023-09-26 14:26:07 +02:00
XFTimeoutManager::XFTimeoutManager() {
2023-10-17 14:18:38 +02:00
this->pMutex_ = interface::XFMutex::create();
}
2023-10-05 11:50:03 +02:00
XFTimeoutManager::~XFTimeoutManager() {
}
void XFTimeoutManager::addTimeout(XFTimeout *pNewTimeout) {
2023-10-17 14:18:38 +02:00
this->pMutex_->lock();
this->timeouts_.push_front(pNewTimeout);
this->pMutex_->unlock();
2023-10-05 11:50:03 +02:00
}
2023-09-26 14:26:07 +02:00
void XFTimeoutManager::returnTimeout(XFTimeout *pTimeout) {
2023-10-17 14:18:38 +02:00
this->pMutex_->lock();
2023-10-05 11:50:03 +02:00
XFDispatcher::getInstance()->pushEvent(pTimeout);
2023-10-17 14:18:38 +02:00
this->timeouts_.remove(pTimeout);
this->pMutex_->unlock();
2023-09-26 14:26:07 +02:00
}
void XFTimeoutManager::start(std::function<void (uint32_t)> startTimeoutManagerTimer) {
2023-10-17 14:18:38 +02:00
startTimeoutManagerTimer(this->tickInterval_);
2023-09-26 14:26:07 +02:00
}
void XFTimeoutManager::scheduleTimeout(int32_t timeoutId, int32_t interval, interface::XFBehavior *pBehavior) {
2023-10-17 14:18:38 +02:00
string str = "[DEBUG] - Schedule Timeout: ";
str += to_string(timeoutId);
//Trace::out(str);
2023-09-26 14:26:07 +02:00
::XFTimeout* timeout = new XFTimeout(timeoutId, interval, pBehavior);
addTimeout(timeout);
2023-09-26 14:26:07 +02:00
}
void XFTimeoutManager::unscheduleTimeout(int32_t timeoutId, interface::XFBehavior *pBehavior) {
2023-10-17 14:18:38 +02:00
string str = "[DEBUG] - Unschedule Timeout: ";
str += to_string(timeoutId);
//Trace::out(str);
this->pMutex_->lock();
TimeoutList::iterator it;
for(it = this->timeouts_.begin(); it != this->timeouts_.end(); it++){
if((*it)->getId()==timeoutId && (*it)->getBehavior() == pBehavior) {
it = this->timeouts_.erase(it);
}
2023-10-17 14:18:38 +02:00
}
2023-10-17 14:18:38 +02:00
this->pMutex_->unlock();
}
void XFTimeoutManager::tick() {
2023-10-17 14:18:38 +02:00
//Trace::out("[DEBUG] - Tick");
if(!this->timeouts_.empty()) {
TimeoutList::iterator it;
this->pMutex_->lock();
for(it=this->timeouts_.begin(); it != this->timeouts_.end(); it++) {
if((*it)->getRelTicks() > 0) {
(*it)->substractFromRelTicks(this->tickInterval_);
} else {
XFEvent* ev = *(it);
XFDispatcher::getInstance()->pushEvent(ev);
it = this->timeouts_.erase(it);
}
}
this->pMutex_->unlock();
}
2023-09-26 14:26:07 +02:00
}
2023-09-19 15:59:49 +02:00
#endif // USE_XF_COMMON_TIMEOUTMANAGER_CLASS