Skip to content

Commit

Permalink
make fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
TTaq committed Apr 22, 2024
1 parent d211633 commit c41b958
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 81 deletions.
58 changes: 25 additions & 33 deletions kernel/src/net/event_poll/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use core::{
cell::Cell, fmt::Debug, sync::atomic::{AtomicBool, Ordering}
cell::Cell,
fmt::Debug,
sync::atomic::{AtomicBool, Ordering},
};

use alloc::{
Expand Down Expand Up @@ -866,19 +868,17 @@ bitflags! {
}
}



#[derive(Debug, Clone, Copy)]
#[repr(C)]
struct CPollfd{
struct CPollfd {
fd: i32,
events: i16,
revents: i16,
}

impl Default for CPollfd{
impl Default for CPollfd {
fn default() -> Self {
Self{
Self {
fd: 0,
events: 0,
revents: 0,
Expand All @@ -893,59 +893,51 @@ pub struct Pollfd {
revents: Cell<EPollEventType>,
}

impl Pollfd{
pub fn new(fd: Option<i32>,events: EPollEventType) -> Self{
impl Pollfd {
pub fn new(fd: Option<i32>, events: EPollEventType) -> Self {
let revents = Cell::new(EPollEventType::empty());
Self{
Self {
fd,
events,
revents,
}
}

pub fn fd(&self) -> Option<i32>{
pub fn fd(&self) -> Option<i32> {
self.fd
}

pub fn events(&self) -> EPollEventType{
pub fn events(&self) -> EPollEventType {
self.events
}

pub fn revents(&self) -> &Cell<EPollEventType>{
pub fn revents(&self) -> &Cell<EPollEventType> {
&self.revents
}
}

impl From<CPollfd> for Pollfd{
fn from(from: CPollfd) -> Self{
let fd = if from.fd>=0{
Some(from.fd)
}else{
None
};
impl From<CPollfd> for Pollfd {
fn from(from: CPollfd) -> Self {
let fd = if from.fd >= 0 { Some(from.fd) } else { None };
let events = EPollEventType::from_bits_truncate(from.events as u32);
let revents = Cell::new(EPollEventType::from_bits_truncate(from.revents as u32));
Self {
fd,
events,
Self {
fd,
events,
revents,
}
}
}
}

impl From<Pollfd> for CPollfd{
fn from(from: Pollfd) -> Self{
let fd = if let Some(fd) = from.fd(){
fd
}else{
-1
};
impl From<Pollfd> for CPollfd {
fn from(from: Pollfd) -> Self {
let fd = if let Some(fd) = from.fd() { fd } else { -1 };
let events = from.events().bits() as i16;
let revents = from.revents().get().bits() as i16;
Self {
Self {
fd,
events,
events,
revents,
}
}
}
}
91 changes: 50 additions & 41 deletions kernel/src/net/event_poll/syscall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,19 @@ use alloc::vec::Vec;
use system_error::SystemError;

use crate::{
arch::ipc::signal::SigSet, filesystem::vfs::file::FileMode, ipc::signal::set_current_sig_blocked, mm::VirtAddr, process::ProcessManager, syscall::{
arch::ipc::signal::SigSet,
filesystem::vfs::file::FileMode,
ipc::signal::set_current_sig_blocked,
mm::VirtAddr,
process::ProcessManager,
syscall::{
user_access::{UserBufferReader, UserBufferWriter},
Syscall,
},
time::{timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper}, PosixTimeSpec},
time::{
timer::{next_n_us_timer_jiffies, Timer, WakeUpHelper},
PosixTimeSpec,
},
};

use super::{CPollfd, EPollCtlOption, EPollEvent, EPollEventType, EventPoll, Pollfd};
Expand Down Expand Up @@ -104,20 +112,18 @@ impl Syscall {
wait_ret
}

pub fn poll(ufds: VirtAddr,nfds: u32,timeout_msecs: i32) -> Result<usize,SystemError>{


pub fn poll(ufds: VirtAddr, nfds: u32, timeout_msecs: i32) -> Result<usize, SystemError> {
let fds = {
let mut read_add = ufds;
let mut fds:Vec<Pollfd> = Vec::with_capacity(nfds as usize);
for _ in 0..nfds{
let mut fds: Vec<Pollfd> = Vec::with_capacity(nfds as usize);
for _ in 0..nfds {
let reader = UserBufferReader::new(
read_add.as_ptr::<CPollfd>(),
core::mem::size_of::<CPollfd>(),
true
true,
)?;
let mut cfd =CPollfd::default();
reader.copy_one_from_user::<CPollfd>( &mut cfd, 0)?;
let mut cfd = CPollfd::default();
reader.copy_one_from_user::<CPollfd>(&mut cfd, 0)?;
// kdebug!("{:?}",cfd);
let fd = Pollfd::from(cfd);
// kdebug!("{:?}",fd);
Expand All @@ -128,82 +134,85 @@ impl Syscall {
};

let mut timespec = None;
if timeout_msecs>=0 {
if timeout_msecs >= 0 {
let sec = timeout_msecs as i64 / 1000;
let nsec = 1000000 *(timeout_msecs as i64 % 1000);
timespec = Some(PosixTimeSpec::new(sec,nsec));
let nsec = 1000000 * (timeout_msecs as i64 % 1000);
timespec = Some(PosixTimeSpec::new(sec, nsec));
}

let nums_events = Self::do_poll(&fds,timespec)?;
let nums_events = Self::do_poll(&fds, timespec)?;

let mut write_add = ufds;

for fd in fds {
let cfd = CPollfd::from(fd);
let mut writer = UserBufferWriter::new(
write_add.as_ptr::<CPollfd>(),
core::mem::size_of::<CPollfd>(),
false)?;
write_add.as_ptr::<CPollfd>(),
core::mem::size_of::<CPollfd>(),
false,
)?;
writer.copy_one_to_user(&cfd, 0)?;
write_add += core::mem::size_of::<CPollfd>();
}

Ok(nums_events)
}
pub fn do_poll(poll_fds: &[Pollfd],timespec: Option<PosixTimeSpec>) -> Result<usize,SystemError> {
pub fn do_poll(
poll_fds: &[Pollfd],
timespec: Option<PosixTimeSpec>,
) -> Result<usize, SystemError> {
let mut timer = None;
let mut timeout =false;
loop{
let mut timeout = false;
loop {
let mut revent_nums = 0;
for poll_fd in poll_fds{

let fd = match poll_fd.fd(){
for poll_fd in poll_fds {
let fd = match poll_fd.fd() {
Some(fd) => fd,
None => continue,
};
if fd > 0{
if fd > 0 {
let current_pcb = ProcessManager::current_pcb();
let fd_table = current_pcb.fd_table();
let fd_table_guard = fd_table.read();
let file = fd_table_guard
.get_file_by_fd(fd)
.ok_or(SystemError::EBADF)?.clone();
.ok_or(SystemError::EBADF)?
.clone();
drop(fd_table_guard);
let revents = EPollEventType::from_bits_truncate(file.poll()? as u32);
if !revents.is_empty() {
poll_fd.revents().set(revents);
revent_nums += 1;
poll_fd.revents().set(revents);
revent_nums += 1;
}

}
}
}
// kdebug!("{:?}",revent_nums);
if revent_nums > 0 {
return Ok(revent_nums);
}

if timeout{
if timeout {
return Ok(0);
}

if !timespec.is_none()&&timespec.unwrap().tv_sec==0 {
if !timespec.is_none() && timespec.unwrap().tv_sec == 0 {
return Ok(0);
}

// 若无事件发生,则注册定时器,超时后直接返回0
if let Some(timespec) = timespec {
if timer.is_none(){
if timer.is_none() {
let handle = WakeUpHelper::new(ProcessManager::current_pcb());
let jiffies = next_n_us_timer_jiffies(
(timespec.tv_sec * 1000000 + timespec.tv_nsec / 1000) as u64
);
let inner = Timer::new(handle,jiffies);
inner.activate();
timer = Some(inner);
let jiffies = next_n_us_timer_jiffies(
(timespec.tv_sec * 1000000 + timespec.tv_nsec / 1000) as u64,
);
let inner = Timer::new(handle, jiffies);
inner.activate();
timer = Some(inner);
}
}
if let Some(ref timer) = timer {
if timer.timeout(){
if timer.timeout() {
// 超时
timeout = true;
}
Expand Down
8 changes: 1 addition & 7 deletions kernel/src/syscall/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -824,13 +824,7 @@ impl Syscall {
}

#[cfg(target_arch = "x86_64")]
SYS_POLL => {
Self::poll(
VirtAddr::new(args[0]),
args[1] as u32,
args[2] as i32,
)
}
SYS_POLL => Self::poll(VirtAddr::new(args[0]), args[1] as u32, args[2] as i32),

SYS_SETPGID => {
kwarn!("SYS_SETPGID has not yet been implemented");
Expand Down

0 comments on commit c41b958

Please sign in to comment.