@@ -3,9 +3,14 @@ use crate::{unistd, Result};
33use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , OwnedFd , RawFd } ;
44
55libc_bitflags ! {
6+ /// Eventfd flags.
67 pub struct EfdFlags : libc:: c_int {
8+ /// Set the close-on-exec (`FD_CLOEXEC`) flag on the new event file descriptor.
79 EFD_CLOEXEC ; // Since Linux 2.6.27/FreeBSD 13.0
10+ /// Set the `O_NONBLOCK` file status flag on the new event file description.
811 EFD_NONBLOCK ; // Since Linux 2.6.27/FreeBSD 13.0
12+ /// Provide semaphore-like semantics for reads from the new event file
13+ /// descriptor.
914 EFD_SEMAPHORE ; // Since Linux 2.6.30/FreeBSD 13.0
1015 }
1116}
@@ -14,20 +19,24 @@ libc_bitflags! {
1419 since = "0.28.0" ,
1520 note = "Use EventFd::from_value_and_flags() instead"
1621) ]
22+ #[ allow( missing_docs) ]
1723pub fn eventfd ( initval : libc:: c_uint , flags : EfdFlags ) -> Result < OwnedFd > {
1824 let res = unsafe { libc:: eventfd ( initval, flags. bits ( ) ) } ;
1925
2026 Errno :: result ( res) . map ( |r| unsafe { OwnedFd :: from_raw_fd ( r) } )
2127}
2228
29+ /// An eventfd file descriptor.
2330#[ derive( Debug ) ]
2431#[ repr( transparent) ]
2532pub struct EventFd ( OwnedFd ) ;
33+
2634impl EventFd {
2735 /// [`EventFd::from_value_and_flags`] with `init_val = 0` and `flags = EfdFlags::empty()`.
2836 pub fn new ( ) -> Result < Self > {
2937 Self :: from_value_and_flags ( 0 , EfdFlags :: empty ( ) )
3038 }
39+
3140 /// Constructs [`EventFd`] with the given `init_val` and `flags`.
3241 ///
3342 /// Wrapper around [`libc::eventfd`].
@@ -38,35 +47,58 @@ impl EventFd {
3847 let res = unsafe { libc:: eventfd ( init_val, flags. bits ( ) ) } ;
3948 Errno :: result ( res) . map ( |r| Self ( unsafe { OwnedFd :: from_raw_fd ( r) } ) )
4049 }
50+
4151 /// [`EventFd::from_value_and_flags`] with `init_val = 0` and given `flags`.
4252 pub fn from_flags ( flags : EfdFlags ) -> Result < Self > {
4353 Self :: from_value_and_flags ( 0 , flags)
4454 }
55+
4556 /// [`EventFd::from_value_and_flags`] with given `init_val` and `flags = EfdFlags::empty()`.
4657 pub fn from_value ( init_val : u32 ) -> Result < Self > {
4758 Self :: from_value_and_flags ( init_val, EfdFlags :: empty ( ) )
4859 }
60+
4961 /// Arms `self`, a following call to `poll`, `select` or `epoll` will return immediately.
5062 ///
5163 /// [`EventFd::write`] with `1`.
5264 pub fn arm ( & self ) -> Result < usize > {
5365 self . write ( 1 )
5466 }
67+
5568 /// Defuses `self`, a following call to `poll`, `select` or `epoll` will block.
5669 ///
5770 /// [`EventFd::write`] with `0`.
5871 pub fn defuse ( & self ) -> Result < usize > {
5972 self . write ( 0 )
6073 }
61- /// Enqueues `value` triggers.
74+
75+ /// Enqueues `value` triggers, i.e., adds the integer value supplied in `value`
76+ /// to the counter.
6277 ///
6378 /// The next `value` calls to `poll`, `select` or `epoll` will return immediately.
6479 ///
6580 /// [`EventFd::write`] with `value`.
6681 pub fn write ( & self , value : u64 ) -> Result < usize > {
6782 unistd:: write ( & self . 0 , & value. to_ne_bytes ( ) )
6883 }
69- // Reads the value from the file descriptor.
84+
85+ /// Reads the value from the file descriptor.
86+ ///
87+ /// * If [`EFD_SEMAPHORE`](EfdFlags::EFD_SEMAPHORE) was not specified and
88+ /// the eventfd counter has a nonzero value, then this function returns
89+ /// an `u64` containing that value, and the counter's value is reset to
90+ /// zero.
91+ ///
92+ /// * If [`EFD_SEMAPHORE`](EfdFlags::EFD_SEMAPHORE) was specified and the
93+ /// eventfd counter has a nonzero value, then this function returns an
94+ /// `u64` containing the value 1, and the counter's value is decremented
95+ /// by 1.
96+ ///
97+ /// * If the eventfd counter is zero at the time of this call, then the
98+ /// call either blocks until the counter becomes nonzero (at which time,
99+ /// this function proceeds as described above) or fails with the error
100+ /// `EAGAIN` if the file descriptor has been made nonblocking with
101+ /// [`EFD_NONBLOCK`](EfdFlags::EFD_NONBLOCK).
70102 pub fn read ( & self ) -> Result < u64 > {
71103 let mut arr = [ 0 ; std:: mem:: size_of :: < u64 > ( ) ] ;
72104 unistd:: read ( & self . 0 , & mut arr) ?;
0 commit comments