Skip to content

Commit

Permalink
runqueue: use newtypes for ids
Browse files Browse the repository at this point in the history
  • Loading branch information
elenaf9 committed May 2, 2024
1 parent 37a31dc commit 09046f9
Show file tree
Hide file tree
Showing 7 changed files with 164 additions and 145 deletions.
4 changes: 2 additions & 2 deletions src/riot-rs-embassy/src/blocker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const THREAD_FLAG_WAKER: ThreadFlags = 1; // TODO: find more appropriate value

fn wake(ptr: *const ()) {
// wake
let thread_id = ptr as usize as ThreadId;
let thread_id = ThreadId(ptr as usize as u8);
flags::set(thread_id, THREAD_FLAG_WAKER);
}

Expand All @@ -24,7 +24,7 @@ pub fn block_on<F: Future>(mut fut: F) -> F::Output {
// safety: we don't move the future after this line.
let mut fut = unsafe { Pin::new_unchecked(&mut fut) };

let raw_waker = RawWaker::new(current_pid().unwrap() as usize as *const (), &VTABLE);
let raw_waker = RawWaker::new(usize::from(current_pid().unwrap()) as *const (), &VTABLE);
let waker = unsafe { Waker::from_raw(raw_waker) };
let mut cx = Context::from_waker(&waker);
loop {
Expand Down
92 changes: 46 additions & 46 deletions src/riot-rs-runqueue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,88 +11,88 @@ mod tests {
fn test_rq_basic() {
let mut runqueue: RunQueue<8, 32> = RunQueue::new();

runqueue.add(0, 0);
runqueue.add(1, 0);
runqueue.add(2, 0);
runqueue.add(ThreadId(0), RunqueueId(0));
runqueue.add(ThreadId(1), RunqueueId(0));
runqueue.add(ThreadId(2), RunqueueId(0));

assert_eq!(runqueue.get_next(), Some(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(0)));

runqueue.advance(0);
runqueue.advance(RunqueueId(0));

assert_eq!(runqueue.get_next(), Some(1));
runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(ThreadId(1)));
runqueue.advance(RunqueueId(0));

assert_eq!(runqueue.get_next(), Some(2));
assert_eq!(runqueue.get_next(), Some(2));
assert_eq!(runqueue.get_next(), Some(ThreadId(2)));
assert_eq!(runqueue.get_next(), Some(ThreadId(2)));

runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(0));
runqueue.advance(RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(0)));

runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(1));
runqueue.advance(RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(1)));

runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(2));
runqueue.advance(RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(2)));
}

#[test]
fn test_rq_all32() {
let mut runqueue: RunQueue<8, 32> = RunQueue::new();

for i in 0..=31 {
runqueue.add(i, 0);
runqueue.add(ThreadId(i), RunqueueId(0));
}

for i in 0..=31 {
assert_eq!(runqueue.get_next(), Some(i));
runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(ThreadId(i)));
runqueue.advance(RunqueueId(0));
}

for i in 0..=31 {
assert_eq!(runqueue.get_next(), Some(i));
runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(ThreadId(i)));
runqueue.advance(RunqueueId(0));
}
}

#[test]
fn test_rq_basic_twoprio() {
let mut runqueue: RunQueue<8, 32> = RunQueue::new();

runqueue.add(0, 0);
runqueue.add(1, 0);
runqueue.add(3, 0);

runqueue.add(2, 1);
runqueue.add(4, 1);

assert_eq!(runqueue.get_next(), Some(2));
runqueue.del(2, 1);
assert_eq!(runqueue.get_next(), Some(4));
runqueue.del(4, 1);
assert_eq!(runqueue.get_next(), Some(0));
runqueue.del(0, 0);
assert_eq!(runqueue.get_next(), Some(1));
runqueue.del(1, 0);
assert_eq!(runqueue.get_next(), Some(3));
runqueue.del(3, 0);
runqueue.add(ThreadId(0), RunqueueId(0));
runqueue.add(ThreadId(1), RunqueueId(0));
runqueue.add(ThreadId(3), RunqueueId(0));

runqueue.add(ThreadId(2), RunqueueId(1));
runqueue.add(ThreadId(4), RunqueueId(1));

assert_eq!(runqueue.get_next(), Some(ThreadId(2)));
runqueue.del(ThreadId(2), RunqueueId(1));
assert_eq!(runqueue.get_next(), Some(ThreadId(4)));
runqueue.del(ThreadId(4), RunqueueId(1));
assert_eq!(runqueue.get_next(), Some(ThreadId(0)));
runqueue.del(ThreadId(0), RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(1)));
runqueue.del(ThreadId(1), RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(3)));
runqueue.del(ThreadId(3), RunqueueId(0));
assert_eq!(runqueue.get_next(), None);
}
#[test]
fn test_push_twice() {
let mut runqueue: RunQueue<8, 32> = RunQueue::new();

runqueue.add(0, 0);
runqueue.add(1, 0);
runqueue.add(ThreadId(0), RunqueueId(0));
runqueue.add(ThreadId(1), RunqueueId(0));

assert_eq!(runqueue.get_next(), Some(0));
runqueue.del(0, 0);
assert_eq!(runqueue.get_next(), Some(1));
assert_eq!(runqueue.get_next(), Some(ThreadId(0)));
runqueue.del(ThreadId(0), RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(1)));

runqueue.add(0, 0);
runqueue.add(ThreadId(0), RunqueueId(0));

assert_eq!(runqueue.get_next(), Some(1));
assert_eq!(runqueue.get_next(), Some(ThreadId(1)));

runqueue.advance(0);
assert_eq!(runqueue.get_next(), Some(0));
runqueue.advance(RunqueueId(0));
assert_eq!(runqueue.get_next(), Some(ThreadId(0)));
}
}
Loading

0 comments on commit 09046f9

Please sign in to comment.