Skip to content

Commit 8f7b057

Browse files
committed
feat(kernel): add State::task_ready_queue
The modification to `constance_port_std` in this commit is a work-around for <rust-lang/rust#43475>.
1 parent a85847a commit 8f7b057

File tree

4 files changed

+35
-9
lines changed

4 files changed

+35
-9
lines changed

src/constance/src/kernel.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
//! The RTOS kernel
22
use atomic_ref::AtomicRef;
3-
use core::{fmt, mem::forget, num::NonZeroUsize, sync::atomic::Ordering};
3+
use core::{borrow::BorrowMut, fmt, mem::forget, num::NonZeroUsize, sync::atomic::Ordering};
44

5-
use crate::utils::{BinUInteger, Init, PrioBitmap};
5+
use crate::utils::{intrusive_list::StaticListHead, BinUInteger, Init, PrioBitmap};
66

77
#[macro_use]
88
mod cfg;
@@ -218,6 +218,8 @@ pub unsafe trait KernelCfg2: Port + Sized {
218218

219219
type TaskReadyBitmap: PrioBitmap;
220220

221+
type TaskReadyQueue: BorrowMut<[StaticListHead<TaskCb<Self>>]> + Init + 'static;
222+
221223
/// Access the kernel's global state.
222224
fn state() -> &'static State<Self>;
223225

@@ -239,6 +241,7 @@ pub struct State<
239241
System: KernelCfg2,
240242
PortTaskState: 'static = <System as Port>::PortTaskState,
241243
TaskReadyBitmap: PrioBitmap = <System as KernelCfg2>::TaskReadyBitmap,
244+
TaskReadyQueue: 'static = <System as KernelCfg2>::TaskReadyQueue,
242245
TaskPriority: 'static = <System as KernelCfg1>::TaskPriority,
243246
> {
244247
// TODO: Make `running_task` non-null to simplify runtime code
@@ -248,38 +251,51 @@ pub struct State<
248251
/// The task ready bitmap, in which each bit indicates whether the
249252
/// task ready queue corresponding to that bit contains a task or not.
250253
task_ready_bitmap: utils::CpuLockCell<System, TaskReadyBitmap>,
254+
255+
/// The task ready queues, in which each queue represents the list of
256+
/// runnable task at the corresponding priority level.
257+
task_ready_queue: utils::CpuLockCell<System, TaskReadyQueue>,
251258
}
252259

253260
impl<
254261
System: KernelCfg2,
255262
PortTaskState: 'static,
256263
TaskReadyBitmap: PrioBitmap,
264+
TaskReadyQueue: 'static + Init,
257265
TaskPriority: 'static,
258-
> Init for State<System, PortTaskState, TaskReadyBitmap, TaskPriority>
266+
> Init for State<System, PortTaskState, TaskReadyBitmap, TaskReadyQueue, TaskPriority>
259267
{
260268
const INIT: Self = Self {
261269
running_task: AtomicRef::new(None),
262270
task_ready_bitmap: Init::INIT,
271+
task_ready_queue: Init::INIT,
263272
};
264273
}
265274

266275
impl<
267276
System: Kernel,
268277
PortTaskState: 'static + fmt::Debug,
269278
TaskReadyBitmap: PrioBitmap,
279+
TaskReadyQueue: 'static + fmt::Debug,
270280
TaskPriority: 'static,
271-
> fmt::Debug for State<System, PortTaskState, TaskReadyBitmap, TaskPriority>
281+
> fmt::Debug for State<System, PortTaskState, TaskReadyBitmap, TaskReadyQueue, TaskPriority>
272282
{
273283
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
274284
f.debug_struct("State")
275285
.field("running_task", &self.running_task)
276286
.field("task_ready_bitmap", &self.task_ready_bitmap)
287+
.field("task_ready_queue", &self.task_ready_queue)
277288
.finish()
278289
}
279290
}
280291

