mirror of
https://github.com/RGBCube/serenity
synced 2025-06-01 08:28:11 +00:00
Implement event loop timers.
GObjects can now register a timer with the GEventLoop. This will eventually cause GTimerEvents to be dispatched to the GObject. This needed a few supporting changes in the kernel: - The PIT now ticks 1000 times/sec. - select() now supports an arbitrary timeout. - gettimeofday() now returns something in the tv_usec field. With these changes, the clock window in guitest2 finally ticks on its own.
This commit is contained in:
parent
9153666e72
commit
95c3442d59
10 changed files with 140 additions and 7 deletions
|
@ -1442,7 +1442,7 @@ int Process::sys$gettimeofday(timeval* tv)
|
|||
InterruptDisabler disabler;
|
||||
auto now = RTC::now();
|
||||
tv->tv_sec = now;
|
||||
tv->tv_usec = 0;
|
||||
tv->tv_usec = PIT::ticks_since_boot() % 1000;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1976,8 +1976,12 @@ int Process::sys$select(const Syscall::SC_select_params* params)
|
|||
// FIXME: Implement exceptfds support.
|
||||
ASSERT(!exceptfds);
|
||||
|
||||
// FIXME: Implement timeout support.
|
||||
ASSERT(!timeout || (!timeout->tv_sec && !timeout->tv_usec));
|
||||
if (timeout) {
|
||||
m_select_timeout = *timeout;
|
||||
m_select_has_timeout = true;
|
||||
} else {
|
||||
m_select_has_timeout = false;
|
||||
}
|
||||
|
||||
if (nfds < 0)
|
||||
return -EINVAL;
|
||||
|
|
|
@ -331,6 +331,8 @@ private:
|
|||
int m_blocked_fd { -1 };
|
||||
Vector<int> m_select_read_fds;
|
||||
Vector<int> m_select_write_fds;
|
||||
timeval m_select_timeout;
|
||||
bool m_select_has_timeout { false };
|
||||
size_t m_max_open_file_descriptors { 16 };
|
||||
SignalActionData m_signal_action_data[32];
|
||||
dword m_pending_signals { 0 };
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#include "Scheduler.h"
|
||||
#include "Process.h"
|
||||
#include "system.h"
|
||||
#include "RTC.h"
|
||||
#include "i8253.h"
|
||||
|
||||
//#define LOG_EVERY_CONTEXT_SWITCH
|
||||
//#define SCHEDULER_DEBUG
|
||||
|
@ -29,7 +31,7 @@ bool Scheduler::pick_next()
|
|||
}
|
||||
|
||||
// Check and unblock processes whose wait conditions have been met.
|
||||
Process::for_each([] (auto& process) {
|
||||
Process::for_each([] (Process& process) {
|
||||
if (process.state() == Process::BlockedSleep) {
|
||||
if (process.wakeup_time() <= system.uptime)
|
||||
process.unblock();
|
||||
|
@ -71,6 +73,14 @@ bool Scheduler::pick_next()
|
|||
process.unblock();
|
||||
return true;
|
||||
}
|
||||
if (process.m_select_has_timeout) {
|
||||
auto now_sec = RTC::now();
|
||||
auto now_usec = PIT::ticks_since_boot() % 1000;
|
||||
if (now_sec > process.m_select_timeout.tv_sec || (now_sec == process.m_select_timeout.tv_sec && now_usec >= process.m_select_timeout.tv_usec)) {
|
||||
process.unblock();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
for (int fd : process.m_select_read_fds) {
|
||||
if (process.m_fds[fd].descriptor->can_read(process)) {
|
||||
process.unblock();
|
||||
|
|
|
@ -57,16 +57,26 @@ asm(
|
|||
|
||||
#define BASE_FREQUENCY 1193182
|
||||
|
||||
static dword s_ticks_since_boot;
|
||||
|
||||
void timer_interrupt_handler(RegisterDump& regs)
|
||||
{
|
||||
IRQHandlerScope scope(IRQ_TIMER);
|
||||
++s_ticks_since_boot;
|
||||
Scheduler::timer_tick(regs);
|
||||
}
|
||||
|
||||
namespace PIT {
|
||||
|
||||
dword ticks_since_boot()
|
||||
{
|
||||
return s_ticks_since_boot;
|
||||
}
|
||||
|
||||
void initialize()
|
||||
{
|
||||
s_ticks_since_boot = 0;
|
||||
|
||||
word timer_reload;
|
||||
|
||||
IO::out8(PIT_CTL, TIMER0_SELECT | WRITE_WORD | MODE_SQUARE_WAVE);
|
||||
|
|
|
@ -1,9 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#define TICKS_PER_SECOND 600
|
||||
#include <AK/Types.h>
|
||||
|
||||
#define TICKS_PER_SECOND 1000
|
||||
|
||||
namespace PIT {
|
||||
|
||||
void initialize();
|
||||
dword ticks_since_boot();
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue