takeaway 0.1.4

An efficient work-stealing task queue with prioritization and batching.
Documentation
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
//! Enqueuing tasks.

use alloc::{boxed::Box, vec::Vec};
use core::{
    alloc::Layout,
    cell::UnsafeCell,
    mem::MaybeUninit,
    ptr::{self, NonNull},
    slice,
    task::Waker,
};

use crate::Task;

//----------- Enqueuer ---------------------------------------------------------

/// A handle to enqueue tasks for a [`Worker`].
///
/// [`Worker::enqueue()`] / [`enqueue_one()`] is the most convenient way to
/// enqueue new tasks, but can be troublesome if the [`Worker`] is not directly
/// available or borrowed elsewhere.  An [`Enqueuer`] is a dedicated handle to
/// the [`Worker`] for enqueueing new tasks, and can help work around such
/// issues.  Most notably, it can be used while the worker is asleep.  It can be
/// accessed via [`Worker::enqueuer()`] (which returns `&Rc<Enqueuer<T>>`).
///
/// [`Worker`]: crate::Worker
/// [`Worker::enqueue()`]: crate::Worker::enqueue()
/// [`enqueue_one()`]: crate::Worker::enqueue_one()
/// [`Worker::enqueuer()`]: crate::Worker::enqueuer()
pub struct Enqueuer<T: Task> {
    inner: UnsafeCell<Inner<T>>,
}

struct Inner<T: Task> {
    /// The task counter.
    ///
    /// This is a very compact state variable for enqueueing new tasks.  It is
    /// optimized for the hot path of adding new tasks while not borrowed.
    ///
    /// ## States
    ///
    /// - Normal (`v <= -2 * esz`): The buffer has space for `-v - esz` more
    ///   bytes (`-v / esz - 1` more tasks), and is not borrowed.  The next
    ///   task should be written to `end + v + esz`.
    ///
    /// - Full (`v = -esz`, no waker): The buffer is out of space, but is not
    ///   borrowed.  It must be reallocated to add new tasks.
    ///
    /// - Asleep (`v = -esz`, waker set): The buffer was recently cleared, and a
    ///   waker has been set.  It is not borrowed, and a normal state can be set
    ///   once the waker is used.
    ///
    /// - Locked (`v = 0`): The buffer has been locked by another actor.  This
    ///   is usually grounds for panicking.
    ///
    /// The value is always a multiple of the element size, and is always zero
    /// or negative.  If the element size is 0, `esz` is treated as 1.
    ///
    /// There was a choice of positive or negative states, and of counting up
    /// or counting down.  The counter uses negative states and counts up so
    /// that tasks can be appended to the buffer instead of prepended, and the
    /// counter (with a single addition) determines the address to write to.
    ///
    /// This is especially well-suited to architectures with flag registers,
    /// where the state transition for enqueueing new tasks also immediately
    /// indicates whether it is full or locked.
    counter: isize,

    /// The buffer of tasks.
    end: NonNull<T>,

    /// The capacity of the buffer, in bytes.
    capacity: isize,

    /// A waker for the worker, if it is asleep.
    waker: Option<Waker>,
}

impl<T: Task> Enqueuer<T> {
    /// Construct a new [`Enqueuer`].
    pub(crate) const fn new() -> Self {
        let capacity = if Self::ZST { isize::MAX } else { 0 };
        let counter = -capacity - Self::ESZ;
        Self {
            inner: UnsafeCell::new(Inner {
                counter,
                end: NonNull::dangling(),
                capacity,
                waker: None,
            }),
        }
    }

    /// Set a waker in this [`Enqueuer`].
    ///
    /// # Panics
    ///
    /// Panics if:
    /// - The enqueuer is already borrowed.
    /// - The enqueuer has any tasks.
    pub(crate) fn set_waker(&self, waker: Waker) {
        match self.waker() {
            Some(slot) => {
                // State: Asleep.
                *slot = waker;
            }

            slot @ None => {
                // State: Normal / Empty / Full / Locked.
                assert!(!self.is_locked(), "the enqueuer is locked");

                // State: Normal / Empty / Full.
                let counter = self.counter();
                let empty = -self.byte_capacity() - Self::ESZ;
                assert!(*counter == empty, "the enqueuer is not empty");

                // State: Empty.
                *slot = Some(waker);
                *counter = -Self::ESZ;
            }
        }
    }

    /// Take the waker from this [`Enqueuer`].
    pub(crate) fn take_waker(&self) -> Option<Waker> {
        let waker = self.waker().take()?;
        *self.counter() = -self.byte_capacity() - Self::ESZ;
        Some(waker)
    }

    /// Enqueue a task.
    ///
    /// If the worker is asleep, it will be woken up.
    ///
    /// # Panics
    ///
    /// Panics if this is called while the enqueuer is borrowed, e.g. it is
    /// within a call to [`Enqueuer::extend()`].
    pub fn add(&self, task: T) {
        let inner = self.inner.get();
        let counter = unsafe { (*inner).counter };
        let next = counter + Self::ESZ;

        if next < 0 {
            // Current state: Normal.
            let end = unsafe { (*inner).end };
            if !Self::ZST {
                let ptr = unsafe { end.byte_offset(next) };
                unsafe { ptr.write(task) };
            }
            unsafe { (*inner).counter = next };
            return;
        }

        self.add_slow(task, next)
    }

