1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use std::sync::Arc;
use vks;
use ::{VdResult, Device, Handle, SemaphoreCreateFlags, SemaphoreCreateInfo};



#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[repr(C)]
pub struct SemaphoreHandle(pub(crate) vks::VkSemaphore);

impl SemaphoreHandle {
    #[inline(always)]
    pub fn to_raw(&self) -> vks::VkSemaphore {
        self.0
    }
}

unsafe impl Handle for SemaphoreHandle {
    type Target = SemaphoreHandle;

    /// Returns this object's handle.
    #[inline(always)]
    fn handle(&self) -> Self::Target {
        *self
    }
}


#[derive(Debug)]
struct Inner {
    handle: SemaphoreHandle,
    device: Device,
}

#[derive(Debug, Clone)]
pub struct Semaphore {
    inner: Arc<Inner>,
}

impl Semaphore {
    /// Creates and returns a new `Semaphore`.
    pub fn new(device: Device, flags: SemaphoreCreateFlags) -> VdResult<Semaphore> {
        let create_info = SemaphoreCreateInfo::builder()
            .flags(flags)
            .build();

        let handle = unsafe { device.create_semaphore(&create_info, None)? };

        Ok(Semaphore {
            inner: Arc::new(Inner {
                handle,
                device,
            })
        })
    }

    /// Returns this object's handle.
    pub fn handle(&self) -> SemaphoreHandle {
        self.inner.handle
    }

    /// Returns a reference to the associated device.
    pub fn device(&self) -> &Device {
        &self.inner.device
    }
}

unsafe impl<'h> Handle for &'h Semaphore {
    type Target = SemaphoreHandle;

    #[inline(always)]
    fn handle(&self) -> Self::Target {
        self.inner.handle
    }
}

impl Drop for Inner {
    fn drop(&mut self) {
        unsafe {
            self.device.destroy_semaphore(self.handle, None);
        }
    }
}