Remove priority field from OSTD Task struct

This commit is contained in:
jellllly420
2024-09-14 15:32:21 +08:00
committed by Tate, Hongliang Tian
parent 0a36760f7a
commit 8927031426
18 changed files with 258 additions and 252 deletions

View File

@ -2,11 +2,11 @@
use ostd::{
cpu::CpuSet,
task::{Priority, Task, TaskOptions},
task::{Task, TaskOptions},
};
use super::{status::ThreadStatus, Thread};
use crate::prelude::*;
use crate::{prelude::*, sched::priority::Priority};
/// The inner data of a kernel thread
pub struct KernelThread;
@ -54,12 +54,17 @@ pub fn create_new_kernel_task(mut thread_options: ThreadOptions) -> Arc<Task> {
let thread = {
let kernel_thread = KernelThread;
let status = ThreadStatus::Init;
Arc::new(Thread::new(weak_task.clone(), kernel_thread, status))
let priority = thread_options.priority;
Arc::new(Thread::new(
weak_task.clone(),
kernel_thread,
status,
priority,
))
};
TaskOptions::new(thread_fn)
.data(thread)
.priority(thread_options.priority)
.cpu_affinity(thread_options.cpu_affinity)
.build()
.unwrap()
@ -81,7 +86,7 @@ impl ThreadOptions {
let cpu_affinity = CpuSet::new_full();
Self {
func: Some(Box::new(func)),
priority: Priority::normal(),
priority: Priority::default(),
cpu_affinity,
}
}

View File

@ -7,7 +7,10 @@ use core::sync::atomic::Ordering;
use ostd::task::Task;
use self::status::{AtomicThreadStatus, ThreadStatus};
use crate::prelude::*;
use crate::{
prelude::*,
sched::priority::{AtomicPriority, Priority},
};
pub mod exception;
pub mod kernel_thread;
@ -26,16 +29,25 @@ pub struct Thread {
data: Box<dyn Send + Sync + Any>,
// mutable part
/// Thread status
status: AtomicThreadStatus,
/// Thread priority
priority: AtomicPriority,
}
impl Thread {
/// Never call these function directly
pub fn new(task: Weak<Task>, data: impl Send + Sync + Any, status: ThreadStatus) -> Self {
pub fn new(
task: Weak<Task>,
data: impl Send + Sync + Any,
status: ThreadStatus,
priority: Priority,
) -> Self {
Thread {
task,
data: Box::new(data),
status: AtomicThreadStatus::new(status),
priority: AtomicPriority::new(priority),
}
}
@ -79,11 +91,26 @@ impl Thread {
self.status.load(Ordering::Acquire)
}
/// Updates the status with the `new` value.
/// Updates the status with the new value.
pub fn set_status(&self, new_status: ThreadStatus) {
self.status.store(new_status, Ordering::Release);
}
/// Returns the reference to the atomic priority.
pub fn atomic_priority(&self) -> &AtomicPriority {
&self.priority
}
/// Returns the current priority.
pub fn priority(&self) -> Priority {
self.priority.load(Ordering::Relaxed)
}
/// Updates the priority with the new value.
pub fn set_priority(&self, new_priority: Priority) {
self.priority.store(new_priority, Ordering::Relaxed)
}
pub fn yield_now() {
Task::yield_now()
}

View File

@ -2,14 +2,12 @@
#![allow(dead_code)]
use ostd::{
cpu::CpuSet,
task::{Priority, Task},
};
use ostd::{cpu::CpuSet, task::Task};
use super::worker_pool::WorkerPool;
use crate::{
prelude::*,
sched::priority::{Priority, PriorityRange},
thread::kernel_thread::{create_new_kernel_task, ThreadOptions},
Thread,
};
@ -50,9 +48,10 @@ impl Worker {
});
let mut cpu_affinity = CpuSet::new_empty();
cpu_affinity.add(bound_cpu);
let mut priority = Priority::normal();
let mut priority = Priority::default();
if worker_pool.upgrade().unwrap().is_high_priority() {
priority = Priority::high();
// FIXME: remove the use of real-time priority.
priority = Priority::new(PriorityRange::new(0));
}
let bound_task = create_new_kernel_task(
ThreadOptions::new(task_fn)

View File

@ -7,15 +7,12 @@ use core::{
time::Duration,
};
use ostd::{
cpu::CpuSet,
sync::WaitQueue,
task::{Priority, Task},
};
use ostd::{cpu::CpuSet, sync::WaitQueue, task::Task};
use super::{simple_scheduler::SimpleScheduler, worker::Worker, WorkItem, WorkPriority, WorkQueue};
use crate::{
prelude::*,
sched::priority::{Priority, PriorityRange},
thread::kernel_thread::{create_new_kernel_task, ThreadOptions},
Thread,
};
@ -236,9 +233,13 @@ impl Monitor {
current_monitor.run_monitor_loop();
});
let cpu_affinity = CpuSet::new_full();
// FIXME: remove the use of real-time priority.
// Logically all monitors should be of default normal priority.
// This workaround is to make the monitor of high-priority worker pool
// starvation-free under the current scheduling policy.
let priority = match priority {
WorkPriority::High => Priority::high(),
WorkPriority::Normal => Priority::normal(),
WorkPriority::High => Priority::new(PriorityRange::new(0)),
WorkPriority::Normal => Priority::default(),
};
let bound_task = create_new_kernel_task(
ThreadOptions::new(task_fn)