Skip to content

Commit

Permalink
fix lint and add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
binary-bruce committed May 9, 2024
1 parent e27873c commit 5ed0641
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ members = [
"crates/axfs_ramfs",
"crates/axfs_vfs",
"crates/axio",
"crates/eventfd",
"crates/capability",
"crates/crate_interface",
"crates/driver_block",
Expand Down
35 changes: 25 additions & 10 deletions crates/eventfd/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,52 @@
use bitflags::bitflags;

bitflags! {
/// https://sites.uclouvain.be/SystInfo/usr/include/sys/eventfd.h.html
/// The EventFd object to be used by syscall
#![no_std]
use core::convert::From;
use core::convert::Into;

bitflags::bitflags! {
/// The flags used to create an EventFd object
/// <https://sites.uclouvain.be/SystInfo/usr/include/sys/eventfd.h.html>
#[derive(Clone, Copy, Debug)]
pub struct EventFdFlag: u32 {
/// SEMAPHORE flag
const EFD_SEMAPHORE = 0x1;
/// NONBLOCK flag
const EFD_NONBLOCK = 0x800;
/// CLOEXEC flag
const EFD_CLOEXEC = 0x80000;
}
}

/// the result of writing to a EventFd object
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EventFdWriteResult {
/// the write is done successfully
OK,

/// an attempt is made to write the value 0xffffffffffffffff (f64::MAX)
BadInput,

/// it's not writable yet
NotReady,
}

/// https://man7.org/linux/man-pages/man2/eventfd2.2.html
/// <https://man7.org/linux/man-pages/man2/eventfd2.2.html>
pub struct EventFd {
value: u64,
flags: u32,
}

/// the EventFd data type
impl EventFd {
/// create an EventFd object with initial value and flags
pub fn new(initval: u64, flags: u32) -> EventFd {
EventFd {
value: initval,
flags,
}
}

/// read the EventFd
pub fn read(&mut self) -> Option<u64> {
// If EFD_SEMAPHORE was not specified and the eventfd counter has a nonzero value, then a read returns 8 bytes containing that value,
// and the counter's value is reset to zero.
Expand All @@ -54,6 +69,7 @@ impl EventFd {
None
}

/// write to EventFd
pub fn write(&mut self, val: u64) -> EventFdWriteResult {
if val == u64::MAX {
return EventFdWriteResult::BadInput;
Expand All @@ -70,6 +86,7 @@ impl EventFd {
}
}

/// check if the specified flag is set or not
pub fn is_flag_set(&self, flag: EventFdFlag) -> bool {
self.flags & flag.bits() != 0
}
Expand All @@ -79,21 +96,19 @@ impl EventFd {
}
}

/// create eventfd with flags of zero default value
/// create an EventFd object with flags of zero default value
pub fn create_eventfd(value: u64) -> EventFd {
value.into()
}

/// the util function to create an Eventfd object
pub fn create_eventfd_with_flags(value: u64, flags: u32) -> EventFd {
(value, flags).into()
}

impl From<u64> for EventFd {
fn from(value: u64) -> Self {
EventFd {
value: value,
flags: 0,
}
EventFd { value, flags: 0 }
}
}

Expand Down

0 comments on commit 5ed0641

Please sign in to comment.