1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-27 10:13:28 +04:00
rCore/src/process/mod.rs

90 lines
2.3 KiB
Rust
Raw Normal View History

use memory::MemoryController;
use spin::{Once, Mutex};
2018-05-13 11:06:44 +04:00
use core::slice;
use alloc::String;
use self::process::*;
use self::processor::*;
mod process;
mod processor;
/// 平台相关依赖struct TrapFrame
///
/// ## 必须实现的特性
///
/// * Debug: 用于Debug输出
use arch::interrupt::TrapFrame;
pub fn init(mut mc: MemoryController) {
PROCESSOR.call_once(|| {Mutex::new({
let initproc = Process::new_init(&mut mc);
let idleproc = Process::new("idle", idle_thread, &mut mc);
let mut processor = Processor::new();
processor.add(initproc);
processor.add(idleproc);
processor
})});
MC.call_once(|| Mutex::new(mc));
}
static PROCESSOR: Once<Mutex<Processor>> = Once::new();
static MC: Once<Mutex<MemoryController>> = Once::new();
/// Called by timer handler in arch
/// 设置rsp指向接下来要执行线程的 内核栈顶
/// 之后中断处理例程会重置rsp恢复对应线程的上下文
pub fn schedule(rsp: &mut usize) {
PROCESSOR.try().unwrap().lock().schedule(rsp);
}
extern fn idle_thread() {
loop {
println!("idle ...");
let mut i = 0;
while i < 1 << 22 {
i += 1;
}
}
2018-05-12 23:41:41 +04:00
}
/// Fork the current process
2018-05-17 18:15:26 +04:00
pub fn sys_fork(tf: &TrapFrame) -> i32 {
let mut processor = PROCESSOR.try().unwrap().lock();
let mut mc = MC.try().unwrap().lock();
let new = processor.current().fork(tf, &mut mc);
processor.add(new);
2018-05-17 18:15:26 +04:00
0
}
/// Kill the process
pub fn sys_kill(pid: usize) -> i32 {
PROCESSOR.try().unwrap().lock().kill(pid);
0
}
/// Get the current process id
pub fn sys_getpid() -> i32 {
PROCESSOR.try().unwrap().lock().current().pid as i32
}
/// Exit the current process
pub fn sys_exit(rsp: &mut usize, error_code: usize) -> i32 {
let mut processor = PROCESSOR.try().unwrap().lock();
let pid = processor.current().pid;
processor.schedule(rsp);
processor.exit(pid, error_code);
0
}
pub fn add_user_process(name: &str, data: &[u8]) {
let mut processor = PROCESSOR.try().unwrap().lock();
let mut mc = MC.try().unwrap().lock();
let mut new = Process::new_user(data, &mut mc);
new.name = String::from(name);
processor.add(new);
}
pub fn print() {
debug!("{:#x?}", *PROCESSOR.try().unwrap().lock());
}