CreateSteadyTimer

Principles

Figure 1 Process of invoking the interfaces for creating a steady timer shows the process of creating a steady timer.

Figure 1 Process of invoking the interfaces for creating a steady timer
  1. Initialize resources.

    The process is similar to that of Timers > CreateTimer > Initialize resources. You must invoke the OpenHiva::Init interface to initialize the timer.

  2. Create a steady timer.
    1. Before creating a timer, you must register a node, that is, create a node handle OpenHiva::Node n.
    2. Use the created node handle n to invoke the Hiva::CreateSteadyTimer interface to create a steady timer. After the interface is invoked, the Hiva::SteadyTimer object is returned.

      When the CreateSteadyTimer interface is invoked, the input parameter groupName must be the same as that set in the OpenHiva::Init interface. In addition, the settings of the input parameters oneshot and autostart affect the number of times that the steady timer can be triggered and the startup mode of the steady timer.

      • oneshot: indicates the number of times that the steady timer can be triggered.
        • True: The timer uses the one-off oneshot mode. During the running time of the process, the timer is triggered only once.
        • False (default value): indicates that the timer uses the periodic mode. During the running time of the process, the timer is triggered periodically until the process resources are cleared.
      • autostart: indicates the startup mode of the steady timer.
        • True (default value): indicates automatic startup. When the CreateSteadyTimer interface is returned, the timer is started.
        • False: indicates that automatic startup is disabled. When the CreateSteadyTimer interface is returned, the timer is not started. You need to manually start the timer by invoking the Hiva::SteadyTimer::Start interface.
    3. You can use the Hiva::WallTimer object to start or stop the timer (Hiva::SteadyTimer::Start/Hiva::SteadyTimer::Stop) and set the timer triggering period (Hiva::SteadyTimer::SetPeriod).

      The timer triggering period is the period for triggering the callback function. When the timer expires, the callback function of the user is executed.

  3. Release resources.

    Before the process ends, invoke the OpenHiva::Shutdown interface to clear resources. After resources are released, the defined OpenHiva interfaces cannot be used.

Sample Code

The following is a code example of key steps for creating a steady timer (for reference only):

#include <cstdlib>
#include <chrono>
#include <thread>
#include <string>
#include <stdlib.h>
#include <Hiva.h>
#include "Hiva_time/Hiva_time.h"
#include "Hiva_timer/steady_timer.h"
#include "open/init.h"
#include "open/node.h"

// Subfunction of the callback function, which is user-defined.
static void PrintEvent(const Hiva::SteadyTimerEvent &evt)
{
    std::cout<<"\tlastExpected:"<<evt.lastExpected<<std::endl;         // Time when the last callback should occur
    std::cout<<"\tlastReal:"<<evt.lastReal<<std::endl;                 // Time when the last callback occurred
    std::cout<<"\tlastExpired:"<<evt.lastExpired<<std::endl;          // Time when the last timer expires on the timer server and a notification is sent to the application
    std::cout<<"\tcurrentExpected:"<<evt.currentExpected<<std::endl;  // Time when the current callback should occur
    std::cout<<"\tcurrenReal:"<<evt.currentReal<<std::endl;           // Time when the current callback occurred
    std::cout<<"\tcurrenExpired:"<<evt.currentExpired<<std::endl;    // Time when the current timer expires on the timer server and a notification is sent to the application
}

// Users define callback functions based on their own service logic for processing after the timer expires.
static void callback1(const Hiva::SteadyTimerEvent &evt)
{
    HIVA_INFO("callback1 called:");
    PrintEvent(evt);
}

int main(int argc, char **argv)
{
    // 1. Initialize resources.
      bool oneshot = false;             // Indicates the triggering mode, which can be one-off triggering or periodic triggering.
    bool autostart = true;             // Indicates whether the timer is automatically started or manually started.
    int32_t sec = 1;                   // Indicates timer triggering period.
    int32_t sleepDuration = 60;        // Indicates run time of the simulation process.
    if (argc == 1) {
        HIVA_INFO("Default: oneshot: %d, autostart: %d, duration: %d, process sleep time:%d", (int)oneshot, (int)autostart, sec, sleepDuration);
    } elseif (argc == 5) {
        oneshot = (bool)strtol(argv[1], NULL, 10);
        autostart = (bool)strtol(argv[2], NULL, 10);
        sec = (int32_t)strtol(argv[3], NULL, 10);
        sleepDuration = (int32_t)strtol(argv[4], NULL, 10);
        HIVA_INFO("oneshot: %d, autostart: %d, duration: %d, process sleep time:%d", (int)oneshot, (int)autostart, sec, sleepDuration);
    } else {
        HIVA_ERROR("Input para is wrong, should be no input or 4 paras: oneshot, autostart, duration, process sleep time\n");
        return -1;
    }
    // Define a thread group.
    std::vector<OpenHiva::ScheduleGroup> threadGroup;
    OpenHiva::ScheduleGroup testGroup;
    testGroup.groupName =  "timer";
    testGroup.scheduleType = OpenHiva::ScheduleType::UNBIND_AICPU;
    threadGroup.push_back(testGroup);
    // Invoke the resource initialization interface.
    OpenHiva::Init(argc,argv,threadGroup);

    // 2. Create a timer.
    // Construct the Node object.
    OpenHiva::Node node("timer_client_test");
    HIVA_INFO("timer test: Client test start timer");
    // Use the CreateSteadyTimer interface to create a timer. The value of groupName must be the same as that of ThreadGroup.groupName in the Init interface.
    Hiva::SteadyTimer t = Hiva::CreateSteadyTimer(Hiva::WallDuration(sec), callback1, "timer", oneshot, autostart);
    HIVA_INFO("timer test: client test start to sleep");
    std::this_thread::sleep_for(std::chrono::seconds(sleepDuration));
    HIVA_INFO("timer test: finish sleep");

    // 3. Release resources.
    // When the process ends, invoke the ShutDown function to release resources.
    OpenHiva::Shutdown();
    return0;
}