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

Run native blocking code #154

Open
anacrolix opened this issue Jul 21, 2024 · 2 comments
Open

Run native blocking code #154

anacrolix opened this issue Jul 21, 2024 · 2 comments

Comments

@anacrolix
Copy link

I'm trying out shuttle for https://github.com/anacrolix/possum. In some tests that are multithreaded, I need to call in to rusqlite code which then calls into C code which blocks the calling thread. After lots of trial and error I think I discovered that shuttle is not using native threads under the hood but its own kind of green threading. This doesn't play well with calling into code that hasn't been instrumented with shuttle primitives.

How can I call into that code? I was going to create a wrapper that spawned a native thread to run blocking code in rusqlite, then wait on the result from a shuttle mpsc channel but then I realised I can't send the result into a shuttle channel from a thread that wasn't created by shuttle.

@sarsko
Copy link
Contributor

sarsko commented Jul 22, 2024

It sounds to me like this would work if you were to use a std::sync::mpsc channel to send results from the spawned thread to the main thread which is running under Shuttle. You would do the call to your wrapper, which would spawn a thread and give you a channel to block on. Your main thread would then block on the receiving end of the channel until the spawned thread is ready with the result. Does this work or am I missing something?

@anacrolix
Copy link
Author

I don't think you can block on a std::sync::mpsc in shuttle, it will stall everything.

I ended up trying this:
https://github.com/anacrolix/possum/blob/a37c55a1c8a781c3dfd7e921e4088ba15b7f0fe2/src/concurrency/mod.rs#L32-L54
probably not ideal, but I do a non-blocking recv from a std::sync::mpsc channel, and tell shuttle to yield if it fails. It's essentially busy waiting, I hope shuttle's scheduler can handle this effectively. It does work though.

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