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
//! A demo driver for experimentation purposes

use std::future::Future;
use std::io;
use std::pin::Pin;
use std::task::{Poll, Context};
use std::thread;

use futures_core::ready;
use once_cell::sync::Lazy;
use parking_lot::Mutex;

const SQ_ENTRIES: u32   = 32;
const CQ_ENTRIES: usize = (SQ_ENTRIES * 2) as usize;

use access_queue::*;

use super::{Drive, Completion};

static SQ: Lazy<AccessQueue<Mutex<iou::SubmissionQueue<'static>>>> = Lazy::new(init_sq);

/// The driver handle
pub struct DemoDriver<'a> {
    sq: Access<'a, Mutex<iou::SubmissionQueue<'static>>>,
}

impl Default for DemoDriver<'_> {
    fn default() -> Self {
        driver()
    }
}

impl Drive for DemoDriver<'_> {
    fn poll_prepare<'cx>(
        mut self: Pin<&mut Self>,
        ctx: &mut Context<'cx>,
        prepare: impl FnOnce(iou::SubmissionQueueEvent<'_>, &mut Context<'cx>) -> Completion<'cx>,
    ) -> Poll<Completion<'cx>> {
        // Wait for access to prepare. When ready, create a new Access future to wait next time we
        // want to prepare with this driver, and lock the SQ.
        //
        // TODO likely we should be using a nonblocking mutex?
        let access = ready!(Pin::new(&mut self.sq).poll(ctx));
        let (sq, access) = access.hold_and_reenqueue();
        self.sq = access;
        let mut sq = sq.lock();
        loop {
            match sq.next_sqe() {
                Some(sqe)   => return Poll::Ready(prepare(sqe, ctx)),
                None        => { let _ = sq.submit(); }
            }
        }
    }

    fn poll_submit(
        self: Pin<&mut Self>,
        _: &mut Context<'_>,
        eager: bool,
    ) -> Poll<io::Result<usize>> {
        let result = if eager {
            self.sq.skip_queue().lock().submit()
        } else {
            Ok(0)
        };
        Poll::Ready(result)
    }
}

/// Construct a demo driver handle
pub fn driver() -> DemoDriver<'static> {
    DemoDriver { sq: SQ.access() }
}

fn init_sq() -> AccessQueue<Mutex<iou::SubmissionQueue<'static>>> {
    unsafe {
        static mut RING: Option<iou::IoUring> = None;
        RING = Some(iou::IoUring::new(SQ_ENTRIES).expect("TODO handle io_uring_init failure"));
        let (sq, cq, _) = RING.as_mut().unwrap().queues();
        thread::spawn(move || complete(cq));
        AccessQueue::new(Mutex::new(sq), CQ_ENTRIES)
    }
}

unsafe fn complete(mut cq: iou::CompletionQueue<'static>) {
    while let Ok(cqe) = cq.wait_for_cqe() {
        let mut ready = cq.ready() as usize + 1;
        SQ.release(ready);

        super::complete(cqe);
        ready -= 1;

        while let Some(cqe) = cq.peek_for_cqe() {
            if ready == 0 {
                ready = cq.ready() as usize + 1;
                SQ.release(ready);
            }

            super::complete(cqe);
            ready -= 1;
        }

        debug_assert!(ready == 0);
    }
}