Compare commits
3 Commits
678121c3ed
...
e97b61ef16
Author | SHA1 | Date | |
---|---|---|---|
e97b61ef16 | |||
d1d53c6891 | |||
8a57d8a9b7 |
@ -14,7 +14,7 @@ void update_time()
|
||||
|
||||
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();
|
||||
}
|
||||
|
24
apps/gol.cpp
24
apps/gol.cpp
@ -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;
|
||||
}
|
||||
|
||||
static void update()
|
||||
{
|
||||
next_generation();
|
||||
draw_cells();
|
||||
os::EventLoop::the().register_timer(100, update);
|
||||
}
|
||||
|
||||
Result<int> luna_main(int argc, char** argv)
|
||||
{
|
||||
ui::App app;
|
||||
@ -112,20 +119,7 @@ Result<int> luna_main(int argc, char** argv)
|
||||
|
||||
TRY(fill_cells());
|
||||
|
||||
int counter = 0;
|
||||
os::EventLoop::the().register_timer(100, update);
|
||||
|
||||
while (app.process_events())
|
||||
{
|
||||
if (counter >= 10)
|
||||
{
|
||||
next_generation();
|
||||
draw_cells();
|
||||
counter = 0;
|
||||
}
|
||||
else
|
||||
counter++;
|
||||
usleep(10000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return app.run();
|
||||
}
|
||||
|
@ -106,9 +106,6 @@ bool Thread::deliver_signal(int signo, Registers* current_regs)
|
||||
|
||||
memcpy(®s, 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
|
||||
|
||||
fp_data.save();
|
||||
@ -165,7 +162,5 @@ void Thread::sigreturn(Registers* current_regs)
|
||||
|
||||
fp_data.restore();
|
||||
|
||||
kinfoln("sigreturn: restored program state, sp=%p, ip=%p", (void*)regs.rsp, (void*)regs.rip);
|
||||
|
||||
memcpy(current_regs, ®s, sizeof(regs));
|
||||
}
|
||||
|
@ -171,13 +171,8 @@ void Thread::process_pending_signals(Registers* current_regs)
|
||||
if (pending_signals.get(i))
|
||||
{
|
||||
pending_signals.set(i, false);
|
||||
kinfoln("signal: executing signal %d for thread %d", signo, id);
|
||||
auto handler = signal_handlers[i];
|
||||
if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN)
|
||||
{
|
||||
kinfoln("signal: ignoring signal (handler=SIG_IGN)");
|
||||
return;
|
||||
}
|
||||
if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN) return;
|
||||
if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP)
|
||||
{
|
||||
default_signal:
|
||||
@ -187,8 +182,6 @@ void Thread::process_pending_signals(Registers* current_regs)
|
||||
return;
|
||||
}
|
||||
|
||||
kinfoln("signal: using default behavior (handler=SIG_DFL)");
|
||||
|
||||
auto action = default_actions[i];
|
||||
switch (action)
|
||||
{
|
||||
|
@ -64,11 +64,11 @@ namespace os
|
||||
/**
|
||||
* @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.
|
||||
* @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.
|
||||
|
@ -12,11 +12,27 @@
|
||||
#include <os/File.h>
|
||||
#include <signal.h>
|
||||
#include <string.h>
|
||||
#include <sys/time.h>
|
||||
#include <time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
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
|
||||
{
|
||||
EventLoop::EventLoop()
|
||||
@ -62,9 +78,9 @@ namespace os
|
||||
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>());
|
||||
timer->target = target;
|
||||
@ -83,7 +99,7 @@ namespace os
|
||||
|
||||
if (m_timer_queue.first().value_or(nullptr) == next)
|
||||
{
|
||||
alarm(seconds);
|
||||
alarm_milliseconds(milliseconds);
|
||||
m_timer_queue.prepend(timer);
|
||||
}
|
||||
else if (next == nullptr) { m_timer_queue.append(timer); }
|
||||
@ -158,7 +174,10 @@ namespace os
|
||||
auto timer = queue.remove(queue.expect_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();
|
||||
delete timer;
|
||||
|
Loading…
Reference in New Issue
Block a user