Skip to content

Commit

Permalink
fix: ksync update
Browse files Browse the repository at this point in the history
  • Loading branch information
Godones committed Dec 6, 2023
1 parent ead2770 commit 494ebc5
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 40 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 34 additions & 34 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,34 +1,34 @@
[workspace]
resolver = "2"
members = [
"kernel",
"userlib",
"boot",
"dep/gmanager",
"dep/basemachine",
"apps/slint-helper",
"apps/cat",
"apps/sleep",
"apps/todo",
"apps/printdemo",
"apps/egui",
"apps/sysinfo",
"apps/init",
"apps/ls",
"apps/mkdir",
"apps/pwd",
"apps/shell",
"apps/touch",
"apps/run_test",
"apps/slint",
"apps/socket_test",
"apps/tests",
"apps/memory-game",
"apps/print",
"apps/final_test",
]


[profile.release]
#lto = true
#codegen-units = 1
[workspace]
resolver = "2"
members = [
"kernel",
"userlib",
"boot",
"dep/gmanager",
"dep/basemachine",
"apps/slint-helper",
"apps/cat",
"apps/sleep",
"apps/todo",
"apps/printdemo",
"apps/egui",
"apps/sysinfo",
"apps/init",
"apps/ls",
"apps/mkdir",
"apps/pwd",
"apps/shell",
"apps/touch",
"apps/run_test",
"apps/slint",
"apps/socket_test",
"apps/tests",
"apps/memory-game",
"apps/print",
"apps/final_test",
]


[profile.release]
#lto = true
#codegen-units = 1
Binary file removed assert/desktop.bmp
Binary file not shown.
Binary file removed assert/file.bmp
Binary file not shown.
Binary file removed assert/folder.bmp
Binary file not shown.
Binary file removed assert/mouse.bmp
Binary file not shown.
4 changes: 4 additions & 0 deletions docs/doc/musl.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# 编译 riscv64gc-unknown-linux-musl rust程序

## 提示找不到运行时库的问题
https://stackoverflow.com/questions/74424444/how-to-build-a-rust-program-for-riscv64gc-unknown-linux-musl
2 changes: 1 addition & 1 deletion kernel/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ rslab = { version = "0.2.1", optional = true }
syscall-table = { git = "https://github.com/os-module/syscall-table.git" }
plic = { git = "https://github.com/os-module/plic" }
pager = { git = "https://github.com/os-module/pager", default-features = false, optional = true }
kernel-sync = { git = "https://github.com/os-module/kernel-sync.git",features = ["lock_api","riscv"] }
kernel-sync = { git = "https://github.com/os-module/kernel-sync.git"}
pconst = { git = "https://github.com/os-module/pconst.git", features = [
"trick",
] }
Expand Down
85 changes: 81 additions & 4 deletions kernel/src/ksync.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,84 @@
use kernel_sync::TicketMutexGuard;
use kernel_sync::{LockAction, ticket::TicketMutexGuard};
use core::cell::{RefCell, RefMut};
use crate::arch::{hart_id, interrupt_disable, interrupt_enable, is_interrupt_enable};
use crate::config::CPU_NUM;

pub type SpinMutex<T> = kernel_sync::SpinMutex<T>;
pub type TicketMutex<T> = kernel_sync::TicketMutex<T>;

pub type SpinMutex<T> = kernel_sync::spin::SpinMutex<T,KernelLockAction>;
pub type TicketMutex<T> = kernel_sync::ticket::TicketMutex<T,KernelLockAction>;
pub type RwLock<T> = kernel_sync::RwLock<T>;
pub type Mutex<T> = TicketMutex<T>;
pub type MutexGuard<'a, T> = TicketMutexGuard<'a, T>;
pub type MutexGuard<'a, T> = TicketMutexGuard<'a, T, KernelLockAction>;

#[derive(Debug, Default, Clone, Copy)]
#[repr(align(64))]
pub struct Cpu {
pub noff: i32, // Depth of push_off() nesting.
pub interrupt_enable: bool, // Were interrupts enabled before push_off()?
}

impl Cpu {
const fn new() -> Self {
Self {
noff: 0,
interrupt_enable: false,
}
}
}

pub struct SafeRefCell<T>(RefCell<T>);

/// # Safety: Only the corresponding cpu will access it.
unsafe impl<Cpu> Sync for SafeRefCell<Cpu> {}

impl<T> SafeRefCell<T> {
const fn new(t: T) -> Self {
Self(RefCell::new(t))
}
}

#[allow(clippy::declare_interior_mutable_const)]
const DEFAULT_CPU: SafeRefCell<Cpu> = SafeRefCell::new(Cpu::new());

static CPUS: [SafeRefCell<Cpu>; CPU_NUM] = [DEFAULT_CPU; CPU_NUM];

pub fn mycpu() -> RefMut<'static, Cpu> {
CPUS[hart_id()].0.borrow_mut()
}

// push_off/pop_off are like intr_off()/intr_on() except that they are matched:
// it takes two pop_off()s to undo two push_off()s. Also, if interrupts
// are initially off, then push_off, pop_off leaves them off.
pub(crate) fn push_off() {
let old = is_interrupt_enable();
interrupt_disable();
let mut cpu = mycpu();
if cpu.noff == 0 {
cpu.interrupt_enable = old;
}
cpu.noff += 1;
}

pub(crate) fn pop_off() {
let mut cpu = mycpu();
if is_interrupt_enable() || cpu.noff < 1 {
panic!("pop_off");
}
cpu.noff -= 1;
let should_enable = cpu.noff == 0 && cpu.interrupt_enable;
drop(cpu);
// NOTICE: intr_on() may lead to an immediate inerrupt, so we *MUST* drop(cpu) in advance.
if should_enable {
interrupt_enable();
}
}

pub struct KernelLockAction;
impl LockAction for KernelLockAction {
fn before_lock() {
push_off();
}
fn after_lock() {
pop_off();
}
}

0 comments on commit 494ebc5

Please sign in to comment.