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
//! # Task Handle
//! A handle that allows operations
//! on unfinished tasks across threads

use crate::{
    RuntimeError, TaskState,
    constants::MAX_TASK_ID,
    executor::Executor,
    modules::handle_kind::{HandleKind, Plain, Waiting},
};
use std::{
    hash::{Hash, Hasher},
    marker::PhantomData,
    mem::ManuallyDrop,
    ptr,
    time::{Duration, Instant},
};

/// A task handle
///
/// Can be cloned freely, sent between threads, and read from
/// any of them
///
/// `W` says what else the handle can do. A handle to a task spawned
/// with `wait_for` is a `TaskHandle<T, Waiting<I>>`, which can
/// [`give`](TaskHandle::give) it input
pub struct TaskHandle<T, W = Plain>
where
    T: Sized,
    W: HandleKind,
{
    id: usize,

    /// What this kind of handle carries beside the id
    extra: W::Extra,

    _pd: PhantomData<T>,
}

impl<T> TaskHandle<T, Plain>
where
    T: Sized,
{
    /// Creates a new task handle
    pub(crate) fn new(id: usize) -> Self {
        Self {
            id,
            extra: (),
            _pd: PhantomData,
        }
    }

    /// Turns this handle into one of another kind, keeping its claim
    /// on the task
    pub(crate) fn into_kind<W>(self, extra: W::Extra) -> TaskHandle<T, W>
    where
        W: HandleKind,
    {
        // Its claim moves to the new handle, so its drop never runs
        let plain = ManuallyDrop::new(self);

        TaskHandle {
            id: plain.id,
            extra,
            _pd: PhantomData,
        }
    }
}

impl<T, W> TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
    /// A handle to no task, which reads `NoSuchTask`
    pub(crate) fn detached() -> Self {
        Self {
            id: MAX_TASK_ID,
            extra: W::detached(),
            _pd: PhantomData,
        }
    }

    /// Another handle of the same kind on the same task, naming a
    /// different output
    pub(crate) fn retyped<O>(&self) -> TaskHandle<O, W> {
        Executor::add_listener(self.id);

        TaskHandle {
            id: self.id,
            extra: W::cloned(&self.extra),
            _pd: PhantomData,
        }
    }

    /// A plain handle on the same task, keeping this handle's claim
    /// on it but not its way to give
    pub(crate) fn into_plain(self) -> TaskHandle<T, Plain> {
        let handle = ManuallyDrop::new(self);

        W::dropped(handle.id, &handle.extra);

        // Moved out whole, since the handle's own drop never runs
        drop(unsafe { ptr::read(&handle.extra) });

        TaskHandle {
            id: handle.id,
            extra: (),
            _pd: PhantomData,
        }
    }

    /// The id of the task behind this handle
    ///
    /// #### Note
    /// Only meaningful while the task is alive. Once every handle
    /// to it is gone, the same id can belong to another task
    pub fn id(&self) -> usize {
        self.id
    }

    /// Whether the task has settled, so a read won't block
    ///
    /// #### Note
    /// Doesn't mean there is an output. Use `is_ready` for that
    pub fn settled(&self) -> bool {
        self.state().terminal()
    }

    /// What the task is doing right now
    ///
    /// #### Note
    /// A snapshot. A repeating task moves on between runs whether
    /// anything is looking or not
    pub fn state(&self) -> TaskState {
        Executor::state(self.id)
    }

    /// Whether the task is queued and waiting to start
    ///
    /// A task spawned with `wait_for` that has never been given
    /// anything reads this too
    pub fn is_pending(&self) -> bool {
        self.state() == TaskState::Pending
    }

    /// Whether the task is running right now
    pub fn is_running(&self) -> bool {
        self.state() == TaskState::Running
    }

    /// Whether an output is written and waiting to be read
    pub fn is_ready(&self) -> bool {
        self.state() == TaskState::Ready
    }

    /// Whether the output has already been moved out by `take`
    pub fn is_taken(&self) -> bool {
        self.state() == TaskState::Taken
    }

    /// Whether a listener abandoned the task
    pub fn is_cancelled(&self) -> bool {
        self.state() == TaskState::Cancelled
    }

    /// Whether a run went past the task's timeout
    pub fn is_timed_out(&self) -> bool {
        self.state() == TaskState::TimedOut
    }

    /// Whether nothing is ever going to produce an output
    pub fn is_failed(&self) -> bool {
        self.state() == TaskState::Failed
    }

    /// Whether the task has settled and won't run again
    ///
    /// ## Behaviour
    /// For a repeat, `settled` is also true between runs. This is
    /// only true once a bounded series has run out, or the series
    /// was cancelled or failed. A settled one shot reads true too
    ///
    /// A task spawned with `wait_for` is only finished once it
    /// takes no more gives
    ///
    /// A series that ran out keeps its last output, so `join` and
    /// `take` still return it
    pub fn is_finished(&self) -> bool {
        Executor::finished(self.id)
    }

    /// Waits until the task settles, without reading it
    ///
    /// ## Returns
    /// How it settled
    ///
    /// #### Note
    /// A repeat settles after each run, so this returns once a run
    /// has published rather than when the series ends
    pub fn wait(&self) -> Result<TaskState, RuntimeError> {
        Executor::wait(self.id)
    }

    /// Reads the output if it is there, without waiting
    ///
    /// ## Returns
    /// The output, `NotReady` if the task hasn't settled, or why
    /// it settled without one
    pub fn try_join(&self) -> Result<T, RuntimeError>
    where
        T: Clone,
    {
        Executor::poll_result(self.id)
    }

    /// Waits up to `timeout` for the output
    ///
    /// ## Returns
    /// The output, or `NotReady` if the task still hasn't settled
    /// when the time is up. Any other error is the task's own
    ///
    /// Returns as soon as the task settles
    pub fn join_with_timeout(&self, timeout: Duration) -> Result<T, RuntimeError>
    where
        T: Clone,
    {
        Executor::clone_result_until(self.id, deadline(timeout))
    }

    /// Waits until the data is ready and returns it when it is
    ///
    /// A clone comes back, so every listener can have one
    pub fn join(self) -> Result<T, RuntimeError>
    where
        T: Clone,
    {
        Executor::clone_result(self.id)
    }

    /// Moves the output out if it is there, without waiting
    ///
    /// ## Returns
    /// The output, `NotReady` if the task hasn't settled, or
    /// `AlreadyTaken` if another listener moved it out first
    pub fn try_take(&self) -> Result<T, RuntimeError> {
        Executor::poll_take(self.id)
    }

    /// Waits up to `timeout` to move the output out
    ///
    /// ## Returns
    /// The output, or `NotReady` if the task still hasn't settled
    /// when the time is up
    ///
    /// Giving up leaves the output where it was, for a later read
    pub fn take_with_timeout(&self, timeout: Duration) -> Result<T, RuntimeError> {
        Executor::take_result_until(self.id, deadline(timeout))
    }

    /// Waits until the data is ready and moves it out of the task
    ///
    /// ## Returns
    /// The output, or `AlreadyTaken` if another listener moved it
    /// out first
    ///
    /// Only one caller can ever get it. Use `join` when more than
    /// one listener needs the value
    pub fn take(self) -> Result<T, RuntimeError> {
        Executor::take_result(self.id)
    }

    /// Abandons the task
    ///
    /// Every other listener's read returns `Cancelled` from here on
    ///
    /// #### Note
    /// A task already running may take a moment to stop, but its
    /// output is thrown away either way
    pub fn cancel(self) {
        Executor::cancel(self.id);
    }
}

