summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-03-22 13:55:44 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-03-22 13:56:36 +0100
commit5d11c45b45862ca86e0aee392885e7e6d8d66bfb (patch)
tree658735e90f0a383cbec1e2e5cfb5b8ae1c9f64f8
parentdf831b1b73de1a15109be2f85eee7e10326fe0a1 (diff)
[FEAT] User Scheduler improvements and critical tweaks.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--src/kernel/KernelKit/CoreProcessScheduler.h2
-rw-r--r--src/kernel/src/BinaryMutex.cpp20
-rw-r--r--src/kernel/src/UserProcessScheduler.cpp75
3 files changed, 57 insertions, 40 deletions
diff --git a/src/kernel/KernelKit/CoreProcessScheduler.h b/src/kernel/KernelKit/CoreProcessScheduler.h
index 71a75e1a..2f105036 100644
--- a/src/kernel/KernelKit/CoreProcessScheduler.h
+++ b/src/kernel/KernelKit/CoreProcessScheduler.h
@@ -146,7 +146,7 @@ enum struct ProcessStatusKind : Int32 {
/***********************************************************************************/
enum struct AffinityKind : Int32 {
kInvalid = 0,
- kRealTime = 100,
+ kUltraHigh = 100,
kVeryHigh = 150,
kHigh = 200,
kStandard = 1000,
diff --git a/src/kernel/src/BinaryMutex.cpp b/src/kernel/src/BinaryMutex.cpp
index e321ba69..c5fd6a50 100644
--- a/src/kernel/src/BinaryMutex.cpp
+++ b/src/kernel/src/BinaryMutex.cpp
@@ -11,14 +11,23 @@ namespace Kernel {
/// @brief Unlocks the binary mutex.
/***********************************************************************************/
+#ifndef __NE_TIMEOUT_CONFIG__
+#define __NE_TIMEOUT_CONFIG__ 10000
+#endif
+
Bool BinaryMutex::Unlock() {
- if (fLockingProcess->Status == ProcessStatusKind::kRunning) {
- fLockingProcess = nullptr;
+ auto timeout = 0UL;
+ constexpr auto kTimoutLimit = __NE_TIMEOUT_CONFIG__;
+
+ while (fLockingProcess->Status == ProcessStatusKind::kRunning) {
+ ++timeout;
- return Yes;
+ if (timeout > kTimoutLimit)
+ return No;
}
- return No;
+ fLockingProcess = nullptr;
+ return Yes;
}
/***********************************************************************************/
@@ -49,7 +58,8 @@ Bool BinaryMutex::LockAndWait(BinaryMutex::LockedPtr process, ITimer* timer) {
if (timer == nullptr) return No;
timer->Wait();
- return this->Lock(process);
+ this->Lock(process);
+ return this->Unlock();
}
/***********************************************************************************/
diff --git a/src/kernel/src/UserProcessScheduler.cpp b/src/kernel/src/UserProcessScheduler.cpp
index 4b8d788b..8c048f07 100644
--- a/src/kernel/src/UserProcessScheduler.cpp
+++ b/src/kernel/src/UserProcessScheduler.cpp
@@ -12,6 +12,7 @@
#include <NeKit/KString.h>
#include <NeKit/Utils.h>
#include <SignalKit/Signals.h>
+#include "KernelKit/CoreProcessScheduler.h"
///! BUG COUNT: 0
@@ -474,47 +475,53 @@ Bool UserProcessScheduler::HasMP() {
/***********************************************************************************/
SizeT UserProcessScheduler::Run() {
- if (mTeam.mProcessCur < 1) {
- return 0UL;
- }
-
- SizeT process_index = 0UL; //! we store this guy to tell the scheduler how many
- //! things we have scheduled.
+ STATIC SizeT process_index{}; //! we store this guy to tell the scheduler how many
+ //! things we have scheduled.
- for (; process_index < mTeam.AsArray().Capacity(); ++process_index) {
- auto& process = mTeam.AsArray()[process_index];
+ UserProcessTeam& team = mTeam;
+ SizeT limit = team.AsArray().Capacity();
- //! Check if the process needs to be run.
- if (UserProcessHelper::CanBeScheduled(process)) {
- kout << process.Name << " will be scheduled to run...\r";
+ if (team.mProcessCur < 1) {
+ return {};
+ }
- //! Increase the usage time of the process.
- if (process.UTime < process.PTime) {
- ++process.UTime;
- }
+ if (process_index > limit)
+ process_index = 0UL;
+ else
+ ++process_index;
- this->TheCurrentProcess() = process;
+ auto& process = team.AsArray()[process_index];
- if (UserProcessHelper::Switch(process.StackFrame, process.ProcessId)) {
- process.PTime = static_cast<Int32>(process.Affinity);
+ //! Check if the process needs to be run.
+ if (UserProcessHelper::CanBeScheduled(process)) {
+ kout << process.Name << " will be scheduled to run...\r";
- // We add a bigger cooldown according to the RTime and affinity here.
- if (process.PTime < process.RTime && AffinityKind::kRealTime != process.Affinity) {
- if (process.RTime < (Int32) AffinityKind::kVeryHigh)
- process.RTime += (Int32) AffinityKind::kLowUsage;
- else if (process.RTime < (Int32) AffinityKind::kHigh)
- process.RTime += (Int32) AffinityKind::kStandard;
- else if (process.RTime < (Int32) AffinityKind::kStandard)
- process.RTime += (Int32) AffinityKind::kHigh;
+ //! Increase the usage time of the process.
+ if (process.UTime < process.PTime) {
+ ++process.UTime;
+ }
- process.PTime -= process.RTime;
- process.RTime = 0UL;
- }
+ this->TheCurrentProcess() = process;
+
+ if (UserProcessHelper::Switch(process.StackFrame, process.ProcessId)) {
+ // We add a bigger cooldown according to the RTime and affinity here.
+ if (process.PTime < process.RTime && AffinityKind::kUltraHigh != process.Affinity) {
+ if (process.RTime < (Int32) AffinityKind::kVeryHigh)
+ process.RTime += (Int32) AffinityKind::kLowUsage;
+ else if (process.RTime < (Int32) AffinityKind::kHigh)
+ process.RTime += (Int32) AffinityKind::kStandard;
+ else if (process.RTime < (Int32) AffinityKind::kStandard)
+ process.RTime += (Int32) AffinityKind::kHigh;
+
+ process.PTime -= process.RTime;
+ process.RTime = 0UL;
+ } else if (AffinityKind::kUltraHigh != process.Affinity) {
+ process.PTime += (Int32)AffinityKind::kUltraHigh;
}
- } else {
- ++process.RTime;
- --process.PTime;
}
+ } else {
+ ++process.RTime;
+ --process.PTime;
}
return process_index;
@@ -562,7 +569,7 @@ ErrorOr<ProcessID> UserProcessHelper::TheCurrentPID() {
/// @retval true can be schedulded.
/// @retval false cannot be schedulded.
Bool UserProcessHelper::CanBeScheduled(const UserProcess& process) {
- if (process.Affinity == AffinityKind::kRealTime) return Yes;
+ if (process.Affinity == AffinityKind::kUltraHigh) return Yes;
if (process.Status != ProcessStatusKind::kRunning) return No;
if (process.Affinity == AffinityKind::kInvalid) return No;
@@ -594,7 +601,7 @@ SizeT UserProcessHelper::StartScheduling() {
Bool UserProcessHelper::Switch(HAL::StackFramePtr frame_ptr, ProcessID new_pid) {
(Void)(kout << "IP: " << hex_number(frame_ptr->IP) << kendl);
- for (SizeT index = 0UL; index < HardwareThreadScheduler::The().Capacity(); ++index) {
+ for (SizeT index{}; index < HardwareThreadScheduler::The().Capacity(); ++index) {
if (!HardwareThreadScheduler::The()[index].Leak()) continue;
if (HardwareThreadScheduler::The()[index].Leak()->Kind() == ThreadKind::kAPInvalid ||