Skip to content

基于RISC-V原子指令重构并发模块 (Concurrency Module Refactoring Using RISC-V Atomic Instructions) #163

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: ch8
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .swp
Binary file not shown.
4 changes: 2 additions & 2 deletions os/src/lang_items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ fn panic(info: &PanicInfo) -> ! {
"[kernel] Panicked at {}:{} {}",
location.file(),
location.line(),
info.message().unwrap()
info.message()
);
} else {
error!("[kernel] Panicked: {}", info.message().unwrap());
error!("[kernel] Panicked: {}", info.message());
}
unsafe {
backtrace();
Expand Down
1 change: 0 additions & 1 deletion os/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#![no_std]
#![no_main]
#![feature(panic_info_message)]
#![feature(alloc_error_handler)]

extern crate alloc;
Expand Down
2 changes: 1 addition & 1 deletion os/src/mm/heap_allocator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn init_heap() {
unsafe {
HEAP_ALLOCATOR
.lock()
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
.init(&raw mut HEAP_SPACE as usize, KERNEL_HEAP_SIZE);
}
}

Expand Down
3 changes: 3 additions & 0 deletions os/src/mm/memory_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ impl MemorySet {
pub fn translate(&self, vpn: VirtPageNum) -> Option<PageTableEntry> {
self.page_table.translate(vpn)
}
pub fn translate_va(&self, va: VirtAddr) -> Option<PhysAddr> {
self.page_table.translate_va(va)
}
pub fn recycle_data_pages(&mut self) {
//*self = Self::new_bare();
self.areas.clear();
Expand Down
39 changes: 0 additions & 39 deletions os/src/sync/condvar.rs

This file was deleted.

77 changes: 72 additions & 5 deletions os/src/sync/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
mod condvar;
mod mutex;
mod semaphore;
mod up;

pub use condvar::Condvar;
pub use mutex::{Mutex, MutexBlocking, MutexSpin};
pub use semaphore::Semaphore;
pub use mutex::{FUTEX_WAIT, FUTEX_WAKE};
pub use mutex::{Mutex, FutexQ};
pub use up::UPSafeCell;

#[inline(always)]
pub fn load_reserved(addr: *const u32) -> u32 {
let val;
unsafe {
core::arch::asm!(
"lr.w {}, ({})",
out(reg) val,
in(reg) addr
);
}
val
}

/// return true if successfully modify `addr` in memory
#[inline(always)]
#[allow(unused)]
pub fn store_conditional(addr: *mut u32, val: u32) -> bool {
let res: u32;
unsafe {
core::arch::asm!(
"sc.w {}, {}, ({})",
out(reg) res,
in(reg) val,
in(reg) addr
);
}
res == 0
}

#[inline(always)]
#[allow(unused)]
pub fn atomic_increment(addr: *mut u32) {
loop {
let val = load_reserved(addr);
if store_conditional(addr, val + 1) { break; }
}
}

#[inline(always)]
#[allow(unused)]
pub fn atomic_decrement(addr: *mut u32) {
loop {
let val = load_reserved(addr);
if store_conditional(addr, val - 1) { break; }
}
}

#[inline(always)]
#[allow(unused)]
pub fn atomic_test_and_set(addr: *mut u32, bit: u32) -> u32 {
loop {
let val = load_reserved(addr);
if store_conditional(addr, val | (1 << bit)) { return val; }
}
}

/// 原子地使 `(addr)` 自加 `addend`,并比较结果与 `expected` 是否相等。
/// 相等则返回 `true`
#[inline(always)]
#[allow(unused)]
pub fn atomic_add_and_compare(addr: *mut u32, addend: u32, expected: u32) -> bool {
loop {
let val = load_reserved(addr);
let res = val + addend;
if store_conditional(addr, res) {
return res == expected
}
}
}
80 changes: 29 additions & 51 deletions os/src/sync/mutex.rs
Original file line number Diff line number Diff line change
@@ -1,88 +1,66 @@
use super::UPSafeCell;
use super::{load_reserved, store_conditional};
use core::cell::UnsafeCell;
use crate::task::TaskControlBlock;
use crate::task::{block_current_and_run_next, suspend_current_and_run_next};
use crate::task::{current_task, wakeup_task};
use alloc::{collections::VecDeque, sync::Arc};

pub const FUTEX_WAIT: usize = 0;
pub const FUTEX_WAKE: usize = 1;
pub trait Mutex: Sync + Send {
fn lock(&self);
fn unlock(&self);
}

pub struct MutexSpin {
locked: UPSafeCell<bool>,
locked: UnsafeCell<u32>,
}

unsafe impl Send for MutexSpin {}
unsafe impl Sync for MutexSpin {}

impl MutexSpin {
pub fn new() -> Self {
Self {
locked: unsafe { UPSafeCell::new(false) },
locked: UnsafeCell::new(0),
}
}
}

impl Mutex for MutexSpin {
fn lock(&self) {
let addr = self.locked.get();
loop {
let mut locked = self.locked.exclusive_access();
if *locked {
drop(locked);
suspend_current_and_run_next();
continue;
} else {
*locked = true;
return;
}
while load_reserved(addr) == 1 {}
if store_conditional(addr, 1) { return; }
}
}

fn unlock(&self) {
let mut locked = self.locked.exclusive_access();
*locked = false;
let addr = self.locked.get();
unsafe { *addr = 0; }
}
}

pub struct MutexBlocking {
inner: UPSafeCell<MutexBlockingInner>,
}

pub struct MutexBlockingInner {
locked: bool,
wait_queue: VecDeque<Arc<TaskControlBlock>>,
// Futex的等待队列,由位于内核的进程控制块维护
pub struct FutexQ {
pub guard: MutexSpin,
queue: UnsafeCell<VecDeque<Arc<TaskControlBlock>>>,
}

impl MutexBlocking {
impl FutexQ {
pub fn new() -> Self {
Self {
inner: unsafe {
UPSafeCell::new(MutexBlockingInner {
locked: false,
wait_queue: VecDeque::new(),
})
},
guard: MutexSpin::new(),
queue: UnsafeCell::new(VecDeque::new()),
}
}
}

impl Mutex for MutexBlocking {
fn lock(&self) {
let mut mutex_inner = self.inner.exclusive_access();
if mutex_inner.locked {
mutex_inner.wait_queue.push_back(current_task().unwrap());
drop(mutex_inner);
block_current_and_run_next();
} else {
mutex_inner.locked = true;
}
}
/// 外部调用必须确保已拿到 FutexQ 的自旋锁
pub fn push_back(&self, task: Arc<TaskControlBlock>) {
unsafe { &mut *self.queue.get() }.push_back(task);
}

fn unlock(&self) {
let mut mutex_inner = self.inner.exclusive_access();
assert!(mutex_inner.locked);
if let Some(waking_task) = mutex_inner.wait_queue.pop_front() {
wakeup_task(waking_task);
} else {
mutex_inner.locked = false;
}
/// 外部调用必须确保已拿到 FutexQ 的自旋锁
pub fn pop_front(&self) -> Option<Arc<TaskControlBlock>> {
unsafe { &mut *self.queue.get() }.pop_front()
}
}
}
45 changes: 0 additions & 45 deletions os/src/sync/semaphore.rs

This file was deleted.

Loading