1use 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 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 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 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 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}