Hiva::CreateSteadyTimer

Description

Creates a SteadyTimer.

Prototype

Hiva::SteadyTimer Hiva::CreateSteadyTimer(const Hiva::WallDuration &steadyPeriod, const SteadyTimerCallback &steadyCallback, const std::string &steadyGroupName, const bool oneshot = false, const bool autostart = true)

Parameters

Parameter

Input/Output

Description

steadyPeriod

Input

Period for triggering the callback function. The value is of the standard Duration type for the framework.

steadyCallback

Input

Callback function after the timer expires. The parameter type is SteadyTimerCallback. WallTimerEvent is as follows:

using SteadyTimerCallback = std::function<void (const SteadyTimerEvent &)>;

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
struct SteadyTimerEvent
{
    Hiva::SteadyTime last_expected;    // Time when the last callback should occur
    Hiva::SteadyTime last_real;       // Actual time when the last callback occurs
    Hiva::SteadyTime last_expired;    // Time when the last timer expires on the timer server and a notification is sent to the application
    Hiva::SteadyTime current_expected; // Time when the current callback should occur
    Hiva::SteadyTime current_real;     // Actual time when the current callback occurs
    Hiva::SteadyTime current_expired;  // Time when the current timer expires on the timer server and a notification is sent to the application
    struct{
    Hiva::WallDuration last_duration;  // Last callback execution duration
    } profile;
};

steadyGroupName

Input

Thread group created when OpenHiva::Init is invoked. For a deterministic group, the deterministic scheduling framework is used. For other groups, the OS scheduling framework is used.

oneshot

Input

Mode flag.

  • true indicates that the one-off oneshot mode is used and the timer is triggered only once.
  • false indicates that the periodic mode is used. The timer is triggered periodically until the process resources are cleared.

autostart

Input

Start flag.

  • true indicates that the timer is automatically started. When the interface returns a value, the timer is started.
  • false indicates that the timer is not automatically started. When the interface returns a value, the timer is not started and needs to be manually started through Hiva::SteadyTimer::Start.

Returns

The handle of Hiva::SteadyTimer is returned. The start and stop operations can be performed based on this handle.

Usage

A process invokes this interface to create and run a timer.

  • When creating a SteadyTimer, an application subscribes to a timer event. The timer name is generated in the format of application name + timer + sequence number to ensure uniqueness.
  • The application sends the timer name and related information to the timer server through the topic communication mode of the server/client. After the time expires, the timer server publishes the topic, notifies the original application, and executes the user callback function.

Precautions

None