From 5ed064122cb437110e7cf6f3b8cae9254ce640f5 Mon Sep 17 00:00:00 2001 From: "binary.bruce" Date: Thu, 9 May 2024 20:58:57 +0800 Subject: [PATCH] fix lint and add doc --- Cargo.toml | 1 - crates/eventfd/src/lib.rs | 35 +++++++++++++++++++++++++---------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c4b7aee42c..59adab4f94 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,6 @@ members = [ "crates/axfs_ramfs", "crates/axfs_vfs", "crates/axio", - "crates/eventfd", "crates/capability", "crates/crate_interface", "crates/driver_block", diff --git a/crates/eventfd/src/lib.rs b/crates/eventfd/src/lib.rs index 970e54dd5c..b84d2e2c87 100644 --- a/crates/eventfd/src/lib.rs +++ b/crates/eventfd/src/lib.rs @@ -1,30 +1,44 @@ -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 + /// #[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 +/// 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, @@ -32,6 +46,7 @@ impl EventFd { } } + /// read the EventFd pub fn read(&mut self) -> Option { // 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. @@ -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; @@ -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 } @@ -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 for EventFd { fn from(value: u64) -> Self { - EventFd { - value: value, - flags: 0, - } + EventFd { value, flags: 0 } } }