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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

//! A thread-local executor based on poll(2).

use super::wait::FdWait;
use crate::fd::FdReadyDriver;
use crate::fd::PollFdReady;
use crate::interest::InterestSlot;
use crate::interest::PollEvents;
use crate::interest::PollInterestSet;
use crate::local::LocalDriver;
use crate::local::LocalInner;
use crate::sparsevec::SparseVec;
use crate::wait::WaitDriver;
use crate::waker::WakerList;
use pal::unix::while_eintr;
use pal::unix::SyscallResult;
use pal_event::Event;
use std::io;
use std::os::unix::prelude::*;
use std::sync::Arc;
use std::sync::OnceLock;
use std::task::Context;
use std::task::Poll;
use std::time::Duration;

#[derive(Debug, Default)]
pub(crate) struct WaitState {
    pollfds: Vec<libc::pollfd>,
}

#[derive(Debug, Default)]
pub(crate) struct WaitCancel {
    event: OnceLock<Event>,
}

impl WaitCancel {
    pub fn cancel_wait(&self) {
        self.event.get().unwrap().signal();
    }
}

#[derive(Debug, Default)]
pub(crate) struct State {
    entries: SparseVec<FdEntry>,
}

#[derive(Debug)]
struct FdEntry {
    fd: RawFd,
    interests: PollInterestSet,
}

impl State {
    fn add_fd(&mut self, fd: RawFd) -> usize {
        self.entries.add(FdEntry {
            fd,
            interests: Default::default(),
        })
    }

    fn remove_fd(&mut self, index: usize) {
        self.entries.remove(index);
    }

    fn poll_fd(
        &mut self,
        cx: &mut Context<'_>,
        index: usize,
        slot: InterestSlot,
        events: PollEvents,
    ) -> Poll<PollEvents> {
        let entry = &mut self.entries[index];
        entry.interests.poll_ready(cx, slot, events)
    }

    fn clear_fd_ready(&mut self, index: usize, slot: InterestSlot) {
        let entry = &mut self.entries[index];
        entry.interests.clear_ready(slot)
    }

    pub fn pre_wait(&mut self, wait_state: &mut WaitState, wait_cancel: &WaitCancel) {
        wait_state.pollfds.clear();
        wait_state
            .pollfds
            .extend(self.entries.iter().map(|(_, entry)| {
                let events = entry.interests.events_to_poll();
                if !events.is_empty() {
                    libc::pollfd {
                        fd: entry.fd,
                        events: events.to_poll_events(),
                        revents: 0,
                    }
                } else {
                    libc::pollfd {
                        fd: -1,
                        events: 0,
                        revents: 0,
                    }
                }
            }));

        let event = wait_cancel.event.get_or_init(Event::new);
        wait_state.pollfds.push(libc::pollfd {
            fd: event.as_fd().as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        });
    }

    pub fn post_wait(&mut self, wait_state: &mut WaitState, wakers: &mut WakerList) {
        for ((_, entry), pollfd) in self.entries.iter_mut().zip(wait_state.pollfds.iter_mut()) {
            let revents = PollEvents::from_poll_events(pollfd.revents);
            if !revents.is_empty() {
                entry.interests.wake_ready(revents, wakers);
            }
        }
    }
}

#[cfg(target_os = "linux")]
fn poll(pollfds: &mut [libc::pollfd], timeout: Option<&Duration>) -> i32 {
    let timeout = timeout.map(|timeout| libc::timespec {
        tv_sec: timeout.as_secs().try_into().unwrap(),
        tv_nsec: timeout.subsec_nanos().into(),
    });

    // SAFETY: calling as documented.
    unsafe {
        libc::ppoll(
            pollfds.as_mut_ptr(),
            pollfds.len().try_into().unwrap(),
            timeout.as_ref().map_or(std::ptr::null(), |t| t),
            std::ptr::null(),
        )
    }
}

#[cfg(not(target_os = "linux"))]
fn poll(pollfds: &mut [libc::pollfd], timeout: Option<&Duration>) -> i32 {
    // SAFETY: calling as documented.
    unsafe {
        libc::poll(
            pollfds.as_mut_ptr(),
            pollfds.len().try_into().unwrap(),
            timeout.map_or(-1, |t| t.as_millis().min(i32::MAX as u128) as i32),
        )
    }
}

impl WaitState {
    pub fn wait(&mut self, wait_cancel: &WaitCancel, timeout: Option<Duration>) {
        while_eintr(|| poll(&mut self.pollfds, timeout.as_ref()).syscall_result())
            .expect("ppoll unexpectedly failed");
        if self.pollfds.last().unwrap().revents != 0 {
            // Consume the wake event.
            assert!(wait_cancel.event.get().unwrap().try_wait());
        }
    }
}

impl FdReadyDriver for LocalDriver {
    type FdReady = FdReady;

    fn new_fd_ready(&self, socket: RawFd) -> io::Result<Self::FdReady> {
        let index = self.inner.lock_sys_state().add_fd(socket);
        Ok(FdReady {
            inner: self.inner.clone(),
            index,
        })
    }
}

#[derive(Debug)]
pub struct FdReady {
    inner: Arc<LocalInner>,
    index: usize,
}

impl Drop for FdReady {
    fn drop(&mut self) {
        self.inner.lock_sys_state().remove_fd(self.index);
    }
}

impl PollFdReady for FdReady {
    fn poll_fd_ready(
        &mut self,
        cx: &mut Context<'_>,
        slot: InterestSlot,
        events: PollEvents,
    ) -> Poll<PollEvents> {
        self.inner
            .lock_sys_state()
            .poll_fd(cx, self.index, slot, events)
    }

    fn clear_fd_ready(&mut self, slot: InterestSlot) {
        self.inner.lock_sys_state().clear_fd_ready(self.index, slot)
    }
}

impl WaitDriver for LocalDriver {
    type Wait = FdWait<FdReady>;

    fn new_wait(&self, fd: RawFd, read_size: usize) -> io::Result<Self::Wait> {
        Ok(FdWait::new(fd, self.new_fd_ready(fd)?, read_size))
    }
}