    #[cold]
    fn add_slow(&self, task: T, next: isize) {
        let inner = self.inner.get();

        // Current state: Full / Asleep / Borrowed.
        assert!(next == 0, "the enqueuer is already borrowed");

        // Current state: Full / Asleep.
        let capacity = unsafe { (*inner).capacity };
        if capacity == 0 {
            // Current state: Empty / Full / Asleep.
            cold_path();
            debug_assert!(!Self::ZST);

            let mut buffer = Box::new_uninit_slice(16);
            buffer[0].write(task);
            let ptr = Box::into_raw(buffer);
            let end = unsafe { ptr.cast::<T>().add(16) };
            let end = unsafe { NonNull::new_unchecked(end) };
            unsafe { (*inner).end = end };
            unsafe { (*inner).capacity = 16 * Self::ESZ };
            unsafe { (*inner).counter = -16 * Self::ESZ };

            if let Some(waker) = unsafe { (*inner).waker.take() } {
                waker.wake();
            }

            return;
        }

        // Current state: Full / Asleep.
        let end = unsafe { (*inner).end };
        let ptr = unsafe { end.byte_offset(-capacity) };
        if unsafe { (*inner).waker.is_some() } {
            // Current state: Asleep.
            let waker = unsafe { (*inner).waker.take() }.unwrap();
            unsafe { ptr.write(task) };
            unsafe { (*inner).counter = -capacity };
            waker.wake();
        } else {
            // Current state: Full.
            let mut vec = unsafe {
                Vec::from_raw_parts(
                    ptr.as_ptr(),
                    (capacity / Self::ESZ) as usize,
                    (capacity / Self::ESZ) as usize,
                )
            };
            vec.push(task);
            let old_cap = capacity;
            let capacity = vec.capacity() as isize * Self::ESZ;
            let ptr = unsafe { NonNull::new_unchecked(vec.as_mut_ptr()) };
            let end = unsafe { ptr.byte_offset(capacity) };
            core::mem::forget(vec);
            unsafe { (*inner).end = end };
            unsafe { (*inner).capacity = capacity };
            unsafe { (*inner).counter = -capacity + old_cap };
        }
    }

    /// Enqueue a set of tasks.
    ///
    /// If the worker is asleep, it will be woken up.
    ///
    /// # Panics
    ///
    /// Panics if this is called while the enqueuer is borrowed, e.g. it is
    /// within a(nother) call to [`Enqueuer::extend()`].
    pub fn extend(&self, tasks: impl IntoIterator<Item = T>) {
        // TODO
        for task in tasks {
            self.add(task);
        }
    }

    /// Drain the enqueued tasks.
    ///
    /// Returns the number of freshly enqueued tasks.
    ///
    /// # Panics
    ///
    /// Panics if this is called while the enqueuer is borrowed, e.g. it is
    /// within a call to [`Enqueuer::extend()`].
    pub(crate) fn drain(&self, tasks: &mut Vec<T>) -> usize {
        // Determine how many tasks are in the enqueuer.
        let inner = self.inner.get();
        let counter = unsafe { (*inner).counter };
        let capacity = unsafe { (*inner).capacity };
        assert!(counter < 0, "the enqueuer is already borrowed");

        let end = unsafe { (*inner).end };
        let ptr = unsafe { end.byte_offset(-capacity) };
        let ptr = ptr.cast::<MaybeUninit<T>>().as_ptr();
        let len = ((capacity + counter) / Self::ESZ + 1) as usize;

        tasks.reserve(len);
        let space = tasks.spare_capacity_mut().as_mut_ptr();
        unsafe { ptr::copy_nonoverlapping(ptr, space, len) };
        unsafe { tasks.set_len(tasks.len() + len) };

        unsafe { (*inner).counter = -capacity - Self::ESZ };

        len
    }
}

impl<T: Task> Extend<T> for &Enqueuer<T> {
    /// Enqueue a set of tasks.
    ///
    /// This is equivalent to (and is defined as) [`Enqueuer::extend()`].
    ///
    /// If the worker is asleep, it will be woken up.
    ///
    /// # Panics
    ///
    /// Panics if this is called while the enqueuer is borrowed, e.g. it is
    /// within a(nother) call to [`Enqueuer::extend()`].
    fn extend<I: IntoIterator<Item = T>>(&mut self, tasks: I) {
        (*self).extend(tasks)
    }
}

