summaryrefslogtreecommitdiff
path: root/Kernel/Scheduler.h
AgeCommit message (Collapse)Author
2021-08-22Kernel: Rename SpinLock => SpinlockAndreas Kling
2021-08-08Kernel: Rename queue_runnable_thread() => enqueue_runnable_thread()Andreas Kling
2021-08-07Kernel: Move SpinLock.h into Locking/Jean-Baptiste Boric
2021-07-26Kernel: Remove unused Scheduler::yield_from_critical()Andreas Kling
2021-07-18Everywhere: Make tracking cpu usage independent from system ticksTom
This switches tracking CPU usage to more accurately measure time in user and kernel land using either the TSC or another time source. This will also come in handy when implementing a tickless kernel mode.
2021-07-18Everywhere: Improve CPU usage calculationTom
As threads come and go, we can't simply account for how many time slices the threads at any given point may have been using. We need to also account for threads that have since disappeared. This means we also need to track how many time slices we have expired globally. However, because this doesn't account for context switches outside of the system timer tick values may still be under-reported. To solve this we will need to track more accurate time information on each context switch. This also fixes top's cpu usage calculation which was still based on the number of context switches. Fixes #6473
2021-07-15Kernel: Optionally dump scheduler state with stack tracesTom
This will dump stack traces of all threads when pressing Ctrl+Shift+Alt+F12
2021-07-12Kernel: Avoid unnecessary context switch when no other thread is readyTom
If no other thread is ready to be run we don't need to switch to the idle thread and wait for the next timer interrupt. We can just give the thread another timeslice and keep it running.
2021-07-11Kernel: Use Forward.h headers moreAndreas Kling
2021-07-05Kernel+LibC: Remove sys$donate()Andreas Kling
This was an old SerenityOS-specific syscall for donating the remainder of the calling thread's time-slice to another thread within the same process. Now that Threading::Lock uses a pthread_mutex_t internally, we no longer need this syscall, which allows us to get rid of a surprising amount of unnecessary scheduler logic. :^)
2021-06-25Kernel: Add Scheduler::is_initializedSahan Fernando
2021-05-30Kernel: Don't log profile data before/after the process/thread lifetimeGunnar Beutner
There were a few cases where we could end up logging profiling events before or after the associated process or thread exists in the profile: After enabling profiling we might end up with CPU samples before we had a chance to synthesize process/thread creation events. After a thread exits we would still log associated kmalloc/kfree events. Instead we now just ignore those events.
2021-05-19Kernel: Add support for profiling kmalloc()/kfree()Gunnar Beutner
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-04-18Kernel: Add magic key combo (Alt+Shift+F12) to dump scheduler stateAndreas Kling
Pressing this combo will dump a list of all threads and their state to the debug console. This might be useful to figure out why the system is not responding.
2021-02-07Kernel: Remove ancient unused Scheduler::beep() declarationAndreas Kling
2021-01-28Kernel: Retire SchedulerData and add Thread lookup tableTom
This allows us to get rid of the thread lists in SchedulerData. Also, instead of iterating over all threads to find a thread by id, just use a lookup table. In the rare case of having to iterate over all threads, just iterate the lookup table.
2021-01-27Kernel: Implement thread priority queuesTom
Rather than walking all Thread instances and putting them into a vector to be sorted by priority, queue them into priority sorted linked lists as soon as they become ready to be executed.
2020-12-12Kernel: Fix some issues related to fixes and block conditionsTom
Fix some problems with join blocks where the joining thread block condition was added twice, which lead to a crash when trying to unblock that condition a second time. Deferred block condition evaluation by File objects were also not properly keeping the File object alive, which lead to some random crashes and corruption problems. Other problems were caused by the fact that the Queued state didn't handle signals/interruptions consistently. To solve these issues we remove this state entirely, along with Thread::wait_on and change the WaitQueue into a BlockCondition instead. Also, deliver signals even if there isn't going to be a context switch to another thread. Fixes #4336 and #4330
2020-12-01Kernel: Fix some problems with Thread::wait_on and LockTom
This changes the Thread::wait_on function to not enable interrupts upon leaving, which caused some problems with page fault handlers and in other situations. It may now be called from critical sections, with interrupts enabled or disabled, and returns to the same state. This also requires some fixes to Lock. To aid debugging, a new define LOCK_DEBUG is added that enables checking for Lock leaks upon finalization of a Thread.
2020-11-30Kernel: Allow passing a thread argument for new kernel threadsTom
This adds the ability to pass a pointer to kernel thread/process. Also add the ability to use a closure as thread function, which allows passing information to a kernel thread more easily.
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-09-27Kernel: Make Thread refcountedTom
Similar to Process, we need to make Thread refcounted. This will solve problems that will appear once we schedule threads on more than one processor. This allows us to hold onto threads without necessarily holding the scheduler lock for the entire duration.
2020-09-15Kernel: Fix thread donation hanging the systemTom
Fixes two flaws in the thread donation logic: Scheduler::donate_to would never really donate, but just trigger a deferred yield. And that deferred yield never actually donated to the beneficiary. So, when we can't immediately donate, we need to save the beneficiary and use this information as soon as we can perform the deferred context switch. Fixes #3495
2020-08-03Kernel: Fix a few Thread::block related racesTom
We need to have a Thread lock to protect threading related operations, such as Thread::m_blocker which is used in Thread::block. Also, if a Thread::Blocker indicates that it should be unblocking immediately, don't actually block the Thread and instead return immediately in Thread::block.
2020-08-02Kernel: Prevent recursive calls into the schedulerTom
Upon leaving a critical section (such as a SpinLock) we need to check if we're already asynchronously invoking the Scheduler. Otherwise we might end up triggering another context switch as soon as leaving the scheduler lock. Fixes #2883
2020-07-06Kernel: Add SMP IPI supportTom
We can now properly initialize all processors without crashing by sending SMP IPI messages to synchronize memory between processors. We now initialize the APs once we have the scheduler running. This is so that we can process IPI messages from the other cores. Also rework interrupt handling a bit so that it's more of a 1:1 mapping. We need to allocate non-sharable interrupts for IPIs. This also fixes the occasional hang/crash because all CPUs now synchronize memory with each other.
2020-07-06Kernel: Various context switch fixesTom
These changes solve a number of problems with the software context swithcing: * The scheduler lock really should be held throughout context switches * Transitioning from the initial (idle) thread to another needs to hold the scheduler lock * Transitioning from a dying thread to another also needs to hold the scheduler lock * Dying threads cannot necessarily be finalized if they haven't switched out of it yet, so flag them as active while a processor is running it (the Running state may be switched to Dying while it still is actually running)
2020-07-01Kernel: Turn Thread::current and Process::current into functionsTom
This allows us to query the current thread and process on a per processor basis
2020-07-01Kernel: Implement software context switching and Processor structureTom
Moving certain globals into a new Processor structure for each CPU allows us to eventually run an instance of the scheduler on each CPU.
2020-05-16Kernel: Remove the "kernel info page" used for fast gettimeofday()Andreas Kling
We stopped using gettimeofday() in Core::EventLoop a while back, in favor of clock_gettime() for monotonic time. Maintaining an optimization for a syscall we're not using doesn't make a lot of sense, so let's go back to the old-style sys$gettimeofday().
2020-03-19Scheduler: Use monotonic time for blocking threadsLiav A
2020-03-19Kernel: Use a const reference to RegisterState in IRQ handlingLiav A
2020-02-17Kernel: Replace "current" with Thread::current and Process::currentAndreas Kling
Suggested by Sergey. The currently running Thread and Process are now Thread::current and Process::current respectively. :^)
2020-02-16Kernel: Move all code into the Kernel namespaceAndreas Kling
2020-02-16Kernel: Rename RegisterDump => RegisterStateAndreas Kling
2020-02-01Kernel: Finalizer should not go back to sleep if there's more to doAndreas Kling
Before putting itself back on the wait queue, the finalizer task will now check if there's more work to do, and if so, do it first. :^) This patch also puts a bunch of process/thread debug logging behind PROCESS_DEBUG and THREAD_DEBUG since it was unbearable to debug this stuff with all the spam.
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.
2020-01-01Kernel: Switch to eagerly restoring x86 FPU state on context switchAndreas Kling
Lazy FPU restore is well known to be vulnerable to timing attacks, and eager restore is a lot simpler anyway, so let's just do it eagerly.
2019-12-22Kernel: Get rid of "main thread" conceptAndreas Kling
The idea of all processes reliably having a main thread was nice in some ways, but cumbersome in others. More importantly, it didn't match up with POSIX thread semantics, so let's move away from it. This thread gets rid of Process::main_thread() and you now we just have a bunch of Thread objects floating around each Process. When the finalizer nukes the last Thread in a Process, it will also tear down the Process. There's a bunch of more things to fix around this, but this is where we get started :^)
2019-12-01Kernel: Use a WaitQueue to implement finalizer wakeupAndreas Kling
This gets rid of the special "Lurking" thread state and replaces it with a generic WaitQueue :^)
2019-09-14Kernel: Stop idling after handling an IRQAndreas Kling
If we receive an IRQ while the idle task is running, prevent it from re-halting the CPU after the IRQ handler returns. Instead have the idle task yield to the scheduler, so we can see if the IRQ has unblocked something.
2019-08-07Kernel: Don't create Function objects in the scheduling codeAndreas Kling
Each Function is a heap allocation, so let's make an effort to avoid doing that during scheduling. Because of header dependencies, I had to put the runnables iteration helpers in Thread.h, which is a bit meh but at least this cuts out all the kmalloc() traffic in pick_next().
2019-07-22Move runnable/non-runnable list control entirely over to SchedulerRobin Burchell
This way, we can change how the scheduler works without having to change Thread too.
2019-07-03AK: Rename the common integer typedefs to make it obvious what they are.Andreas Kling
These types can be picked up by including <AK/Types.h>: * u8, u16, u32, u64 (unsigned) * i8, i16, i32, i64 (signed)
2019-05-28Add clang-format fileRobin Burchell
Also run it across the whole tree to get everything using the One True Style. We don't yet run this in an automated fashion as it's a little slow, but there is a snippet to do so in makeall.sh.
2019-05-15Kernel: Add a beep() syscall that beeps the PC speaker.Andreas Kling
Hook this up in Terminal so that the '\a' character generates a beep. Finally emit an '\a' character in the shell line editing code when backspacing at the start of the line.
2019-04-14Kernel: Remove system.h and make the uptime global a qword.Andreas Kling
2019-03-23Kernel: Introduce threads, and refactor everything in support of it.Andreas Kling
The scheduler now operates on threads, rather than on processes. Each process has a main thread, and can have any number of additional threads. The process exits when the main thread exits. This patch doesn't actually spawn any additional threads, it merely does all the plumbing needed to make it possible. :^)
2019-02-07Kernel: When a lock is busy, donate remaining process ticks to lock holder.Andreas Kling
Since we know who's holding the lock, and we're gonna have to yield anyway, we can just ask the scheduler to donate any remaining ticks to that process.