ssheldon / rust-dispatch Goto Github PK
View Code? Open in Web Editor NEWRust wrapper for Apple's Grand Central Dispatch.
Home Page: https://crates.io/crates/dispatch
Rust wrapper for Apple's Grand Central Dispatch.
Home Page: https://crates.io/crates/dispatch
error[E0080]: this static likely exhibits undefined behavior
--> /Users/mehcode/.cargo/registry/src/github.com-1ecc6299db9ec823/dispatch-0.1.3/src/ffi.rs:138:1
|
138 | pub static DISPATCH_QUEUE_CONCURRENT: &'static dispatch_object_s = unsafe { &_dispatch_queue_attr_concurrent };
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ type validation failed: encountered a value of an uninhabited type at .<deref>
|
= note: The rules on what exactly is undefined behavior aren't clear, so this check might be overzealous. Please open an issue on the rust compiler repository if you believe it should not be considered undefined behavior
❯ rustc --version
rustc 1.31.0-nightly (424a749a0 2018-10-21)
Apple recently open sourced Swift, and brought with it an up-to-date libdispatch that they intend to make work correctly on Linux.
I'm wondering whether it would be worthwhile to rebase onto that target?
I guess they use shared state?
It appears that you currently can't set the priority for the queue, and that it will just use the default priority instead. It would be great to allow for customization of this.
There's a neat little Once
struct that wraps dispatch_once
, but it's not usable since there's no way to initialize a const UnsafeCell
.
Once const fns are stabilized in rust-lang/rust#24111, the Once::new
method can be marked const and the struct will be usable.
dispatch_once_f
is redefined as:
void _dispatch_once_f(dispatch_once_t *predicate, void *context, dispatch_function_t function)
{
if (__builtin_expect(*predicate, ~0l) != ~0l) {
dispatch_once_f(predicate, context, function);
}
}
This requires the expect builtin, which isn't exposed as an intrinsic in Rust. Implementing this would be possible if rust-lang/rfcs#918 is accepted and implemented.
It would be nice if this library could support dispatch sources.
I've created a more or less minimal example (based on the dispatch example) which shows that using exec_async on the main queue causes the closure to be executed on a different thread than the main thread:
extern crate dispatch;
use std::thread;
use std::process::exit;
use dispatch::{Queue, QueuePriority};
use objc::{{sel, sel_impl, msg_send, class}, runtime::{BOOL, YES, NO}};
pub fn is_main_thread() -> bool {
unsafe {
let thread = class!(NSThread);
let b : BOOL = msg_send![thread, isMainThread];
match b {
YES => true,
NO => false
}
}
}
fn test_on_main(queue: Queue) {
queue.clone().exec_async(move || {
let main = Queue::main();
main.exec_async(|| {
println!("Running from {:?} isMainThread? {}", thread::current().name(), is_main_thread());
exit(0);
});
});
}
fn main() {
// Read from stdin on a background queue so that the main queue is free
// to handle other events. All printing still occurs through the main
// queue to avoid jumbled output.
cacao::utils::activate_cocoa_multithreading();
println!("Initial Thread {:?} isMainThread? {}", thread::current().name(), is_main_thread());
test_on_main(Queue::global(QueuePriority::Default));
unsafe {
dispatch::ffi::dispatch_main();
}
}
Expected output:
Initial Thread ThreadId(1) isMainThread? true
This is another queue can execute on any thread ThreadId(X) isMainThread? false
Running from ThreadId(1) isMainThread? true
Actual output:
Initial Thread ThreadId(1) isMainThread? true
This is another queue can execute on any thread ThreadId(2) isMainThread? false
Running from ThreadId(3) isMainThread? false
Would be useful if Queue
exposed a method that returned ptr
, for use with other FFI code.
This crate and repo don't have the MIT license text, which is a requirement for the MIT license.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.