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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use std::ops::{Deref, DerefMut};
use core::{self, OclPrm, ClWaitListPtr, ClNullEventPtr, MemMap as MemMapCore, Mem as MemCore, AsMem};
use standard::{ClWaitListPtrEnum, ClNullEventPtrEnum, Event, EventList, Queue};
use async::{Result as AsyncResult};
#[must_use = "commands do nothing unless enqueued"]
#[derive(Debug)]
pub struct MemUnmapCmd<'c, T> where T: 'c + OclPrm {
queue: Option<&'c Queue>,
mem_map: &'c mut MemMap<T>,
ewait: Option<ClWaitListPtrEnum<'c>>,
enew: Option<ClNullEventPtrEnum<'c>>,
}
impl<'c, T> MemUnmapCmd<'c, T> where T: OclPrm {
fn new(mem_map: &'c mut MemMap<T>) -> MemUnmapCmd<'c, T>
{
MemUnmapCmd {
queue: None,
mem_map: mem_map,
ewait: None,
enew: None,
}
}
pub fn queue<'q, Q>(mut self, queue: &'q Q) -> MemUnmapCmd<'c, T>
where 'q: 'c, Q: 'q + AsRef<Queue>
{
self.queue = Some(queue.as_ref());
self
}
pub fn ewait<EWL>(mut self, ewait: EWL) -> MemUnmapCmd<'c, T>
where EWL: Into<ClWaitListPtrEnum<'c>>
{
self.ewait = Some(ewait.into());
self
}
pub fn ewait_opt<EWL>(mut self, ewait: Option<EWL>) -> MemUnmapCmd<'c, T> where EWL: Into<ClWaitListPtrEnum<'c>> {
self.ewait = ewait.map(|el| el.into());
self
}
pub fn enew<NE>(mut self, enew: NE) -> MemUnmapCmd<'c, T>
where NE: Into<ClNullEventPtrEnum<'c>>
{
self.enew = Some(enew.into());
self
}
pub fn enew_opt<NE>(mut self, enew: Option<NE>) -> MemUnmapCmd<'c, T>
where NE: Into<ClNullEventPtrEnum<'c>>
{
self.enew = enew.map(|e| e.into());
self
}
pub fn enq(mut self) -> AsyncResult<()> {
self.mem_map.enqueue_unmap(self.queue, self.ewait, self.enew)
}
}
#[derive(Debug)]
pub struct MemMap<T> where T: OclPrm {
core: MemMapCore<T>,
len: usize,
buffer: MemCore,
queue: Queue,
unmap_wait_list: Option<EventList>,
unmap_target_event: Option<Event>,
callback_is_set: bool,
is_unmapped: bool,
}
impl<T> MemMap<T> where T: OclPrm {
pub unsafe fn new(core: MemMapCore<T>, len: usize, unmap_wait_list: Option<EventList>,
unmap_target_event: Option<Event>, buffer: MemCore, queue: Queue) -> MemMap<T>
{
MemMap {
core: core,
len: len,
buffer: buffer,
queue: queue,
unmap_wait_list: unmap_wait_list,
unmap_target_event: unmap_target_event,
callback_is_set: false,
is_unmapped: false,
}
}
pub fn unmap<'c>(&'c mut self) -> MemUnmapCmd<'c, T> {
MemUnmapCmd::new(self)
}
pub fn enqueue_unmap<Ewl, En>(&mut self, queue: Option<&Queue>, ewait_opt: Option<Ewl>,
mut enew_opt: Option<En>) -> AsyncResult<()>
where En: ClNullEventPtr, Ewl: ClWaitListPtr
{
if !self.is_unmapped {
assert!(!(ewait_opt.is_some() && self.unmap_wait_list.is_some()),
"MemMap::enqueue_unmap: Cannot set an event wait list for the unmap command \
when the 'unmap_wait_list' has already been set.");
let mut origin_event_opt = if self.unmap_target_event.is_some() || enew_opt.is_some() {
Some(Event::empty())
} else {
None
};
core::enqueue_unmap_mem_object(queue.unwrap_or(&self.queue), &self.buffer,
&self.core, ewait_opt.and(self.unmap_wait_list.as_ref()), origin_event_opt.as_mut())?;
self.is_unmapped = true;
if let Some(origin_event) = origin_event_opt {
if let Some(ref mut enew) = enew_opt {
unsafe { enew.clone_from(&origin_event) }
}
if cfg!(not(feature = "async_block")) {
if self.unmap_target_event.is_some() {
#[cfg(not(feature = "async_block"))]
self.register_event_trigger(&origin_event)?;
#[cfg(not(feature = "async_block"))]
::std::mem::forget(origin_event);
}
} else {
if let Some(ref mut um_tar) = self.unmap_target_event {
origin_event.wait_for()?;
um_tar.set_complete()?;
}
}
}
Ok(())
} else {
Err("ocl_core::- ::unmap: Already unmapped.".into())
}
}
#[cfg(not(feature = "async_block"))]
fn register_event_trigger(&mut self, event: &Event) -> AsyncResult<()> {
debug_assert!(self.is_unmapped && self.unmap_target_event.is_some());
if !self.callback_is_set {
if let Some(ref ev) = self.unmap_target_event {
unsafe {
let unmap_target_event_ptr = ev.clone().into_raw();
event.set_callback(core::_complete_user_event, unmap_target_event_ptr)?;
}
self.callback_is_set = true;
Ok(())
} else {
panic!("- ::register_event_trigger: No unmap event target \
has been configured with this MemMap.");
}
} else {
Err("Callback already set.".into())
}
}
pub fn unmap_target_event(&self) -> Option<&Event> {
self.unmap_target_event.as_ref()
}
pub fn unmap_wait_list(&self) -> Option<&EventList> {
self.unmap_wait_list.as_ref()
}
#[inline] pub fn is_unmapped(&self) -> bool { self.is_unmapped }
#[inline] pub fn as_ptr(&self) -> *const T { self.core.as_ptr() }
#[inline] pub fn as_mut_ptr(&mut self) -> *mut T { self.core.as_mut_ptr() }
#[inline] pub fn queue(&self) -> &Queue { &self.queue }
}
impl<T> Deref for MemMap<T> where T: OclPrm {
type Target = [T];
fn deref(&self) -> &[T] {
assert!(!self.is_unmapped, "Mapped memory has been unmapped and cannot be accessed.");
unsafe { self.core.as_slice(self.len) }
}
}
impl<T> DerefMut for MemMap<T> where T: OclPrm {
fn deref_mut(&mut self) -> &mut [T] {
assert!(!self.is_unmapped, "Mapped memory has been unmapped and cannot be accessed.");
unsafe { self.core.as_slice_mut(self.len) }
}
}
impl<T: OclPrm> Drop for MemMap<T> {
fn drop(&mut self) {
if !self.is_unmapped {
self.enqueue_unmap::<&Event, &mut Event>(None, None, None).ok();
}
}
}
impl<T: OclPrm> AsMem<T> for MemMap<T> {
fn as_mem(&self) -> &MemCore {
self.core.as_mem()
}
}