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
//! Core traits defining the Disruptor pattern interface.
//!
//! This module contains the fundamental traits that make up the Disruptor pattern:
//! - Event processing and handling
//! - Sequence management
//! - Data access and storage
//! - Execution control
//!
//! # Core Traits Overview
//!
//! ## Event Processing
//! - [`EventProcessor`]: Processes events from the ring buffer
//! - [`EventHandler`]: Handles individual events
//! - [`EventProducer`]: Produces events into the ring buffer
//!
//! ## Sequence Management
//! - [`Sequencer`]: Manages sequences for event coordination
//! - [`SequenceBarrier`]: Controls access to sequences
//! - [`WaitingStrategy`]: Defines how threads wait for available sequences
//!
//! ## Data Access
//! - [`DataProvider`]: Provides safe and unsafe access to the underlying buffer
//!
//! ## Execution
//! - [`Runnable`]: Base interface for executable components
//! - [`EventProcessorExecutor`]: Manages execution of event processors
//! - [`ExecutorHandle`]: Controls executor lifecycle
//!
//! # Examples
//!
//! ```rust
//! use disruptor_rs::{EventHandler, sequence::Sequence};
//!
//! struct MyHandler;
//! impl EventHandler<i64> for MyHandler {
//! fn on_event(&self, event: &i64, sequence: Sequence, end_of_batch: bool) {
//! // Process the event
//! }
//! fn on_start(&self) {}
//! fn on_shutdown(&self) {}
//! }
//! ```
use Arc;
use crate;
/// Controls access to sequences in the ring buffer.
///
/// A sequence barrier determines when sequences are available for processing
/// and manages coordination between different components.
///
/// # Methods
/// * `wait_for` - Blocks until a sequence becomes available
/// * `signal` - Signals that new sequences may be available
/// Manages sequence generation and coordination in the ring buffer.
///
/// The sequencer is responsible for generating new sequence numbers and
/// managing the relationship between publishers and subscribers.
///
/// # Type Parameters
/// * `Barrier` - The type of sequence barrier used by this sequencer
///
/// # Methods
/// * `add_gating_sequence` - Adds a gating sequence to the sequencer
/// * `remove_gating_sequence` - Removes a gating sequence from the sequencer
/// * `create_sequence_barrier` - Creates a sequence barrier with the given gating sequences
/// * `get_cursor` - Returns the current cursor value
/// * `next` - Gets the next sequence value
/// * `publish` - Publishes the given sequence range
/// * `drain` - Drains the sequencer
/// Defines how threads wait for available sequences.
///
/// Implements the strategy pattern for different waiting behaviors when
/// sequences are not yet available.
///
/// # Examples
///
/// ```
/// use disruptor_rs::traits::WaitingStrategy;
/// use disruptor_rs::sequence::AtomicSequence;
///
/// #[derive(Default)]
/// struct BlockingWaitStrategy;
///
/// impl WaitingStrategy for BlockingWaitStrategy {
/// fn new() -> Self {
/// BlockingWaitStrategy
/// }
///
/// fn wait_for<F: Fn() -> bool>(
/// &self,
/// sequence: i64,
/// dependencies: &[std::sync::Arc<AtomicSequence>],
/// check_alert: F
/// ) -> Option<i64> {
/// // Implementation would go here
/// None
/// }
///
/// fn signal_all_when_blocking(&self) {
/// // Implementation would go here
/// }
/// }
/// ```
///
/// # Methods
/// * `new` - Creates a new instance of the waiting strategy
/// * `wait_for` - Waits for the given sequence to be available
/// * `signal_all_when_blocking` - Signals that all threads should be blocked
/// Provides safe and unsafe access to the underlying ring buffer.
///
/// # Safety
///
/// This trait provides both safe and unsafe methods for accessing the buffer.
/// Implementors must ensure proper bounds checking and memory safety.
///
/// # Type Parameters
/// * `T` - The type of elements stored in the buffer.
///
/// This lint allow is necessary because DataProvider::get_mut returns a mutable reference from an immutable one.
/// This is safe in our case because we use interior mutability (UnsafeCell) in the RingBuffer implementation,
/// and the safety is guaranteed by the Sequencer which ensures proper synchronization of access to the buffer.
/// Defines the lifecycle operations for executable components.
///
/// Provides basic control operations for starting, stopping, and
/// checking the status of running components.
///
/// # Methods
/// * `run` - Starts the component
/// * `stop` - Stops the component
/// * `is_running` - Checks if the component is running
/// A trait for providing an event processor.
/// # Types
/// - `T`: The type of events to process.
/// # Methods
/// * `create`: Creates a new event processor.
/// * `get_sequence`: Returns the sequence of the event processor.
/// A trait for providing an event processor with mutable access to the event.
/// A trait for providing an event handler.
/// # Types
/// - `T`: The type of events to handle.
/// # Methods
/// * `on_event`: Handles the given event.
/// * `on_start`: Called when the event handler starts.
/// * `on_shutdown`: Called when the event handler shuts down.
/// A trait for providing an event handler with mutable access to the event.
/// # Types
/// - `T`: The type of events to handle.
/// # Methods
/// * `on_event`: Handles the given event.
/// * `on_start`: Called when the event handler starts.
/// * `on_shutdown`: Called when the event handler shuts down.
/// A trait for providing an executor thread handle.
/// # Methods
/// * `join`: Joins the executor thread.
/// A trait for providing an executor.
/// # Types
/// - `Handle`: The type of executor handle.
/// # Methods
/// * `with_runnales`: Creates a new executor with the given runnables.
/// * `spwan`: Spawns the executor.
/// A trait for producing events.
/// # Types
/// - `Item`: The type of events to produce.
/// # Methods
/// * `write`: Writes the given event.
/// * `drain`: Drains the event producer.