Skip to main content

mtl_gpu/device/
events.rs

1//! Device event and fence creation methods.
2//!
3//! Corresponds to event/fence creation methods in `Metal/MTLDevice.hpp`.
4
5use std::ffi::c_void;
6
7use mtl_foundation::Referencing;
8use mtl_sys::{msg_send_0, msg_send_1, sel};
9
10use super::Device;
11use crate::sync::{Event, Fence, SharedEvent, SharedEventHandle};
12
13impl Device {
14    // =========================================================================
15    // Event Creation
16    // =========================================================================
17
18    /// Create a new event.
19    ///
20    /// C++ equivalent: `Event* newEvent()`
21    pub fn new_event(&self) -> Option<Event> {
22        unsafe {
23            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(newEvent));
24            Event::from_raw(ptr)
25        }
26    }
27
28    /// Create a new shared event.
29    ///
30    /// C++ equivalent: `SharedEvent* newSharedEvent()`
31    pub fn new_shared_event(&self) -> Option<SharedEvent> {
32        unsafe {
33            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(newSharedEvent));
34            SharedEvent::from_raw(ptr)
35        }
36    }
37
38    /// Create a shared event from a handle.
39    ///
40    /// C++ equivalent: `SharedEvent* newSharedEvent(const SharedEventHandle*)`
41    pub fn new_shared_event_with_handle(&self, handle: &SharedEventHandle) -> Option<SharedEvent> {
42        unsafe {
43            let ptr: *mut c_void = msg_send_1(
44                self.as_ptr(),
45                sel!(newSharedEventWithHandle:),
46                handle.as_raw(),
47            );
48            SharedEvent::from_raw(ptr)
49        }
50    }
51
52    // =========================================================================
53    // Fence Creation
54    // =========================================================================
55
56    /// Create a new fence.
57    ///
58    /// C++ equivalent: `Fence* newFence()`
59    pub fn new_fence(&self) -> Option<Fence> {
60        unsafe {
61            let ptr: *mut c_void = msg_send_0(self.as_ptr(), sel!(newFence));
62            Fence::from_raw(ptr)
63        }
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use crate::device::system_default;
70
71    #[test]
72    fn test_new_event() {
73        let device = system_default().expect("no Metal device");
74        let event = device.new_event();
75        assert!(event.is_some());
76
77        let event = event.unwrap();
78        event.set_label("Test Event");
79        assert_eq!(event.label(), Some("Test Event".to_string()));
80    }
81
82    #[test]
83    fn test_new_shared_event() {
84        let device = system_default().expect("no Metal device");
85        let event = device.new_shared_event();
86        assert!(event.is_some());
87
88        let event = event.unwrap();
89        assert_eq!(event.signaled_value(), 0);
90
91        event.set_signaled_value(42);
92        assert_eq!(event.signaled_value(), 42);
93    }
94
95    #[test]
96    fn test_new_fence() {
97        let device = system_default().expect("no Metal device");
98        let fence = device.new_fence();
99        assert!(fence.is_some());
100
101        let fence = fence.unwrap();
102        fence.set_label("Test Fence");
103        assert_eq!(fence.label(), Some("Test Fence".to_string()));
104    }
105}