Comments (7)
Simpler reproducer:
use std::process::exit;
use std::thread;
use dispatch::Queue;
use objc::runtime::{Object, BOOL, NO};
use objc::{class, msg_send};
fn activate_cocoa_multithreading() {
unsafe {
let thread: *mut Object = msg_send![class!(NSThread), new];
let _: () = msg_send![thread, start];
}
}
fn is_main_thread() -> bool {
unsafe {
let thread = class!(NSThread);
let b: BOOL = msg_send![thread, isMainThread];
b != NO
}
}
fn log(message: &str) {
println!(
"{message}: {:?} isMainThread? {}",
thread::current().id(),
is_main_thread()
);
}
fn main() {
activate_cocoa_multithreading();
log("main");
Queue::main().exec_async(|| {
log("exec_async");
exit(0);
});
unsafe {
dispatch::ffi::dispatch_main();
}
}
Which makes me suspect the issue is actually with dispatch_main
?
from rust-dispatch.
Even simpler reproducer showing that the problem is not with Objective-C in any way:
use std::process;
use std::thread;
use dispatch::Queue;
use libc::pthread_main_np;
fn log(message: &str) {
println!(
"{message}: {:?} / main={}",
thread::current().id(),
unsafe { pthread_main_np() } != 0,
);
}
fn main() {
log("main");
Queue::main().exec_async(|| {
log("exec_async");
process::exit(0);
});
unsafe {
dispatch::ffi::dispatch_main();
}
}
from rust-dispatch.
Yet again, in C this time:
#include <stdio.h>
#include <stdlib.h>
#include <dispatch/dispatch.h>
#include <pthread/pthread.h>
// #import <CoreFoundation/CoreFoundation.h>
int main() {
fprintf(stderr, "main: %i\n", pthread_main_np());
dispatch_async(dispatch_get_main_queue(), ^{
fprintf(stderr, "dispatch_async: %i\n", pthread_main_np());
sleep(1);
exit(0);
});
dispatch_main();
// CFRunLoopRun();
}
If you replace the dispatch_main
with CFRunLoopRun
, both threads will show up as the main thread, showing that the problem is indeed in dispatch_main
(which is a good reason for not making that safe in the future!).
from rust-dispatch.
Ok so it looks like an upstream issue, do you know where I can file a bug? It seems pretty serious that dispatch_main causes main queue to violate the most important contract it has.
from rust-dispatch.
I think there is some way, if you have an Apple Developer account, to file an issue with them? Or maybe you should just ask on the Apple forums? Or maybe, if you can translate the question into Swift, on their forums, they might know more?
Getting into contact with Apple devs is not something I've ever tried myself though, sorry I can't help you more on that.
from rust-dispatch.
I tried your C example but it does seem to do the right thing with dispatch_main (tried in GCC and clang)
$ gcc dispatch_is_broken.c -o dispatch_is_broken
$ ./dispatch_is_broken
main: 1
dispatch_async: 1
$ clang dispatch_is_broken.c -o dispatch_is_broken_clang
$./dispatch_is_broken_clang
main: 1
dispatch_async: 1
from rust-dispatch.
Huh, try recreating the nested example you gave first, but in C? Might be because I'm running the fairly old macOS 10.14 that I experience the bug without nesting the two queues?
from rust-dispatch.
Related Issues (12)
- Implement optimized dispatch_once HOT 1
- Make Once struct publicly available
- Link libdispatch on platforms where it isn't present in libSystem HOT 9
- Add support for dispatch sources
- Add support for QOS
- Calling dispatch_async twice results in the first closure's variables being replaced, if it hasn't been run yet HOT 4
- does it run on linux? HOT 1
- DISPATCH_QUEUE_CONCURRENT flagged as undefined behavior
- would you pls add a demo sample run on iOS ?
- Expose Queue's underlying dispatch_queue_t
- Missing MIT license text
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rust-dispatch.