jhpratt / num_threads

Obtain the number of threads in the current process
https://docs.rs/num_threads
Apache License 2.0
12 stars 7 forks source link

Add support for Android and iOS #7

Closed complexspaces closed 2 years ago

complexspaces commented 2 years ago

This PR adds support for compiling and running on Android and iOS devices.

Test Code

[package]
name = "companion"
version = "0.1.0"
edition = "2021"

[lib]
crate-type = ["cdylib"]

[dependencies]
android_logger = "0.10"
log = "0.4"

num_threads = { path = "../../github/num_threads" }
use std::time::Duration;

#[no_mangle]
pub extern "C" fn do_something() {
    android_logger::init_once(
        android_logger::Config::default().with_min_level(log::Level::Trace),
    );

    log::info!("trying to get thread number");

    for i in 0..10 {
        log::info!("{:?} threads running", num_threads::num_threads());

        if i % 2 == 0 {
            std::thread::spawn(|| {
                std::thread::sleep(Duration::from_secs(5));
            });
        }
    } 

    std::thread::sleep(Duration::from_secs(60));
    log::info!("{:?} threads running, closing", num_threads::num_threads());
}

Android

This patch is very simple, as the existing Linux implementation already works great inside the Android app sandbox.

This was tested on both a physical Android device and in an emulator by compiling, then loading, a minimal Rust dylib via the JNI.

Output (note the thread count is always high because of the JVM in the process):

2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: trying to get thread number
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(18) threads running
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(19) threads running
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(19) threads running
2022-03-14 11:16:47.270 8185-8185/com.library.rustlibtesting I/companion: Some(20) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(20) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(21) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(21) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(22) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(22) threads running
2022-03-14 11:16:47.272 8185-8185/com.library.rustlibtesting I/companion: Some(23) threads running
2022-03-14 11:17:47.273 8185-8185/com.library.rustlibtesting I/companion: Some(14) threads running, closing

iOS

The patch for iOS, like Android, is very simple because the MacOS implementation can be reused entirely with no changes.

This was tested inside the iOS simulator and on a physical iPad.

Output (the thread count here is high too because of the Swift runtime):

Trying to get thread number
Some(9) threads running
Some(10) threads running
Some(10) threads running
Some(11) threads running
Some(11) threads running
Some(12) threads running
Some(12) threads running
Some(13) threads running
Some(13) threads running
Some(14) threads running
Some(4) threads running, exiting
jhpratt commented 2 years ago

Awesome! Diff looks good. Would you mind adding CI runs for this? It should only be a matter of adding targets here, just like there is for FreeBSD.

You might have to rebase because of a commit I just pushed up; I realized while typing this comment that there was some extraneous stuff in the CI file. I don't think you'll have to if you append the new runs, though.

complexspaces commented 2 years ago

I'm not sure why the iOS typechecking job is failing. My best guess is that Rust 1.28 didn't support the aarch64-apple-ios target since rustup doesn't appear to recognize it at all.

jhpratt commented 2 years ago

Hmm…that's probably correct. Let me do some digging to see what version it was introduced in.

jhpratt commented 2 years ago

I can't immediately determine what version it was introduced in, as it's not mentioned in the release notes. However, it looks like it could only be built on MacOS prior to 1.56.

How familiar are you with GitHub Actions? I think the best way would be to run on macos-latest for this specific target. I can try this if you're not too familiar.

complexspaces commented 2 years ago

I can give this a try :thumbsup:

complexspaces commented 2 years ago

CI looks green to me now. Please let me know if there are tweaks you'd like made to it.

jhpratt commented 2 years ago

The MacOS check doesn't actually need to be on MacOS, as it works as expected on the Ubuntu host. But that's not a big deal. Thanks!