Luna/libos/include/os/Timer.h
apio 53f8a583dc
Some checks failed
Build and test / build (push) Failing after 1m21s
libluna+libos+libui: Move Action to libluna and make it usable in the kernel
This commit adds an error-propagating constructor for Action and Function, which makes them usable in the kernel.
2024-10-19 21:25:17 +02:00

46 lines
1001 B
C++

/**
* @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;
};
}