axtask/run_queue.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666
use alloc::collections::VecDeque;
use alloc::sync::Arc;
use core::mem::MaybeUninit;
#[cfg(feature = "smp")]
use alloc::sync::Weak;
use kernel_guard::BaseGuard;
use kspin::SpinRaw;
use lazyinit::LazyInit;
use scheduler::BaseScheduler;
use axhal::cpu::this_cpu_id;
use crate::task::{CurrentTask, TaskState};
use crate::wait_queue::WaitQueueGuard;
use crate::{AxCpuMask, AxTaskRef, Scheduler, TaskInner, WaitQueue};
macro_rules! percpu_static {
($(
$(#[$comment:meta])*
$name:ident: $ty:ty = $init:expr
),* $(,)?) => {
$(
$(#[$comment])*
#[percpu::def_percpu]
static $name: $ty = $init;
)*
};
}
percpu_static! {
RUN_QUEUE: LazyInit<AxRunQueue> = LazyInit::new(),
EXITED_TASKS: VecDeque<AxTaskRef> = VecDeque::new(),
WAIT_FOR_EXIT: WaitQueue = WaitQueue::new(),
IDLE_TASK: LazyInit<AxTaskRef> = LazyInit::new(),
/// Stores the weak reference to the previous task that is running on this CPU.
#[cfg(feature = "smp")]
PREV_TASK: Weak<crate::AxTask> = Weak::new(),
}
/// An array of references to run queues, one for each CPU, indexed by cpu_id.
///
/// This static variable holds references to the run queues for each CPU in the system.
///
/// # Safety
///
/// Access to this variable is marked as `unsafe` because it contains `MaybeUninit` references,
/// which require careful handling to avoid undefined behavior. The array should be fully
/// initialized before being accessed to ensure safe usage.
static mut RUN_QUEUES: [MaybeUninit<&'static mut AxRunQueue>; axconfig::SMP] =
[ARRAY_REPEAT_VALUE; axconfig::SMP];
#[allow(clippy::declare_interior_mutable_const)] // It's ok because it's used only for initialization `RUN_QUEUES`.
const ARRAY_REPEAT_VALUE: MaybeUninit<&'static mut AxRunQueue> = MaybeUninit::uninit();
/// Returns a reference to the current run queue in [`CurrentRunQueueRef`].
///
/// ## Safety
///
/// This function returns a static reference to the current run queue, which
/// is inherently unsafe. It assumes that the `RUN_QUEUE` has been properly
/// initialized and is not accessed concurrently in a way that could cause
/// data races or undefined behavior.
///
/// ## Returns
///
/// * [`CurrentRunQueueRef`] - a static reference to the current [`AxRunQueue`].
#[inline(always)]
pub(crate) fn current_run_queue<G: BaseGuard>() -> CurrentRunQueueRef<'static, G> {
let irq_state = G::acquire();
CurrentRunQueueRef {
inner: unsafe { RUN_QUEUE.current_ref_mut_raw() },
current_task: crate::current(),
state: irq_state,
_phantom: core::marker::PhantomData,
}
}
/// Selects the run queue index based on a CPU set bitmap and load balancing.
///
/// This function filters the available run queues based on the provided `cpumask` and
/// selects the run queue index for the next task. The selection is based on a round-robin algorithm.
///
/// ## Arguments
///
/// * `cpumask` - A bitmap representing the CPUs that are eligible for task execution.
///
/// ## Returns
///
/// The index (cpu_id) of the selected run queue.
///
/// ## Panics
///
/// This function will panic if `cpu_mask` is empty, indicating that there are no available CPUs for task execution.
///
#[cfg(feature = "smp")]
// The modulo operation is safe here because `axconfig::SMP` is always greater than 1 with "smp" enabled.
#[allow(clippy::modulo_one)]
#[inline]
fn select_run_queue_index(cpumask: AxCpuMask) -> usize {
use core::sync::atomic::{AtomicUsize, Ordering};
static RUN_QUEUE_INDEX: AtomicUsize = AtomicUsize::new(0);
assert!(!cpumask.is_empty(), "No available CPU for task execution");
// Round-robin selection of the run queue index.
loop {
let index = RUN_QUEUE_INDEX.fetch_add(1, Ordering::SeqCst) % axconfig::SMP;
if cpumask.get(index) {
return index;
}
}
}
/// Retrieves a `'static` reference to the run queue corresponding to the given index.
///
/// This function asserts that the provided index is within the range of available CPUs
/// and returns a reference to the corresponding run queue.
///
/// ## Arguments
///
/// * `index` - The index of the run queue to retrieve.
///
/// ## Returns
///
/// A reference to the `AxRunQueue` corresponding to the provided index.
///
/// ## Panics
///
/// This function will panic if the index is out of bounds.
///
#[cfg(feature = "smp")]
#[inline]
fn get_run_queue(index: usize) -> &'static mut AxRunQueue {
unsafe { RUN_QUEUES[index].assume_init_mut() }
}
/// Selects the appropriate run queue for the provided task.
///
/// * In a single-core system, this function always returns a reference to the global run queue.
/// * In a multi-core system, this function selects the run queue based on the task's CPU affinity and load balance.
///
/// ## Arguments
///
/// * `task` - A reference to the task for which a run queue is being selected.
///
/// ## Returns
///
/// * [`AxRunQueueRef`] - a static reference to the selected [`AxRunQueue`] (current or remote).
///
/// ## TODO
///
/// 1. Implement better load balancing across CPUs for more efficient task distribution.
/// 2. Use a more generic load balancing algorithm that can be customized or replaced.
///
#[inline]
pub(crate) fn select_run_queue<G: BaseGuard>(task: &AxTaskRef) -> AxRunQueueRef<'static, G> {
let irq_state = G::acquire();
#[cfg(not(feature = "smp"))]
{
let _ = task;
// When SMP is disabled, all tasks are scheduled on the same global run queue.
AxRunQueueRef {
inner: unsafe { RUN_QUEUE.current_ref_mut_raw() },
state: irq_state,
_phantom: core::marker::PhantomData,
}
}
#[cfg(feature = "smp")]
{
// When SMP is enabled, select the run queue based on the task's CPU affinity and load balance.
let index = select_run_queue_index(task.cpumask());
AxRunQueueRef {
inner: get_run_queue(index),
state: irq_state,
_phantom: core::marker::PhantomData,
}
}
}
/// [`AxRunQueue`] represents a run queue for global system or a specific CPU.
pub(crate) struct AxRunQueue {
/// The ID of the CPU this run queue is associated with.
cpu_id: usize,
/// The core scheduler of this run queue.
/// Since irq and preempt are preserved by the kernel guard hold by `AxRunQueueRef`,
/// we just use a simple raw spin lock here.
scheduler: SpinRaw<Scheduler>,
}
/// A reference to the run queue with specific guard.
///
/// Note:
/// [`AxRunQueueRef`] is used to get a reference to the run queue on current CPU
/// or a remote CPU, which is used to add tasks to the run queue or unblock tasks.
/// If you want to perform scheduling operations on the current run queue,
/// see [`CurrentRunQueueRef`].
pub(crate) struct AxRunQueueRef<'a, G: BaseGuard> {
inner: &'a mut AxRunQueue,
state: G::State,
_phantom: core::marker::PhantomData<G>,
}
impl<G: BaseGuard> Drop for AxRunQueueRef<'_, G> {
fn drop(&mut self) {
G::release(self.state);
}
}
/// A reference to the current run queue with specific guard.
///
/// Note:
/// [`CurrentRunQueueRef`] is used to get a reference to the run queue on current CPU,
/// in which scheduling operations can be performed.
pub(crate) struct CurrentRunQueueRef<'a, G: BaseGuard> {
inner: &'a mut AxRunQueue,
current_task: CurrentTask,
state: G::State,
_phantom: core::marker::PhantomData<G>,
}
impl<G: BaseGuard> Drop for CurrentRunQueueRef<'_, G> {
fn drop(&mut self) {
G::release(self.state);
}
}
/// Management operations for run queue, including adding tasks, unblocking tasks, etc.
impl<G: BaseGuard> AxRunQueueRef<'_, G> {
/// Adds a task to the scheduler.
///
/// This function is used to add a new task to the scheduler.
pub fn add_task(&mut self, task: AxTaskRef) {
debug!(
"task add: {} on run_queue {}",
task.id_name(),
self.inner.cpu_id
);
assert!(task.is_ready());
self.inner.scheduler.lock().add_task(task);
}
/// Unblock one task by inserting it into the run queue.
///
/// This function does nothing if the task is not in [`TaskState::Blocked`],
/// which means the task is already unblocked by other cores.
pub fn unblock_task(&mut self, task: AxTaskRef, resched: bool) {
let task_id_name = task.id_name();
// Try to change the state of the task from `Blocked` to `Ready`,
// if successful, the task will be put into this run queue,
// otherwise, the task is already unblocked by other cores.
// Note:
// target task can not be insert into the run queue until it finishes its scheduling process.
if self
.inner
.put_task_with_state(task, TaskState::Blocked, resched)
{
// Since now, the task to be unblocked is in the `Ready` state.
let cpu_id = self.inner.cpu_id;
debug!("task unblock: {} on run_queue {}", task_id_name, cpu_id);
// Note: when the task is unblocked on another CPU's run queue,
// we just ingiore the `resched` flag.
if resched && cpu_id == this_cpu_id() {
#[cfg(feature = "preempt")]
crate::current().set_preempt_pending(true);
}
}
}
}
/// Core functions of run queue.
impl<G: BaseGuard> CurrentRunQueueRef<'_, G> {
#[cfg(feature = "irq")]
pub fn scheduler_timer_tick(&mut self) {
let curr = &self.current_task;
if !curr.is_idle() && self.inner.scheduler.lock().task_tick(curr.as_task_ref()) {
#[cfg(feature = "preempt")]
curr.set_preempt_pending(true);
}
}
/// Yield the current task and reschedule.
/// This function will put the current task into this run queue with `Ready` state,
/// and reschedule to the next task on this run queue.
pub fn yield_current(&mut self) {
let curr = &self.current_task;
trace!("task yield: {}", curr.id_name());
assert!(curr.is_running());
self.inner
.put_task_with_state(curr.clone(), TaskState::Running, false);
self.inner.resched();
}
/// Migrate the current task to a new run queue matching its CPU affinity and reschedule.
/// This function will spawn a new `migration_task` to perform the migration, which will set
/// current task to `Ready` state and select a proper run queue for it according to its CPU affinity,
/// switch to the migration task immediately after migration task is prepared.
///
/// Note: the ownership if migrating task (which is current task) is handed over to the migration task,
/// before the migration task inserted it into the target run queue.
#[cfg(feature = "smp")]
pub fn migrate_current(&mut self, migration_task: AxTaskRef) {
let curr = &self.current_task;
trace!("task migrate: {}", curr.id_name());
assert!(curr.is_running());
// Mark current task's state as `Ready`,
// but, do not put current task to the scheduler of this run queue.
curr.set_state(TaskState::Ready);
// Call `switch_to` to reschedule to the migration task that performs the migration directly.
self.inner.switch_to(crate::current(), migration_task);
}
/// Preempts the current task and reschedules.
/// This function is used to preempt the current task and reschedule
/// to next task on current run queue.
///
/// This function is called by `current_check_preempt_pending` with IRQs and preemption disabled.
///
/// Note:
/// preemption may happened in `enable_preempt`, which is called
/// each time a [`kspin::NoPreemptGuard`] is dropped.
#[cfg(feature = "preempt")]
pub fn preempt_resched(&mut self) {
// There is no need to disable IRQ and preemption here, because
// they both have been disabled in `current_check_preempt_pending`.
let curr = &self.current_task;
assert!(curr.is_running());
// When we call `preempt_resched()`, both IRQs and preemption must
// have been disabled by `kernel_guard::NoPreemptIrqSave`. So we need
// to set `current_disable_count` to 1 in `can_preempt()` to obtain
// the preemption permission.
let can_preempt = curr.can_preempt(1);
debug!(
"current task is to be preempted: {}, allow={}",
curr.id_name(),
can_preempt
);
if can_preempt {
self.inner
.put_task_with_state(curr.clone(), TaskState::Running, true);
self.inner.resched();
} else {
curr.set_preempt_pending(true);
}
}
/// Exit the current task with the specified exit code.
/// This function will never return.
pub fn exit_current(&mut self, exit_code: i32) -> ! {
let curr = &self.current_task;
debug!("task exit: {}, exit_code={}", curr.id_name(), exit_code);
assert!(curr.is_running(), "task is not running: {:?}", curr.state());
assert!(!curr.is_idle());
if curr.is_init() {
// Safety: it is called from `current_run_queue::<NoPreemptIrqSave>().exit_current(exit_code)`,
// which disabled IRQs and preemption.
unsafe {
EXITED_TASKS.current_ref_mut_raw().clear();
}
axhal::misc::terminate();
} else {
curr.set_state(TaskState::Exited);
// Notify the joiner task.
curr.notify_exit(exit_code);
// Safety: it is called from `current_run_queue::<NoPreemptIrqSave>().exit_current(exit_code)`,
// which disabled IRQs and preemption.
unsafe {
// Push current task to the `EXITED_TASKS` list, which will be consumed by the GC task.
EXITED_TASKS.current_ref_mut_raw().push_back(curr.clone());
// Wake up the GC task to drop the exited tasks.
WAIT_FOR_EXIT.current_ref_mut_raw().notify_one(false);
}
// Schedule to next task.
self.inner.resched();
}
unreachable!("task exited!");
}
/// Block the current task, put current task into the wait queue and reschedule.
/// Mark the state of current task as `Blocked`, set the `in_wait_queue` flag as true.
/// Note:
/// 1. The caller must hold the lock of the wait queue.
/// 2. The caller must ensure that the current task is in the running state.
/// 3. The caller must ensure that the current task is not the idle task.
/// 4. The lock of the wait queue will be released explicitly after current task is pushed into it.
pub fn blocked_resched(&mut self, mut wq_guard: WaitQueueGuard) {
let curr = &self.current_task;
assert!(curr.is_running());
assert!(!curr.is_idle());
// we must not block current task with preemption disabled.
// Current expected preempt count is 2.
// 1 for `NoPreemptIrqSave`, 1 for wait queue's `SpinNoIrq`.
#[cfg(feature = "preempt")]
assert!(curr.can_preempt(2));
// Mark the task as blocked, this has to be done before adding it to the wait queue
// while holding the lock of the wait queue.
curr.set_state(TaskState::Blocked);
curr.set_in_wait_queue(true);
wq_guard.push_back(curr.clone());
// Drop the lock of wait queue explictly.
drop(wq_guard);
// Current task's state has been changed to `Blocked` and added to the wait queue.
// Note that the state may have been set as `Ready` in `unblock_task()`,
// see `unblock_task()` for details.
debug!("task block: {}", curr.id_name());
self.inner.resched();
}
#[cfg(feature = "irq")]
pub fn sleep_until(&mut self, deadline: axhal::time::TimeValue) {
let curr = &self.current_task;
debug!("task sleep: {}, deadline={:?}", curr.id_name(), deadline);
assert!(curr.is_running());
assert!(!curr.is_idle());
let now = axhal::time::wall_time();
if now < deadline {
crate::timers::set_alarm_wakeup(deadline, curr.clone());
curr.set_state(TaskState::Blocked);
self.inner.resched();
}
}
pub fn set_current_priority(&mut self, prio: isize) -> bool {
self.inner
.scheduler
.lock()
.set_priority(self.current_task.as_task_ref(), prio)
}
}
impl AxRunQueue {
/// Create a new run queue for the specified CPU.
/// The run queue is initialized with a per-CPU gc task in its scheduler.
fn new(cpu_id: usize) -> Self {
let gc_task = TaskInner::new(gc_entry, "gc".into(), axconfig::TASK_STACK_SIZE).into_arc();
// gc task should be pinned to the current CPU.
gc_task.set_cpumask(AxCpuMask::one_shot(cpu_id));
let mut scheduler = Scheduler::new();
scheduler.add_task(gc_task);
Self {
cpu_id,
scheduler: SpinRaw::new(scheduler),
}
}
/// Puts target task into current run queue with `Ready` state
/// if its state matches `current_state` (except idle task).
///
/// If `preempt`, keep current task's time slice, otherwise reset it.
///
/// Returns `true` if the target task is put into this run queue successfully,
/// otherwise `false`.
fn put_task_with_state(
&mut self,
task: AxTaskRef,
current_state: TaskState,
preempt: bool,
) -> bool {
// If the task's state matches `current_state`, set its state to `Ready` and
// put it back to the run queue (except idle task).
if task.transition_state(current_state, TaskState::Ready) && !task.is_idle() {
// If the task is blocked, wait for the task to finish its scheduling process.
// See `unblock_task()` for details.
if current_state == TaskState::Blocked {
// Wait for next task's scheduling process to complete.
// If the owning (remote) CPU is still in the middle of schedule() with
// this task (next task) as prev, wait until it's done referencing the task.
//
// Pairs with the `clear_prev_task_on_cpu()`.
//
// Note:
// 1. This should be placed after the judgement of `TaskState::Blocked,`,
// because the task may have been woken up by other cores.
// 2. This can be placed in the front of `switch_to()`
#[cfg(feature = "smp")]
while task.on_cpu() {
// Wait for the task to finish its scheduling process.
core::hint::spin_loop();
}
}
// TODO: priority
self.scheduler.lock().put_prev_task(task, preempt);
true
} else {
false
}
}
/// Core reschedule subroutine.
/// Pick the next task to run and switch to it.
fn resched(&mut self) {
let next = self
.scheduler
.lock()
.pick_next_task()
.unwrap_or_else(|| unsafe {
// Safety: IRQs must be disabled at this time.
IDLE_TASK.current_ref_raw().get_unchecked().clone()
});
assert!(
next.is_ready(),
"next {} is not ready: {:?}",
next.id_name(),
next.state()
);
self.switch_to(crate::current(), next);
}
fn switch_to(&mut self, prev_task: CurrentTask, next_task: AxTaskRef) {
// Make sure that IRQs are disabled by kernel guard or other means.
#[cfg(all(not(test), feature = "irq"))] // Note: irq is faked under unit tests.
assert!(
!axhal::arch::irqs_enabled(),
"IRQs must be disabled during scheduling"
);
trace!(
"context switch: {} -> {}",
prev_task.id_name(),
next_task.id_name()
);
#[cfg(feature = "preempt")]
next_task.set_preempt_pending(false);
next_task.set_state(TaskState::Running);
if prev_task.ptr_eq(&next_task) {
return;
}
// Claim the task as running, we do this before switching to it
// such that any running task will have this set.
#[cfg(feature = "smp")]
next_task.set_on_cpu(true);
unsafe {
let prev_ctx_ptr = prev_task.ctx_mut_ptr();
let next_ctx_ptr = next_task.ctx_mut_ptr();
// Store the weak pointer of **prev_task** in percpu variable `PREV_TASK`.
#[cfg(feature = "smp")]
{
*PREV_TASK.current_ref_mut_raw() = Arc::downgrade(prev_task.as_task_ref());
}
// The strong reference count of `prev_task` will be decremented by 1,
// but won't be dropped until `gc_entry()` is called.
assert!(Arc::strong_count(prev_task.as_task_ref()) > 1);
assert!(Arc::strong_count(&next_task) >= 1);
CurrentTask::set_current(prev_task, next_task);
(*prev_ctx_ptr).switch_to(&*next_ctx_ptr);
// Current it's **next_task** running on this CPU, clear the `prev_task`'s `on_cpu` field
// to indicate that it has finished its scheduling process and no longer running on this CPU.
#[cfg(feature = "smp")]
clear_prev_task_on_cpu();
}
}
}
fn gc_entry() {
loop {
// Drop all exited tasks and recycle resources.
let n = EXITED_TASKS.with_current(|exited_tasks| exited_tasks.len());
for _ in 0..n {
// Do not do the slow drops in the critical section.
let task = EXITED_TASKS.with_current(|exited_tasks| exited_tasks.pop_front());
if let Some(task) = task {
if Arc::strong_count(&task) == 1 {
// If I'm the last holder of the task, drop it immediately.
drop(task);
} else {
// Otherwise (e.g, `switch_to` is not compeleted, held by the
// joiner, etc), push it back and wait for them to drop first.
EXITED_TASKS.with_current(|exited_tasks| exited_tasks.push_back(task));
}
}
}
// Note: we cannot block current task with preemption disabled,
// use `current_ref_raw` to get the `WAIT_FOR_EXIT`'s reference here to avoid the use of `NoPreemptGuard`.
// Since gc task is pinned to the current CPU, there is no affection if the gc task is preempted during the process.
unsafe { WAIT_FOR_EXIT.current_ref_raw() }.wait();
}
}
/// The task routine for migrating the current task to the correct CPU.
///
/// It calls `select_run_queue` to get the correct run queue for the task, and
/// then puts the task to the scheduler of target run queue.
#[cfg(feature = "smp")]
pub(crate) fn migrate_entry(migrated_task: AxTaskRef) {
select_run_queue::<kernel_guard::NoPreemptIrqSave>(&migrated_task)
.inner
.scheduler
.lock()
.put_prev_task(migrated_task, false)
}
/// Clear the `on_cpu` field of previous task running on this CPU.
#[cfg(feature = "smp")]
pub(crate) unsafe fn clear_prev_task_on_cpu() {
unsafe {
PREV_TASK
.current_ref_raw()
.upgrade()
.expect("Invalid prev_task pointer or prev_task has been dropped")
.set_on_cpu(false);
}
}
pub(crate) fn init() {
let cpu_id = this_cpu_id();
// Create the `idle` task (not current task).
const IDLE_TASK_STACK_SIZE: usize = 4096;
let idle_task = TaskInner::new(|| crate::run_idle(), "idle".into(), IDLE_TASK_STACK_SIZE);
// idle task should be pinned to the current CPU.
idle_task.set_cpumask(AxCpuMask::one_shot(cpu_id));
IDLE_TASK.with_current(|i| {
i.init_once(idle_task.into_arc());
});
// Put the subsequent execution into the `main` task.
let main_task = TaskInner::new_init("main".into()).into_arc();
main_task.set_state(TaskState::Running);
unsafe { CurrentTask::init_current(main_task) }
RUN_QUEUE.with_current(|rq| {
rq.init_once(AxRunQueue::new(cpu_id));
});
unsafe {
RUN_QUEUES[cpu_id].write(RUN_QUEUE.current_ref_mut_raw());
}
}
pub(crate) fn init_secondary() {
let cpu_id = this_cpu_id();
// Put the subsequent execution into the `idle` task.
let idle_task = TaskInner::new_init("idle".into()).into_arc();
idle_task.set_state(TaskState::Running);
IDLE_TASK.with_current(|i| {
i.init_once(idle_task.clone());
});
unsafe { CurrentTask::init_current(idle_task) }
RUN_QUEUE.with_current(|rq| {
rq.init_once(AxRunQueue::new(cpu_id));
});
unsafe {
RUN_QUEUES[cpu_id].write(RUN_QUEUE.current_ref_mut_raw());
}
}