Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multithreading is broken #650

Open
m-mueller678 opened this issue Dec 13, 2024 · 4 comments
Open

Multithreading is broken #650

m-mueller678 opened this issue Dec 13, 2024 · 4 comments

Comments

@m-mueller678
Copy link

When running this program with 2 CPUs, only the main thread and one additional thread get to run. Uncommenting the yield on the main thread fixes it. I would expect that all threads get to run, since all threads but the main thread continuously yield.

fn main(){
    for i in 0..{
        std::thread::spawn(move || {
            dbg!(i);
            loop{
                std::thread::yield_now();
            }
        });
        // std::thread::yield_now();
    }
}

qemu:

qemu-system-x86_64 -cpu host -enable-kvm -display none -smp 2 -m 8G -serial stdio -kernel ~/Downloads/hermit-loader-x86_64 -initrd target/x86_64-unknown-hermit/debug/hermit-rs-template
@mkroening
Copy link
Member

This is a side effect of Hermit having a cooperative scheduler and sys_spawn2 not yielding implicitly.

We could, of course, make sys_spawn2 implicitly sys_yield after spawning. This could be unfortunate, though, when combined with the fact that Hermit has no work stealing. That would mean a main thread that wants to spawn multiple threads onto the other cores could not do so without yielding on every spawn.

@m-mueller678, did you experience issues with this behavior in real applications, or only with these synthetic examples? Spawning an infinite number of threads without yielding implicitly in one form or another seems quite exotic to me.

@m-mueller678
Copy link
Author

I ran into this with tokio. I am looking at it right now, and it seems the issue is parking_lot_core using the generic implementation on hermit, which is just _mm_pause in a loop.

Do you think one of these would be more appropriate?

@m-mueller678
Copy link
Author

I want to port the redox parking lot implementation. Ideally, I want to use hermit_abi::futex_wait with an absolute deadline. For that, I'd need some way of extracting the hermit_abi::timespec from std::time::Instant.

The alternative is to get the current time via Instant::now, computing the time remaining to the deadline and then doing a relative futex_wait

@m-mueller678
Copy link
Author

yet another option would be to have variations of futex_wait that take Instant and Duration under std::os::hermit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants