Struct hinix::eventfd::EventFd

source ·
pub struct EventFd(_);
Expand description

An event object that can be used as a wait/notify mechanism between user-space applications, threads in an app, or between the kernel and user-space.

This is a simpler, more efficient signaling mechanism than a pipe, if event notification is all that is required by the application.

The event is seen as a normal file handle, and thus can be used in combination with other handles such as from sockets, pipes, etc, in a poll/epoll/select call to provide additional signaling capabilities.

Implementations§

source§

impl EventFd

source

pub fn new(initval: u64) -> Result<EventFd>

Create a new event object.

This is the default configuration of the event object with no flags. When read, the value is returned and the count is reset to zero.

Parameters

initval The initial value held by the object

Examples found in repository?
examples/eventfd.rs (line 22)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() -> hinix::Result<()> {
    let evtfd = EventFd::new(0)?;
    println!("Got it as: {:?}", evtfd);

    let threvtfd = evtfd.try_clone()?;
    println!("Clone is: {:?}\n", evtfd);

    thread::spawn(move || {
        for i in 1..11 {
            thread::sleep(ONE_SEC);
            println!("Signaling the event [{}]...", i);
            threvtfd.write(i).expect("Failed writing to the event");
        }

        thread::sleep(TEN_MS);
        threvtfd.write(42).expect("Failed writing to the event");
    });

    loop {
        println!("Waiting on the event...");
        let n = evtfd.read()?;
        if n == 42 {
            break;
        }
        println!("Got the event: {}", n);
    }

    println!("Done");
    Ok(())
}
source

pub fn new_semaphore(initval: u64) -> Result<EventFd>

Create a new event object with the semaphore option.

This is applies the EDF_SEMAPHORE flag. When read, the value returned is 1, and the value is decremented by 1.

Parameters

initval The initial value held by the object

source

pub fn with_flags(initval: u64, flags: EfdFlags) -> Result<EventFd>

Create a new event object with the specified flags.

Parameters

initval The initial value held by the object flags The flags used to create the object

http://man7.org/linux/man-pages/man2/eventfd.2.html

source

pub fn try_clone(&self) -> Result<Self>

Try to clone the event object by making a dup() of the OS file handle.

Examples found in repository?
examples/eventfd.rs (line 25)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() -> hinix::Result<()> {
    let evtfd = EventFd::new(0)?;
    println!("Got it as: {:?}", evtfd);

    let threvtfd = evtfd.try_clone()?;
    println!("Clone is: {:?}\n", evtfd);

    thread::spawn(move || {
        for i in 1..11 {
            thread::sleep(ONE_SEC);
            println!("Signaling the event [{}]...", i);
            threvtfd.write(i).expect("Failed writing to the event");
        }

        thread::sleep(TEN_MS);
        threvtfd.write(42).expect("Failed writing to the event");
    });

    loop {
        println!("Waiting on the event...");
        let n = evtfd.read()?;
        if n == 42 {
            break;
        }
        println!("Got the event: {}", n);
    }

    println!("Done");
    Ok(())
}
source

pub fn read(&self) -> Result<u64>

Reads the value of the event object.

Examples found in repository?
examples/eventfd.rs (line 41)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() -> hinix::Result<()> {
    let evtfd = EventFd::new(0)?;
    println!("Got it as: {:?}", evtfd);

    let threvtfd = evtfd.try_clone()?;
    println!("Clone is: {:?}\n", evtfd);

    thread::spawn(move || {
        for i in 1..11 {
            thread::sleep(ONE_SEC);
            println!("Signaling the event [{}]...", i);
            threvtfd.write(i).expect("Failed writing to the event");
        }

        thread::sleep(TEN_MS);
        threvtfd.write(42).expect("Failed writing to the event");
    });

    loop {
        println!("Waiting on the event...");
        let n = evtfd.read()?;
        if n == 42 {
            break;
        }
        println!("Got the event: {}", n);
    }

    println!("Done");
    Ok(())
}
source

pub fn write(&self, val: u64) -> Result<()>

Writes a value to the event object.

Parameters

val The value to add to the one held by the object.

Examples found in repository?
examples/eventfd.rs (line 32)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
fn main() -> hinix::Result<()> {
    let evtfd = EventFd::new(0)?;
    println!("Got it as: {:?}", evtfd);

    let threvtfd = evtfd.try_clone()?;
    println!("Clone is: {:?}\n", evtfd);

    thread::spawn(move || {
        for i in 1..11 {
            thread::sleep(ONE_SEC);
            println!("Signaling the event [{}]...", i);
            threvtfd.write(i).expect("Failed writing to the event");
        }

        thread::sleep(TEN_MS);
        threvtfd.write(42).expect("Failed writing to the event");
    });

    loop {
        println!("Waiting on the event...");
        let n = evtfd.read()?;
        if n == 42 {
            break;
        }
        println!("Got the event: {}", n);
    }

    println!("Done");
    Ok(())
}

Trait Implementations§

source§

impl AsFd for EventFd

source§

fn as_fd(&self) -> BorrowedFd<'_>

Gets the raw file handle for the event object.

source§

impl AsRawFd for EventFd

source§

fn as_raw_fd(&self) -> RawFd

Gets the raw file handle for the event object.

source§

impl Debug for EventFd

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.