Remove cpu_affinity field from OSTD Task struct

This commit is contained in:
jellllly420 2024-08-28 17:47:46 +08:00 committed by Tate, Hongliang Tian
parent 8927031426
commit 9cc63149f1
6 changed files with 29 additions and 27 deletions

View File

@ -2,7 +2,7 @@
#![allow(dead_code)] #![allow(dead_code)]
use ostd::{task::Task, user::UserSpace}; use ostd::{cpu::CpuSet, task::Task, user::UserSpace};
use super::{thread_table, PosixThread}; use super::{thread_table, PosixThread};
use crate::{ use crate::{
@ -113,11 +113,13 @@ impl PosixThreadBuilder {
let status = ThreadStatus::Init; let status = ThreadStatus::Init;
let priority = Priority::default(); let priority = Priority::default();
let cpu_affinity = CpuSet::new_full();
let thread = Arc::new(Thread::new( let thread = Arc::new(Thread::new(
weak_task.clone(), weak_task.clone(),
posix_thread, posix_thread,
status, status,
priority, priority,
cpu_affinity,
)); ));
thread_table::add_thread(tid, thread.clone()); thread_table::add_thread(tid, thread.clone());

View File

@ -2,6 +2,7 @@
use ostd::{ use ostd::{
cpu::{num_cpus, CpuSet, PinCurrentCpu}, cpu::{num_cpus, CpuSet, PinCurrentCpu},
sync::PreemptDisabled,
task::{ task::{
scheduler::{inject_scheduler, EnqueueFlags, LocalRunQueue, Scheduler, UpdateFlags}, scheduler::{inject_scheduler, EnqueueFlags, LocalRunQueue, Scheduler, UpdateFlags},
AtomicCpuId, Task, AtomicCpuId, Task,
@ -259,8 +260,11 @@ impl PreemptSchedInfo for Task {
&self.schedule_info().cpu &self.schedule_info().cpu
} }
fn cpu_affinity(&self) -> &CpuSet { fn cpu_affinity(&self) -> SpinLockGuard<CpuSet, PreemptDisabled> {
&self.schedule_info().cpu_affinity self.data()
.downcast_ref::<Arc<Thread>>()
.unwrap()
.lock_cpu_affinity()
} }
} }
@ -272,7 +276,7 @@ trait PreemptSchedInfo {
fn cpu(&self) -> &AtomicCpuId; fn cpu(&self) -> &AtomicCpuId;
fn cpu_affinity(&self) -> &CpuSet; fn cpu_affinity(&self) -> SpinLockGuard<CpuSet, PreemptDisabled>;
fn is_real_time(&self) -> bool { fn is_real_time(&self) -> bool {
self.priority() < Self::REAL_TIME_TASK_PRIORITY self.priority() < Self::REAL_TIME_TASK_PRIORITY

View File

@ -55,19 +55,17 @@ pub fn create_new_kernel_task(mut thread_options: ThreadOptions) -> Arc<Task> {
let kernel_thread = KernelThread; let kernel_thread = KernelThread;
let status = ThreadStatus::Init; let status = ThreadStatus::Init;
let priority = thread_options.priority; let priority = thread_options.priority;
let cpu_affinity = thread_options.cpu_affinity;
Arc::new(Thread::new( Arc::new(Thread::new(
weak_task.clone(), weak_task.clone(),
kernel_thread, kernel_thread,
status, status,
priority, priority,
cpu_affinity,
)) ))
}; };
TaskOptions::new(thread_fn) TaskOptions::new(thread_fn).data(thread).build().unwrap()
.data(thread)
.cpu_affinity(thread_options.cpu_affinity)
.build()
.unwrap()
}) })
} }

View File

