Luna/libos/include/os/Timer.h

40 lines
837 B
C++

/**
* @file Timer.h
* @author apio (cloudapio.eu)
* @brief Millisecond-precision timer.
*
* @copyright Copyright (c) 2024, the Luna authors.
*
*/
#pragma once
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.h>
#include <os/Action.h>
#include <time.h>
namespace os
{
class EventLoop;
class Timer : LinkedListNode<Timer>
{
public:
static Result<OwnedPtr<Timer>> create_singleshot(unsigned int milliseconds, Action&& action);
static Result<OwnedPtr<Timer>> create_repeating(unsigned int milliseconds, Action&& action);
~Timer();
private:
timer_t m_timerid { -1 };
bool m_repeating;
struct timespec m_interval;
Action m_action;
void check_if_should_invoke_action();
Timer() = default;
friend class EventLoop;
};
}