Skip to content
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

feat(sched): add sched_yield #766

Merged
merged 2 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions kernel/src/sched/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod fair;
pub mod idle;
pub mod pelt;
pub mod prio;
pub mod syscall;

use core::{
intrinsics::{likely, unlikely},
Expand Down
37 changes: 37 additions & 0 deletions kernel/src/sched/syscall.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use system_error::SystemError;

use crate::arch::cpu::current_cpu_id;
use crate::exception::InterruptArch;
use crate::process::ProcessManager;
use crate::sched::CurrentIrqArch;
use crate::sched::Scheduler;
use crate::syscall::Syscall;

use super::fair::CompletelyFairScheduler;
use super::{cpu_rq, schedule, SchedMode};

impl Syscall {
pub fn do_sched_yield() -> Result<usize, SystemError> {
// 禁用中断
let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };

let pcb = ProcessManager::current_pcb();
let rq = cpu_rq(pcb.sched_info().on_cpu().unwrap_or(current_cpu_id()).data() as usize);
let (rq, guard) = rq.self_lock();

// TODO: schedstat_inc(rq->yld_count);

CompletelyFairScheduler::yield_task(rq);

pcb.preempt_disable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的preempt_disablepreempt_enable似乎没有什么必要?,这里已经禁用中断,是不会被时钟中断调度走的,所以我感觉可以不需要这个两个调用。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里完全参考linux实现,应该是保证在一些特殊情况下也不会被抢占,保证drop(guard)和drop(irq_guard)成功执行。可以暂时先保持原状。


drop(guard);
drop(irq_guard);

pcb.preempt_enable(); // sched_preempt_enable_no_resched();

schedule(SchedMode::SM_NONE);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里我看Linux也是这样写的,但是我有个问题是,根据man手册:
image

调用进程会被放置至队尾,但是目前我们的调度系统SM_NONE是不会将其放入队尾的。应该是__schedule函数某个地方有问题。。你有兴趣看一下吗<

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可以的~


Ok(0)
}
}
2 changes: 2 additions & 0 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,8 @@ impl Syscall {
Self::fchmodat(dirfd, pathname, mode)
}

SYS_SCHED_YIELD => Self::do_sched_yield(),

SYS_SCHED_GETAFFINITY => {
let pid = args[0] as i32;
let size = args[1];
Expand Down
Loading