atap 0.1.0

Threadsafe futureless async runtime for macOS
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! # Unix task
//! The tasks the `Unix` constructors, a `UnixListener` and a
//! `UnixDatagram` return, and everything they do once run

use crate::modules::input::Token;
use crate::{
    RuntimeError,
    constants::INLINE_PAYLOAD,
    futures::{
        net::{
            datagram::{recv_datagram, send_datagram},
            socket::{Options, begin_connect, configure, finished_connecting, open},
            step::{Progress, settle, wait_on},
        },
        task::{
            Nothing, Task,
            sealed::{self, Step},
        },
        unix::{
            path::{Bound, from_raw, to_raw},
            unix_socket::{UnixConnection, UnixDatagram, UnixListener},
        },
    },
    modules::{fd::Fd, int_check::IntCheck, park},
};
use std::{
    mem,
    path::{Path, PathBuf},
    ptr,
    sync::Arc,
};

// Anything larger costs a page mapping per task
const _: () = assert!(mem::size_of::<Result<UnixConnection, RuntimeError>>() <= INLINE_PAYLOAD);
const _: () = assert!(mem::size_of::<Result<UnixListener, RuntimeError>>() <= INLINE_PAYLOAD);
const _: () = assert!(mem::size_of::<Result<UnixDatagram, RuntimeError>>() <= INLINE_PAYLOAD);
const _: () =
    assert!(mem::size_of::<Result<(Vec<u8>, Option<PathBuf>), RuntimeError>>() <= INLINE_PAYLOAD);

/// Binds a fresh socket of `kind` to `path`
///
/// ## Returns
/// The socket and its file, which is removed again if anything
/// after the bind fails
fn bind_path(path: &Path, kind: libc::c_int) -> Result<(Fd, Bound), RuntimeError> {
    let (raw, len) = to_raw(path)?;
    let fd = open(libc::AF_UNIX, kind)?;

    unsafe {
        libc::bind(
            fd.raw(),
            (&raw as *const libc::sockaddr_un).cast::<libc::sockaddr>(),
            len,
        )
    }
    .check()?;

    // The file exists from here, so it is this socket's to remove
    Ok((fd, Bound::new(path)))
}

/// Opens a Unix connection
///
/// ## Returns
/// The connection
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixConnectTask {
    /// Where to connect
    path: PathBuf,

    /// A socket part way through connecting
    trying: Progress<Option<Fd>>,
}

impl UnixConnectTask {
    /// Connects to the socket at `path`
    pub(crate) fn new(path: PathBuf) -> Self {
        Self {
            path,
            trying: Progress::default(),
        }
    }

    /// Does as much of the connect as can be done without waiting
    fn advance(&mut self) -> Result<Step<Result<UnixConnection, RuntimeError>>, RuntimeError> {
        let fd = match self.trying.0.take() {
            Some(fd) => fd,

            None => {
                let (raw, len) = to_raw(&self.path)?;
                let fd = open(libc::AF_UNIX, libc::SOCK_STREAM)?;

                let at_once = begin_connect(
                    fd.raw(),
                    (&raw as *const libc::sockaddr_un).cast::<libc::sockaddr>(),
                    len,
                )?;

                if at_once {
                    return Ok(Step::Done(Ok(UnixConnection::new(fd, self.path.clone()))));
                }

                fd
            }
        };

        if finished_connecting(fd.raw())? {
            return Ok(Step::Done(Ok(UnixConnection::new(fd, self.path.clone()))));
        }

        let step = wait_on(fd.raw(), libc::EVFILT_WRITE)?;
        self.trying.0 = Some(fd);

        Ok(step)
    }
}

/// Opens a Unix socket that waits for connections
///
/// ## Returns
/// The listener
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixListenTask {
    /// Where to listen
    path: PathBuf,

    /// What the socket is set up with
    options: Options,
}

impl UnixListenTask {
    /// Listens at `path`
    pub(crate) fn new(path: PathBuf) -> Self {
        Self {
            path,
            options: Options::default(),
        }
    }

    /// How many connections may wait to be accepted
    ///
    /// ## Behaviour
    /// The kernel's own limit caps it
    ///
    /// ## Returns
    /// The task. Calling it twice keeps the last
    pub fn backlog(mut self, backlog: u32) -> Self {
        self.options.backlog = Some(backlog);
        self
    }

    /// Binds and listens
    fn listen(&self) -> Result<UnixListener, RuntimeError> {
        let (fd, bound) = bind_path(&self.path, libc::SOCK_STREAM)?;

        unsafe { libc::listen(fd.raw(), self.options.backlog()) }.check()?;

        Ok(UnixListener::new(fd, bound))
    }
}

/// Takes the next connection off a Unix listener
///
/// ## Returns
/// The connection
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixAcceptTask {
    /// Where the connections come from
    listener: UnixListener,
}

impl UnixAcceptTask {
    /// Accepts from `listener`
    pub(crate) fn new(listener: UnixListener) -> Self {
        Self { listener }
    }

    /// Takes a connection if one is waiting
    fn advance(&mut self) -> Result<Step<Result<UnixConnection, RuntimeError>>, RuntimeError> {
        let fd = self.listener.fd();

        loop {
            let accepted = unsafe { libc::accept(fd, ptr::null_mut(), ptr::null_mut()) }.check();

            match accepted {
                Ok(raw) => {
                    let conn = Fd::new(raw);
                    configure(conn.raw())?;

                    let path = self.listener.path().to_path_buf();

                    return Ok(Step::Done(Ok(UnixConnection::new(conn, path))));
                }

                // Gone again before it could be taken, so the next one
                Err(RuntimeError::CheckError(Some(libc::EINTR | libc::ECONNABORTED))) => {}

                Err(RuntimeError::CheckError(Some(libc::EAGAIN))) => {
                    return wait_on(fd, libc::EVFILT_READ);
                }

                Err(error) => return Err(error),
            }
        }
    }
}

