libos: Use setitimer() for millisecond precision
This commit is contained in:
parent
678121c3ed
commit
8a57d8a9b7
@ -14,7 +14,7 @@ void update_time()
|
|||||||
|
|
||||||
g_label->set_text(StringView { buf });
|
g_label->set_text(StringView { buf });
|
||||||
|
|
||||||
os::EventLoop::the().register_timer(1, update_time);
|
os::EventLoop::the().register_timer(1000, update_time);
|
||||||
|
|
||||||
ui::App::the().main_window()->draw();
|
ui::App::the().main_window()->draw();
|
||||||
}
|
}
|
||||||
|
@ -64,11 +64,11 @@ namespace os
|
|||||||
/**
|
/**
|
||||||
* @brief Register a function to be called in a certain amount of time.
|
* @brief Register a function to be called in a certain amount of time.
|
||||||
*
|
*
|
||||||
* @param seconds The number of seconds to wait before invoking the function.
|
* @param milliseconds The number of milliseconds to wait before invoking the function.
|
||||||
* @param callback The function to call when the time is up.
|
* @param callback The function to call when the time is up.
|
||||||
* @return Result<void> Whether the operation succeeded.
|
* @return Result<void> Whether the operation succeeded.
|
||||||
*/
|
*/
|
||||||
Result<void> register_timer(unsigned int seconds, Action&& callback);
|
Result<void> register_timer(long milliseconds, Action&& callback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Run the event loop until it is asked to quit.
|
* @brief Run the event loop until it is asked to quit.
|
||||||
|
@ -12,11 +12,27 @@
|
|||||||
#include <os/File.h>
|
#include <os/File.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <sys/time.h>
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
static os::EventLoop* s_the = nullptr;
|
static os::EventLoop* s_the = nullptr;
|
||||||
|
|
||||||
|
static void alarm_milliseconds(long target)
|
||||||
|
{
|
||||||
|
struct itimerval itimer;
|
||||||
|
memset(&itimer.it_interval, 0, sizeof(itimer.it_interval));
|
||||||
|
itimer.it_value = { .tv_sec = target / 1000, .tv_usec = (target % 1000) * 1000 };
|
||||||
|
setitimer(ITIMER_REAL, &itimer, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
static long get_monotonic_time_in_milliseconds()
|
||||||
|
{
|
||||||
|
struct timespec time;
|
||||||
|
clock_gettime(CLOCK_MONOTONIC, &time);
|
||||||
|
return time.tv_sec * 1000 + time.tv_nsec / 1'000'000;
|
||||||
|
}
|
||||||
|
|
||||||
namespace os
|
namespace os
|
||||||
{
|
{
|
||||||
EventLoop::EventLoop()
|
EventLoop::EventLoop()
|
||||||
@ -62,9 +78,9 @@ namespace os
|
|||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
Result<void> EventLoop::register_timer(unsigned int seconds, Action&& callback)
|
Result<void> EventLoop::register_timer(long milliseconds, Action&& callback)
|
||||||
{
|
{
|
||||||
long target = time(NULL) + seconds;
|
long target = get_monotonic_time_in_milliseconds() + milliseconds;
|
||||||
|
|
||||||
auto* timer = TRY(make<Timer>());
|
auto* timer = TRY(make<Timer>());
|
||||||
timer->target = target;
|
timer->target = target;
|
||||||
@ -83,7 +99,7 @@ namespace os
|
|||||||
|
|
||||||
if (m_timer_queue.first().value_or(nullptr) == next)
|
if (m_timer_queue.first().value_or(nullptr) == next)
|
||||||
{
|
{
|
||||||
alarm(seconds);
|
alarm_milliseconds(milliseconds);
|
||||||
m_timer_queue.prepend(timer);
|
m_timer_queue.prepend(timer);
|
||||||
}
|
}
|
||||||
else if (next == nullptr) { m_timer_queue.append(timer); }
|
else if (next == nullptr) { m_timer_queue.append(timer); }
|
||||||
@ -158,7 +174,10 @@ namespace os
|
|||||||
auto timer = queue.remove(queue.expect_first());
|
auto timer = queue.remove(queue.expect_first());
|
||||||
|
|
||||||
auto first = queue.first();
|
auto first = queue.first();
|
||||||
if (first.has_value()) { alarm((unsigned int)(first.value()->target - time(NULL))); }
|
if (first.has_value())
|
||||||
|
{
|
||||||
|
alarm((unsigned int)(first.value()->target - get_monotonic_time_in_milliseconds()));
|
||||||
|
}
|
||||||
|
|
||||||
timer->action();
|
timer->action();
|
||||||
delete timer;
|
delete timer;
|
||||||
|
Loading…
Reference in New Issue
Block a user