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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
use std::fmt;
use std::mem::ManuallyDrop;
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard};

use crate::{
    Buffer, ClNumber, CommandQueueOptions, CommandQueueProperties, Context, Device, Kernel, Output,
    Waitlist, Work,
};

use crate::ll::{ClCommandQueue, ClContext, ClDeviceID, CommandQueuePtr, ContextPtr, DevicePtr};

pub trait CommandQueueLock<P>
where
    P: CommandQueuePtr,
{
    unsafe fn write_lock(&self) -> RwLockWriteGuard<P>;
    unsafe fn read_lock(&self) -> RwLockReadGuard<P>;
    unsafe fn rw_lock(&self) -> &RwLock<P>;

    fn address(&self) -> String {
        unsafe {
            let read_lock = self.read_lock();
            let ptr = read_lock.command_queue_ptr();
            format!("{:?}", ptr)
        }
    }
}

pub struct CommandQueue {
    _queue: ManuallyDrop<Arc<RwLock<ClCommandQueue>>>,
    _context: ManuallyDrop<ClContext>,
    _device: ManuallyDrop<ClDeviceID>,
    _unconstructable: (),
}

impl CommandQueueLock<ClCommandQueue> for CommandQueue {
    unsafe fn read_lock(&self) -> RwLockReadGuard<ClCommandQueue> {
        (*self._queue).read().unwrap()
    }
    unsafe fn write_lock(&self) -> RwLockWriteGuard<ClCommandQueue> {
        (*self._queue).write().unwrap()
    }
    unsafe fn rw_lock(&self) -> &RwLock<ClCommandQueue> {
        &(*self._queue)
    }
}

impl fmt::Debug for CommandQueue {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "CommandQueue{{{:?}}}", self.address())
    }
}

impl Drop for CommandQueue {
    fn drop(&mut self) {
        unsafe {
            debug!("cl_command_queue {:?} - CommandQueue::drop", self.address());
            ManuallyDrop::drop(&mut self._queue);
            ManuallyDrop::drop(&mut self._context);
            ManuallyDrop::drop(&mut self._device);
        }
    }
}

impl Clone for CommandQueue {
    fn clone(&self) -> CommandQueue {
        CommandQueue {
            _queue: ManuallyDrop::new((*self._queue).clone()),
            _context: self._context.clone(),
            _device: self._device.clone(),
            _unconstructable: (),
        }
    }
}

unsafe impl Send for CommandQueue {}
unsafe impl Sync for CommandQueue {}

impl CommandQueue {
    /// Builds a CommandQueue from a low-level ClCommandQueue, a Context and a Device.
    /// 
    /// # Safety
    /// Building a CommandQueue with any invalid ClObject, or mismatched ClObjects is undefined behavior.
    unsafe fn new(queue: ClCommandQueue, context: &Context, device: &Device) -> CommandQueue {
        CommandQueue::new_from_low_level(
            queue,
            context.low_level_context(),
            device.low_level_device(),
        )
    }

    /// Builds a CommandQueue from a low-level ClObjects
    /// 
    /// # Safety
    /// Building a CommandQueue with any invalid ClObject, or mismatched ClObjects is undefined behavior.
    unsafe fn new_from_low_level(
        queue: ClCommandQueue,
        context: &ClContext,
        device: &ClDeviceID,
    ) -> CommandQueue {
        CommandQueue {
            _queue: ManuallyDrop::new(Arc::new(RwLock::new(queue))),
            _context: ManuallyDrop::new(context.clone()),
            _device: ManuallyDrop::new(device.clone()),
            _unconstructable: (),
        }
    }

    /// Creates a new CommandQueue with the given Context on the given Device.
    pub fn create(
        context: &Context,
        device: &Device,
        opt_props: Option<CommandQueueProperties>,
    ) -> Output<CommandQueue> {
        unsafe {
            let ll_queue = ClCommandQueue::create(
                context.low_level_context(),
                device.low_level_device(),
                opt_props,
            )?;
            Ok(CommandQueue::new(ll_queue, context, device))
        }
    }

    /// Creates a new copy of a CommandQueue with CommandQueue's Context on the CommandQueue's Device.
    /// 
    /// This function is useful for executing concurrent operations on a device within the same
    /// Context.
    pub fn create_copy(&self) -> Output<CommandQueue> {
        unsafe {
            let props = self.properties()?;
            let ll_queue = ClCommandQueue::create_from_raw_pointers(
                (*self._context).context_ptr(),
                (*self._device).device_ptr(),
                props.into(),
            )?;
            Ok(CommandQueue::new_from_low_level(
                ll_queue,
                &self._context,
                &self._device,
            ))
        }
    }

