use super::{sys, DispatchQueue};
use crate::core::{Arc, ObjectType};
use std::{
cell::UnsafeCell,
ffi::c_void,
panic::RefUnwindSafe,
ptr::{self, NonNull},
};
#[repr(C)]
pub struct DispatchObject {
_data: UnsafeCell<[u8; 0]>,
}
impl ObjectType for DispatchObject {
#[inline]
#[doc(alias = "dispatch_retain")]
fn retain(obj: &Self) -> Arc<Self> {
unsafe {
sys::dispatch_retain(obj);
Arc::from_raw(obj)
}
}
#[inline]
#[doc(alias = "dispatch_release")]
unsafe fn release(obj: NonNull<Self>) {
sys::dispatch_release(obj.as_ptr());
}
}
unsafe impl Send for DispatchObject {}
unsafe impl Sync for DispatchObject {}
impl RefUnwindSafe for DispatchObject {}
impl AsRef<DispatchObject> for DispatchObject {
#[inline]
fn as_ref(&self) -> &Self {
self
}
}
impl DispatchObject {
#[inline]
#[doc(alias = "dispatch_activate")]
pub fn activate(&self) {
unsafe { sys::dispatch_activate(self) };
}
#[inline]
#[doc(alias = "dispatch_resume")]
pub fn resume(&self) {
unsafe { sys::dispatch_resume(self) };
}
#[inline]
#[doc(alias = "dispatch_suspend")]
pub fn suspend(&self) {
unsafe { sys::dispatch_suspend(self) };
}
#[inline]
#[doc(alias = "dispatch_set_target_queue")]
pub unsafe fn set_target<Q>(&self, queue: Option<&DispatchQueue>) {
let queue = match queue {
Some(queue) => queue,
None => ptr::null(),
};
sys::dispatch_set_target_queue(self, queue);
}
#[inline]
#[doc(alias = "dispatch_get_context")]
pub fn context(&self) -> *mut c_void {
unsafe { sys::dispatch_get_context(self) }
}
#[inline]
#[doc(alias = "dispatch_set_context")]
pub unsafe fn set_context(&self, context: *mut c_void) {
sys::dispatch_set_context(self, context);
}
}