Compare commits
No commits in common. "e97b61ef1669b1a20a6de4cbb9c2d577c205d8c2" and "678121c3edc33c5da7b290a87dfde949ac2a1ee9" have entirely different histories.
e97b61ef16
...
678121c3ed
@ -14,7 +14,7 @@ void update_time()
|
||||
|
||||
g_label->set_text(StringView { buf });
|
||||
|
||||
os::EventLoop::the().register_timer(1000, update_time);
|
||||
os::EventLoop::the().register_timer(1, update_time);
|
||||
|
||||
ui::App::the().main_window()->draw();
|
||||
}
|
||||
|
24
apps/gol.cpp
24
apps/gol.cpp
@ -101,13 +101,6 @@ 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;
|
||||
@ -119,7 +112,20 @@ Result<int> luna_main(int argc, char** argv)
|
||||
|
||||
TRY(fill_cells());
|
||||
|
||||
os::EventLoop::the().register_timer(100, update);
|
||||
int counter = 0;
|
||||
|
||||
return app.run();
|
||||
while (app.process_events())
|
||||
{
|
||||
if (counter >= 10)
|
||||
{
|
||||
next_generation();
|
||||
draw_cells();
|
||||
counter = 0;
|
||||
}
|
||||
else
|
||||
counter++;
|
||||
usleep(10000);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -106,6 +106,9 @@ 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();
|
||||
@ -162,5 +165,7 @@ 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,8 +171,13 @@ 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) return;
|
||||
if (signo != SIGKILL && signo != SIGSTOP && handler.sa_handler == SIG_IGN)
|
||||
{
|
||||
kinfoln("signal: ignoring signal (handler=SIG_IGN)");
|
||||
return;
|
||||
}
|
||||
if (handler.sa_handler == SIG_DFL || signo == SIGKILL || signo == SIGSTOP)
|
||||
{
|
||||
default_signal:
|
||||
@ -182,6 +187,8 @@ 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 milliseconds The number of milliseconds to wait before invoking the function.
|
||||
* @param seconds The number of seconds 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(long milliseconds, Action&& callback);
|
||||
Result<void> register_timer(unsigned int seconds, Action&& callback);
|
||||
|
||||
/**
|
||||
* @brief Run the event loop until it is asked to quit.
|
||||
|
@ -12,27 +12,11 @@
|
||||
#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()
|
||||
@ -78,9 +62,9 @@ namespace os
|
||||
return {};
|
||||
}
|
||||
|
||||
Result<void> EventLoop::register_timer(long milliseconds, Action&& callback)
|
||||
Result<void> EventLoop::register_timer(unsigned int seconds, Action&& callback)
|
||||
{
|
||||
long target = get_monotonic_time_in_milliseconds() + milliseconds;
|
||||
long target = time(NULL) + seconds;
|
||||
|
||||
auto* timer = TRY(make<Timer>());
|
||||
timer->target = target;
|
||||
@ -99,7 +83,7 @@ namespace os
|
||||
|
||||
if (m_timer_queue.first().value_or(nullptr) == next)
|
||||
{
|
||||
alarm_milliseconds(milliseconds);
|
||||
alarm(seconds);
|
||||
m_timer_queue.prepend(timer);
|
||||
}
|
||||
else if (next == nullptr) { m_timer_queue.append(timer); }
|
||||
@ -174,10 +158,7 @@ namespace os
|
||||
auto timer = queue.remove(queue.expect_first());
|
||||
|
||||
auto first = queue.first();
|
||||
if (first.has_value())
|
||||
{
|
||||
alarm((unsigned int)(first.value()->target - get_monotonic_time_in_milliseconds()));
|
||||
}
|
||||
if (first.has_value()) { alarm((unsigned int)(first.value()->target - time(NULL))); }
|
||||
|
||||
timer->action();
|
||||
delete timer;
|
||||
|
Loading…
Reference in New Issue
Block a user