velocityx 0.4.1

A production-ready Rust crate for lock-free concurrent data structures with performance monitoring
Documentation
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
//! Lock-Free Stack Implementation
//!
//! A high-performance lock-free stack based on Treiber's algorithm.
//! Provides wait-free push operations and lock-free pop operations.

use crate::metrics::{AtomicMetrics, MetricsCollector};
use crate::util::CachePadded;
use core::sync::atomic::{AtomicPtr, Ordering};
use std::sync::atomic::{AtomicUsize, Ordering as StdOrdering};

#[cfg(feature = "std")]
use std::boxed::Box;
#[cfg(feature = "std")]
use std::vec::Vec;

/// A node in the lock-free stack
#[derive(Debug)]
struct Node<T> {
    data: T,
    next: AtomicPtr<Node<T>>,
}

/// A lock-free stack implementation using Treiber's algorithm
///
/// This stack provides:
/// - Wait-free push operations
/// - Lock-free pop operations  
/// - ABA problem prevention through proper memory management
/// - Performance metrics collection
///
/// # Type Parameters
///
/// * `T` - The type of elements stored in the stack
///
/// # Examples
///
/// ```rust
/// use velocityx::stack::LockFreeStack;
///
/// let stack = LockFreeStack::new();
///
/// // Push elements
/// stack.push(1);
/// stack.push(2);
/// stack.push(3);
///
/// // Pop elements
/// assert_eq!(stack.pop(), Some(3));
/// assert_eq!(stack.pop(), Some(2));
/// assert_eq!(stack.pop(), Some(1));
/// assert_eq!(stack.pop(), None);
/// ```
#[derive(Debug)]
pub struct LockFreeStack<T> {
    /// The head pointer of the stack
    head: CachePadded<AtomicPtr<Node<T>>>,
    /// Performance metrics
    metrics: AtomicMetrics,
    /// Metrics enabled flag
    metrics_enabled: AtomicUsize,
}

impl<T> LockFreeStack<T> {
    /// Create a new empty lock-free stack
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack: LockFreeStack<i32> = LockFreeStack::new();
    /// ```
    pub fn new() -> Self {
        Self {
            head: CachePadded::new(AtomicPtr::new(std::ptr::null_mut())),
            metrics: AtomicMetrics::default(),
            metrics_enabled: AtomicUsize::new(1), // Enabled by default
        }
    }

    /// Push a value onto the stack
    ///
    /// This operation is wait-free and will always complete in a bounded number of steps.
    ///
    /// # Arguments
    ///
    /// * `value` - The value to push onto the stack
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack = LockFreeStack::new();
    /// stack.push(42);
    /// ```
    pub fn push(&self, value: T) {
        #[cfg(feature = "std")]
        let start = std::time::Instant::now();

        // Create a new node
        let node = Box::into_raw(Box::new(Node {
            data: value,
            next: AtomicPtr::new(std::ptr::null_mut()),
        }));

        // Treiber's algorithm for push
        loop {
            let head = self.head.get().load(Ordering::Acquire);

            // Set the next pointer to current head
            unsafe {
                (*node).next.store(head, Ordering::Relaxed);
            }

            // Try to update head to point to our node
            match self.head.get().compare_exchange_weak(
                head,
                node,
                Ordering::Release,
                Ordering::Relaxed,
            ) {
                Ok(_) => {
                    #[cfg(feature = "std")]
                    self.metrics.record_success(start.elapsed());
                    break;
                }
                Err(_) => {
                    #[cfg(feature = "std")]
                    self.metrics.record_contention();
                    // Retry with the new head
                }
            }
        }
    }