impl<T, I> TaskHandle<T, Waiting<I>>
where
    T: Sized,
    I: Send + 'static,
{
    /// Gives the task the value its next run is handed
    ///
    /// ## Behaviour
    /// A task waiting for a give starts its next run, or its next
    /// series, with it. Gives don't queue: one while a run is waiting
    /// to start or under way leaves only the newest value. A give
    /// during a single run owes the task one more run, and a give
    /// during a series only changes what the rest of the series is
    /// handed
    ///
    /// Only a give that starts something counts against the task's
    /// `count`
    ///
    /// ## Returns
    /// `Finished` once the task takes no more gives, and `Cancelled`
    /// or `TaskFailed` once it has ended another way. The value is
    /// dropped in those cases
    ///
    /// A handle from a plain `spawn` can't give:
    ///
    /// ```compile_fail,E0599
    /// use atap::{Runtime, compute::Compute};
    ///
    /// let handle = Runtime::task(Compute::compute(|()| 1)).spawn();
    /// let _ = handle.give(());
    /// ```
    pub fn give(&self, value: I) -> Result<(), RuntimeError> {
        Executor::give(self.id, &self.extra, value)
    }

    /// Whether the task is waiting for a give, with nothing owed
    ///
    /// False while a run or series a give started is queued or under
    /// way, and once the task takes no more gives. A give while this
    /// reads true starts something
    ///
    /// #### Note
    /// A snapshot. A give from another thread can change it at once
    pub fn is_waiting(&self) -> bool {
        self.extra.gate().waiting()
    }

    /// Whether the task still takes gives
    #[inline(always)]
    pub(crate) fn takes_gives(&self) -> bool {
        !self.extra.gate().finished()
    }
}

/// Turns a timeout into the moment it runs out, or `None` if
/// the clock can't hold it
#[inline(always)]
fn deadline(timeout: Duration) -> Option<Instant> {
    Instant::now().checked_add(timeout)
}

impl<T, W> Clone for TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
    /// Duplicates the `TaskHandle`
    fn clone(&self) -> Self {
        Executor::add_listener(self.id);

        Self {
            id: self.id,
            extra: W::cloned(&self.extra),
            _pd: PhantomData,
        }
    }
}

/// Two handles are equal when they point at the same task
impl<T, W> PartialEq for TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
    }
}

impl<T, W> Eq for TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
}

/// Hashed on the task, the same as equality
impl<T, W> Hash for TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
    fn hash<H>(&self, state: &mut H)
    where
        H: Hasher,
    {
        self.id.hash(state);
    }
}

impl<T, W> std::fmt::Debug for TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
    /// The id and the task's current state
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("TaskHandle")
            .field("id", &self.id)
            .field("state", &self.state())
            .finish()
    }
}

impl<T, W> Drop for TaskHandle<T, W>
where
    T: Sized,
    W: HandleKind,
{
    /// Gives up this handle's claim on the task
    ///
    /// Dropping all handles clears the task entirely
    fn drop(&mut self) {
        W::dropped(self.id, &self.extra);
        Executor::drop_listener(self.id);
    }
}