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
use std::{
  marker::PhantomData,
  mem,
  ops::{BitAnd, Range},
  sync::atomic::Ordering,
};

use bit_bounds::{usize::Int, IsPowerOf2};

use crate::{
  helpers::{active_phase_bit, one_shifted, slot_index},
  queue::{Inner, TaskQueue, INDEX_SHIFT},
  TaskRef,
};

/// The responsibilty to process a yet to be assigned set of tasks on the queue. Once converted
/// into a [`TaskAssignment`] the task range responsible for processing will be bounded and further
/// tasks enqueued will be of a new batch. Assignment of a task range can be deferred until
/// resources such as database connections are ready as a way to process tasks in larger batches.
pub struct PendingAssignment<T: TaskQueue, const N: usize = 2048>
where
  Int<N>: IsPowerOf2,
{
  base_slot: usize,
  queue_ptr: *const Inner<T, N>,
}

impl<T, const N: usize> PendingAssignment<T, N>
where
  T: TaskQueue,
  Int<N>: IsPowerOf2,
{
  pub(crate) fn new(base_slot: usize, queue_ptr: *const Inner<T, N>) -> Self {
    PendingAssignment {
      base_slot,
      queue_ptr,
    }
  }

  #[inline(always)]
  fn queue(&self) -> &Inner<T, N> {
    // This can be safely dereferenced because Inner is immovable by design and will suspend it's
    // thread termination as necessary until no references exist
    unsafe { &*self.queue_ptr }
  }

  // Converted into a bounded task assignment
  pub fn into_assignment(self) -> TaskAssignment<T, N> {
    let phase_bit = active_phase_bit::<N>(&self.base_slot);

    let end_slot = self.queue().slot.fetch_xor(phase_bit, Ordering::Relaxed);

    // If the N bit has changed it means the queue has wrapped around the beginning
    let task_range = if (N << INDEX_SHIFT).bitand(self.base_slot ^ end_slot).eq(&0) {
      // (base_slot >> INDEX_SHIFT) extracts the current index
      // Because N is a power of 2, N - 1 will be a mask with N bits set
      // For all values N, index & (N - 1) is always index % N however with fewer instructions
      let start = (self.base_slot >> INDEX_SHIFT) & (N - 1);

      // It can be known that the end slot will never wrap around because usize::MAX is divisable by
      // all values N
      let end = (end_slot >> INDEX_SHIFT) & (N - 1);

      start..end
    } else {
      // The active phase bit alternated when the N bit changed as a consequence of this wrapping
      // around to the beginning, and so this batch goes to the end and the queue will create a new
      // task batch while ignoring that the now inactive phase bit was changed.
      (self.base_slot >> INDEX_SHIFT) & (N - 1)..N
    };

    let queue_ptr = self.queue_ptr;

    mem::forget(self);

    TaskAssignment::new(task_range, queue_ptr)
  }

  fn deoccupy_buffer(&self) {
    let index = slot_index::<N>(&self.base_slot);
    let shifted_sub = one_shifted::<N>(&index);

    self
      .queue()
      .occupancy
      .fetch_sub(shifted_sub, Ordering::Relaxed);
  }
}

// This is safe because queue_ptr is guaranteed to be immovable and non-null while
// references exist
unsafe impl<T> Send for PendingAssignment<T> where T: TaskQueue {}
unsafe impl<T> Sync for PendingAssignment<T> where T: TaskQueue {}

impl<T, const N: usize> Drop for PendingAssignment<T, N>
where
  T: TaskQueue,
  Int<N>: IsPowerOf2,
{
  fn drop(&mut self) {
    self.deoccupy_buffer();
  }
}

pub struct TaskAssignment<T: TaskQueue, const N: usize = 2048>
where
  Int<N>: IsPowerOf2,
{
  task_range: Range<usize>,
  queue_ptr: *const Inner<T, N>,
}

impl<T, const N: usize> TaskAssignment<T, N>
where
  T: TaskQueue,
  Int<N>: IsPowerOf2,
{
  fn new(task_range: Range<usize>, queue_ptr: *const Inner<T, N>) -> Self {
    TaskAssignment {
      task_range,
      queue_ptr,
    }
  }

  #[inline(always)]
  fn queue(&self) -> &Inner<T, N> {
    // This can be safely dereferenced because Inner is immovable by design and will block it's
    // thread termination as necessary until no references exist
    unsafe { &*self.queue_ptr }
  }

  // A transmuted slice of the assigned task range
  pub fn tasks(&self) -> &[TaskRef<T>] {
    unsafe { mem::transmute(self.queue().buffer.get_unchecked(self.task_range.clone())) }
  }

  // Map each task into it's respective value and resolve, in parallel
  pub fn map<F>(self, op: F) -> CompletionReceipt<T>
  where
    F: Fn(T::Task) -> T::Value + Sync,
  {
    self.tasks().iter().for_each(|task_ref| unsafe {
      let task = task_ref.take_task_unchecked();
      task_ref.resolve_unchecked(op(task));
    });

    self.into_completion_receipt()
  }

  fn deoccupy_buffer(&self) {
    let shifted_sub = one_shifted::<N>(&self.task_range.start);

    self
      .queue()
      .occupancy
      .fetch_sub(shifted_sub, Ordering::Relaxed);
  }

  fn into_completion_receipt(self) -> CompletionReceipt<T> {
    self.deoccupy_buffer();

    mem::forget(self);

    CompletionReceipt::new()
  }
}

impl<T, const N: usize> Drop for TaskAssignment<T, N>
where
  T: TaskQueue,
  Int<N>: IsPowerOf2,
{
  fn drop(&mut self) {
    self.deoccupy_buffer();
  }
}

// This is safe because queue_ptr is guaranteed to be immovable and non-null while
// references exist
unsafe impl<T> Send for TaskAssignment<T> where T: TaskQueue {}
unsafe impl<T> Sync for TaskAssignment<T> where T: TaskQueue {}

// A type-state proof of completion for a task assignment. This ensures all task are eventually
// processed while not precluding the usage of [`tokio::task::spawn_blocking`]
pub struct CompletionReceipt<T: TaskQueue>(PhantomData<T>);

impl<T> CompletionReceipt<T>
where
  T: TaskQueue,
{
  fn new() -> Self {
    CompletionReceipt(PhantomData)
  }
}