    /// Pop a value from the stack
    ///
    /// This operation is lock-free and will complete if other threads make progress.
    ///
    /// # Returns
    ///
    /// * `Some(value)` if the stack was not empty
    /// * `None` if the stack was empty
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack = LockFreeStack::new();
    /// stack.push(42);
    ///
    /// let value = stack.pop();
    /// assert_eq!(value, Some(42));
    /// ```
    pub fn pop(&self) -> Option<T> {
        #[cfg(feature = "std")]
        let start = std::time::Instant::now();

        loop {
            let head = self.head.get().load(Ordering::Acquire);

            if head.is_null() {
                #[cfg(feature = "std")]
                self.metrics.record_failure();
                return None;
            }

            // Get the next node
            let next = unsafe { (*head).next.load(Ordering::Relaxed) };

            // Try to update head to point to next
            match self.head.get().compare_exchange_weak(
                head,
                next,
                Ordering::Release,
                Ordering::Relaxed,
            ) {
                Ok(_) => {
                    // Successfully removed head, extract data
                    let data = unsafe {
                        let node = Box::from_raw(head);
                        node.data
                    };

                    #[cfg(feature = "std")]
                    self.metrics.record_success(start.elapsed());

                    return Some(data);
                }
                Err(_) => {
                    #[cfg(feature = "std")]
                    self.metrics.record_contention();
                    // Retry with the new head
                }
            }
        }
    }

    /// Check if the stack is empty
    ///
    /// This operation is lock-free and provides a snapshot of the stack's state.
    ///
    /// # Returns
    ///
    /// `true` if the stack is empty, `false` otherwise
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack = LockFreeStack::new();
    /// assert!(stack.is_empty());
    ///
    /// stack.push(42);
    /// assert!(!stack.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.head.get().load(Ordering::Acquire).is_null()
    }

    /// Get the approximate number of elements in the stack
    ///
    /// Note: This is an expensive operation as it traverses the entire stack.
    /// Use only for debugging or monitoring purposes.
    ///
    /// # Returns
    ///
    /// The approximate number of elements in the stack
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack = LockFreeStack::new();
    /// assert_eq!(stack.len(), 0);
    ///
    /// stack.push(1);
    /// stack.push(2);
    /// assert_eq!(stack.len(), 2);
    /// ```
    pub fn len(&self) -> usize {
        let mut count = 0;
        let mut current = self.head.get().load(Ordering::Acquire);

        while !current.is_null() {
            count += 1;
            current = unsafe { (*current).next.load(Ordering::Relaxed) };
        }

        count
    }

    /// Try to pop multiple elements at once
    ///
    /// This can be more efficient than individual pops as it reduces atomic operations.
    ///
    /// # Arguments
    ///
    /// * `max_count` - Maximum number of elements to pop
    ///
    /// # Returns
    ///
    /// A vector containing the popped elements (in LIFO order)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack = LockFreeStack::new();
    /// stack.push(1);
    /// stack.push(2);
    /// stack.push(3);
    ///
    /// let elements = stack.pop_batch(2);
    /// assert_eq!(elements.len(), 2);
    /// ```
    pub fn pop_batch(&self, max_count: usize) -> Vec<T> {
        let mut result = Vec::with_capacity(max_count);

        for _ in 0..max_count {
            if let Some(value) = self.pop() {
                result.push(value);
            } else {
                break;
            }
        }

        result
    }

    /// Push multiple elements at once
    ///
    /// Elements are pushed in the order they appear in the iterator.
    /// The last element will be at the top of the stack.
    ///
    /// # Arguments
    ///
    /// * `values` - Iterator of values to push
    ///
    /// # Examples
    ///
    /// ```rust
    /// use velocityx::stack::LockFreeStack;
    ///
    /// let stack = LockFreeStack::new();
    /// let values = vec![1, 2, 3];
    /// stack.push_batch(values);
    ///
    /// assert_eq!(stack.pop(), Some(3));
    /// assert_eq!(stack.pop(), Some(2));
    /// assert_eq!(stack.pop(), Some(1));
    /// ```
    pub fn push_batch<I>(&self, values: I)
    where
        I: IntoIterator<Item = T>,
    {
        for value in values {
            self.push(value);
        }
    }
}

impl<T> Default for LockFreeStack<T> {
    fn default() -> Self {
        Self::new()
    }
}

impl<T> Drop for LockFreeStack<T> {
    fn drop(&mut self) {
        // Clean up all remaining nodes
        let mut current = self.head.get().load(Ordering::Acquire);

        while !current.is_null() {
            let next = unsafe { (*current).next.load(Ordering::Relaxed) };
            unsafe {
                drop(Box::from_raw(current));
            }
            current = next;
        }
    }
}

#[cfg(feature = "std")]
impl<T> MetricsCollector for LockFreeStack<T> {
    fn metrics(&self) -> crate::metrics::PerformanceMetrics {
        self.metrics.snapshot()
    }

