1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright 2022 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

use base::Event;

use crate::AsyncError;
use crate::AsyncResult;
use crate::EventAsync;
use crate::Executor;

impl EventAsync {
    pub fn new(event: Event, ex: &Executor) -> AsyncResult<EventAsync> {
        ex.async_from(event)
            .map(|io_source| EventAsync { io_source })
    }

    /// Gets the next value from the eventfd.
    pub async fn next_val(&self) -> AsyncResult<u64> {
        let (n, v) = self
            .io_source
            .read_to_vec(None, 0u64.to_ne_bytes().to_vec())
            .await?;
        if n != 8 {
            return Err(AsyncError::EventAsync(base::Error::new(libc::ENODATA)));
        }
        Ok(u64::from_ne_bytes(v.try_into().unwrap()))
    }
}

#[cfg(test)]
mod tests {
    use std::sync::Arc;

    use base::EventExt;

    use super::super::fd_executor::EpollReactor;
    use super::super::uring_executor::UringReactor;
    use super::*;
    use crate::common_executor::RawExecutor;
    use crate::sys::unix::uring_executor::is_uring_stable;

    pub(crate) fn new_poll(
        event: Event,
        ex: &Arc<RawExecutor<EpollReactor>>,
    ) -> AsyncResult<EventAsync> {
        ex.new_source(event)
            .map(|io_source| EventAsync { io_source })
    }

    pub(crate) fn new_uring(
        event: Event,
        ex: &Arc<RawExecutor<UringReactor>>,
    ) -> AsyncResult<EventAsync> {
        ex.new_source(event)
            .map(|io_source| EventAsync { io_source })
    }

    #[test]
    fn next_val_reads_value() {
        async fn go(event: Event, ex: &Executor) -> u64 {
            let event_async = EventAsync::new(event, ex).unwrap();
            event_async.next_val().await.unwrap()
        }

        let eventfd = Event::new().unwrap();
        eventfd.write_count(0xaa).unwrap();
        let ex = Executor::new().unwrap();
        let val = ex.run_until(go(eventfd, &ex)).unwrap();
        assert_eq!(val, 0xaa);
    }

    #[test]
    fn next_val_reads_value_poll_and_ring() {
        if !is_uring_stable() {
            return;
        }

        async fn go(event_async: EventAsync) -> u64 {
            event_async.next_val().await.unwrap()
        }

        let eventfd = Event::new().unwrap();
        eventfd.write_count(0xaa).unwrap();
        let uring_ex = RawExecutor::<UringReactor>::new().unwrap();
        let val = uring_ex
            .run_until(go(new_uring(eventfd, &uring_ex).unwrap()))
            .unwrap();
        assert_eq!(val, 0xaa);

        let eventfd = Event::new().unwrap();
        eventfd.write_count(0xaa).unwrap();
        let poll_ex = RawExecutor::<EpollReactor>::new().unwrap();
        let val = poll_ex
            .run_until(go(new_poll(eventfd, &poll_ex).unwrap()))
            .unwrap();
        assert_eq!(val, 0xaa);
    }
}