dispatcher.rs

  1#![allow(non_upper_case_globals)]
  2#![allow(non_camel_case_types)]
  3#![allow(non_snake_case)]
  4
  5use crate::{PlatformDispatcher, TaskLabel};
  6use async_task::Runnable;
  7use objc::{
  8    class, msg_send,
  9    runtime::{BOOL, YES},
 10    sel, sel_impl,
 11};
 12use parking::{Parker, Unparker};
 13use parking_lot::Mutex;
 14use std::{
 15    ffi::c_void,
 16    ptr::{NonNull, addr_of},
 17    sync::Arc,
 18    time::Duration,
 19};
 20
 21/// All items in the generated file are marked as pub, so we're gonna wrap it in a separate mod to prevent
 22/// these pub items from leaking into public API.
 23pub(crate) mod dispatch_sys {
 24    include!(concat!(env!("OUT_DIR"), "/dispatch_sys.rs"));
 25}
 26
 27use dispatch_sys::*;
 28pub(crate) fn dispatch_get_main_queue() -> dispatch_queue_t {
 29    addr_of!(_dispatch_main_q) as *const _ as dispatch_queue_t
 30}
 31
 32pub(crate) struct MacDispatcher {
 33    parker: Arc<Mutex<Parker>>,
 34}
 35
 36impl Default for MacDispatcher {
 37    fn default() -> Self {
 38        Self::new()
 39    }
 40}
 41
 42impl MacDispatcher {
 43    pub fn new() -> Self {
 44        MacDispatcher {
 45            parker: Arc::new(Mutex::new(Parker::new())),
 46        }
 47    }
 48}
 49
 50impl PlatformDispatcher for MacDispatcher {
 51    fn is_main_thread(&self) -> bool {
 52        let is_main_thread: BOOL = unsafe { msg_send![class!(NSThread), isMainThread] };
 53        is_main_thread == YES
 54    }
 55
 56    fn dispatch(&self, runnable: Runnable, _: Option<TaskLabel>) {
 57        unsafe {
 58            dispatch_async_f(
 59                dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH.try_into().unwrap(), 0),
 60                runnable.into_raw().as_ptr() as *mut c_void,
 61                Some(trampoline),
 62            );
 63        }
 64    }
 65
 66    fn dispatch_on_main_thread(&self, runnable: Runnable) {
 67        unsafe {
 68            dispatch_async_f(
 69                dispatch_get_main_queue(),
 70                runnable.into_raw().as_ptr() as *mut c_void,
 71                Some(trampoline),
 72            );
 73        }
 74    }
 75
 76    fn dispatch_after(&self, duration: Duration, runnable: Runnable) {
 77        unsafe {
 78            let queue =
 79                dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH.try_into().unwrap(), 0);
 80            let when = dispatch_time(DISPATCH_TIME_NOW as u64, duration.as_nanos() as i64);
 81            dispatch_after_f(
 82                when,
 83                queue,
 84                runnable.into_raw().as_ptr() as *mut c_void,
 85                Some(trampoline),
 86            );
 87        }
 88    }
 89
 90    fn park(&self, timeout: Option<Duration>) -> bool {
 91        if let Some(timeout) = timeout {
 92            self.parker.lock().park_timeout(timeout)
 93        } else {
 94            self.parker.lock().park();
 95            true
 96        }
 97    }
 98
 99    fn unparker(&self) -> Unparker {
100        self.parker.lock().unparker()
101    }
102}
103
104extern "C" fn trampoline(runnable: *mut c_void) {
105    let task = unsafe { Runnable::<()>::from_raw(NonNull::new_unchecked(runnable as *mut ())) };
106    task.run();
107}