    fn reset_metrics(&self) {
        self.metrics.reset();
    }

    fn set_metrics_enabled(&self, enabled: bool) {
        self.metrics_enabled
            .store(enabled as usize, StdOrdering::Relaxed);
    }

    fn is_metrics_enabled(&self) -> bool {
        self.metrics_enabled.load(StdOrdering::Relaxed) != 0
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;
    use std::thread;
    use std::vec;

    #[test]
    fn test_basic_operations() {
        let stack = LockFreeStack::new();

        // Test empty stack
        assert!(stack.is_empty());
        assert_eq!(stack.len(), 0);
        assert_eq!(stack.pop(), None);

        // Test push and pop
        stack.push(1);
        stack.push(2);
        stack.push(3);

        assert!(!stack.is_empty());
        assert_eq!(stack.len(), 3);

        assert_eq!(stack.pop(), Some(3));
        assert_eq!(stack.pop(), Some(2));
        assert_eq!(stack.pop(), Some(1));
        assert_eq!(stack.pop(), None);

        assert!(stack.is_empty());
        assert_eq!(stack.len(), 0);
    }

    #[test]
    fn test_batch_operations() {
        let stack = LockFreeStack::new();

        // Test batch push
        stack.push_batch(vec![1, 2, 3, 4, 5]);
        assert_eq!(stack.len(), 5);

        // Test batch pop
        let elements = stack.pop_batch(3);
        assert_eq!(elements.len(), 3);
        assert_eq!(stack.len(), 2);

        // Verify LIFO order
        assert_eq!(stack.pop(), Some(2));
        assert_eq!(stack.pop(), Some(1));
        assert_eq!(stack.pop(), None);
    }

    #[test]
    fn test_concurrent_operations() {
        let stack = Arc::new(LockFreeStack::new());
        let mut handles = vec![];

        // Spawn multiple producer threads
        for i in 0..4 {
            let stack_clone = Arc::clone(&stack);
            let handle = thread::spawn(move || {
                for j in 0..100 {
                    stack_clone.push(i * 100 + j);
                }
            });
            handles.push(handle);
        }

        // Wait for all producers to finish
        for handle in handles {
            handle.join().unwrap();
        }

        // Verify all elements are present
        let mut count = 0;
        while stack.pop().is_some() {
            count += 1;
        }

        assert_eq!(count, 400);
        assert!(stack.is_empty());
    }

    #[test]
    fn test_producer_consumer() {
        let stack = Arc::new(LockFreeStack::new());
        let _handles: Vec<std::thread::JoinHandle<()>> = vec![];

        // Producer thread
        let producer_stack = Arc::clone(&stack);
        let producer = thread::spawn(move || {
            for i in 0..1000 {
                producer_stack.push(i);
            }
        });

        // Consumer thread
        let consumer_stack = Arc::clone(&stack);
        let consumer = thread::spawn(move || {
            let mut sum = 0;
            let mut count = 0;
            while count < 1000 {
                if let Some(value) = consumer_stack.pop() {
                    sum += value;
                    count += 1;
                }
                // Small delay to allow producer to add more elements
                thread::yield_now();
            }
            sum
        });

        producer.join().unwrap();
        let result = consumer.join().unwrap();

        // Sum of 0..999 = 499500
        assert_eq!(result, 499500);
    }

    #[cfg(feature = "std")]
    #[test]
    fn test_metrics() {
        let stack = LockFreeStack::new();

        // Perform some operations
        stack.push(1);
        stack.push(2);
        stack.push(3);

        let _ = stack.pop();
        let _ = stack.pop();
        let _ = stack.pop();
        let _ = stack.pop(); // This will fail

        // Check metrics
        let metrics = stack.metrics();
        assert_eq!(metrics.total_operations, 7);
        assert_eq!(metrics.successful_operations, 6);
        assert_eq!(metrics.failed_operations, 1);
        assert!(metrics.success_rate() > 80.0);

        // Test metrics control
        stack.set_metrics_enabled(false);
        assert!(!stack.is_metrics_enabled());

        stack.reset_metrics();
        let reset_metrics = stack.metrics();
        assert_eq!(reset_metrics.total_operations, 0);
    }
}