Luna/libos/include/os/Timer.h

46 lines
1001 B
C
Raw Normal View History

/**
* @file Timer.h
* @author apio (cloudapio.eu)
* @brief Millisecond-precision timer.
*
* @copyright Copyright (c) 2024, the Luna authors.
*
*/
#pragma once
#include <luna/Action.h>
#include <luna/LinkedList.h>
#include <luna/OwnedPtr.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);
void restart();
void reset(unsigned int milliseconds);
void stop();
~Timer();
private:
timer_t m_timerid { -1 };
bool m_repeating;
bool m_stopped { false };
struct timespec m_interval;
Action m_action;
void check_if_should_invoke_action();
void readd_if_necessary();
Timer() = default;
friend class EventLoop;
};
}