Skip to content

Commit

Permalink
[feat] add cpumask and set_cpumask apis
Browse files Browse the repository at this point in the history
  • Loading branch information
hky1999 committed Oct 13, 2024
1 parent d25a3e0 commit c0ad247
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
14 changes: 14 additions & 0 deletions modules/axtask/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,20 @@ pub fn set_priority(prio: isize) -> bool {
current_run_queue::<NoPreemptIrqSave>().set_current_priority(prio)
}

/// Set the affinity for the current task.
/// [`cpumask::CpuMask`] is used to specify the CPU affinity.
/// Returns `true` if the affinity is set successfully.
///
/// TODO: support set the affinity for other tasks.
pub fn set_current_affinity(cpumask: CpuMask) -> bool {
if cpumask.is_empty() {
false
} else {
current().set_cpumask(cpumask);
true
}
}

/// Current task gives up the CPU time voluntarily, and switches to another
/// ready task.
pub fn yield_now() {
Expand Down
28 changes: 17 additions & 11 deletions modules/axtask/src/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ impl TaskInner {
None
}
}

/// Gets the cpu affinity mask of the task.
///
/// Returns the cpu affinity mask of the task in type [`cpumask::CpuMask`].
#[inline]
pub fn cpumask(&self) -> CpuMask {
*self.cpumask.lock()
}

/// Sets the cpu affinity mask of the task.
///
/// # Arguments
/// `cpumask` - The cpu affinity mask to be set in type [`cpumask::CpuMask`].
#[inline]
pub fn set_cpumask(&self, cpumask: CpuMask) {
*self.cpumask.lock() = cpumask
}
}

// private methods
Expand Down Expand Up @@ -285,17 +302,6 @@ impl TaskInner {
self.is_idle
}

#[allow(unused)]
#[inline]
pub(crate) fn cpumask(&self) -> CpuMask {
*self.cpumask.lock()
}

#[inline]
pub(crate) fn set_cpumask(&self, cpumask: CpuMask) {
*self.cpumask.lock() = cpumask
}

#[inline]
pub(crate) fn in_wait_queue(&self) -> bool {
self.in_wait_queue.load(Ordering::Acquire)
Expand Down

0 comments on commit c0ad247

Please sign in to comment.