Coverage Report

Created: 2024-09-08 06:35

/rust/registry/src/index.crates.io-6f17d22bba15001f/nix-0.28.0/src/sys/eventfd.rs
Line
Count
Source (jump to first uncovered line)
1
use crate::errno::Errno;
2
use crate::{Result,unistd};
3
use std::os::unix::io::{FromRawFd, OwnedFd, AsRawFd, AsFd, RawFd, BorrowedFd};
4
5
libc_bitflags! {
6
    pub struct EfdFlags: libc::c_int {
7
        EFD_CLOEXEC; // Since Linux 2.6.27/FreeBSD 13.0
8
        EFD_NONBLOCK; // Since Linux 2.6.27/FreeBSD 13.0
9
        EFD_SEMAPHORE; // Since Linux 2.6.30/FreeBSD 13.0
10
    }
11
}
12
13
#[deprecated(since = "0.28.0", note = "Use EventFd::from_value_and_flags() instead")]
14
0
pub fn eventfd(initval: libc::c_uint, flags: EfdFlags) -> Result<OwnedFd> {
15
0
    let res = unsafe { libc::eventfd(initval, flags.bits()) };
16
0
17
0
    Errno::result(res).map(|r| unsafe { OwnedFd::from_raw_fd(r) })
18
0
}
19
20
#[derive(Debug)]
21
#[repr(transparent)]
22
pub struct EventFd(OwnedFd);
23
impl EventFd {
24
    /// [`EventFd::from_value_and_flags`] with `init_val = 0` and `flags = EfdFlags::empty()`.
25
0
    pub fn new() -> Result<Self> {
26
0
        Self::from_value_and_flags(0, EfdFlags::empty())
27
0
    }
28
    /// Constructs [`EventFd`] with the given `init_val` and `flags`.
29
    /// 
30
    /// Wrapper around [`libc::eventfd`].
31
0
    pub fn from_value_and_flags(init_val: u32, flags: EfdFlags) -> Result<Self> {
32
0
        let res = unsafe { libc::eventfd(init_val, flags.bits()) };
33
0
        Errno::result(res).map(|r| Self(unsafe { OwnedFd::from_raw_fd(r) }))
34
0
    }
35
    /// [`EventFd::from_value_and_flags`] with `init_val = 0` and given `flags`.
36
0
    pub fn from_flags(flags: EfdFlags) -> Result<Self> {
37
0
        Self::from_value_and_flags(0, flags)
38
0
    }
39
    /// [`EventFd::from_value_and_flags`] with given `init_val` and `flags = EfdFlags::empty()`.
40
0
    pub fn from_value(init_val: u32) -> Result<Self> {
41
0
        Self::from_value_and_flags(init_val, EfdFlags::empty())
42
0
    }
43
    /// Arms `self`, a following call to `poll`, `select` or `epoll` will return immediately.
44
    /// 
45
    /// [`EventFd::write`] with `1`.
46
0
    pub fn arm(&self) -> Result<usize> {
47
0
        self.write(1)
48
0
    }
49
    /// Defuses `self`, a following call to `poll`, `select` or `epoll` will block.
50
    /// 
51
    /// [`EventFd::write`] with `0`.
52
0
    pub fn defuse(&self) -> Result<usize> {
53
0
        self.write(0)
54
0
    }
55
    /// Enqueues `value` triggers.
56
    /// 
57
    /// The next `value` calls to `poll`, `select` or `epoll` will return immediately.
58
    /// 
59
    /// [`EventFd::write`] with `value`.
60
0
    pub fn write(&self, value: u64) -> Result<usize> { 
61
0
        unistd::write(&self.0,&value.to_ne_bytes())
62
0
    }
63
    // Reads the value from the file descriptor.
64
0
    pub fn read(&self) -> Result<u64> {
65
0
        let mut arr = [0; std::mem::size_of::<u64>()];
66
0
        unistd::read(self.0.as_raw_fd(),&mut arr)?;
67
0
        Ok(u64::from_ne_bytes(arr))
68
0
    }
69
}
70
impl AsFd for EventFd {
71
0
    fn as_fd(&self) -> BorrowedFd {
72
0
        self.0.as_fd()
73
0
    }
74
}
75
impl AsRawFd for EventFd {
76
0
    fn as_raw_fd(&self) -> RawFd {
77
0
        self.0.as_raw_fd()
78
0
    }
79
}
80
impl From<EventFd> for OwnedFd {
81
0
    fn from(x: EventFd) -> OwnedFd {
82
0
        x.0
83
0
    }
84
}