@@ -3,9 +3,14 @@ use crate::{unistd, Result};
3
3
use std:: os:: unix:: io:: { AsFd , AsRawFd , BorrowedFd , FromRawFd , OwnedFd , RawFd } ;
4
4
5
5
libc_bitflags ! {
6
+ /// Eventfd flags.
6
7
pub struct EfdFlags : libc:: c_int {
8
+ /// Set the close-on-exec (`FD_CLOEXEC`) flag on the new event file descriptor.
7
9
EFD_CLOEXEC ; // Since Linux 2.6.27/FreeBSD 13.0
10
+ /// Set the `O_NONBLOCK` file status flag on the new event file description.
8
11
EFD_NONBLOCK ; // Since Linux 2.6.27/FreeBSD 13.0
12
+ /// Provide semaphore-like semantics for reads from the new event file
13
+ /// descriptor.
9
14
EFD_SEMAPHORE ; // Since Linux 2.6.30/FreeBSD 13.0
10
15
}
11
16
}
@@ -14,20 +19,24 @@ libc_bitflags! {
14
19
since = "0.28.0" ,
15
20
note = "Use EventFd::from_value_and_flags() instead"
16
21
) ]
22
+ #[ allow( missing_docs) ]
17
23
pub fn eventfd ( initval : libc:: c_uint , flags : EfdFlags ) -> Result < OwnedFd > {
18
24
let res = unsafe { libc:: eventfd ( initval, flags. bits ( ) ) } ;
19
25
20
26
Errno :: result ( res) . map ( |r| unsafe { OwnedFd :: from_raw_fd ( r) } )
21
27
}
22
28
29
+ /// An eventfd file descriptor.
23
30
#[ derive( Debug ) ]
24
31
#[ repr( transparent) ]
25
32
pub struct EventFd ( OwnedFd ) ;
33
+
26
34
impl EventFd {
27
35
/// [`EventFd::from_value_and_flags`] with `init_val = 0` and `flags = EfdFlags::empty()`.
28
36
pub fn new ( ) -> Result < Self > {
29
37
Self :: from_value_and_flags ( 0 , EfdFlags :: empty ( ) )
30
38
}
39
+
31
40
/// Constructs [`EventFd`] with the given `init_val` and `flags`.
32
41
///
33
42
/// Wrapper around [`libc::eventfd`].
@@ -38,35 +47,58 @@ impl EventFd {
38
47
let res = unsafe { libc:: eventfd ( init_val, flags. bits ( ) ) } ;
39
48
Errno :: result ( res) . map ( |r| Self ( unsafe { OwnedFd :: from_raw_fd ( r) } ) )
40
49
}
50
+
41
51
/// [`EventFd::from_value_and_flags`] with `init_val = 0` and given `flags`.
42
52
pub fn from_flags ( flags : EfdFlags ) -> Result < Self > {
43
53
Self :: from_value_and_flags ( 0 , flags)
44
54
}
55
+
45
56
/// [`EventFd::from_value_and_flags`] with given `init_val` and `flags = EfdFlags::empty()`.
46
57
pub fn from_value ( init_val : u32 ) -> Result < Self > {
47
58
Self :: from_value_and_flags ( init_val, EfdFlags :: empty ( ) )
48
59
}
60
+
49
61
/// Arms `self`, a following call to `poll`, `select` or `epoll` will return immediately.
50
62
///
51
63
/// [`EventFd::write`] with `1`.
52
64
pub fn arm ( & self ) -> Result < usize > {
53
65
self . write ( 1 )
54
66
}
67
+
55
68
/// Defuses `self`, a following call to `poll`, `select` or `epoll` will block.
56
69
///
57
70
/// [`EventFd::write`] with `0`.
58
71
pub fn defuse ( & self ) -> Result < usize > {
59
72
self . write ( 0 )
60
73
}
61
- /// Enqueues `value` triggers.
74
+
75
+ /// Enqueues `value` triggers, i.e., adds the integer value supplied in `value`
76
+ /// to the counter.
62
77
///
63
78
/// The next `value` calls to `poll`, `select` or `epoll` will return immediately.
64
79
///
65
80
/// [`EventFd::write`] with `value`.
66
81
pub fn write ( & self , value : u64 ) -> Result < usize > {
67
82
unistd:: write ( & self . 0 , & value. to_ne_bytes ( ) )
68
83
}
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).
70
102
pub fn read ( & self ) -> Result < u64 > {
71
103
let mut arr = [ 0 ; std:: mem:: size_of :: < u64 > ( ) ] ;
72
104
unistd:: read ( & self . 0 , & mut arr) ?;
0 commit comments