/// Opens a Unix datagram socket
///
/// ## Returns
/// The socket, bound to its path
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixBindTask {
    /// Where to bind, or `None` for a socket with no path
    path: Option<PathBuf>,
}

impl UnixBindTask {
    /// Binds at `path`
    pub(crate) fn new(path: PathBuf) -> Self {
        Self { path: Some(path) }
    }

    /// Opens a socket bound nowhere
    pub(crate) fn unbound() -> Self {
        Self { path: None }
    }

    /// Binds
    fn bind(&self) -> Result<UnixDatagram, RuntimeError> {
        let (fd, bound) = match &self.path {
            Some(path) => bind_path(path, libc::SOCK_DGRAM)?,
            None => (open(libc::AF_UNIX, libc::SOCK_DGRAM)?, Bound::unbound()),
        };

        Ok(UnixDatagram::new(fd, bound))
    }
}

/// Opens both ends of a connection, with nothing on disk
///
/// ## Returns
/// The two connected ends
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixPairTask;

impl UnixPairTask {
    fn pair(&self) -> Result<(UnixConnection, UnixConnection), RuntimeError> {
        let mut ends = [0; 2];

        unsafe { libc::socketpair(libc::AF_UNIX, libc::SOCK_STREAM, 0, ends.as_mut_ptr()) }
            .check()?;

        let (first, second) = (Fd::new(ends[0]), Fd::new(ends[1]));

        configure(first.raw())?;
        configure(second.raw())?;

        Ok((
            UnixConnection::new(first, PathBuf::new()),
            UnixConnection::new(second, PathBuf::new()),
        ))
    }
}

/// Sends one datagram to a path
///
/// ## Returns
/// The number of bytes sent, which is all of them
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixSendToTask {
    /// What to send from
    socket: UnixDatagram,

    /// Where to send
    path: PathBuf,

    /// What to send
    data: Arc<[u8]>,
}

impl UnixSendToTask {
    /// Sends `data` from `socket` to the socket at `path`
    pub(crate) fn new(socket: UnixDatagram, path: PathBuf, data: Arc<[u8]>) -> Self {
        Self { socket, path, data }
    }

    /// Sends
    fn send(&self) -> Result<usize, RuntimeError> {
        let (raw, len) = to_raw(&self.path)?;

        let sent = send_datagram(
            self.socket.fd(),
            &self.data,
            (&raw as *const libc::sockaddr_un).cast::<libc::sockaddr>(),
            len,
        )?;

        // No room is an answer rather than a wait
        sent.ok_or(RuntimeError::CheckError(Some(libc::EAGAIN)))
    }
}

/// Receives one datagram on a Unix socket
///
/// ## Returns
/// The whole datagram, and the path it came from if the sender
/// has one
#[derive(Debug, Clone)]
#[must_use = "a task does nothing until it is run or spawned"]
pub struct UnixRecvFromTask {
    /// Where to receive
    socket: UnixDatagram,
}

impl UnixRecvFromTask {
    /// Receives on `socket`
    pub(crate) fn new(socket: UnixDatagram) -> Self {
        Self { socket }
    }

    /// Takes a datagram if one is waiting
    fn advance(
        &mut self,
    ) -> Result<Step<Result<(Vec<u8>, Option<PathBuf>), RuntimeError>>, RuntimeError> {
        let fd = self.socket.fd();

        match recv_datagram(fd, false)? {
            Some((data, storage, len)) => Ok(Step::Done(Ok((data, from_raw(&storage, len))))),
            None => wait_on(fd, libc::EVFILT_READ),
        }
    }
}

impl sealed::Sealed for UnixConnectTask {}
impl sealed::Sealed for UnixListenTask {}
impl sealed::Sealed for UnixAcceptTask {}
impl sealed::Sealed for UnixBindTask {}
impl sealed::Sealed for UnixSendToTask {}
impl sealed::Sealed for UnixRecvFromTask {}

impl Task for UnixConnectTask {
    type Output = Result<UnixConnection, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn prepare(&mut self, _token: Token) {
        self.trying = Progress::default();
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl Task for UnixListenTask {
    type Output = Result<UnixListener, RuntimeError>;
    type Input = Nothing;

    /// Never waits on the socket, so this is the whole task
    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.listen()
    }
}

impl Task for UnixAcceptTask {
    type Output = Result<UnixConnection, RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}

impl sealed::Sealed for UnixPairTask {}

impl Task for UnixPairTask {
    type Output = Result<(UnixConnection, UnixConnection), RuntimeError>;
    type Input = Nothing;

    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.pair()
    }
}

impl Task for UnixBindTask {
    type Output = Result<UnixDatagram, RuntimeError>;
    type Input = Nothing;

    /// Never waits on the socket, so this is the whole task
    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.bind()
    }
}

impl Task for UnixSendToTask {
    type Output = Result<usize, RuntimeError>;
    type Input = Nothing;

    /// Never waits on the socket, so this is the whole task
    fn execute(&self, _token: Token, _reactor_id: i32, _task_id: usize) -> Self::Output {
        self.send()
    }
}

impl Task for UnixRecvFromTask {
    type Output = Result<(Vec<u8>, Option<PathBuf>), RuntimeError>;
    type Input = Nothing;

    /// Waits on this thread, for `Runtime::block`
    fn execute(&self, _token: Token, reactor_id: i32, task_id: usize) -> Self::Output {
        park::drive(self.clone(), reactor_id, task_id)
    }

    fn step(&mut self, _token: Token, _reactor_id: i32, _task_id: usize) -> Step<Self::Output> {
        settle(self.advance())
    }
}