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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
// Smelling Salts
//
// Copyright (c) 2020 Jeron Aldaron Lau
//
// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
// https://apache.org/licenses/LICENSE-2.0>, or the Zlib License, <LICENSE-ZLIB
// or http://opensource.org/licenses/Zlib>, at your option. This file may not be
// copied, modified, or distributed except according to those terms.

use crate::watcher::{Watcher, EPOLLIN};

use std::convert::TryInto;
use std::mem;
use std::mem::MaybeUninit;
use std::os::raw;
use std::sync::Once;
use std::sync::{Condvar, Mutex};
use std::task::Waker;
use std::thread;
use std::os::unix::io::RawFd;

const EPOLL_CTL_ADD: i32 = 1;
const EPOLL_CTL_DEL: i32 = 2;

const O_CLOEXEC: raw::c_int = 0x0008_0000;

#[repr(C)]
#[derive(Copy, Clone)]
union EpollData {
    ptr: *mut raw::c_void,
    fd: RawFd,
    uint32: u32,
    uint64: u64,
}

#[repr(packed, C)]
#[derive(Copy, Clone)]
struct EpollEvent {
    events: u32,     /* Epoll events */
    data: EpollData, /* User data variable */
}

#[allow(non_camel_case_types)]
type c_ssize = isize; // True for most unix
#[allow(non_camel_case_types)]
type c_size = usize; // True for most unix

extern "C" {
    fn epoll_create1(flags: raw::c_int) -> raw::c_int;
    // fn close(fd: RawFd) -> raw::c_int;
    fn epoll_ctl(
        epfd: RawFd,
        op: raw::c_int,
        fd: RawFd,
        event: *mut EpollEvent,
    ) -> raw::c_int;
    fn epoll_wait(
        epfd: RawFd,
        events: *mut EpollEvent,
        maxevents: raw::c_int,
        timeout: raw::c_int,
    ) -> raw::c_int;
    fn pipe2(pipefd: *mut [RawFd; 2], flags: raw::c_int) -> raw::c_int;
    fn write(fd: RawFd, buf: *const raw::c_void, count: c_size) -> c_ssize;
    fn read(fd: RawFd, buf: *mut raw::c_void, count: c_size) -> c_ssize;
}

// Used to initialize the hardware thread.
static INIT: Once = Once::new();
static mut SHARED_CONTEXT: SharedContext = SharedContext::new();

#[derive(Debug)]
struct DeviceID(u64);

/// A message sent from a thread to the hardware thread.
#[derive(Debug)]
enum Message {
    /// Add device (ID, FD).
    Device(DeviceID, RawFd, Watcher),
    /// There's a new waker for a device.
    Waker(DeviceID, Waker),
    /// Disconnect a device.
    Disconnect(RawFd, *const (Mutex<bool>, Condvar)),
}

// This function checks for hardware events using epoll_wait (blocking I/O) in
// a loop.
fn hardware_thread(recver: RawFd) {
    // Wakers
    let mut wakers: Vec<Option<Waker>> = vec![None];

    // Create a new epoll instance.
    let epoll_fd = unsafe { epoll_create1(O_CLOEXEC) };
    error(epoll_fd).unwrap();

    // Add receiver to epoll.
    unsafe {
        error(epoll_ctl(
            epoll_fd,
            EPOLL_CTL_ADD,
            recver,
            &mut EpollEvent {
                events: EPOLLIN,               /* available for read operations */
                data: EpollData { uint64: 0 }, // Use reserved ID, 0 for pipe
            },
        ))
        .unwrap();
    }

    // An infinite loop that goes until the program exits.
    loop {
        // Wait
        let mut ev = MaybeUninit::<EpollEvent>::uninit();

        // Wait for something to happen.
        if unsafe {
            epoll_wait(
                epoll_fd,
                ev.as_mut_ptr(),
                1,  /* Get one event at a time */
                -1, /* wait indefinitely */
            )
        } < 0
        {
            // Ignore error
            continue;
        }
        let index = DeviceID(unsafe { ev.assume_init().data.uint64 });

        // Check epoll event for which hardware (or recv).
        if index.0 == 0 {
            // Pipe
            let mut buffer = mem::MaybeUninit::<Message>::uninit();
            let len = unsafe {
                read(
                    recver,
                    buffer.as_mut_ptr().cast(),
                    mem::size_of::<Message>(),
                )
            };
            let message = unsafe { buffer.assume_init() };
            assert_eq!(len as usize, mem::size_of::<Message>());
            match message {
                Message::Device(device_id, device_fd, events) => {
                    let index: usize = device_id.0.try_into().unwrap();
                    // Resize wakers Vec
                    wakers.resize(wakers.len().max(index), None);
                    // Register into epoll
                    unsafe {
                        error(epoll_ctl(
                            epoll_fd,
                            EPOLL_CTL_ADD,
                            device_fd,
                            &mut EpollEvent {
                                events: events.0,
                                data: EpollData {
                                    uint64: device_id.0,
                                },
                            },
                        ))
                        .unwrap();
                    }
                }
                Message::Waker(device_id, waker) => {
                    let index: usize = device_id.0.try_into().unwrap();
                    // Place waker into wakers Vec
                    wakers[index - 1] = Some(waker);
                }
                Message::Disconnect(device_fd, pair) => unsafe {
                    // Unregister from epoll
                    error(epoll_ctl(
                        epoll_fd,
                        EPOLL_CTL_DEL,
                        device_fd,
                        &mut EpollEvent {
                            /* ignored, can't be null, though */
                            events: 0,
                            data: EpollData { uint64: 0 },
                        },
                    ))
                    .unwrap();
                    // Let the device thread know we're done.
                    let (lock, cvar) = &*pair;
                    let mut started = lock.lock().unwrap();
                    *started = true;
                    cvar.notify_one();
                },
            }
            continue;
        }

        // File descriptor (device wake)
        let id: usize = index.0.try_into().unwrap();
        if let Some(waker) = wakers[id - 1].take() {
            waker.wake();
        }
    }
}

