Compare commits

...

3 Commits

Author SHA1 Message Date
e97b61ef16
gol: Use EventLoop timers
All checks were successful
continuous-integration/drone/push Build is passing
2023-11-16 22:02:31 +01:00
d1d53c6891
kernel: Remove signal debug messages 2023-11-16 22:02:17 +01:00
8a57d8a9b7
libos: Use setitimer() for millisecond precision 2023-11-16 21:58:45 +01:00
6 changed files with 36 additions and 35 deletions

View File

@ -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();
} }

View File

@ -101,6 +101,13 @@ static void next_generation()
for (isize i = 0; i < (g_num_rows * g_num_columns); i++) g_cells[i].state = g_cells[i].new_state; for (isize i = 0; i < (g_num_rows * g_num_columns); i++) g_cells[i].state = g_cells[i].new_state;
} }
static void update()
{
next_generation();
draw_cells();
os::EventLoop::the().register_timer(100, update);
}
Result<int> luna_main(int argc, char** argv) Result<int> luna_main(int argc, char** argv)
{ {
ui::App app; ui::App app;
@ -112,20 +119,7 @@ Result<int> luna_main(int argc, char** argv)
TRY(fill_cells()); TRY(fill_cells());
int counter = 0; os::EventLoop::the().register_timer(100, update);
while (app.process_events()) return app.run();
{
if (counter >= 10)
{
next_generation();
draw_cells();
counter = 0;
}
else
counter++;
usleep(10000);
}
return 0;
} }

View File

@ -106,9 +106,6 @@ bool Thread::deliver_signal(int signo, Registers* current_regs)
memcpy(&regs, current_regs, sizeof(regs)); memcpy(&regs, current_regs, sizeof(regs));
kinfoln("signal: delivering signal %d for thread %d, ip=%p, sp=%p, handler=%p, sigreturn=%p", signo, id,
(void*)regs.rip, (void*)regs.rsp, (void*)handler.sa_handler, (void*)handler.__sa_sigreturn);
regs.rsp -= 128; // Skip the red zone regs.rsp -= 128; // Skip the red zone
fp_data.save(); fp_data.save();
@ -165,7 +162,5 @@ void Thread::sigreturn(Registers* current_regs)
fp_data.restore(); fp_data.restore();
kinfoln("sigreturn: restored program state, sp=%p, ip=%p", (void*)regs.rsp, (void*)regs.rip);
memcpy(current_regs, &regs, sizeof(regs)); memcpy(current_regs, &regs, sizeof(regs));
} }

View File

@ -171,13 +171,8 @@ void Thread::process_pending_signals(Registers* current_regs)
if (pending_signals.get(i)) if (pending_signals.get(i))
{ {
pending_signals.set(i, false); pending_signals.set(i, false);
kinfoln("signal: executing signal %d for thread %d", signo, id);
auto handler = signal_handlers[i]; auto handler = signal_handlers[i];
if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN) if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN) return;
{
kinfoln("signal: ignoring signal (handler=SIG_IGN)");
return;
}
if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP) if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP)
{ {
default_signal: default_signal:
@ -187,8 +182,6 @@ void Thread::process_pending_signals(Registers* current_regs)
return; return;
} }
kinfoln("signal: using default behavior (handler=SIG_DFL)");
auto action = default_actions[i]; auto action = default_actions[i];
switch (action) switch (action)
{ {

View File

@ -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.

View File

@ -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;