2018-12-13 22:37:51 +04:00
|
|
|
pub use self::context::Process;
|
2019-01-03 15:37:04 +04:00
|
|
|
pub use rcore_process::*;
|
2018-11-19 13:21:06 +04:00
|
|
|
use crate::consts::{MAX_CPU_NUM, MAX_PROCESS_NUM};
|
|
|
|
use crate::arch::cpu;
|
2018-12-17 19:54:13 +04:00
|
|
|
use alloc::{boxed::Box, sync::Arc};
|
2018-11-19 13:21:06 +04:00
|
|
|
use log::*;
|
2018-04-27 19:10:39 +04:00
|
|
|
|
2018-11-05 16:37:05 +04:00
|
|
|
pub mod context;
|
2018-11-19 21:08:39 +04:00
|
|
|
|
2018-07-12 08:13:39 +04:00
|
|
|
pub fn init() {
|
2018-11-03 17:45:03 +04:00
|
|
|
// NOTE: max_time_slice <= 5 to ensure 'priority' test pass
|
|
|
|
let scheduler = Box::new(scheduler::RRScheduler::new(5));
|
2018-11-16 18:27:03 +04:00
|
|
|
let manager = Arc::new(ProcessManager::new(scheduler, MAX_PROCESS_NUM));
|
2018-11-03 17:45:03 +04:00
|
|
|
|
2018-11-17 08:39:48 +04:00
|
|
|
unsafe {
|
|
|
|
for cpu_id in 0..MAX_CPU_NUM {
|
2018-12-13 22:37:51 +04:00
|
|
|
PROCESSORS[cpu_id].init(cpu_id, Process::new_init(), manager.clone());
|
2018-11-17 08:39:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-15 16:17:02 +04:00
|
|
|
// Add idle threads
|
2018-11-03 17:45:03 +04:00
|
|
|
extern fn idle(_arg: usize) -> ! {
|
|
|
|
loop { cpu::halt(); }
|
|
|
|
}
|
2018-12-15 16:17:02 +04:00
|
|
|
use core::str::FromStr;
|
|
|
|
let cores = usize::from_str(env!("SMP")).unwrap();
|
|
|
|
for i in 0..cores {
|
2018-12-13 22:37:51 +04:00
|
|
|
manager.add(Process::new_kernel(idle, i), 0);
|
2018-11-03 17:45:03 +04:00
|
|
|
}
|
2018-11-19 13:21:06 +04:00
|
|
|
crate::shell::run_user_shell();
|
2018-11-03 17:45:03 +04:00
|
|
|
|
2018-08-07 10:59:45 +04:00
|
|
|
info!("process init end");
|
2018-04-27 19:10:39 +04:00
|
|
|
}
|
|
|
|
|
2018-11-03 17:45:03 +04:00
|
|
|
static PROCESSORS: [Processor; MAX_CPU_NUM] = [Processor::new(), Processor::new(), Processor::new(), Processor::new(), Processor::new(), Processor::new(), Processor::new(), Processor::new()];
|
|
|
|
|
|
|
|
/// Get current thread struct
|
2018-12-13 22:37:51 +04:00
|
|
|
///
|
|
|
|
/// FIXME: It's obviously unsafe to get &mut !
|
|
|
|
pub fn process() -> &'static mut Process {
|
2018-11-03 17:45:03 +04:00
|
|
|
use core::mem::transmute;
|
2018-12-13 22:37:51 +04:00
|
|
|
let (process, _): (&mut Process, *const ()) = unsafe {
|
2018-11-03 17:45:03 +04:00
|
|
|
transmute(processor().context())
|
|
|
|
};
|
|
|
|
process
|
2018-07-17 15:06:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-03 17:45:03 +04:00
|
|
|
// Implement dependencies for std::thread
|
2018-07-17 15:06:30 +04:00
|
|
|
|
2018-11-03 17:45:03 +04:00
|
|
|
#[no_mangle]
|
|
|
|
pub fn processor() -> &'static Processor {
|
|
|
|
&PROCESSORS[cpu::id()]
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub fn new_kernel_context(entry: extern fn(usize) -> !, arg: usize) -> Box<Context> {
|
2018-12-13 22:37:51 +04:00
|
|
|
Process::new_kernel(entry, arg)
|
2018-11-06 17:44:12 +04:00
|
|
|
}
|