framealloc 0.11.1

Intent-aware, thread-smart memory allocation for Rust game engines
Documentation
//! CPU-GPU synchronization primitives

use std::sync::{Arc, Mutex};

/// Direction of data transfer between CPU and GPU
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TransferDirection {
    /// CPU to GPU transfer
    CpuToGpu,
    /// GPU to CPU transfer
    GpuToCpu,
    /// Bidirectional transfer
    Bidirectional,
}

/// A barrier for synchronizing CPU and GPU operations
pub struct CpuGpuBarrier {
    /// Current frame number
    frame: Arc<Mutex<u64>>,
    /// GPU fence for tracking completion
    gpu_fence: Arc<Mutex<Option<GpuFence>>>,
}

/// Simple GPU fence abstraction
struct GpuFence {
    /// Fence value
    value: u64,
    /// Whether the fence has been signaled
    signaled: bool,
}

impl CpuGpuBarrier {
    /// Create a new CPU-GPU barrier
    pub fn new() -> Self {
        Self {
            frame: Arc::new(Mutex::new(0)),
            gpu_fence: Arc::new(Mutex::new(None)),
        }
    }
    
    /// Signal that CPU work for the current frame is complete
    pub fn signal_cpu_complete(&self) {
        let mut frame = self.frame.lock().unwrap();
        *frame += 1;
    }
    
    /// Signal that GPU work for the current frame is complete
    pub fn signal_gpu_complete(&self, frame_value: u64) {
        let mut fence = self.gpu_fence.lock().unwrap();
        *fence = Some(GpuFence {
            value: frame_value,
            signaled: true,
        });
    }
    
    /// Wait until both CPU and GPU have completed the current frame
    pub fn wait_current_frame(&self) {
        let frame = *self.frame.lock().unwrap();
        
        // Wait for GPU fence
        let fence = self.gpu_fence.lock().unwrap();
        if let Some(ref f) = *fence {
            if f.value >= frame && f.signaled {
                // Both CPU and GPU have completed
            }
        }
    }
    
    /// Get the current frame number
    pub fn current_frame(&self) -> u64 {
        *self.frame.lock().unwrap()
    }
}

impl Default for CpuGpuBarrier {
    fn default() -> Self {
        Self::new()
    }
}