summaryrefslogtreecommitdiff
path: root/Kernel/TimerQueue.h
AgeCommit message (Collapse)Author
2022-04-01Everywhere: Run clang-formatIdan Horowitz
2021-09-10AK+Everywhere: Reduce the number of template parameters of IntrusiveListAli Mohammad Pur
This makes the user-facing type only take the node member pointer, and lets the compiler figure out the other needed types from that.
2021-09-07Kernel: Remove a bunch of unused TimerQueue functionsAndreas Kling
2021-08-03Kernel: Do not cancel stale timers when servicing sys$alarmBrian Gianforcaro
The sys$alarm() syscall has logic to cache a m_alarm_timer to avoid allocating a new timer for every call to alarm. Unfortunately that logic was broken, and there were conditions in which we could have a timer allocated, but it was no longer on the timer queue, and we would attempt to cancel that timer again resulting in an infinite loop waiting for the timers callback to fire. To fix this, we need to track if a timer is currently in use or not, allowing us to avoid attempting to cancel inactive timers. Luke and Tom did the initial investigation, I just happened to have time to write a repro and attempt a fix, so I'm adding them as the as co-authors of this commit. Co-authored-by: Luke <luke.wilde@live.co.uk> Co-authored-by: Tom <tomut@yahoo.com>
2021-07-12Kernel: Fix deadlock cancelling timerTom
It's possible that a timer may have been queued to be executed by the timer irq handler, but if we're in a critical section on the same processor and are trying to cancel that timer, we would spin forever waiting for it to be executed.
2021-06-16Kernel: Replace TimerQueue InlinedLinkedList usage with IntrusiveListBrian Gianforcaro
Note that there are a few minor differences between the InlineLinekdList and IntrusiveList API, so this isn't just a pure data structure change. - first()/last() instead of head()/tail() - There is no need for a for_each(..) implementation, as it already exposes the ability to do range based for loops.
2021-05-20Kernel: Remove an allocation when blocking a threadGunnar Beutner
When blocking a thread with a timeout we would previously allocate a Timer object. This removes the allocation for that Timer object.
2021-04-22Everything: Move to SPDX license identifiers in all files.Brian Gianforcaro
SPDX License Identifiers are a more compact / standardized way of representing file license information. See: https://spdx.dev/resources/use/#identifiers This was done with the `ambr` search and replace tool. ambr --no-parent-ignore --key-from-file --rep-from-file key.txt rep.txt *
2021-03-02Kernel: Make TimerQueue use AK::Time in interfaceBen Wiederhake
2021-03-02Kernel: Make Thread use AK::Time internallyBen Wiederhake
This commit is very invasive, because Thread likes to take a pointer and write to it. This means that translating between timespec/timeval/Time would have been more difficult than just changing everything that hands a raw pointer to Thread, in bulk.
2021-03-02Kernel: Make TimerId a distinct typeBen Wiederhake
Well, that was easy\! :^)
2021-03-02Kernel: Make TimerQueue use AK::Time internallyBen Wiederhake
2021-02-23Everywhere: Rename ASSERT => VERIFYAndreas Kling
(...and ASSERT_NOT_REACHED => VERIFY_NOT_REACHED) Since all of these checks are done in release builds as well, let's rename them to VERIFY to prevent confusion, as everyone is used to assertions being compiled out in release. We can introduce a new ASSERT macro that is specifically for debug checks, but I'm doing this wholesale conversion first since we've accumulated thousands of these already, and it's not immediately obvious which ones are suitable for ASSERT.
2021-01-04Kernel: Specify default memory order for some non-synchronizing AtomicsTom
2020-12-21Kernel: Improve time keeping and dramatically reduce interrupt loadTom
This implements a number of changes related to time: * If a HPET is present, it is now used only as a system timer, unless the Local APIC timer is used (in which case the HPET timer will not trigger any interrupts at all). * If a HPET is present, the current time can now be as accurate as the chip can be, independently from the system timer. We now query the HPET main counter for the current time in CPU #0's system timer interrupt, and use that as a base line. If a high precision time is queried, that base line is used in combination with quering the HPET timer directly, which should give a much more accurate time stamp at the expense of more overhead. For faster time stamps, the more coarse value based on the last interrupt will be returned. This also means that any missed interrupts should not cause the time to drift. * The default system interrupt rate is reduced to about 250 per second. * Fix calculation of Thread CPU usage by using the amount of ticks they used rather than the number of times a context switch happened. * Implement CLOCK_REALTIME_COARSE and CLOCK_MONOTONIC_COARSE and use it for most cases where precise timestamps are not needed.
2020-12-11Kernel: Fix leaking Timer instancesTom
When a Timer is queued we add a reference, so whenever we remove a timer or fire it we should drop that reference. Fixes #4382
2020-12-02Kernel: Add CLOCK_REALTIME support to the TimerQueueTom
This allows us to use blocking timeouts with either monotonic or real time for all blockers. Which means that clock_nanosleep() now also supports CLOCK_REALTIME. Also, switch alarm() to use CLOCK_REALTIME as per specification.
2020-12-02Kernel: Use TimerQueue for SIGALRMTom
2020-12-02Kernel: TimerQueue::cancel_timer needs to wait if timer is executingTom
We need to be able to guarantee that a timer won't be executing after TimerQueue::cancel_timer returns. In the case of multiple processors this means that we may need to wait while the timer handler finishes execution on another core. This also fixes a problem in Thread::block and Thread::wait_on where theoretically the timer could execute after the function returned and the Thread disappeared.
2020-11-30Kernel: Move some time related code from Scheduler into TimeManagementTom
Use the TimerQueue to expire blocking operations, which is one less thing the Scheduler needs to check on every iteration. Also, add a BlockTimeout class that will automatically handle relative or absolute timeouts as well as overriding timeouts (e.g. socket timeouts) more consistently. Also, rework the TimerQueue class to be able to fire events from any processor, which requires Timer to be RefCounted. Also allow creating id-less timers for use by blocking operations.
2020-08-25Kernel: Switch singletons to use new Singleton classTom
MemoryManager cannot use the Singleton class because MemoryManager::initialize is called before the global constructors are run. That caused the Singleton to be re-initialized, causing it to create another MemoryManager instance. Fixes #3226
2020-08-22Revert "Kernel: Switch singletons to use new Singleton class"Andreas Kling
This reverts commit f48feae0b2a300992479abf0b2ded85e45ac6045.
2020-08-21Kernel: Switch singletons to use new Singleton classTom
Fixes #3226
2020-04-27Kernel: Expose timers via a TimerId typeBrian Gianforcaro
The public consumers of the timer API shouldn't need to know the how timer id's are tracked internally. Expose a typedef instead to allow the internal implementation to be protected from potential churn in the future. It's also just good API design.
2020-04-26Kernel: Refactor TimeQueue::add_timer to use timevalBrian Gianforcaro
The current API of add_timer makes it hard to use as you are forced to do a bunch of time arithmetic at the caller. Ideally we would have overloads for common time types like timespec or timeval to keep the API as straight forward as possible. This change moves us in that direction. While I'm here, we should really also use the machines actual ticks per second, instead of the OPTIMAL_TICKS_PER_SECOND_RATE.
2020-03-19Kernel: Introduce the new Time management subsystemLiav A
This new subsystem includes better abstractions of how time will be handled in the OS. We take advantage of the existing RTC timer to aid in keeping time synchronized. This is standing in contrast to how we handled time-keeping in the kernel, where the PIT was responsible for that function in addition to update the scheduler about ticks. With that new advantage, we can easily change the ticking dynamically and still keep the time synchronized. In the process context, we no longer use a fixed declaration of TICKS_PER_SECOND, but we call the TimeManagement singleton class to provide us the right value. This allows us to use dynamic ticking in the future, a feature known as tickless kernel. The scheduler no longer does by himself the calculation of real time (Unix time), and just calls the TimeManagment singleton class to provide the value. Also, we can use 2 new boot arguments: - the "time" boot argument accpets either the value "modern", or "legacy". If "modern" is specified, the time management subsystem will try to setup HPET. Otherwise, for "legacy" value, the time subsystem will revert to use the PIT & RTC, leaving HPET disabled. If this boot argument is not specified, the default pattern is to try to setup HPET. - the "hpet" boot argumet accepts either the value "periodic" or "nonperiodic". If "periodic" is specified, the HPET will scan for periodic timers, and will assert if none are found. If only one is found, that timer will be assigned for the time-keeping task. If more than one is found, both time-keeping task & scheduler-ticking task will be assigned to periodic timers. If this boot argument is not specified, the default pattern is to try to scan for HPET periodic timers. This boot argument has no effect if HPET is disabled. In hardware context, PIT & RealTimeClock classes are merely inheriting from the HardwareTimer class, and they allow to use the old i8254 (PIT) and RTC devices, managing them via IO ports. By default, the RTC will be programmed to a frequency of 1024Hz. The PIT will be programmed to a frequency close to 1000Hz. About HPET, depending if we need to scan for periodic timers or not, we try to set a frequency close to 1000Hz for the time-keeping timer and scheduler-ticking timer. Also, if possible, we try to enable the Legacy replacement feature of the HPET. This feature if exists, instructs the chipset to disconnect both i8254 (PIT) and RTC. This behavior is observable on QEMU, and was verified against the source code: https://github.com/qemu/qemu/commit/ce967e2f33861b0e17753f97fa4527b5943c94b6 The HPETComparator class is inheriting from HardwareTimer class, and is responsible for an individual HPET comparator, which is essentially a timer. Therefore, it needs to call the singleton HPET class to perform HPET-related operations. The new abstraction of Hardware timers brings an opportunity of more new features in the foreseeable future. For example, we can change the callback function of each hardware timer, thus it makes it possible to swap missions between hardware timers, or to allow to use a hardware timer for other temporary missions (e.g. calibrating the LAPIC timer, measuring the CPU frequency, etc).
2020-02-24Kernel: Include the new PIT class in system componentsLiav A
2020-02-16Kernel: Move all code into the Kernel namespaceAndreas Kling
2020-01-18Meta: Add license header to source filesAndreas Kling
As suggested by Joshua, this commit adds the 2-clause BSD license as a comment block to the top of every source file. For the first pass, I've just added myself for simplicity. I encourage everyone to add themselves as copyright holders of any file they've added or modified in some significant way. If I've added myself in error somewhere, feel free to replace it with the appropriate copyright holder instead. Going forward, all new source files should include a license header.
2019-12-27Kernel: Add kernel-level timer queue (heavily based on @juliusf's work)Conrad Pankoff
PR #591 defines the rationale for kernel-level timers. They're most immediately useful for TCP retransmission, but will most likely see use in many other areas as well.