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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use std::mem;
use std::ptr;
use std::sync::Arc;
use OomError;
use SafeDeref;
use VulkanObject;
use check_errors;
use device::Device;
use device::DeviceOwned;
use vk;
#[derive(Debug)]
pub struct Semaphore<D = Arc<Device>>
where D: SafeDeref<Target = Device>
{
semaphore: vk::Semaphore,
device: D,
must_put_in_pool: bool,
}
impl<D> Semaphore<D>
where D: SafeDeref<Target = Device>
{
pub fn from_pool(device: D) -> Result<Semaphore<D>, OomError> {
let maybe_raw_sem = device.semaphore_pool().lock().unwrap().pop();
match maybe_raw_sem {
Some(raw_sem) => {
Ok(Semaphore {
device: device,
semaphore: raw_sem,
must_put_in_pool: true,
})
},
None => {
Semaphore::alloc_impl(device, true)
},
}
}
#[inline]
pub fn alloc(device: D) -> Result<Semaphore<D>, OomError> {
Semaphore::alloc_impl(device, false)
}
fn alloc_impl(device: D, must_put_in_pool: bool) -> Result<Semaphore<D>, OomError> {
let semaphore = unsafe {
static mut INFOS: vk::SemaphoreCreateInfo = vk::SemaphoreCreateInfo {
sType: vk::STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO,
pNext: 0 as *const _,
flags: 0,
};
let vk = device.pointers();
let mut output = mem::uninitialized();
check_errors(vk.CreateSemaphore(device.internal_object(),
&INFOS,
ptr::null(),
&mut output))?;
output
};
Ok(Semaphore {
device: device,
semaphore: semaphore,
must_put_in_pool: must_put_in_pool,
})
}
}
unsafe impl DeviceOwned for Semaphore {
#[inline]
fn device(&self) -> &Arc<Device> {
&self.device
}
}
unsafe impl<D> VulkanObject for Semaphore<D>
where D: SafeDeref<Target = Device>
{
type Object = vk::Semaphore;
const TYPE: vk::DebugReportObjectTypeEXT = vk::DEBUG_REPORT_OBJECT_TYPE_SEMAPHORE_EXT;
#[inline]
fn internal_object(&self) -> vk::Semaphore {
self.semaphore
}
}
impl<D> Drop for Semaphore<D>
where D: SafeDeref<Target = Device>
{
#[inline]
fn drop(&mut self) {
unsafe {
if self.must_put_in_pool {
let raw_sem = self.semaphore;
self.device.semaphore_pool().lock().unwrap().push(raw_sem);
} else {
let vk = self.device.pointers();
vk.DestroySemaphore(self.device.internal_object(), self.semaphore, ptr::null());
}
}
}
}
#[cfg(test)]
mod tests {
use VulkanObject;
use sync::Semaphore;
#[test]
fn semaphore_create() {
let (device, _) = gfx_dev_and_queue!();
let _ = Semaphore::alloc(device.clone());
}
#[test]
fn semaphore_pool() {
let (device, _) = gfx_dev_and_queue!();
assert_eq!(device.semaphore_pool().lock().unwrap().len(), 0);
let sem1_internal_obj = {
let sem = Semaphore::from_pool(device.clone()).unwrap();
assert_eq!(device.semaphore_pool().lock().unwrap().len(), 0);
sem.internal_object()
};
assert_eq!(device.semaphore_pool().lock().unwrap().len(), 1);
let sem2 = Semaphore::from_pool(device.clone()).unwrap();
assert_eq!(device.semaphore_pool().lock().unwrap().len(), 0);
assert_eq!(sem2.internal_object(), sem1_internal_obj);
}
}