-
-
Notifications
You must be signed in to change notification settings - Fork 142
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
调整pcb的sched_info和rwlock,以避免调度器死锁问题 #341
Merged
fslongjin
merged 9 commits into
DragonOS-Community:patch-refactor-process-management
from
Chiichen:refactor-process-deadlock-avoid
Aug 26, 2023
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5a02273
调整pcb的sched_info和rwlock,以避免调度器死锁问题
Chiichen dd0bf6e
修改为在 WriterGuard 中维护 Irq_guard
Chiichen 309cfc7
Merge pull request #5 from DragonOS-Community/patch-refactor-process-…
Chiichen 0ae8dfc
修正了 write_irqsave方法
Chiichen 2414685
优化了代码
Chiichen 7a9209b
把 set state 操作从 wakup 移动到 sched_enqueue 中
Chiichen c1fdc74
Merge branch 'patch-refactor-process-management' into refactor-proces…
Chiichen 3ad49cf
修正为在 wakeup 中设置 running ,以保留 set_state 的私有性
Chiichen ac2fd94
移除了 process_wakeup
Chiichen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,7 @@ use core::{ | |
hash::{Hash, Hasher}, | ||
intrinsics::unlikely, | ||
mem::ManuallyDrop, | ||
ptr::null_mut, | ||
sync::atomic::{compiler_fence, AtomicBool, AtomicIsize, AtomicUsize, Ordering}, | ||
sync::atomic::{compiler_fence, AtomicBool, AtomicI32, AtomicIsize, AtomicUsize, Ordering}, | ||
}; | ||
|
||
use alloc::{ | ||
|
@@ -14,7 +13,7 @@ use alloc::{ | |
use hashbrown::HashMap; | ||
|
||
use crate::{ | ||
arch::{asm::current::current_pcb, process::ArchPCBInfo}, | ||
arch::{cpu, process::ArchPCBInfo}, | ||
filesystem::vfs::{file::FileDescriptorVec, FileType}, | ||
include::bindings::bindings::CLONE_SIGNAL, | ||
kdebug, | ||
|
@@ -34,7 +33,10 @@ use crate::{ | |
init::initial_kernel_thread, | ||
kthread::{KernelThreadClosure, KernelThreadCreateInfo, KernelThreadMechanism}, | ||
}, | ||
sched::{core::CPU_EXECUTING, SchedPolicy, SchedPriority}, | ||
sched::{ | ||
core::{sched_enqueue, CPU_EXECUTING}, | ||
SchedPolicy, SchedPriority, | ||
}, | ||
smp::kick_cpu, | ||
syscall::SystemError, | ||
}; | ||
|
@@ -399,6 +401,10 @@ impl ProcessControlBlock { | |
return self.sched_info.write(); | ||
} | ||
|
||
pub fn sched_info_mut_irqsave(&self) -> RwLockWriteGuard<ProcessSchedulerInfo> { | ||
return self.sched_info.write_irqsave(); | ||
} | ||
|
||
pub fn worker_private(&self) -> SpinLockGuard<Option<WorkerPrivate>> { | ||
return self.worker_private.lock(); | ||
} | ||
|
@@ -527,10 +533,10 @@ impl ProcessBasicInfo { | |
#[derive(Debug)] | ||
pub struct ProcessSchedulerInfo { | ||
/// 当前进程所在的cpu | ||
on_cpu: Option<u32>, | ||
on_cpu: AtomicI32, | ||
/// 如果当前进程等待被迁移到另一个cpu核心上(也就是flags中的PF_NEED_MIGRATE被置位), | ||
/// 该字段存储要被迁移到的目标处理器核心号 | ||
migrate_to: Option<u32>, | ||
migrate_to: AtomicI32, | ||
|
||
/// 当前进程的状态 | ||
state: ProcessState, | ||
|
@@ -546,9 +552,13 @@ pub struct ProcessSchedulerInfo { | |
|
||
impl ProcessSchedulerInfo { | ||
pub fn new(on_cpu: Option<u32>) -> RwLock<Self> { | ||
let cpu_id = match on_cpu { | ||
Some(cpu_id) => cpu_id as i32, | ||
None => -1, | ||
}; | ||
return RwLock::new(Self { | ||
on_cpu, | ||
migrate_to: None, | ||
on_cpu: AtomicI32::new(cpu_id), | ||
migrate_to: AtomicI32::new(-1), | ||
state: ProcessState::Blocked(false), | ||
sched_policy: SchedPolicy::CFS, | ||
virtual_runtime: AtomicIsize::new(0), | ||
|
@@ -558,26 +568,44 @@ impl ProcessSchedulerInfo { | |
} | ||
|
||
pub fn on_cpu(&self) -> Option<u32> { | ||
return self.on_cpu; | ||
let on_cpu = self.on_cpu.load(Ordering::SeqCst); | ||
if on_cpu == -1 { | ||
return None; | ||
} else { | ||
return Some(on_cpu as u32); | ||
} | ||
} | ||
|
||
pub fn set_on_cpu(&mut self, on_cpu: Option<u32>) { | ||
self.on_cpu = on_cpu; | ||
pub fn set_on_cpu(&self, on_cpu: Option<u32>) { | ||
if let Some(cpu_id) = on_cpu { | ||
self.on_cpu.store(cpu_id as i32, Ordering::SeqCst); | ||
} else { | ||
self.on_cpu.store(-1, Ordering::SeqCst); | ||
} | ||
} | ||
|
||
pub fn migrate_to(&self) -> Option<u32> { | ||
return self.migrate_to; | ||
let migrate_to = self.migrate_to.load(Ordering::SeqCst); | ||
if migrate_to == -1 { | ||
return None; | ||
} else { | ||
return Some(migrate_to as u32); | ||
} | ||
} | ||
|
||
pub fn set_migrate_to(&mut self, migrate_to: Option<u32>) { | ||
self.migrate_to = migrate_to; | ||
pub fn set_migrate_to(&self, migrate_to: Option<u32>) { | ||
if let Some(data) = migrate_to { | ||
self.migrate_to.store(data as i32, Ordering::SeqCst); | ||
} else { | ||
self.migrate_to.store(-1, Ordering::SeqCst) | ||
} | ||
} | ||
|
||
pub fn state(&self) -> ProcessState { | ||
return self.state; | ||
} | ||
|
||
fn set_state(&mut self, state: ProcessState) { | ||
pub fn set_state(&mut self, state: ProcessState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里还没改哈哈 |
||
self.state = state; | ||
} | ||
|
||
|
@@ -711,3 +739,23 @@ impl Drop for KernelStack { | |
pub fn process_init() { | ||
ProcessManager::init(); | ||
} | ||
|
||
pub fn process_wakeup(pcb: Arc<ProcessControlBlock>) { | ||
// c版本代码 | ||
// BUG_ON(pcb == NULL); | ||
// if (pcb == NULL) | ||
// return -EINVAL; | ||
// // 如果pcb正在调度队列中,则不重复加入调度队列 | ||
// if (pcb->state & PROC_RUNNING) | ||
// return 0; | ||
|
||
// pcb->state |= PROC_RUNNING; | ||
// sched_enqueue_old(pcb, true); | ||
// return 0; | ||
|
||
if pcb.sched_info().state() != ProcessState::Runnable { | ||
pcb.sched_info_mut_irqsave() | ||
.set_state(ProcessState::Runnable); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
然后的话,获得写锁之后一定要再次确认它的state是否真的不为Runnable。 |
||
sched_enqueue(pcb, true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用option的take方法即可