#[derive(Debug, Default)]
#[allow(dead_code)]
pub struct LockFreeCounter {
count: CachePadded<AtomicUsize>,
}
#[allow(dead_code)]
impl LockFreeCounter {
pub fn new(initial_value: usize) -> Self {
Self {
count: CachePadded::new(AtomicUsize::new(initial_value)),
}
}
pub fn increment(&self) -> usize {
self.count.fetch_add(1, Ordering::Relaxed) + 1
}
pub fn decrement(&self) -> usize {
self.count.fetch_sub(1, Ordering::Relaxed)
}
pub fn load(&self) -> usize {
self.count.load(Ordering::Relaxed)
}
pub fn add(&self, value: usize) -> usize {
self.count.fetch_add(value, Ordering::Relaxed) + value
}
pub fn sub(&self, value: usize) -> usize {
self.count.fetch_sub(value, Ordering::Relaxed) - value
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct LockFreeTaskScheduler {
pending_tasks: CachePadded<AtomicUsize>,
completed_tasks: CachePadded<AtomicUsize>,
active_workers: CachePadded<AtomicUsize>,
shutdown: AtomicBool,
}
#[allow(dead_code)]
impl LockFreeTaskScheduler {
pub fn new() -> Self {
Self {
pending_tasks: CachePadded::new(AtomicUsize::new(0)),
completed_tasks: CachePadded::new(AtomicUsize::new(0)),
active_workers: CachePadded::new(AtomicUsize::new(0)),
shutdown: AtomicBool::new(false),
}
}
pub fn submit_task(&self) {
self.pending_tasks.fetch_add(1, Ordering::Relaxed);
}
pub fn start_task(&self) -> bool {
let pending: _ = self.pending_tasks.fetch_sub(1, Ordering::AcqRel);
if pending > 0 {
self.active_workers.fetch_add(1, Ordering::Relaxed);
true
} else {
self.pending_tasks.fetch_add(1, Ordering::Relaxed);
false
}
}
pub fn complete_task(&self) {
self.active_workers.fetch_sub(1, Ordering::Relaxed);
self.completed_tasks.fetch_add(1, Ordering::Relaxed);
}
pub fn pending_count(&self) -> usize {
self.pending_tasks.load(Ordering::Relaxed)
}
pub fn completed_count(&self) -> usize {
self.completed_tasks.load(Ordering::Relaxed)
}
pub fn active_workers(&self) -> usize {
self.active_workers.load(Ordering::Relaxed)
}
pub fn shutdown(&self) {
self.shutdown.store(true, Ordering::Release);
}
pub fn should_shutdown(&self) -> bool {
self.shutdown.load(Ordering::Acquire)
}
}
#[derive(Debug)]
struct Node<T> {
data: Option<T>,
next: *mut Node<T>,
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct LockFreeQueue<T> {
head: Arc<CachePadded<AtomicPtr<Node<T>>>>,
tail: Arc<CachePadded<AtomicPtr<Node<T>>>>,
_phantom: std::marker::PhantomData<T>,
}
type AtomicPtr<T> = AtomicUsize;
#[allow(dead_code)]
impl<T> LockFreeQueue<T> {
pub fn new() -> Self {
let sentinel: _ = Box::into_raw(Box::new(Node {
data: None,
next: std::ptr::null_mut(),
}));
Self {
head: Arc::new(Mutex::new(CachePadded::new(AtomicPtr::new(sentinel as usize)))
tail: Arc::new(Mutex::new(CachePadded::new(AtomicPtr::new(sentinel as usize)))
_phantom: std::marker::PhantomData,
}
}
pub fn try_enqueue(&self, item: T) -> bool {
let new_node: _ = Box::into_raw(Box::new(Node {
data: Some(item),
next: std::ptr::null_mut(),
}));
loop {
let tail_ptr: _ = self.tail.load(Ordering::Acquire);
let tail: _ = unsafe { &*(tail_ptr as *const Node<T>) };
let next_ptr: _ = tail.next;
if !next_ptr.is_null() {
self.tail.compare_exchange_weak(
tail_ptr,
next_ptr,
Ordering::Release,
Ordering::Acquire,
).ok();
continue;
}
let new_node_ptr: _ = new_node as usize;
if unsafe {
(&(*tail_ptr as *const Node<T>)).next
}.is_null() {
if unsafe {
(&mut (*tail_ptr as *mut Node<T>)).next
}.write(new_node_ptr) {
self.tail.compare_exchange_weak(
tail_ptr,
new_node_ptr,
Ordering::Release,
Ordering::Acquire,
).ok();
return true;
}
}
}
}
pub fn try_dequeue(&self) -> Option<T> {
loop {
let head_ptr: _ = self.head.load(Ordering::Acquire);
let head: _ = unsafe { &*(head_ptr as *const Node<T>) };
let next_ptr: _ = head.next;
if next_ptr.is_null() {
return None;
}
let next: _ = unsafe { &*(next_ptr as *const Node<T>) };
let data: _ = unsafe { Box::from_raw(next_ptr as *mut Node<T>>).data };
if self.head.compare_exchange_weak(
head_ptr,
next_ptr,
Ordering::Release,
Ordering::Acquire,
).is_ok() {
unsafe {
let _: _ = Box::from_raw(head_ptr as *mut Node<T>);
}
return data;
}
}
}
pub fn len(&self) -> usize {
let mut count = 0;
let mut current = self.head.load(Ordering::Acquire);
unsafe {
while !current.is_null() {
let node: _ = &*(current as *const Node<T>);
if !node.next.is_null() {
count += 1;
}
current = node.next;
}
}
count
}
pub fn is_empty(&self) -> bool {
let head_ptr: _ = self.head.load(Ordering::Acquire);
unsafe {
let node: _ = &*(head_ptr as *const Node<T>);
node.next.is_null()
}
}
}
#[allow(dead_code)]
impl<T> Drop for LockFreeQueue<T> {
fn drop(&mut self) {
while let Some(_) = self.try_dequeue() {}
let head_ptr: _ = self.head.load(Ordering::Acquire);
unsafe {
let _: _ = Box::from_raw(head_ptr as *mut Node<T>);
}
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct ShardedLock<T> {
shards: Vec<CachePadded<Mutex<T>>>,
shard_count: usize,
}
#[allow(dead_code)]
impl<T> ShardedLock<T> {
pub fn new(shard_count: usize, initial_value: T) -> Self
where
T: Clone,
{
let mut shards = Vec::with_capacity(shard_count);
for _ in 0..shard_count {
shards.push(CachePadded::new(Mutex::new(initial_value.clone());
}
Self {
shards,
shard_count,
}
}
let hash: _ = self.simple_hash(key);
let index: _ = hash % self.shard_count;
self.shards[index].lock().await
}
fn simple_hash(&self, key: &str) -> usize {
let mut hash = 0usize;
for byte in key.bytes() {
hash = hash.wrapping_mul(31).wrapping_add(byte as usize);
}
hash
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct LockFreeBufferPool {
available_buffers: LockFreeCounter,
total_allocations: LockFreeCounter,
active_buffers: LockFreeCounter,
}
#[allow(dead_code)]
impl LockFreeBufferPool {
pub fn new() -> Self {
Self {
available_buffers: LockFreeCounter::new(0),
total_allocations: LockFreeCounter::new(0),
active_buffers: LockFreeCounter::new(0),
}
}
pub fn allocate(&self) {
self.active_buffers.increment();
self.total_allocations.increment();
}
pub fn deallocate(&self) {
self.active_buffers.decrement();
self.available_buffers.increment();
}
pub fn active_count(&self) -> usize {
self.active_buffers.load()
}
pub fn total_allocations(&self) -> usize {
self.total_allocations.load()
}
pub fn available_count(&self) -> usize {
self.available_buffers.load()
}
}
#[allow(dead_code)]
pub type OptimizedRwLock<T> = RwLock<T>;
#[derive(Debug)]
#[allow(dead_code)]
pub struct AtomicStats {
pub total_operations: LockFreeCounter,
pub cache_line_contention: LockFreeCounter,
pub false_sharing_detected: LockFreeCounter,
}
#[allow(dead_code)]
impl AtomicStats {
pub fn new() -> Self {
Self {
total_operations: LockFreeCounter::new(0),
cache_line_contention: LockFreeCounter::new(0),
false_sharing_detected: LockFreeCounter::new(0),
}
}
pub fn record_operation(&self) {
self.total_operations.increment();
}
pub fn record_contention(&self) {
self.cache_line_contention.increment();
}
pub fn record_false_sharing(&self) {
self.false_sharing_detected.increment();
}
pub fn get_report(&self) -> String {
format!(
"总操作数: {}, 缓存行竞争: {}, 伪共享检测: {}",
self.total_operations.load(),
self.cache_line_contention.load(),
self.false_sharing_detected.load()
)
}
}
#[cfg(test)]
mod tests {
#[test]
fn test_lock_free_counter() {
let counter: _ = LockFreeCounter::new(0);
assert_eq!(counter.increment(), 1);
assert_eq!(counter.load(), 1);
assert_eq!(counter.add(5), 6);
assert_eq!(counter.load(), 6);
assert_eq!(counter.sub(3), 3);
assert_eq!(counter.load(), 3);
}
#[test]
fn test_task_scheduler() {
let scheduler: _ = LockFreeTaskScheduler::new();
scheduler.submit_task();
assert_eq!(scheduler.pending_count(), 1);
assert!(scheduler.start_task());
assert_eq!(scheduler.active_workers(), 1);
scheduler.complete_task();
assert_eq!(scheduler.active_workers(), 0);
assert_eq!(scheduler.completed_count(), 1);
}
#[tokio::test]
#[ignore] async fn test_sharded_lock() {
let sharded_lock: _ = ShardedLock::new(4, 0u64);
let guard1: _ = sharded_lock.shard("key1").await;
let guard2: _ = sharded_lock.shard("key2").await;
let guard3: _ = sharded_lock.shard("key3").await;
let guard4: _ = sharded_lock.shard("key4").await;
drop(guard1);
drop(guard2);
drop(guard3);
drop(guard4);
let guard5: _ = sharded_lock.shard("key1").await;
let guard6: _ = sharded_lock.shard("key1").await;
assert_eq!(*guard5, 0);
assert_eq!(*guard6, 0);
}
#[test]
fn test_lock_free_buffer_pool() {
let pool: _ = LockFreeBufferPool::new();
assert_eq!(pool.active_count(), 0);
pool.allocate();
assert_eq!(pool.active_count(), 1);
assert_eq!(pool.total_allocations(), 1);
pool.deallocate();
assert_eq!(pool.active_count(), 0);
assert_eq!(pool.available_count(), 1);
}
#[test]
fn test_concurrent_operations() {
let counter: _ = Arc::new(Mutex::new(LockFreeCounter::new(0)),;
let iterations: _ = 1000;
let thread_count: _ = 10;
let handles: Vec<_> = (0..thread_count))
.map(|_| {
let counter: _ = counter.clone();
thread::spawn(move || {
for _ in 0..iterations {
counter.increment();
}
})
})
.collect();
for handle in handles {
handle.join().unwrap();
}
assert_eq!(counter.load(), thread_count * iterations);
}
#[test]
fn test_atomic_stats() {
let stats: _ = Arc::new(Mutex::new(AtomicStats::new()),;
stats.record_operation();
stats.record_operation();
stats.record_contention();
assert_eq!(stats.total_operations.load(), 2);
assert_eq!(stats.cache_line_contention.load(), 1);
let report: _ = stats.get_report();
assert!(report.contains("总操作数: 2"));
assert!(report.contains("缓存行竞争: 1"));
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct CpuAffinity {
cpu_id: usize,
affinity_mask: u64,
}
#[allow(dead_code)]
impl CpuAffinity {
pub fn new(cpu_id: usize) -> Result<Self, String> {
#[cfg(target_os = "linux")]
{
let affinity_mask: _ = 1u64 << cpu_id;
Ok(Self {
cpu_id,
affinity_mask,
})
}
#[cfg(not(target_os = "linux"))]
{
Err("CPU affinity not supported on this platform".to_string())
}
}
pub fn cpu_id(&self) -> usize {
self.cpu_id
}
pub fn affinity_mask(&self) -> u64 {
self.affinity_mask
}
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct WorkStealingTask {
pub id: usize,
pub data: Vec<u8>,
pub priority: u8,
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct WorkStealingScheduler {
queues: Vec<Arc<LockFreeQueue<WorkStealingTask>>>,
stealers: Vec<Arc<AtomicUsize>>,
active_workers: CachePadded<AtomicUsize>,
cpu_affinity: Vec<Option<CpuAffinity>>,
task_counter: CachePadded<AtomicUsize>,
}
#[allow(dead_code)]
impl WorkStealingScheduler {
pub fn new(num_workers: usize) -> Self {
let mut queues = Vec::with_capacity(num_workers);
let mut stealers = Vec::with_capacity(num_workers);
let mut cpu_affinity = Vec::with_capacity(num_workers);
for i in 0..num_workers {
queues.push(Arc::new(Mutex::new(LockFreeQueue::new()),;
stealers.push(Arc::new(Mutex::new(AtomicUsize::new(0)),;
match CpuAffinity::new(i) {
Ok(affinity) => {
cpu_affinity.push(Some(affinity));
println!("✅ Worker {} 绑定到 CPU {}", i, i);
}
Err(_) => {
cpu_affinity.push(None);
println!("⚠️ Worker {} 未绑定到特定 CPU", i);
}
}
}
Self {
queues,
stealers,
active_workers: CachePadded::new(AtomicUsize::new(0)),
cpu_affinity,
task_counter: CachePadded::new(AtomicUsize::new(0)),
}
}
pub fn submit(&self, task: WorkStealingTask) -> Result<(), String> {
let worker_id: _ = self.task_counter.fetch_add(1, Ordering::Relaxed) % self.queues.len();
let queue: _ = &self.queues[worker_id];
if queue.try_enqueue(task) {
Ok(())
} else {
Err("Failed to enqueue task".to_string())
}
}
pub fn take_local_task(&self, worker_id: usize) -> Option<WorkStealingTask> {
let queue: _ = &self.queues[worker_id];
queue.try_dequeue()
}
pub fn steal_task(&self, stealer_id: usize) -> Option<WorkStealingTask> {
let num_queues: _ = self.queues.len();
if num_queues <= 1 {
return None;
}
for _ in 0..num_queues {
let victim_id: _ = self.stealers[stealer_id].fetch_add(1, Ordering::Relaxed) % num_queues;
if victim_id == stealer_id {
continue;
}
if let Some(task) = self.queues[victim_id].try_dequeue() {
println!("✅ Worker {} 从 Worker {} 窃取了任务", stealer_id, victim_id);
return Some(task);
}
}
None
}
pub fn active_workers(&self) -> usize {
self.active_workers.load(Ordering::Relaxed)
}
pub fn increment_active_workers(&self) {
self.active_workers.fetch_add(1, Ordering::Relaxed);
}
pub fn decrement_active_workers(&self) {
self.active_workers.fetch_sub(1, Ordering::Relaxed);
}
pub fn queue_lengths(&self) -> Vec<usize> {
self.queues.iter().map(|q| q.len()).collect()
}
}
#[derive(Debug)]
#[allow(dead_code)]
pub struct ConcurrencyMonitor {
pub active_tasks: Arc<LockFreeCounter>,
pub completed_tasks: Arc<LockFreeCounter>,
pub failed_tasks: Arc<LockFreeCounter>,
pub avg_latency_ns: Arc<LockFreeCounter>,
pub throughput_ops: Arc<LockFreeCounter>,
pub start_time: std::time::Instant,
}
#[allow(dead_code)]
impl ConcurrencyMonitor {
pub fn new() -> Self {
Self {
active_tasks: Arc::new(Mutex::new(LockFreeCounter::new(0)))
completed_tasks: Arc::new(Mutex::new(LockFreeCounter::new(0)))
failed_tasks: Arc::new(Mutex::new(LockFreeCounter::new(0)))
avg_latency_ns: Arc::new(Mutex::new(LockFreeCounter::new(0)))
throughput_ops: Arc::new(Mutex::new(LockFreeCounter::new(0)))
start_time: std::time::Instant::now(),
}
}
pub fn task_started(&self) {
self.active_tasks.increment();
}
pub fn task_completed(&self, latency_ns: u64) {
self.active_tasks.decrement();
self.completed_tasks.increment();
self.avg_latency_ns.add(latency_ns);
self.throughput_ops.increment();
}
pub fn task_failed(&self) {
self.active_tasks.decrement();
self.failed_tasks.increment();
}
pub fn get_stats(&self) -> ConcurrencyStatsSnapshot {
let elapsed: _ = self.start_time.elapsed();
let elapsed_secs: _ = elapsed.as_secs_f64();
ConcurrencyStatsSnapshot {
active_tasks: self.active_tasks.load(),
completed_tasks: self.completed_tasks.load(),
failed_tasks: self.failed_tasks.load(),
avg_latency_ns: if self.completed_tasks.load() > 0 {
self.avg_latency_ns.load() / self.completed_tasks.load()
} else {
0
},
throughput_ops_per_sec: if elapsed_secs > 0.0 {
self.throughput_ops.load() as f64 / elapsed_secs
} else {
0.0
},
uptime: elapsed,
}
}
pub fn generate_report(&self) -> String {
let stats: _ = self.get_stats();
format!(
"并发性能报告:\n\
活跃任务: {}\n\
已完成任务: {}\n\
失败任务: {}\n\
平均延迟: {} ns\n\
吞吐量: {:.2} ops/sec\n\
运行时间: {:?}",
stats.active_tasks,
stats.completed_tasks,
stats.failed_tasks,
stats.avg_latency_ns,
stats.throughput_ops_per_sec,
stats.uptime
)
}
}
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct ConcurrencyStatsSnapshot {
pub active_tasks: usize,
pub completed_tasks: usize,
pub failed_tasks: usize,
pub avg_latency_ns: u64,
pub throughput_ops_per_sec: f64,
pub uptime: std::time::Duration,
}
use tokio::sync::{TokioMutex, TokioRwLock};
use std::time::{Duration, Instant};
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
use std::sync::{Arc, Mutex, RwLock};