-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
syscall: add simple eventfd2 syscall
Signed-off-by: rayylee <[email protected]>
- Loading branch information
1 parent
ef68b1b
commit 570830f
Showing
6 changed files
with
108 additions
and
0 deletions.
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
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 | ||
} | ||
} |
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 |
---|---|---|
|
@@ -8,4 +8,6 @@ pub mod pipe; | |
|
||
pub use file::FileDesc; | ||
|
||
pub mod eventfd; | ||
|
||
pub mod epoll; |
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 |
---|---|---|
@@ -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) | ||
} | ||
} |
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