/rust/registry/src/index.crates.io-1949cf8c6b5b557f/futures-channel-0.3.21/src/mpsc/queue.rs
Line | Count | Source |
1 | | /* Copyright (c) 2010-2011 Dmitry Vyukov. All rights reserved. |
2 | | * Redistribution and use in source and binary forms, with or without |
3 | | * modification, are permitted provided that the following conditions are met: |
4 | | * |
5 | | * 1. Redistributions of source code must retain the above copyright notice, |
6 | | * this list of conditions and the following disclaimer. |
7 | | * |
8 | | * 2. Redistributions in binary form must reproduce the above copyright |
9 | | * notice, this list of conditions and the following disclaimer in the |
10 | | * documentation and/or other materials provided with the distribution. |
11 | | * |
12 | | * THIS SOFTWARE IS PROVIDED BY DMITRY VYUKOV "AS IS" AND ANY EXPRESS OR IMPLIED |
13 | | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
14 | | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT |
15 | | * SHALL DMITRY VYUKOV OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
16 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
17 | | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
18 | | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
19 | | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE |
20 | | * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
21 | | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
22 | | * |
23 | | * The views and conclusions contained in the software and documentation are |
24 | | * those of the authors and should not be interpreted as representing official |
25 | | * policies, either expressed or implied, of Dmitry Vyukov. |
26 | | */ |
27 | | |
28 | | //! A mostly lock-free multi-producer, single consumer queue for sending |
29 | | //! messages between asynchronous tasks. |
30 | | //! |
31 | | //! The queue implementation is essentially the same one used for mpsc channels |
32 | | //! in the standard library. |
33 | | //! |
34 | | //! Note that the current implementation of this queue has a caveat of the `pop` |
35 | | //! method, and see the method for more information about it. Due to this |
36 | | //! caveat, this queue may not be appropriate for all use-cases. |
37 | | |
38 | | // http://www.1024cores.net/home/lock-free-algorithms |
39 | | // /queues/non-intrusive-mpsc-node-based-queue |
40 | | |
41 | | // NOTE: this implementation is lifted from the standard library and only |
42 | | // slightly modified |
43 | | |
44 | | pub(super) use self::PopResult::*; |
45 | | |
46 | | use std::cell::UnsafeCell; |
47 | | use std::ptr; |
48 | | use std::sync::atomic::{AtomicPtr, Ordering}; |
49 | | use std::thread; |
50 | | |
51 | | /// A result of the `pop` function. |
52 | | pub(super) enum PopResult<T> { |
53 | | /// Some data has been popped |
54 | | Data(T), |
55 | | /// The queue is empty |
56 | | Empty, |
57 | | /// The queue is in an inconsistent state. Popping data should succeed, but |
58 | | /// some pushers have yet to make enough progress in order allow a pop to |
59 | | /// succeed. It is recommended that a pop() occur "in the near future" in |
60 | | /// order to see if the sender has made progress or not |
61 | | Inconsistent, |
62 | | } |
63 | | |
64 | | #[derive(Debug)] |
65 | | struct Node<T> { |
66 | | next: AtomicPtr<Self>, |
67 | | value: Option<T>, |
68 | | } |
69 | | |
70 | | /// The multi-producer single-consumer structure. This is not cloneable, but it |
71 | | /// may be safely shared so long as it is guaranteed that there is only one |
72 | | /// popper at a time (many pushers are allowed). |
73 | | #[derive(Debug)] |
74 | | pub(super) struct Queue<T> { |
75 | | head: AtomicPtr<Node<T>>, |
76 | | tail: UnsafeCell<*mut Node<T>>, |
77 | | } |
78 | | |
79 | | unsafe impl<T: Send> Send for Queue<T> {} |
80 | | unsafe impl<T: Send> Sync for Queue<T> {} |
81 | | |
82 | | impl<T> Node<T> { |
83 | 1.55k | unsafe fn new(v: Option<T>) -> *mut Self { |
84 | 1.55k | Box::into_raw(Box::new(Self { next: AtomicPtr::new(ptr::null_mut()), value: v })) |
85 | 1.55k | } <futures_channel::mpsc::queue::Node<devices::virtio::block::asynchronous::WorkerCmd>>::new Line | Count | Source | 83 | 1.55k | unsafe fn new(v: Option<T>) -> *mut Self { | 84 | 1.55k | Box::into_raw(Box::new(Self { next: AtomicPtr::new(ptr::null_mut()), value: v })) | 85 | 1.55k | } |
Unexecuted instantiation: <futures_channel::mpsc::queue::Node<_>>::new |
86 | | } |
87 | | |
88 | | impl<T> Queue<T> { |
89 | | /// Creates a new queue that is safe to share among multiple producers and |
90 | | /// one consumer. |
91 | 778 | pub(super) fn new() -> Self { |
92 | 778 | let stub = unsafe { Node::new(None) }; |
93 | 778 | Self { head: AtomicPtr::new(stub), tail: UnsafeCell::new(stub) } |
94 | 778 | } <futures_channel::mpsc::queue::Queue<devices::virtio::block::asynchronous::WorkerCmd>>::new Line | Count | Source | 91 | 778 | pub(super) fn new() -> Self { | 92 | 778 | let stub = unsafe { Node::new(None) }; | 93 | 778 | Self { head: AtomicPtr::new(stub), tail: UnsafeCell::new(stub) } | 94 | 778 | } |
Unexecuted instantiation: <futures_channel::mpsc::queue::Queue<_>>::new |
95 | | |
96 | | /// Pushes a new value onto this queue. |
97 | 778 | pub(super) fn push(&self, t: T) { |
98 | 778 | unsafe { |
99 | 778 | let n = Node::new(Some(t)); |
100 | 778 | let prev = self.head.swap(n, Ordering::AcqRel); |
101 | 778 | (*prev).next.store(n, Ordering::Release); |
102 | 778 | } |
103 | 778 | } <futures_channel::mpsc::queue::Queue<devices::virtio::block::asynchronous::WorkerCmd>>::push Line | Count | Source | 97 | 778 | pub(super) fn push(&self, t: T) { | 98 | 778 | unsafe { | 99 | 778 | let n = Node::new(Some(t)); | 100 | 778 | let prev = self.head.swap(n, Ordering::AcqRel); | 101 | 778 | (*prev).next.store(n, Ordering::Release); | 102 | 778 | } | 103 | 778 | } |
Unexecuted instantiation: <futures_channel::mpsc::queue::Queue<_>>::push |
104 | | |
105 | | /// Pops some data from this queue. |
106 | | /// |
107 | | /// Note that the current implementation means that this function cannot |
108 | | /// return `Option<T>`. It is possible for this queue to be in an |
109 | | /// inconsistent state where many pushes have succeeded and completely |
110 | | /// finished, but pops cannot return `Some(t)`. This inconsistent state |
111 | | /// happens when a pusher is preempted at an inopportune moment. |
112 | | /// |
113 | | /// This inconsistent state means that this queue does indeed have data, but |
114 | | /// it does not currently have access to it at this time. |
115 | | /// |
116 | | /// This function is unsafe because only one thread can call it at a time. |
117 | 1.95k | pub(super) unsafe fn pop(&self) -> PopResult<T> { |
118 | 1.95k | let tail = *self.tail.get(); |
119 | 1.95k | let next = (*tail).next.load(Ordering::Acquire); |
120 | | |
121 | 1.95k | if !next.is_null() { |
122 | 778 | *self.tail.get() = next; |
123 | 778 | assert!((*tail).value.is_none()); |
124 | 778 | assert!((*next).value.is_some()); |
125 | 778 | let ret = (*next).value.take().unwrap(); |
126 | 778 | drop(Box::from_raw(tail)); |
127 | 778 | return Data(ret); |
128 | 1.17k | } |
129 | | |
130 | 1.17k | if self.head.load(Ordering::Acquire) == tail { |
131 | 1.17k | Empty |
132 | | } else { |
133 | 0 | Inconsistent |
134 | | } |
135 | 1.95k | } <futures_channel::mpsc::queue::Queue<devices::virtio::block::asynchronous::WorkerCmd>>::pop Line | Count | Source | 117 | 1.95k | pub(super) unsafe fn pop(&self) -> PopResult<T> { | 118 | 1.95k | let tail = *self.tail.get(); | 119 | 1.95k | let next = (*tail).next.load(Ordering::Acquire); | 120 | | | 121 | 1.95k | if !next.is_null() { | 122 | 778 | *self.tail.get() = next; | 123 | 778 | assert!((*tail).value.is_none()); | 124 | 778 | assert!((*next).value.is_some()); | 125 | 778 | let ret = (*next).value.take().unwrap(); | 126 | 778 | drop(Box::from_raw(tail)); | 127 | 778 | return Data(ret); | 128 | 1.17k | } | 129 | | | 130 | 1.17k | if self.head.load(Ordering::Acquire) == tail { | 131 | 1.17k | Empty | 132 | | } else { | 133 | 0 | Inconsistent | 134 | | } | 135 | 1.95k | } |
Unexecuted instantiation: <futures_channel::mpsc::queue::Queue<_>>::pop |
136 | | |
137 | | /// Pop an element similarly to `pop` function, but spin-wait on inconsistent |
138 | | /// queue state instead of returning `Inconsistent`. |
139 | | /// |
140 | | /// This function is unsafe because only one thread can call it at a time. |
141 | 1.95k | pub(super) unsafe fn pop_spin(&self) -> Option<T> { |
142 | | loop { |
143 | 1.95k | match self.pop() { |
144 | 1.17k | Empty => return None, |
145 | 778 | Data(t) => return Some(t), |
146 | | // Inconsistent means that there will be a message to pop |
147 | | // in a short time. This branch can only be reached if |
148 | | // values are being produced from another thread, so there |
149 | | // are a few ways that we can deal with this: |
150 | | // |
151 | | // 1) Spin |
152 | | // 2) thread::yield_now() |
153 | | // 3) task::current().unwrap() & return Pending |
154 | | // |
155 | | // For now, thread::yield_now() is used, but it would |
156 | | // probably be better to spin a few times then yield. |
157 | 0 | Inconsistent => { |
158 | 0 | thread::yield_now(); |
159 | 0 | } |
160 | | } |
161 | | } |
162 | 1.95k | } <futures_channel::mpsc::queue::Queue<devices::virtio::block::asynchronous::WorkerCmd>>::pop_spin Line | Count | Source | 141 | 1.95k | pub(super) unsafe fn pop_spin(&self) -> Option<T> { | 142 | | loop { | 143 | 1.95k | match self.pop() { | 144 | 1.17k | Empty => return None, | 145 | 778 | Data(t) => return Some(t), | 146 | | // Inconsistent means that there will be a message to pop | 147 | | // in a short time. This branch can only be reached if | 148 | | // values are being produced from another thread, so there | 149 | | // are a few ways that we can deal with this: | 150 | | // | 151 | | // 1) Spin | 152 | | // 2) thread::yield_now() | 153 | | // 3) task::current().unwrap() & return Pending | 154 | | // | 155 | | // For now, thread::yield_now() is used, but it would | 156 | | // probably be better to spin a few times then yield. | 157 | 0 | Inconsistent => { | 158 | 0 | thread::yield_now(); | 159 | 0 | } | 160 | | } | 161 | | } | 162 | 1.95k | } |
Unexecuted instantiation: <futures_channel::mpsc::queue::Queue<_>>::pop_spin |
163 | | } |
164 | | |
165 | | impl<T> Drop for Queue<T> { |
166 | 778 | fn drop(&mut self) { |
167 | | unsafe { |
168 | 778 | let mut cur = *self.tail.get(); |
169 | 1.55k | while !cur.is_null() { |
170 | 778 | let next = (*cur).next.load(Ordering::Relaxed); |
171 | 778 | drop(Box::from_raw(cur)); |
172 | 778 | cur = next; |
173 | 778 | } |
174 | | } |
175 | 778 | } <futures_channel::mpsc::queue::Queue<devices::virtio::block::asynchronous::WorkerCmd> as core::ops::drop::Drop>::drop Line | Count | Source | 166 | 778 | fn drop(&mut self) { | 167 | | unsafe { | 168 | 778 | let mut cur = *self.tail.get(); | 169 | 1.55k | while !cur.is_null() { | 170 | 778 | let next = (*cur).next.load(Ordering::Relaxed); | 171 | 778 | drop(Box::from_raw(cur)); | 172 | 778 | cur = next; | 173 | 778 | } | 174 | | } | 175 | 778 | } |
Unexecuted instantiation: <futures_channel::mpsc::queue::Queue<_> as core::ops::drop::Drop>::drop |
176 | | } |