move nextpid into atomic i32 in rust

This commit is contained in:
Garen Tyler 2023-11-03 18:48:55 -06:00
parent 50448eb307
commit bae37aa507
Signed by: garentyler
GPG Key ID: D7A048C454CB7054
2 changed files with 7 additions and 11 deletions

View File

@ -10,9 +10,6 @@ struct proc proc[NPROC];
struct proc *initproc;
int nextpid = 1;
struct spinlock pid_lock;
extern void forkret(void);
void freeproc(struct proc *p);
@ -46,8 +43,7 @@ void
procinit(void)
{
struct proc *p;
initlock(&pid_lock, "nextpid");
initlock(&wait_lock, "wait_lock");
for(p = proc; p < &proc[NPROC]; p++) {
initlock(&p->lock, "proc");

View File

@ -10,6 +10,7 @@ use crate::{
use core::{
ffi::{c_char, c_void},
ptr::{addr_of_mut, null_mut},
sync::atomic::{AtomicI32, Ordering},
};
extern "C" {
@ -45,6 +46,8 @@ extern "C" {
pub fn swtch(a: *mut Context, b: *mut Context);
}
pub static NEXT_PID: AtomicI32 = AtomicI32::new(1);
#[repr(C)]
#[derive(PartialEq, Default)]
pub enum ProcState {
@ -164,7 +167,7 @@ impl Proc {
/// Return the current struct proc *, or zero if none.
#[no_mangle]
pub unsafe extern "C" fn myproc() -> *mut Proc {
pub extern "C" fn myproc() -> *mut Proc {
if let Some(p) = Proc::current() {
p as *mut Proc
} else {
@ -173,11 +176,8 @@ pub unsafe extern "C" fn myproc() -> *mut Proc {
}
#[no_mangle]
pub unsafe extern "C" fn allocpid() -> i32 {
let _guard = pid_lock.lock();
let pid = nextpid;
nextpid += 1;
pid
pub extern "C" fn allocpid() -> i32 {
NEXT_PID.fetch_add(1, Ordering::SeqCst)
}
/// Free a proc structure and the data hanging from it, including user pages.