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

Using Mun in a threaded environment #299

Closed
baszalmstra opened this issue Jan 15, 2021 · 3 comments · Fixed by #370
Closed

Using Mun in a threaded environment #299

baszalmstra opened this issue Jan 15, 2021 · 3 comments · Fixed by #370
Assignees
Labels
pri: intermediate An issue resulting in non-critical functionality loss and no significant effect on usability type: feat New feature or request
Milestone

Comments

@baszalmstra
Copy link
Collaborator

The mun_runtime::Runtime is not Sync nor Send since the RuntimeBuilder::spawn function returns a Rc<RefCell<Runtime>>. This makes it hard to use in other programs that make use of threads. The reason we wrap the Runtime in an Rc is because the runtime (or better yet, the memory model) is not thread safe. Wrapping the Runtime in a mutex will also not work because the memory model is shared between other structs (like StructRef). Sending the Runtime to a thread and doing modifications while also accessing a StructRef from another thread will cause threading issues.

We've already talked about this a lot. We currently have no proper plan to support multithreading. However, our current solution is also not very ergonomic. I propose we implement a blocking solution that still doesnt do multithreading of the runtime but does allow the runtime and derived data to be shared across threads. In a later version we can then add proper multithreading hopefully using the same API we design now.

A straighforward solution is to wrap all memory modifying operations inside a mutex. This will block any operation performed by other threads but will enable the use of the Runtime and derived data to be send to different threads.

Curious to your thoughts!

@baszalmstra baszalmstra added type: perf Changes that improve performance pri: intermediate An issue resulting in non-critical functionality loss and no significant effect on usability labels Jan 15, 2021
@baszalmstra
Copy link
Collaborator Author

Looking at it closer, the StructRef is already safe because it is tied to the lifetime of the runtime. If we ensure that you can only use a rooted struct by first converting it to a StructRef (the RootedStruct::as_ref), which is again tied to the runtime, we can make the runtime Send. The as_ref method is currently unsafe. I would prefer to have an API like:

let rooted_struct:RootedStruct = // ...
let ref:StructRef<'r> = runtime.get_ref(rooted_struct).unwrap();
invoke_fn!("main", ref)

This involves making sure that the get_ref method can be safely called. WDYT @Wodann?

@Wodann
Copy link
Collaborator

Wodann commented Jan 19, 2021

IIRC, the API can be made safe if we can guarantee that the rooted struct was created in the runtime that we are using to convert to a StructRef. That could also be achieved by storing a "unique identifier" that matches that of a runtime.

@baszalmstra
Copy link
Collaborator Author

I modified the API the remove the need for Rc<RefCell<..>> in #370 , this makes the Runtime Send which should make it more friendly in a threaded/async environment.

@baszalmstra baszalmstra self-assigned this May 14, 2022
@Wodann Wodann added type: feat New feature or request and removed type: perf Changes that improve performance labels Dec 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pri: intermediate An issue resulting in non-critical functionality loss and no significant effect on usability type: feat New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants