tenferro_tensor/
completion_event.rs

1/// Synchronization event for asynchronous accelerator operations.
2///
3/// Tracks completion of asynchronous operations on accelerator devices,
4/// enabling operation chaining without CPU synchronization.
5///
6/// # Examples
7///
8/// ```ignore
9/// use tenferro_tensor::CompletionEvent;
10///
11/// // CompletionEvent is typically created by GPU backends.
12/// let _event: Option<CompletionEvent> = None;
13/// ```
14#[derive(Clone)]
15pub struct CompletionEvent {
16    #[allow(dead_code)]
17    inner: CompletionEventInner,
18}
19
20impl CompletionEvent {
21    /// Create a no-op completion event.
22    ///
23    /// This is useful for testing and for cases where no synchronization is needed.
24    pub fn noop() -> Self {
25        Self {
26            inner: CompletionEventInner::Noop,
27        }
28    }
29}
30
31#[derive(Clone)]
32#[allow(dead_code)]
33enum CompletionEventInner {
34    Noop,
35    Cuda { _event: *mut std::ffi::c_void },
36    Rocm { _event: *mut std::ffi::c_void },
37}
38
39/// # Safety
40///
41/// `CompletionEvent` can be safely sent across threads because:
42/// - The raw pointer in `CompletionEventInner` is only used as an opaque handle
43/// - CUDA/ROCm events are internally thread-safe when used with proper synchronization
44/// - The event is only used for synchronization and does not expose mutable state
45/// - The pointer is never dereferenced directly by this crate
46unsafe impl Send for CompletionEvent {}
47
48/// # Safety
49///
50/// `CompletionEvent` can be safely shared across threads because:
51/// - The raw pointer is an opaque handle to a GPU event object
52/// - CUDA/ROCm event APIs are thread-safe for query operations
53/// - The event does not contain any Rust-managed mutable state
54/// - Concurrent access to the event is managed by the GPU driver
55unsafe impl Sync for CompletionEvent {}