1
0
mirror of https://github.com/rcore-os/rCore.git synced 2024-11-27 02:03:29 +04:00
rCore/kernel/src/process/mod.rs

72 lines
1.7 KiB
Rust
Raw Normal View History

2019-01-25 18:58:49 +04:00
pub use self::structs::*;
2018-11-19 13:21:06 +04:00
use crate::arch::cpu;
2019-03-27 14:35:08 +04:00
use crate::consts::{MAX_CPU_NUM, MAX_PROCESS_NUM};
2018-12-17 19:54:13 +04:00
use alloc::{boxed::Box, sync::Arc};
2018-11-19 13:21:06 +04:00
use log::*;
2019-03-27 14:35:08 +04:00
pub use rcore_thread::*;
use spin::MutexGuard;
mod abi;
2019-03-27 14:35:08 +04:00
pub mod structs;
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 = scheduler::RRScheduler::new(5);
2019-01-24 15:03:45 +04:00
let manager = Arc::new(ThreadPool::new(scheduler, MAX_PROCESS_NUM));
2018-11-03 17:45:03 +04:00
unsafe {
for cpu_id in 0..MAX_CPU_NUM {
2019-01-25 18:58:49 +04:00
PROCESSORS[cpu_id].init(cpu_id, Thread::new_init(), manager.clone());
}
}
2018-11-19 13:21:06 +04:00
crate::shell::run_user_shell();
2018-11-03 17:45:03 +04:00
2019-03-18 14:20:27 +04:00
info!("process: init end");
}
2019-03-27 14:35:08 +04:00
static PROCESSORS: [Processor; MAX_CPU_NUM] = [
Processor::new(),
Processor::new(),
Processor::new(),
Processor::new(),
Processor::new(),
Processor::new(),
Processor::new(),
Processor::new(),
];
2018-11-03 17:45:03 +04:00
2019-01-25 18:58:49 +04:00
/// Get current process
pub fn process() -> MutexGuard<'static, Process> {
current_thread().proc.lock()
}
/// Get current process, ignoring its lock
/// Only use this when necessary
pub unsafe fn process_unsafe() -> MutexGuard<'static, Process> {
let thread = current_thread();
thread.proc.force_unlock();
thread.proc.lock()
}
2019-01-25 18:58:49 +04:00
/// Get current thread
2018-12-13 22:37:51 +04:00
///
/// FIXME: It's obviously unsafe to get &mut !
2019-01-25 18:58:49 +04:00
pub fn current_thread() -> &'static mut Thread {
2018-11-03 17:45:03 +04:00
use core::mem::transmute;
2019-03-27 14:35:08 +04:00
let (process, _): (&mut Thread, *const ()) = unsafe { transmute(processor().context()) };
2018-11-03 17:45:03 +04:00
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]
2019-03-27 14:35:08 +04:00
pub fn new_kernel_context(entry: extern "C" fn(usize) -> !, arg: usize) -> Box<Context> {
2019-01-25 18:58:49 +04:00
Thread::new_kernel(entry, arg)
}