about summary refs log tree commit diff
path: root/tvix/store/src/proto/sync_read_into_async_read.rs
blob: 0a0ef019781ccfe6d6760009bd7aa5fc405c68c6 (plain) (blame)
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
use bytes::Buf;
use core::task::Poll::Ready;
use futures::ready;
use futures::Future;
use std::io;
use std::io::Read;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Context;
use std::task::Poll;
use tokio::io::AsyncRead;
use tokio::runtime::Handle;
use tokio::sync::Mutex;
use tokio::task::JoinHandle;

#[derive(Debug)]
enum State<Buf: bytes::Buf + bytes::BufMut> {
    Idle(Option<Buf>),
    Busy(JoinHandle<(io::Result<usize>, Buf)>),
}

use State::{Busy, Idle};

/// Use a [`SyncReadIntoAsyncRead`] to asynchronously read from a
/// synchronous API.
#[derive(Debug)]
pub struct SyncReadIntoAsyncRead<R: Read + Send, Buf: bytes::Buf + bytes::BufMut> {
    state: Mutex<State<Buf>>,
    reader: Arc<Mutex<R>>,
    rt: Handle,
}

impl<R: Read + Send, Buf: bytes::Buf + bytes::BufMut> SyncReadIntoAsyncRead<R, Buf> {
    /// This must be called from within a Tokio runtime context, or else it will panic.
    #[track_caller]
    pub fn new(rt: Handle, reader: R) -> Self {
        Self {
            rt,
            state: State::Idle(None).into(),
            reader: Arc::new(reader.into()),
        }
    }

    /// This must be called from within a Tokio runtime context, or else it will panic.
    pub fn new_with_reader(readable: R) -> Self {
        Self::new(Handle::current(), readable)
    }
}

/// Repeats operations that are interrupted.
macro_rules! uninterruptibly {
    ($e:expr) => {{
        loop {
            match $e {
                Err(ref e) if e.kind() == io::ErrorKind::Interrupted => {}
                res => break res,
            }
        }
    }};
}

impl<
        R: Read + Send + 'static + std::marker::Unpin,
        Buf: bytes::Buf + bytes::BufMut + Send + Default + std::marker::Unpin + 'static,
    > AsyncRead for SyncReadIntoAsyncRead<R, Buf>
{
    fn poll_read(
        self: Pin<&mut Self>,
        cx: &mut Context<'_>,
        dst: &mut tokio::io::ReadBuf<'_>,
    ) -> Poll<io::Result<()>> {
        let me = self.get_mut();
        // Do we need this mutex?
        let state = me.state.get_mut();

        loop {
            match state {
                Idle(ref mut buf_cell) => {
                    let mut buf = buf_cell.take().unwrap_or_default();

                    if buf.has_remaining() {
                        // Here, we will split the `buf` into `[..dst.remaining()... ; rest ]`
                        // The `rest` is stuffed into the `buf_cell` for further poll_read.
                        // The other is completely consumed into the unfilled destination.
                        // `rest` can be empty.
                        let mut adjusted_src =
                            buf.copy_to_bytes(std::cmp::min(buf.remaining(), dst.remaining()));
                        let copied_size = adjusted_src.remaining();
                        adjusted_src.copy_to_slice(dst.initialize_unfilled_to(copied_size));
                        dst.set_filled(copied_size);
                        *buf_cell = Some(buf);
                        return Ready(Ok(()));
                    }

                    let reader = me.reader.clone();
                    *state = Busy(me.rt.spawn_blocking(move || {
                        let result = uninterruptibly!(reader.blocking_lock().read(
                            // SAFETY: `reader.read` will *ONLY* write initialized bytes
                            // and never *READ* uninitialized bytes
                            // inside this buffer.
                            //
                            // Furthermore, casting the slice as `*mut [u8]`
                            // is safe because it has the same layout.
                            //
                            // Finally, the pointer obtained is valid and owned
                            // by `buf` only as we have a valid mutable reference
                            // to it, it is valid for write.
                            //
                            // Here, we copy an nightly API: https://doc.rust-lang.org/stable/src/core/mem/maybe_uninit.rs.html#994-998
                            unsafe {
                                &mut *(buf.chunk_mut().as_uninit_slice_mut()
                                    as *mut [std::mem::MaybeUninit<u8>]
                                    as *mut [u8])
                            }
                        ));

                        if let Ok(n) = result {
                            // SAFETY: given we initialize `n` bytes, we can move `n` bytes
                            // forward.
                            unsafe {
                                buf.advance_mut(n);
                            }
                        }

                        (result, buf)
                    }));
                }
                Busy(ref mut rx) => {
                    let (result, mut buf) = ready!(Pin::new(rx).poll(cx))?;

                    match result {
                        Ok(n) => {
                            if n > 0 {
                                let remaining = std::cmp::min(n, dst.remaining());
                                let mut adjusted_src = buf.copy_to_bytes(remaining);
                                adjusted_src.copy_to_slice(dst.initialize_unfilled_to(remaining));
                                dst.advance(remaining);
                            }
                            *state = Idle(Some(buf));
                            return Ready(Ok(()));
                        }
                        Err(e) => {
                            *state = Idle(None);
                            return Ready(Err(e));
                        }
                    }
                }
            }
        }
    }
}

impl<R: Read + Send, Buf: bytes::Buf + bytes::BufMut> From<R> for SyncReadIntoAsyncRead<R, Buf> {
    /// This must be called from within a Tokio runtime context, or else it will panic.
    fn from(value: R) -> Self {
        Self::new_with_reader(value)
    }
}