Skip to content

Commit

Permalink
syscall: add simple eventfd2 syscall
Browse files Browse the repository at this point in the history
Signed-off-by: rayylee <[email protected]>
  • Loading branch information
hbuxiaofei committed Apr 2, 2024
1 parent ef68b1b commit 570830f
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 0 deletions.
83 changes: 83 additions & 0 deletions ulib/axstarry/src/syscall_fs/ctype/eventfd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
extern crate alloc;
use alloc::sync::Arc;
use core::mem::size_of;
use axerrno::{AxError, AxResult};
use axfs::api::{FileIO, FileIOType, SeekFrom};
use axsync::Mutex;
use axtask::yield_now;

pub struct EventfdFile {
pub inner: Arc<Mutex<EventfdFileInner>>,
}

struct EventfdCtx {
count: u64,
}

impl EventfdCtx {
pub fn new() -> Self {
Self {
count: 0,
}
}
}

pub struct EventfdFileInner {
ctx: EventfdCtx,
}

impl EventfdFile {
pub fn new() -> Self {
Self {
inner: Arc::new(Mutex::new(EventfdFileInner {
ctx: EventfdCtx::new(),
})),
}
}
}

impl FileIO for EventfdFile {
fn read(&self, buf: &mut [u8]) -> AxResult<usize> {
let mut cnt = 0;
loop {
let mut inner = self.inner.lock();
if inner.ctx.count > 0 {
cnt = inner.ctx.count; // EFD_SEMAPHORE ?
inner.ctx.count -= cnt;
break
}
drop(inner);
yield_now();
}

let bytes = cnt.to_ne_bytes();
buf.copy_from_slice(&bytes);

Ok(size_of::<u64>())
}
fn write(&self, buf: &[u8]) -> AxResult<usize> {
let mut inner = self.inner.lock();
let ucnt: u64 = u64::from_ne_bytes(buf.try_into().unwrap());
inner.ctx.count += ucnt;

Ok(size_of::<u64>())
}
fn flush(&self) -> AxResult {
Err(AxError::Unsupported)
}
fn seek(&self, _pos: SeekFrom) -> AxResult<u64> {
Err(AxError::Unsupported)
}
fn readable(&self) -> bool {
true
}
fn writable(&self) -> bool {
true
}
fn executable(&self) -> bool {
false
}
fn get_type(&self) -> FileIOType {
FileIOType::FileDesc
}
}
2 changes: 2 additions & 0 deletions ulib/axstarry/src/syscall_fs/ctype/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ pub mod pipe;

pub use file::FileDesc;

pub mod eventfd;

pub mod epoll;
2 changes: 2 additions & 0 deletions ulib/axstarry/src/syscall_fs/fs_syscall_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ numeric_enum_macro::numeric_enum! {
pub enum FsSyscallId {
// fs
GETCWD = 17,
EVENTFD2 = 19,
EPOLL_CREATE = 20,
EPOLL_CTL = 21,
EPOLL_WAIT = 22,
Expand Down Expand Up @@ -70,6 +71,7 @@ numeric_enum_macro::numeric_enum! {
STAT = 4,
GETCWD = 79,
UNLINK = 87,
EVENTFD2 = 290,
EPOLL_CREATE = 213,
EPOLL_CTL = 233,
EPOLL_WAIT = 232,
Expand Down
18 changes: 18 additions & 0 deletions ulib/axstarry/src/syscall_fs/imp/eventfd.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate alloc;
use alloc::sync::Arc;
use crate::{SyscallError, SyscallResult};
use axprocess::current_process;

use crate::syscall_fs::ctype::eventfd::{EventfdFile};

pub fn syscall_eventfd2(_args: [usize; 6]) -> SyscallResult {
let file = EventfdFile::new();
let process = current_process();
let mut fd_table = process.fd_manager.fd_table.lock();
if let Ok(num) = process.alloc_fd(&mut fd_table) {
fd_table[num] = Some(Arc::new(file));
Ok(num as isize)
} else {
Err(SyscallError::EMFILE)
}
}
2 changes: 2 additions & 0 deletions ulib/axstarry/src/syscall_fs/imp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
extern crate alloc;

mod ctl;
mod eventfd;
mod epoll;
mod io;
mod link;
mod mount;
mod poll;
mod stat;
pub use ctl::*;
pub use eventfd::*;
pub use epoll::*;
pub use io::*;
pub use link::*;
Expand Down
1 change: 1 addition & 0 deletions ulib/axstarry/src/syscall_fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub fn fs_syscall(syscall_id: fs_syscall_id::FsSyscallId, args: [usize; 6]) -> S
LINKAT => sys_linkat(args),
UNLINKAT => syscall_unlinkat(args),
UTIMENSAT => syscall_utimensat(args),
EVENTFD2 => syscall_eventfd2(args),
EPOLL_CREATE => syscall_epoll_create1(args),
EPOLL_CTL => syscall_epoll_ctl(args),
EPOLL_WAIT => syscall_epoll_wait(args),
Expand Down

0 comments on commit 570830f

Please sign in to comment.