281-
impl<System: KernelCfg2, PortTaskState: 'static, TaskReadyBitmap: PrioBitmap, TaskPriority>
282-
State<System, PortTaskState, TaskReadyBitmap, TaskPriority>
292+
impl<
293+
System: KernelCfg2,
294+
PortTaskState: 'static,
295+
TaskReadyBitmap: PrioBitmap,
296+
TaskReadyQueue: BorrowMut<[StaticListHead<TaskCb<System, PortTaskState, TaskPriority>>]> + Init + 'static,
297+
TaskPriority,
298+
> State<System, PortTaskState, TaskReadyBitmap, TaskReadyQueue, TaskPriority>
283299
{
284300
/// Get the currently running task.
285301
pub fn running_task(&self) -> Option<&'static TaskCb<System, PortTaskState, TaskPriority>> {

src/constance/src/kernel/cfg.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,10 @@ macro_rules! build {
152152
CfgBuilder, HunkAttr, HunkInitAttr, KernelCfg1, KernelCfg2, Port, State, TaskAttr,
153153
TaskCb,
154154
},
155-
utils::{AlignedStorage, FixedPrioBitmap, Init, RawCell, UIntegerWithBound},
155+
utils::{
156+
intrusive_list::StaticListHead, AlignedStorage, FixedPrioBitmap, Init, RawCell,
157+
UIntegerWithBound,
158+
},
156159
};
157160

158161
// `$configure` produces two values: a `CfgBuilder` and an ID map
@@ -205,6 +208,7 @@ macro_rules! build {
205208
// Safety: We are `build!`, so it's okay to `impl` this
206209
unsafe impl KernelCfg2 for $sys {
207210
type TaskReadyBitmap = TaskReadyBitmap;
211+
type TaskReadyQueue = [StaticListHead<TaskCb<Self>>; CFG.num_task_priority_levels];
208212

209213
fn state() -> &'static KernelState {
210214
&KERNEL_STATE

src/constance/src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ macro_rules! If {
1717
mod aligned_storage;
1818
mod init;
1919
mod int;
20-
pub(crate) mod intrusive_list;
20+
pub mod intrusive_list;
2121
mod prio_bitmap;
2222
mod rawcell;
2323
mod zeroinit;

src/constance_port_std/src/lib.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(unsafe_block_in_unsafe_fn)] // `unsafe fn` doesn't imply `unsafe {}`
22
#![deny(unsafe_op_in_unsafe_fn)]
33
use atomic_ref::AtomicRef;
4-
use constance::prelude::*;
4+
use constance::{prelude::*, utils::intrusive_list::StaticListHead};
55
use parking_lot::{lock_api::RawMutex, Mutex};
66
use std::{
77
mem::ManuallyDrop,
@@ -121,6 +121,8 @@ impl State {
121121
pub unsafe fn dispatch_first_task<System: Kernel>(&'static self) -> !
122122
where
123123
System: Port<PortTaskState = TaskState>,
124+
// FIXME: Work-around for <https://github.com/rust-lang/rust/issues/43475>
125+
System::TaskReadyQueue: std::borrow::BorrowMut<[StaticListHead<TaskCb<System>>]>,
124126
{
125127
log::trace!("dispatch_first_task");
126128
assert!(self.is_cpu_lock_active());
@@ -197,6 +199,8 @@ impl State {
197199
pub unsafe fn yield_cpu<System: Kernel>(&self)
198200
where
199201
System: Port<PortTaskState = TaskState>,
202+
// FIXME: Work-around for <https://github.com/rust-lang/rust/issues/43475>
203+
System::TaskReadyQueue: std::borrow::BorrowMut<[StaticListHead<TaskCb<System>>]>,
200204
{
201205
log::trace!("yield_cpu");
202206
assert!(!self.is_cpu_lock_active());
@@ -208,6 +212,8 @@ impl State {
208212
pub unsafe fn exit_and_dispatch<System: Kernel>(&self) -> !
209213
where
210214
System: Port<PortTaskState = TaskState>,
215+
// Work-around <https://github.com/rust-lang/rust/issues/43475>
216+
System::TaskReadyQueue: std::borrow::BorrowMut<[StaticListHead<TaskCb<System>>]>,
211217
{
212218
log::trace!("exit_and_dispatch");
213219
assert!(self.is_cpu_lock_active());

0 commit comments

Comments
 (0)