PLCnext API Documentation 25.0.2.69
Public Types | Public Member Functions | Public Attributes | List of all members
Arp::System::Commons::Chrono::Timer Class Reference

High resolution timer for interval based executions. More...

#include <Timer.hpp>

Inheritance diagram for Arp::System::Commons::Chrono::Timer:
Inheritance graph

Public Types

typedef event< void > NotifyEvent
 Definition of list of delegates to be called when timer expires.
 

Public Member Functions

 Timer (size_t interval, const String &name, size_t priority, size_t cpuAffinity=0)
 Constructs an Timer instance. More...
 
 ~Timer (void)
 Destructs this instance and frees all resources.
 
bool IsRunning (void) const
 Checks if the timer is running, i.e. Start was already invoked. More...
 
void SetInterval (size_t interval)
 Changes the interval in which the assigned methods are called. More...
 
size_t GetInterval (void) const
 Returns the interval value in microseconds. More...
 
void SetCpuAffinity (size_t affinity)
 Pins the timer to a specific CPUs inside a multiprocessor system. More...
 
size_t GetCpuAffinity (void) const
 Returns the current used CPU affinity mask. More...
 
void SetName (const String &value)
 Sets a new name for the timer. More...
 
const StringGetName (void) const
 Returns the current name of the timer. More...
 
void SetPriority (size_t value)
 Sets a new scheduling priority for the timer. More...
 
size_t GetPriority (void) const
 Returns the current scheduling priority of the timer. More...
 
void Start (void)
 Starts the execution of the timer. More...
 
void Stop (void)
 Stops the execution of the timer.
 

Public Attributes

NotifyEvent Notify
 List of class methods and classes that are called periodically whenever the predefined interval is expired.
 

Detailed Description

High resolution timer for interval based executions.

Instances of this class can be used to execute class methods periodically in a defined time intervall. The timer class itself takes care of computing the next timepoint where method has to be invoked. The developer only has to implement the class method that should be invoked whenever a specific interval expires.

A simple example that implements a counter that is incremented every 100 us is implemented by the following code

struct TimerNotifyHandler
{
TimerNotifyHandler(void) : NotifyCount(0) {}
void OnTimerNotify(void) { ++this->NotifyCount; }
int NotifyCount;
};
TimerNotifyHandler handler;
Timer timer(100, "counterTimer", 0);
timer.Notify += make_delegate(&handler, &TimerNotifyHandler::OnTimerNotify);
timer.Start()
High resolution timer for interval based executions.
Definition: Timer.hpp:55

It is also possible to ad more than one method to the notification list of the timer instance.

TimerNotifyHandler handler2;
timer.Notify += make_delegate(&handler2, &TimerNotifyHandler::OnTimerNotify);

The methods will be called in the order they are assigned to the timers notification list.

Constructor & Destructor Documentation

◆ Timer()

Arp::System::Commons::Chrono::Timer::Timer ( size_t  interval,
const String name,
size_t  priority,
size_t  cpuAffinity = 0 
)

Constructs an Timer instance.

Parameters
intervalThe timer interval in microseconds.
Parameters
nameTime to identify the timer.
priorityScheduling priority for the timer.
cpuAffinityCPU affinity mask determining on which CPU's the timer is allowed to run. For more information on CPU affinity masks see Arp::System::Commons::Threading::Timer::SetCpuAffinity.
Exceptions
Arp::System::Commons::InvalidOperationExceptionif the timer could not be created.

Member Function Documentation

◆ GetCpuAffinity()

size_t Arp::System::Commons::Chrono::Timer::GetCpuAffinity ( void  ) const

Returns the current used CPU affinity mask.

For more information on CPU affinity masks see Arp::System::Commons::Threading::Timer::SetCpuAffinity.

Returns
The current used CPU affinity mask.

◆ GetInterval()

size_t Arp::System::Commons::Chrono::Timer::GetInterval ( void  ) const

Returns the interval value in microseconds.

Returns
Interval value in microseconds.

◆ GetName()

const String & Arp::System::Commons::Chrono::Timer::GetName ( void  ) const

Returns the current name of the timer.

Returns
Current name of the timer.

◆ GetPriority()

size_t Arp::System::Commons::Chrono::Timer::GetPriority ( void  ) const

Returns the current scheduling priority of the timer.

Returns
The current scheduling priority of the timer.

◆ IsRunning()

bool Arp::System::Commons::Chrono::Timer::IsRunning ( void  ) const

Checks if the timer is running, i.e. Start was already invoked.

Returns
True if the timer is already running, otherwise false is returned.

◆ SetCpuAffinity()

void Arp::System::Commons::Chrono::Timer::SetCpuAffinity ( size_t  affinity)

Pins the timer to a specific CPUs inside a multiprocessor system.

For more information on CPU affinity masks see Arp::System::Commons::Threading::Timer::SetCpuAffinity.

Parameters
maskA mask determine which the CPU is allowed to execute the thread.

◆ SetInterval()

void Arp::System::Commons::Chrono::Timer::SetInterval ( size_t  interval)

Changes the interval in which the assigned methods are called.

This has no effect if the timer is already started.

Parameters
intervalNew interval value in microseconds.

◆ SetName()

void Arp::System::Commons::Chrono::Timer::SetName ( const String value)

Sets a new name for the timer.

Parameters
valueNew name for timer.

◆ SetPriority()

void Arp::System::Commons::Chrono::Timer::SetPriority ( size_t  value)

Sets a new scheduling priority for the timer.

Parameters
valueNew scheduling priority.

◆ Start()

void Arp::System::Commons::Chrono::Timer::Start ( void  )

Starts the execution of the timer.

Exceptions
Arp::Exceptionin case of failure or timer was already started.

The documentation for this class was generated from the following files: