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

Allow the thread-impl to be switched by configure option #5043

Merged
merged 3 commits into from
Oct 30, 2021

Conversation

kateinoigakukun
Copy link
Member

@kateinoigakukun kateinoigakukun commented Oct 28, 2021

This is a preparation for porting MRI to WebAssembly. In WebAssembly, there is no thread feature, so we need to add "no thread mode" in MRI.
That mode is not limited to the WebAssembly target, and other platforms will be able to use it also. This means a platform will have multiple candidates for thread implementation.

This patch allows the thread impl to be switched by configure option instead of selecting an implementation based on target platform. e.g. --with-thread=[pthread|win32]

tool/m4/ruby_thread_model.m4 Outdated Show resolved Hide resolved
tool/m4/ruby_thread_model.m4 Outdated Show resolved Hide resolved
@nobu
Copy link
Member

nobu commented Oct 28, 2021

Needs to add THREAD_IMPL_H and THREAD_IMPL_SRC to $(CONFIG_H) in win32/Makefile.sub.

@kateinoigakukun
Copy link
Member Author

@nobu Thank you for your quick review and advice! I was just investigating the ci failure on the Windows environment 😅

@kateinoigakukun kateinoigakukun marked this pull request as ready for review October 28, 2021 10:00
@kateinoigakukun
Copy link
Member Author

kateinoigakukun commented Oct 28, 2021

@ko1 suggested THREAD_MODEL should be renamed to THREAD_IMPL in slack because it's conflicted with a word that represents a model like M:N multithreading.

But THREAD_MODEL is already used in many places, so I want to rename them in another PR. What do you think, @ko1 and @nobu?

This is a first step to allow the thread-model implementation to be
switched by configure's option
This change adds --with-thread=IMPL option to the configure. If not
supplied, default implementation for each platform will be used.
@kateinoigakukun kateinoigakukun changed the title Allow the thread-model to be switched by configure option Allow the thread-impl to be switched by configure option Oct 28, 2021
@kateinoigakukun
Copy link
Member Author

I think this is ready to merge. Is there any other change request?

@ioquatix
Copy link
Member

I think this is a great idea and I'd like to see this expanded to CRuby in general - i.e. a Ruby build without threads. However, we might like to still support threading primitives. Can we also support Fibers in WebAssembly?

@kateinoigakukun
Copy link
Member Author

kateinoigakukun commented Oct 29, 2021

@ioquatix In my understanding, we can't port the current Fiber implementation to WebAssembly due to lack of context switching mechanism. Wasm can switch its execution context to only "back" in a call stack (e.g. like exception unwinding), and it can't jump "across" call stacks.

@ioquatix
Copy link
Member

Do you have access to threads? You can implement crappy fibers using threads with a execution exchange mechanism. There is a pthread implementation of Fibers in CRuby now.

@ioquatix
Copy link
Member

How does this relate to web assembly? https://emscripten.org/docs/api_reference/fiber.h.html

@hsbt
Copy link
Member

hsbt commented Oct 29, 2021

@kateinoigakukun Basically, We need to discuss the new feature on https://bugs.ruby-lang.org/ . I'm not sure this proposal is a trivial changes or bugfix.

@kateinoigakukun
Copy link
Member Author

kateinoigakukun commented Oct 30, 2021

Let me clarify that this patch does not add any new thread implementation in the language but just adds a way to switch implementations in configure. The none thread implementation will come with subsequent patches. I maybe need to create a new ticket for it.

Do you have access to threads? You can implement crappy fibers using threads with a execution exchange mechanism. There is a pthread implementation of Fibers in CRuby now.
How does this relate to web assembly? https://emscripten.org/docs/api_reference/fiber.h.html

Good point. Unfortunately, WebAssembly doesn't have a way to create or join threads by itself yet. The responsibility of creating and joining threads is deferred to the embedder of WebAssembly runtime. https://github.com/WebAssembly/threads/blob/main/proposals/threads/Overview.md

Emscipten depends on JavaScript embedders mechanism to provide such extra features like threads or socket, so it cannot be run with non-JavaScript environments like wasmtime or wasmer.
My project aims to provide JavaScript independent WebAssembly binary of MRI, so can't use the emscripten's API for this purpose.

@mame
Copy link
Member

mame commented Oct 30, 2021

I think that this change is almost for refactoring, and trivial.

As for WebAssembly, not only the implementation approach but also the feasibility are not yet clear. Let's discuss it in https://bugs.ruby-lang.org after a proof-of-concept is created.

@mame mame merged commit 4e0747a into ruby:master Oct 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
6 participants