add ffi for rust's std thread api and use it from c++#48338
Open
add ffi for rust's std thread api and use it from c++#48338
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
After we made logging from C++ go through the Rust logger, I noticed cargo's test harness wasn't capturing the output of logging calls made from subthreads of C++ code. It seems to be a side effect of the test harness and maybe the
logcrate expecting some kind of Rust management of the thread for capturing to work. This means if C++ code spawns threads using the standard library and makes logging calls from those threads, the Rust logger ends up writing directly to stdout and cluttering the output ofcargo test.As a workaround, this PR adds an FFI and C++ wrapper around Rust's
std::threadand updates the C++ code to spawn threads using that instead of through the standard library. We aren't doing anything on the C++ side that would expect some kind of C++ management of threads, so I believe this should be fine. Thread locals are managed by pthreads and shouldn't care which language did the spawning.On the C++ side, I named the API
RustThreadinstead of simplyThreadorthreadto avoid conflicts with existing types. It's a little ugly but it also makes it very clear what's happening at the call sites, which can't hurt.