// Convert a C error (negative on error) into a result.
fn error(err: raw::c_int) -> Result<(), raw::c_int> {
    if err < 0 {
        Err(err)
    } else {
        Ok(())
    }
}

fn context() -> &'static mut Mutex<Context> {
    unsafe { &mut *SHARED_CONTEXT.0.as_mut_ptr() }
}

struct Context {
    // Variables for figuring out the next id
    next: DeviceID,
    garbage: Vec<DeviceID>,
    // Send side of the pipe.
    sender: RawFd,
}

impl Context {
    // Initialize context.
    fn new(sender: RawFd) -> Self {
        Context {
            next: DeviceID(1),
            garbage: Vec::new(),
            sender,
        }
    }

    // Create an ID
    fn create_id(&mut self) -> DeviceID {
        if let Some(id) = self.garbage.pop() {
            id
        } else {
            let ret = DeviceID(self.next.0);
            self.next.0 += 1;
            ret
        }
    }

    // Delete an ID, so it can be re-used.
    fn delete_id(&mut self, device_id: DeviceID) {
        if device_id.0 == self.next.0 - 1 {
            self.next.0 -= 1;
        } else {
            self.garbage.push(device_id);
        }
    }
}

struct SharedContext(MaybeUninit<Mutex<Context>>);

impl SharedContext {
    const fn new() -> Self {
        SharedContext(MaybeUninit::uninit())
    }

    fn init(&mut self, context: Mutex<Context>) {
        self.0 = MaybeUninit::new(context);
    }
}

/// Represents some device.
#[derive(Debug)]
pub struct Device {
    // File descriptor to be registered with epoll.
    fd: RawFd,
    // Software ID for identifying this device.
    device_id: DeviceID,
    // True if old() deconstructor has already been called.
    old: bool,
}

impl Device {
    /// Start checking for events on a new device from a linux file descriptor.
    pub fn new(fd: RawFd, events: Watcher) -> Self {
        // Start thread if it wasn't running before.
        INIT.call_once(|| {
            // Create pipe for communication
            let mut pipe = MaybeUninit::<[RawFd; 2]>::uninit();
            error(unsafe { pipe2(pipe.as_mut_ptr(), O_CLOEXEC) }).unwrap();
            let [recver, sender] = unsafe { pipe.assume_init() };
            // Initialize shared context.
            unsafe { SHARED_CONTEXT.init(Mutex::new(Context::new(sender))) }
            // Start hardware thread
            let _join = thread::spawn(move || hardware_thread(recver));
        });
        // Get a new device ID
        let mut context = context().lock().unwrap();
        let device_id = context.create_id();
        let write_fd = context.sender;
        let message = [Message::Device(DeviceID(device_id.0), fd, events)];

        // Send message to register this device.
        unsafe {
            if write(write_fd, message.as_ptr().cast(), mem::size_of::<Message>()) as usize
                != mem::size_of::<Message>()
            {
                panic!("Failed write to pipe");
            }
        }

        // Deconstructor hasn't run yet.
        let old = false;

        // Return the device
        Device { fd, device_id, old }
    }

    /// Register a waker to wake when the device gets an event.
    pub fn register_waker(&self, waker: &Waker) {
        assert_eq!(self.old, false);
        let context = context().lock().unwrap();
        let write_fd = context.sender;
        let message = [Message::Waker(DeviceID(self.device_id.0), waker.clone())];
        unsafe {
            if write(write_fd, message.as_ptr().cast(), mem::size_of::<Message>()) as usize
                != mem::size_of::<Message>()
            {
                panic!("Failed write to pipe");
            }
        }
    }

    /// Convenience function to get the raw File Descriptor of the Device.
    pub fn fd(&self) -> RawFd {
        self.fd
    }

    /// Stop checking for events on a device from a linux file descriptor.
    #[allow(clippy::mutex_atomic)]
    pub fn old(&mut self) {
        // Make sure that this deconstructor hasn't already run.
        if self.old {
            return;
        }
        self.old = true;

        //
        let mut context = context().lock().unwrap();
        let write_fd = context.sender;
        let pair = Box::pin((Mutex::new(false), Condvar::new()));
        let message = [Message::Disconnect(self.fd, &*pair)];
        // Unregister ID
        unsafe {
            if write(write_fd, message.as_ptr().cast(), mem::size_of::<Message>()) as usize
                != mem::size_of::<Message>()
            {
                panic!("Failed write to pipe");
            }
        }
        // Free ID to be able to be used again.
        context.delete_id(DeviceID(self.device_id.0));
        // Wait for the deregister to complete.
        let (lock, cvar) = &*pair;
        let mut started = lock.lock().unwrap();
        while !*started {
            started = cvar.wait(started).unwrap();
        }
    }
}

impl Drop for Device {
    fn drop(&mut self) {
        self.old();
    }
}