Program Listing for File tasktimer.h#
↰ Return to documentation for file (task_timer\tasktimer.h)
/********************************************************************************
* Copyright (c) 2025-2026 ZF Friedrichshafen AG
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
*
* Contributors:
* Erik Verhoeven - initial API and implementation
********************************************************************************/
#ifndef TASK_TIMER_H
#define TASK_TIMER_H
#include <interfaces/core.h>
#include <interfaces/timer.h>
#include <support/interface_ptr.h>
#include <support/component_impl.h>
#include <map>
#include <set>
#include <fstream>
#include <atomic>
#ifdef _WIN32
// Resolve conflict
#pragma push_macro("interface")
#undef interface
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <WinSock2.h>
#include <Windows.h>
#include <timeapi.h>
// Resolve conflict
#pragma pop_macro("interface")
#ifdef GetClassInfo
#undef GetClassInfo
#endif
#elif defined __unix__
#include <signal.h>
#include <unistd.h>
#include <mutex>
#include <time.h>
#include <sys/times.h>
#else
#error OS is not supported!
#endif
// Forward declaration
class CTaskTimerService;
class CTimer : public sdv::IInterfaceAccess, public sdv::IObjectDestroy
{
public:
CTimer(CTaskTimerService& rtimersvc, uint32_t uiPeriod, sdv::core::ITaskExecute* pExecute);
// Interface map
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::IObjectDestroy)
END_SDV_INTERFACE_MAP()
virtual void DestroyObject() override;
operator bool() const;
private:
#ifdef _WIN32
void ExecuteCallback();
#endif
sdv::CLifetimeCookie m_cookie = sdv::CreateLifetimeCookie();
CTaskTimerService& m_rtimersvc;
sdv::core::ITaskExecute* m_pExecute = nullptr;
#ifdef _WIN32
UINT m_uiTimerID = 0ul;
std::atomic_bool m_bPrioritySet = false;
#elif defined __unix__
timer_t m_timerid = 0;
std::atomic_bool m_bRunning = false;
std::mutex m_mtxExecution;
#endif
};
class CTaskTimerService : public sdv::CSdvObject, public sdv::core::ITaskTimer
{
public:
CTaskTimerService();
virtual ~CTaskTimerService() override;
// Interface map
BEGIN_SDV_INTERFACE_MAP()
SDV_INTERFACE_ENTRY(sdv::core::ITaskTimer)
END_SDV_INTERFACE_MAP()
// Object declarations
DECLARE_OBJECT_CLASS_TYPE(sdv::EObjectType::system_object)
DECLARE_OBJECT_CLASS_NAME("TaskTimerService")
DECLARE_OBJECT_SINGLETON()
virtual bool OnInitialize() override;
virtual void OnShutdown() override;
virtual sdv::IInterfaceAccess* CreateTimer(uint32_t uiPeriod, sdv::IInterfaceAccess* pTask) override;
void RemoveTimer(CTimer* pTimer);
private:
std::mutex m_mtxTasks;
std::map<CTimer*, std::unique_ptr<CTimer>> m_mapTasks;
};
DEFINE_SDV_OBJECT(CTaskTimerService)
#endif // !define TASK_TIMER_H