Skip to content

Commit

Permalink
MSRV workaround DISPATCH_QUEUE_CONCURRENT
Browse files Browse the repository at this point in the history
  • Loading branch information
pronebird committed Dec 24, 2024
1 parent 49270f0 commit ee68d01
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
16 changes: 14 additions & 2 deletions crates/dispatch2/src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,11 @@ create_opaque_type!(dispatch_io_s, dispatch_io_t);
pub const DISPATCH_QUEUE_SERIAL: dispatch_queue_attr_t = core::ptr::null_mut();

/// A dispatch queue that executes blocks concurrently.
pub const DISPATCH_QUEUE_CONCURRENT: dispatch_queue_attr_t = {
pub static DISPATCH_QUEUE_CONCURRENT: ImmutableStatic<dispatch_queue_attr_t> = {
// Safety: immutable external definition
unsafe { &_dispatch_queue_attr_concurrent as *const _ as dispatch_queue_attr_t }
ImmutableStatic(unsafe {
&_dispatch_queue_attr_concurrent as *const _ as dispatch_queue_attr_t
})
};

pub const DISPATCH_APPLY_AUTO: dispatch_queue_t = core::ptr::null_mut();
Expand Down Expand Up @@ -251,3 +253,13 @@ pub extern "C" fn dispatch_get_main_queue() -> dispatch_queue_main_t {
// SAFETY: Always safe to get pointer from static, only needed for MSRV.
unsafe { addr_of!(_dispatch_main_q) as dispatch_queue_main_t }
}

/// Wrapper type for immutable static variables exported from C,
/// that are documented to be safe for sharing and passing between threads.
#[repr(transparent)]
#[derive(Debug)]
pub struct ImmutableStatic<T>(pub T);
// Safety: safety is guaranteed by the external type.
unsafe impl<T> Sync for ImmutableStatic<T> {}
// Safety: safety is guaranteed by the external type.
unsafe impl<T> Send for ImmutableStatic<T> {}
2 changes: 1 addition & 1 deletion crates/dispatch2/src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl From<QueueAttribute> for dispatch_queue_attr_t {
fn from(value: QueueAttribute) -> Self {
match value {
QueueAttribute::Serial => DISPATCH_QUEUE_SERIAL,
QueueAttribute::Concurrent => DISPATCH_QUEUE_CONCURRENT as *const _ as *mut _,
QueueAttribute::Concurrent => DISPATCH_QUEUE_CONCURRENT.0 as *const _ as *mut _,
_ => panic!("Unknown QueueAttribute value: {:?}", value),
}
}
Expand Down

0 comments on commit ee68d01

Please sign in to comment.