Skip to main content

dispatch/
ffi.rs

1#![allow(missing_docs)]
2#![allow(non_camel_case_types)]
3
4use std::os::raw::{c_char, c_long, c_ulong, c_void};
5
6#[repr(C)]
7pub struct dispatch_object_s { _private: [u8; 0] }
8
9// dispatch_block_t
10pub type dispatch_function_t = extern fn(*mut c_void);
11pub type dispatch_semaphore_t = *mut dispatch_object_s;
12pub type dispatch_group_t = *mut dispatch_object_s;
13pub type dispatch_object_t = *mut dispatch_object_s;
14pub type dispatch_once_t = c_long;
15pub type dispatch_queue_t = *mut dispatch_object_s;
16pub type dispatch_time_t = u64;
17// dispatch_source_type_t
18// dispatch_fd_t
19// dispatch_data_t
20// dispatch_data_applier_t
21// dispatch_io_t
22// dispatch_io_handler_t
23// dispatch_io_type_t
24// dispatch_io_close_flags_t
25// dispatch_io_interval_flags_t
26pub type dispatch_queue_attr_t = *const dispatch_object_s;
27
28#[cfg_attr(any(target_os = "macos", target_os = "ios"),
29           link(name = "System", kind = "dylib"))]
30#[cfg_attr(not(any(target_os = "macos", target_os = "ios")),
31           link(name = "dispatch", kind = "dylib"))]
32extern {
33    static _dispatch_main_q: dispatch_object_s;
34    static _dispatch_queue_attr_concurrent: dispatch_object_s;
35
36    pub fn dispatch_get_global_queue(identifier: c_long, flags: c_ulong) -> dispatch_queue_t;
37    pub fn dispatch_queue_create(label: *const c_char, attr: dispatch_queue_attr_t) -> dispatch_queue_t;
38    // dispatch_queue_attr_t dispatch_queue_attr_make_with_qos_class ( dispatch_queue_attr_t attr, dispatch_qos_class_t qos_class, int relative_priority );
39    pub fn dispatch_queue_get_label(queue: dispatch_queue_t) -> *const c_char;
40    pub fn dispatch_set_target_queue(object: dispatch_object_t, queue: dispatch_queue_t);
41    pub fn dispatch_main();
42
43    // void dispatch_async ( dispatch_queue_t queue, dispatch_block_t block );
44    pub fn dispatch_async_f(queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
45    // void dispatch_sync ( dispatch_queue_t queue, dispatch_block_t block );
46    pub fn dispatch_sync_f(queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
47    // void dispatch_after ( dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block );
48    pub fn dispatch_after_f(when: dispatch_time_t, queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
49    // void dispatch_apply ( size_t iterations, dispatch_queue_t queue, void (^block)(size_t) );
50    pub fn dispatch_apply_f(iterations: usize, queue: dispatch_queue_t, context: *mut c_void, work: extern fn(*mut c_void, usize));
51    // void dispatch_once ( dispatch_once_t *predicate, dispatch_block_t block );
52    pub fn dispatch_once_f(predicate: *mut dispatch_once_t, context: *mut c_void, function: dispatch_function_t);
53
54    // void dispatch_group_async ( dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block );
55    pub fn dispatch_group_async_f(group: dispatch_group_t, queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
56    pub fn dispatch_group_create() -> dispatch_group_t;
57    pub fn dispatch_group_enter(group: dispatch_group_t);
58    pub fn dispatch_group_leave(group: dispatch_group_t);
59    // void dispatch_group_notify ( dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block );
60    pub fn dispatch_group_notify_f(group: dispatch_group_t, queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
61    pub fn dispatch_group_wait(group: dispatch_group_t, timeout: dispatch_time_t) -> c_long;
62
63    pub fn dispatch_get_context(object: dispatch_object_t) -> *mut c_void;
64    pub fn dispatch_release(object: dispatch_object_t);
65    pub fn dispatch_resume(object: dispatch_object_t);
66    pub fn dispatch_retain(object: dispatch_object_t);
67    pub fn dispatch_set_context(object: dispatch_object_t, context: *mut c_void);
68    pub fn dispatch_set_finalizer_f(object: dispatch_object_t, finalizer: dispatch_function_t);
69    pub fn dispatch_suspend(object: dispatch_object_t);
70
71    pub fn dispatch_semaphore_create(value: c_long) -> dispatch_semaphore_t;
72    pub fn dispatch_semaphore_signal(dsema: dispatch_semaphore_t) -> c_long;
73    pub fn dispatch_semaphore_wait(dsema: dispatch_semaphore_t, timeout: dispatch_time_t) -> c_long;
74
75    // void dispatch_barrier_async ( dispatch_queue_t queue, dispatch_block_t block );
76    pub fn dispatch_barrier_async_f(queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
77    // void dispatch_barrier_sync ( dispatch_queue_t queue, dispatch_block_t block );
78    pub fn dispatch_barrier_sync_f(queue: dispatch_queue_t, context: *mut c_void, work: dispatch_function_t);
79
80    // void dispatch_source_cancel ( dispatch_source_t source );
81    // dispatch_source_t dispatch_source_create ( dispatch_source_type_t type, uintptr_t handle, unsigned long mask, dispatch_queue_t queue );
82    // unsigned long dispatch_source_get_data ( dispatch_source_t source );
83    // uintptr_t dispatch_source_get_handle ( dispatch_source_t source );
84    // unsigned long dispatch_source_get_mask ( dispatch_source_t source );
85    // void dispatch_source_merge_data ( dispatch_source_t source, unsigned long value );
86    // void dispatch_source_set_registration_handler ( dispatch_source_t source, dispatch_block_t handler );
87    // void dispatch_source_set_registration_handler_f ( dispatch_source_t source, dispatch_function_t handler );
88    // void dispatch_source_set_cancel_handler ( dispatch_source_t source, dispatch_block_t handler );
89    // void dispatch_source_set_cancel_handler_f ( dispatch_source_t source, dispatch_function_t handler );
90    // void dispatch_source_set_event_handler ( dispatch_source_t source, dispatch_block_t handler );
91    // void dispatch_source_set_event_handler_f ( dispatch_source_t source, dispatch_function_t handler );
92    // void dispatch_source_set_timer ( dispatch_source_t source, dispatch_time_t start, uint64_t interval, uint64_t leeway );
93    // long dispatch_source_testcancel ( dispatch_source_t source );
94
95    // void dispatch_read ( dispatch_fd_t fd, size_t length, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error) );
96    // void dispatch_write ( dispatch_fd_t fd, dispatch_data_t data, dispatch_queue_t queue, void (^handler)(dispatch_data_t data, int error) );
97
98    // dispatch_io_t dispatch_io_create ( dispatch_io_type_t type, dispatch_fd_t fd, dispatch_queue_t queue, void (^cleanup_handler)(int error) );
99    // dispatch_io_t dispatch_io_create_with_path ( dispatch_io_type_t type, const char *path, int oflag, mode_t mode, dispatch_queue_t queue, void (^cleanup_handler)(int error) );
100    // dispatch_io_t dispatch_io_create_with_io ( dispatch_io_type_t type, dispatch_io_t io, dispatch_queue_t queue, void (^cleanup_handler)(int error) );
101    // void dispatch_io_read ( dispatch_io_t channel, off_t offset, size_t length, dispatch_queue_t queue, dispatch_io_handler_t io_handler );
102    // void dispatch_io_write ( dispatch_io_t channel, off_t offset, dispatch_data_t data, dispatch_queue_t queue, dispatch_io_handler_t io_handler );
103    // void dispatch_io_close ( dispatch_io_t channel, dispatch_io_close_flags_t flags );
104    // void dispatch_io_barrier ( dispatch_io_t channel, dispatch_block_t barrier );
105    // void dispatch_io_set_high_water ( dispatch_io_t channel, size_t high_water );
106    // void dispatch_io_set_low_water ( dispatch_io_t channel, size_t low_water );
107    // void dispatch_io_set_interval ( dispatch_io_t channel, uint64_t interval, dispatch_io_interval_flags_t flags );
108    // dispatch_fd_t dispatch_io_get_descriptor ( dispatch_io_t channel );
109
110    // dispatch_data_t dispatch_data_create ( const void *buffer, size_t size, dispatch_queue_t queue, dispatch_block_t destructor );
111    // size_t dispatch_data_get_size ( dispatch_data_t data );
112    // dispatch_data_t dispatch_data_create_map ( dispatch_data_t data, const void **buffer_ptr, size_t *size_ptr );
113    // dispatch_data_t dispatch_data_create_concat ( dispatch_data_t data1, dispatch_data_t data2 );
114    // dispatch_data_t dispatch_data_create_subrange ( dispatch_data_t data, size_t offset, size_t length );
115    // bool dispatch_data_apply ( dispatch_data_t data, dispatch_data_applier_t applier );
116    // dispatch_data_t dispatch_data_copy_region ( dispatch_data_t data, size_t location, size_t *offset_ptr );
117
118    pub fn dispatch_time(when: dispatch_time_t, delta: i64) -> dispatch_time_t;
119    // dispatch_time_t dispatch_walltime( const struct timespec *when, int64_t delta);
120
121    // void dispatch_queue_set_specific ( dispatch_queue_t queue, const void *key, void *context, dispatch_function_t destructor );
122    // void * dispatch_queue_get_specific ( dispatch_queue_t queue, const void *key );
123    // void * dispatch_get_specific ( const void *key );
124
125    // dispatch_block_t dispatch_block_create(dispatch_block_flags_t flags, dispatch_block_t block);
126    // dispatch_block_t dispatch_block_create_with_qos_class(dispatch_block_flags_t flags, dispatch_qos_class_t qos_class, int relative_priority, dispatch_block_t block);
127    // void dispatch_block_perform(dispatch_block_flags_t flags, dispatch_block_t block);
128    // long dispatch_block_wait(dispatch_block_t block, dispatch_time_t timeout);
129    // dispatch_block_notify(dispatch_block_t block, dispatch_queue_t queue, dispatch_block_t notification_block);
130    // void dispatch_block_cancel(dispatch_block_t block);
131    // long dispatch_block_testcancel(dispatch_block_t block);
132}
133
134pub fn dispatch_get_main_queue() -> dispatch_queue_t {
135    unsafe { &_dispatch_main_q as *const _ as dispatch_queue_t }
136}
137
138pub const DISPATCH_QUEUE_SERIAL: dispatch_queue_attr_t = 0 as dispatch_queue_attr_t;
139pub static DISPATCH_QUEUE_CONCURRENT: &'static dispatch_object_s = unsafe { &_dispatch_queue_attr_concurrent };
140
141pub const DISPATCH_QUEUE_PRIORITY_HIGH: c_long       = 2;
142pub const DISPATCH_QUEUE_PRIORITY_DEFAULT: c_long    = 0;
143pub const DISPATCH_QUEUE_PRIORITY_LOW: c_long        = -2;
144pub const DISPATCH_QUEUE_PRIORITY_BACKGROUND: c_long = -1 << 15;
145
146pub const DISPATCH_TIME_NOW: dispatch_time_t     = 0;
147pub const DISPATCH_TIME_FOREVER: dispatch_time_t = !0;
148
149#[cfg(test)]
150mod tests {
151    use super::*;
152
153    #[test]
154    fn test_ffi_serial_queue() {
155        use std::os::raw::c_void;
156        use std::ptr;
157
158        extern fn serial_queue_test_add(num: *mut c_void) {
159            unsafe {
160                *(num as *mut u32) = 1;
161            }
162        }
163
164        let mut num: u32 = 0;
165        let num_ptr: *mut u32 = &mut num;
166        unsafe {
167            let q = dispatch_queue_create(ptr::null(), DISPATCH_QUEUE_SERIAL);
168            dispatch_sync_f(q, num_ptr as *mut c_void, serial_queue_test_add);
169            dispatch_release(q);
170        }
171        assert!(num == 1);
172    }
173}