Code Monkey home page Code Monkey logo

Comments (7)

madsmtm avatar madsmtm commented on July 17, 2024

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.

madsmtm avatar madsmtm commented on July 17, 2024

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.

madsmtm avatar madsmtm commented on July 17, 2024

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.

vseguip avatar vseguip commented on July 17, 2024

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.

madsmtm avatar madsmtm commented on July 17, 2024

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.

vseguip avatar vseguip commented on July 17, 2024

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.

madsmtm avatar madsmtm commented on July 17, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.