1use super::{Shared, Synced};
23use crate::runtime::scheduler::Lock;
4use crate::runtime::task;
56use std::sync::atomic::Ordering::Release;
78impl<'a> Lock<Synced> for &'a mut Synced {
9type Handle = &'a mut Synced;
1011fn lock(self) -> Self::Handle {
12self
13}
14}
1516impl AsMut<Synced> for Synced {
17fn as_mut(&mut self) -> &mut Synced {
18self
19}
20}
2122impl<T: 'static> Shared<T> {
23/// Pushes several values into the queue.
24 ///
25 /// # Safety
26 ///
27 /// Must be called with the same `Synced` instance returned by `Inject::new`
28#[inline]
29pub(crate) unsafe fn push_batch<L, I>(&self, shared: L, mut iter: I)
30where
31L: Lock<Synced>,
32 I: Iterator<Item = task::Notified<T>>,
33 {
34let first = match iter.next() {
35Some(first) => first.into_raw(),
36None => return,
37 };
3839// Link up all the tasks.
40let mut prev = first;
41let mut counter = 1;
4243// We are going to be called with an `std::iter::Chain`, and that
44 // iterator overrides `for_each` to something that is easier for the
45 // compiler to optimize than a loop.
46iter.for_each(|next| {
47let next = next.into_raw();
4849// safety: Holding the Notified for a task guarantees exclusive
50 // access to the `queue_next` field.
51unsafe { prev.set_queue_next(Some(next)) };
52 prev = next;
53 counter += 1;
54 });
5556// Now that the tasks are linked together, insert them into the
57 // linked list.
58self.push_batch_inner(shared, first, prev, counter);
59 }
6061/// Inserts several tasks that have been linked together into the queue.
62 ///
63 /// The provided head and tail may be be the same task. In this case, a
64 /// single task is inserted.
65#[inline]
66unsafe fn push_batch_inner<L>(
67&self,
68 shared: L,
69 batch_head: task::RawTask,
70 batch_tail: task::RawTask,
71 num: usize,
72 ) where
73L: Lock<Synced>,
74 {
75debug_assert!(unsafe { batch_tail.get_queue_next().is_none() });
7677let mut synced = shared.lock();
7879if synced.as_mut().is_closed {
80 drop(synced);
8182let mut curr = Some(batch_head);
8384while let Some(task) = curr {
85 curr = task.get_queue_next();
8687let _ = unsafe { task::Notified::<T>::from_raw(task) };
88 }
8990return;
91 }
9293let synced = synced.as_mut();
9495if let Some(tail) = synced.tail {
96unsafe {
97 tail.set_queue_next(Some(batch_head));
98 }
99 } else {
100 synced.head = Some(batch_head);
101 }
102103 synced.tail = Some(batch_tail);
104105// Increment the count.
106 //
107 // safety: All updates to the len atomic are guarded by the mutex. As
108 // such, a non-atomic load followed by a store is safe.
109let len = self.len.unsync_load();
110111self.len.store(len + num, Release);
112 }
113}