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
//! # Ansa
//!
//! Multithreaded, lock-free queue implementation using the Disruptor pattern.
//!
//! todo
//!
//! to mention:
//! - no panics outside of the convenience functions
//! - sequence limit (i64::MAX)
//! - Basics of how the disruptor works
//! - traffic jam analogy
//! - features
//! - examples
//! - terminology (ring buffer, handle, etc...)
//!
//! # Disruptor
//!
//! This crate implements the Disruptor pattern as described in the original [whitepaper][disruptor].
//! In particular, section `4.3 Sequencing` provides a complete description of how accesses to the
//! buffer are synchronised. It is not an understatement to say that understanding just section 4.3
//! of the paper is enough to understand the Disruptor pattern as a whole.
//!
//! [disruptor]: https://github.com/LMAX-Exchange/disruptor/blob/0a5adf2bf35ba508b11fa69fa592d28529e50679/src/docs/files/Disruptor-1.0.pdf
//!
//! A disruptor is made up of three components:
//! - A ring buffer (aka circular buffer, aka buffer).
//! - A lead producer handle which follows the last of the trailing handles.
//! - Any number of consumer and producer handles which trail the lead, and which may be structured
//! into a [directed acyclic graph][dag].
//!
//! [dag]: https://en.wikipedia.org/wiki/Directed_acyclic_graph
//!
//! The buffer is pre-populated with events when a disruptor is created.
//!
//! As their names suggest, produces have mutable access to events on the buffer, while consumers
//! have only immutable access.
//!
//! Every handle is limited in what portion of the buffer it can access by the handles it follows.
//! A handle `h` *cannot* overtake the handles `B` (for barrier) that it follows. This constraint
//! is ensures buffer accesses do not invalidly overlap.
//!
pub use *;
pub use *;
use crate;
/// Construct a Single-Producer Single-Consumer disruptor.
///
/// `size` must be a non-zero power of two. `event_factory` is used to populate the buffer.
///
/// Uses a [`WaitPhased<WaitSleep>`](WaitPhased) strategy which busy-spins for 1 millisecond, then
/// spin-yields the thread for 1 millisecond, and finally spin-sleeps in 50 microsecond increments.
///
/// See: [`DisruptorBuilder`] for configurable disruptor construction.
///
/// # Panics
///
/// If `size` is zero or not a power of two.
///
/// # Examples
/// ```
/// let (producer, consumer) = ansa::spsc(64, || 0);
/// ```
/// Construct a Multi-Producer Single-Consumer disruptor.
///
/// `size` must be a non-zero power of two. `event_factory` is used to populate the buffer.
///
/// Uses a [`WaitPhased<WaitSleep>`](WaitPhased) strategy which busy-spins for 1 millisecond, then
/// spins and yields the thread for 1 millisecond, and finally spins and sleeps for 50 microseconds.
///
/// The returned multi producer can be cloned to enable distributed writes.
///
/// See: [`DisruptorBuilder`] for configurable disruptor construction.
///
/// # Panics
///
/// If `size` is zero or not a power of two.
///
/// # Examples
/// ```
/// let (multi_producer, consumer) = ansa::mpsc(64, || 0);
/// let multi_clone = multi_producer.clone();
/// ```
/// Construct a Single-Producer Multi-Consumer disruptor.
///
/// `size` must be a non-zero power of two. `num_consumers` is the number of consumers to create.
/// `event_factory` is used to populate the buffer.
///
/// Uses a [`WaitPhased<WaitSleep>`](WaitPhased) strategy which busy-spins for 1 millisecond, then
/// spins and yields the thread for 1 millisecond, and finally spins and sleeps for 50 microseconds.
///
/// See: [`DisruptorBuilder`] for configurable disruptor construction.
///
/// # Panics
///
/// If `size` is zero or not a power of two.
///
/// # Examples
/// ```
/// let num_consumers = 5;
/// let (producer, consumers) = ansa::spmc(64, num_consumers, || 0);
/// assert_eq!(consumers.len(), 5);
/// ```
/// Construct a Multi-Producer Multi-Consumer disruptor.
///
/// `size` must be a non-zero power of two. `num_consumers` is the number of consumers to create.
/// `event_factory` is used to populate the buffer.
///
/// Uses a [`WaitPhased<WaitSleep>`](WaitPhased) strategy which busy-spins for 1 millisecond, then
/// spins and yields the thread for 1 millisecond, and finally spins and sleeps for 50 microseconds.
///
/// The returned multi producer can be cloned to enable distributed writes.
///
/// See: [`DisruptorBuilder`] for configurable disruptor construction.
///
/// # Panics
///
/// If `size` is zero or not a power of two.
///
/// # Examples
/// ```
/// let num_consumers = 5;
/// let (multi_producer, consumers) = ansa::mpmc(64, num_consumers, || 0);
/// let multi_clone = multi_producer.clone();
/// assert_eq!(consumers.len(), 5);
/// ```