impl<T: Task> Drop for Enqueuer<T> {
    fn drop(&mut self) {
        if self.is_locked() {
            // State: Locked.
            return;
        }

        // Drop any live tasks.
        if self.waker().is_none() {
            // State: Normal or Full.

            // SAFETY: State is neither Asleep nor Locked.
            for task in unsafe { self.buffer_init() } {
                // SAFETY: All tasks in 'buffer_init()' are initialized.
                unsafe { task.assume_init_drop() };
            }
        }

        // De-allocate the buffer itself.
        if !Self::ZST && self.byte_capacity() != 0 {
            // SAFETY: State is not Locked.
            let buffer = unsafe { self.buffer() };

            // SAFETY: Such an allocation exists, so the layout must be valid.
            let layout =
                unsafe { Layout::array::<T>(buffer.len()).unwrap_unchecked() };

            // SAFETY: 'buffer' was allocated with this layout.
            unsafe {
                alloc::alloc::dealloc(buffer.as_mut_ptr().cast(), layout)
            };
        }
    }
}

impl<T: Task> Enqueuer<T> {
    /// Whether the task is zero-sized.
    const ZST: bool = size_of::<T>() == 0;

    /// The element size.
    const ESZ: isize = if !Self::ZST {
        size_of::<T>() as isize
    } else {
        1
    };

    /// Whether the state is Locked.
    fn is_locked(&self) -> bool {
        unsafe { (*self.inner.get()).counter == 0 }
    }

    /// The capacity, in bytes.
    fn byte_capacity(&self) -> isize {
        unsafe { (*self.inner.get()).capacity }
    }

    /// The enqueued task length, in bytes.
    ///
    /// ## Safety
    ///
    /// - State must not be Asleep or Locked.
    fn byte_len(&self) -> isize {
        let inner = self.inner.get();
        unsafe { (*inner).capacity + (*inner).counter + Self::ESZ }
    }

    /// The length.
    ///
    /// ## Safety
    ///
    /// - State must not be Asleep or Locked.
    fn len(&self) -> usize {
        (self.byte_len() / Self::ESZ) as usize
    }

    /// The capacity.
    fn capacity(&self) -> usize {
        (self.byte_capacity() / Self::ESZ) as usize
    }

    /// The start of the buffer.
    fn ptr(&self) -> *mut T {
        let inner = self.inner.get();
        let off = if Self::ZST {
            0
        } else {
            unsafe { (*inner).capacity }
        };
        unsafe { (*inner).end.as_ptr().byte_offset(-off) }
    }

    /// The whole buffer.
    ///
    /// ## Safety
    ///
    /// - State must not be Locked.
    #[allow(clippy::mut_from_ref)] // Internal API
    unsafe fn buffer(&self) -> &mut [MaybeUninit<T>] {
        unsafe { slice::from_raw_parts_mut(self.ptr().cast(), self.capacity()) }
    }

    /// The initialized part of the buffer.
    ///
    /// ## Safety
    ///
    /// - State must not be Asleep or Locked.
    #[allow(clippy::mut_from_ref)] // Internal API
    unsafe fn buffer_init(&self) -> &mut [MaybeUninit<T>] {
        unsafe { slice::from_raw_parts_mut(self.ptr().cast(), self.len()) }
    }

    /// The waker.
    #[allow(clippy::mut_from_ref)] // Internal API
    fn waker(&self) -> &mut Option<Waker> {
        unsafe { &mut (*self.inner.get()).waker }
    }

    /// The counter.
    #[allow(clippy::mut_from_ref)] // Internal API
    fn counter(&self) -> &mut isize {
        unsafe { &mut (*self.inner.get()).counter }
    }
}

#[cold]
fn cold_path() {}

#[cfg(test)]
mod test {
    use std::vec::Vec;

    use crate::Task;

    use super::Enqueuer;

    #[derive(Copy, Clone, PartialEq, Eq, Debug)]
    struct Foo(u32);

    impl Task for Foo {
        type Priority = ();
        fn priority(&self) -> Self::Priority {}
    }

    #[test]
    fn new() {
        let _ = Enqueuer::<()>::new();
        let _ = Enqueuer::<Foo>::new();
    }

    #[test]
    fn add() {
        let enqueuer = Enqueuer::<()>::new();
        enqueuer.add(());
        enqueuer.add(());
        enqueuer.add(());
        enqueuer.add(());
        assert_eq!(enqueuer.len(), 4);

        let enqueuer = Enqueuer::<Foo>::new();
        enqueuer.add(Foo(0));
        enqueuer.add(Foo(1));
        enqueuer.add(Foo(2));
        enqueuer.add(Foo(3));
        assert_eq!(enqueuer.len(), 4);
        assert_eq!(enqueuer.byte_len(), 16);
        let mut tasks = Vec::new();
        enqueuer.drain(&mut tasks);
        assert_eq!(tasks, [Foo(0), Foo(1), Foo(2), Foo(3)]);
    }

    #[test]
    fn realloc() {
        let enqueuer = Enqueuer::<Foo>::new();
        for n in 1..4 {
            let mut expected = Vec::new();
            for i in 0..32 * n {
                let task = Foo(n * 32 + i);
                enqueuer.add(task);
                expected.push(task);
            }
            assert_eq!(enqueuer.len(), 32 * n as usize);
            assert_eq!(enqueuer.byte_len(), 32 * n as isize * 4);
            let mut tasks = Vec::new();
            enqueuer.drain(&mut tasks);
            assert_eq!(tasks, expected);
        }
    }
}