    /// The low-level context of the CommandQueue 
    pub fn low_level_context(&self) -> ClContext {
        (*self._context).clone()
    }

    pub fn low_level_device(&self) -> ClDeviceID {
        (*self._device).clone()
    }

    /// write_buffer is used to move data from the host buffer (buffer: &[T]) to
    /// the OpenCL cl_mem pointer inside `d_mem: &Buffer<T>`.
    pub fn write_buffer<T>(
        &self,
        device_buffer: &Buffer<T>,
        host_buffer: &[T],
        opts: Option<CommandQueueOptions>,
    ) -> Output<()>
    where
        T: ClNumber,
    {
        unsafe {
            let mut qlock = self.write_lock();
            let mut buf_lock = device_buffer.write_lock();
            let event = qlock.write_buffer(&mut *buf_lock, host_buffer, opts.into())?;
            event.wait()
        }
    }

    /// read_buffer is used to move data from the `device_mem` (`cl_mem` pointer
    /// inside `&DeviceMem<T>`) into a `host_buffer` (`&mut [T]`).
    pub fn read_buffer<T: ClNumber>(
        &self,
        device_buffer: &Buffer<T>,
        host_buffer: &mut [T],
        opts: Option<CommandQueueOptions>,
    ) -> Output<Option<Vec<T>>> {
        unsafe {
            let mut qlock = self.write_lock();
            let buf_lock = device_buffer.read_lock();
            let mut event = qlock.read_buffer(&*buf_lock, host_buffer, opts)?;
            event.wait()
        }
    }

    pub fn enqueue_kernel(
        &self,
        kernel: Kernel,
        work: &Work,
        opts: Option<CommandQueueOptions>,
    ) -> Output<()> {
        unsafe {
            let mut kernel_lock = kernel.write_lock();
            let mut qlock = self.write_lock();
            let event = qlock.enqueue_kernel(&mut (*kernel_lock), work, opts)?;
            event.wait()
        }
    }

    pub fn finish(&self) -> Output<()> {
        unsafe {
            let mut lock = self.write_lock();
            lock.finish()
        }
    }

    pub fn reference_count(&self) -> Output<u32> {
        unsafe { self.read_lock().reference_count() }
    }

    pub fn properties(&self) -> Output<CommandQueueProperties> {
        unsafe { self.read_lock().properties() }
    }
}

impl PartialEq for CommandQueue {
    fn eq(&self, other: &Self) -> bool {
        unsafe {
            std::ptr::eq(
                self.read_lock().command_queue_ptr(),
                other.read_lock().command_queue_ptr(),
            )
        }
    }
}

impl Eq for CommandQueue {}

#[cfg(test)]
mod tests {
    use crate::testing;
    use crate::ll::{CommandQueuePtr, ClContext, ClDeviceID, CommandQueueProperties};

    const SRC: &'static str = "
    __kernel void test(__global int *i) {
        *i += 1;
    }";

    #[test]
    pub fn command_queue_method_context_works() {
        // testing::init_logger();
        let session = testing::get_session(SRC);
        let _context: ClContext = unsafe { session.read_queue().context().unwrap() };
    }

    #[test]
    pub fn command_queue_method_device_works() {
        let session = testing::get_session(SRC);
        let _device: ClDeviceID = unsafe { session.read_queue().device().unwrap() };
    }

    #[test]
    pub fn command_queue_method_reference_count_works() {
        let session = testing::get_session(SRC);
        let ref_count: u32 = unsafe {
                session
                .read_queue()
                .reference_count()
            }
            .expect("CommandQueue method reference_count() failed");
        assert_eq!(ref_count, 1);
    }

    #[test]
    pub fn command_queue_method_properties_works() {
        let session = testing::get_session(SRC);
        let props: CommandQueueProperties = unsafe {
                session
                .read_queue()
                .properties()
            }
            .expect("CommandQueue method properties() failed");
        let bits = props.bits();
        let maybe_same_prop = CommandQueueProperties::from_bits(bits);
        if !maybe_same_prop.is_some() {
            panic!(
                "
                CommandQueue method properties returned \
                an invalid CommandQueueProperties bitflag {:?}\
                ",
                bits
            );
        }
    }

    #[test]
    pub fn command_queue_copy_new_works() {
        let session = testing::get_session(SRC);
        unsafe {
            let cq2 = session
                .read_queue()
                .create_copy()
                .unwrap();
            assert!(cq2.command_queue_ptr() != session.read_queue().command_queue_ptr());
        }
    }
}