@ -4,7 +4,7 @@
use core::sync::atomic::Ordering; use core::sync::atomic::Ordering;
use ostd::task::Task; use ostd::{cpu::CpuSet, sync::PreemptDisabled, task::Task};
use self::status::{AtomicThreadStatus, ThreadStatus}; use self::status::{AtomicThreadStatus, ThreadStatus};
use crate::{ use crate::{
@ -33,6 +33,8 @@ pub struct Thread {
status: AtomicThreadStatus, status: AtomicThreadStatus,
/// Thread priority /// Thread priority
priority: AtomicPriority, priority: AtomicPriority,
/// Thread cpu affinity
cpu_affinity: SpinLock<CpuSet>,
} }
impl Thread { impl Thread {
@ -42,12 +44,14 @@ impl Thread {
data: impl Send + Sync + Any, data: impl Send + Sync + Any,
status: ThreadStatus, status: ThreadStatus,
priority: Priority, priority: Priority,
cpu_affinity: CpuSet,
) -> Self { ) -> Self {
Thread { Thread {
task, task,
data: Box::new(data), data: Box::new(data),
status: AtomicThreadStatus::new(status), status: AtomicThreadStatus::new(status),
priority: AtomicPriority::new(priority), priority: AtomicPriority::new(priority),
cpu_affinity: SpinLock::new(cpu_affinity),
} }
} }
@ -111,6 +115,16 @@ impl Thread {
self.priority.store(new_priority, Ordering::Relaxed) self.priority.store(new_priority, Ordering::Relaxed)
} }
/// Acquires the lock of cpu affinity.
pub fn lock_cpu_affinity(&self) -> SpinLockGuard<CpuSet, PreemptDisabled> {
self.cpu_affinity.lock()
}
/// Updates the cpu affinity with the new value.
pub fn set_cpu_affinity(&self, new_cpu_affinity: CpuSet) {
*self.cpu_affinity.lock() = new_cpu_affinity;
}
pub fn yield_now() { pub fn yield_now() {
Task::yield_now() Task::yield_now()
} }

View File

@ -19,7 +19,7 @@ pub use self::{
scheduler::info::{AtomicCpuId, TaskScheduleInfo}, scheduler::info::{AtomicCpuId, TaskScheduleInfo},
}; };
pub(crate) use crate::arch::task::{context_switch, TaskContext}; pub(crate) use crate::arch::task::{context_switch, TaskContext};
use crate::{cpu::CpuSet, prelude::*, user::UserSpace}; use crate::{prelude::*, user::UserSpace};
/// A task that executes a function to the end. /// A task that executes a function to the end.
/// ///
@ -124,7 +124,6 @@ pub struct TaskOptions {
func: Option<Box<dyn Fn() + Send + Sync>>, func: Option<Box<dyn Fn() + Send + Sync>>,
data: Option<Box<dyn Any + Send + Sync>>, data: Option<Box<dyn Any + Send + Sync>>,
user_space: Option<Arc<UserSpace>>, user_space: Option<Arc<UserSpace>>,
cpu_affinity: CpuSet,
} }
impl TaskOptions { impl TaskOptions {
@ -137,7 +136,6 @@ impl TaskOptions {
func: Some(Box::new(func)), func: Some(Box::new(func)),
data: None, data: None,
user_space: None, user_space: None,
cpu_affinity: CpuSet::new_full(),
} }
} }
@ -165,15 +163,6 @@ impl TaskOptions {
self self
} }
/// Sets the CPU affinity mask for the task.
///
/// The `cpu_affinity` parameter represents
/// the desired set of CPUs to run the task on.
pub fn cpu_affinity(mut self, cpu_affinity: CpuSet) -> Self {
self.cpu_affinity = cpu_affinity;
self
}
/// Builds a new task without running it immediately. /// Builds a new task without running it immediately.
pub fn build(self) -> Result<Task> { pub fn build(self) -> Result<Task> {
/// all task will entering this function /// all task will entering this function
@ -212,7 +201,6 @@ impl TaskOptions {
kstack, kstack,
schedule_info: TaskScheduleInfo { schedule_info: TaskScheduleInfo {
cpu: AtomicCpuId::default(), cpu: AtomicCpuId::default(),
cpu_affinity: self.cpu_affinity,
}, },
}; };

View File

@ -4,8 +4,6 @@
use core::sync::atomic::{AtomicU32, Ordering}; use core::sync::atomic::{AtomicU32, Ordering};
use crate::cpu::CpuSet;
/// Fields of a task that OSTD will never touch. /// Fields of a task that OSTD will never touch.
/// ///
/// The type ought to be defined by the OSTD user and injected into the task. /// The type ought to be defined by the OSTD user and injected into the task.
@ -17,8 +15,6 @@ use crate::cpu::CpuSet;
pub struct TaskScheduleInfo { pub struct TaskScheduleInfo {
/// The CPU that the task would like to be running on. /// The CPU that the task would like to be running on.
pub cpu: AtomicCpuId, pub cpu: AtomicCpuId,
/// The CPUs that this task can run on.
pub cpu_affinity: CpuSet,
} }
/// An atomic CPUID container. /// An atomic CPUID container.