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

Multi-threading possible? #48

Open
Frawstcrabs opened this issue Feb 12, 2021 · 1 comment
Open

Multi-threading possible? #48

Frawstcrabs opened this issue Feb 12, 2021 · 1 comment
Assignees

Comments

@Frawstcrabs
Copy link

In the README.md of the repo, it says that the lib only supports single-threaded applications, but given that the inner type of a Gc pointer now requires Send and Sync, I wanted to check if this was still the case.

@jacob-hughes
Copy link
Contributor

jacob-hughes commented Feb 12, 2021

Good question. Multi-threaded Rust is still not supported, no. Send and Sync are a requirement for single-threaded applications. This is because the collector finalizes values off the main thread. Consider the following (untested) example:

struct S {
   inner: Rc<String>
}

impl Drop for S {
   fn drop(&mut self) {
     // deref a non-send, non-sync field. This must be done from the same thread.
     println("Hello {}", self.inner);
  }
}

fn main() {
   let rc = Rc::new(String::from("Hello World"));
   let s = Gc::new(Rc::clone(rc));

   // 1. gc collection happens, `s` is finalized off-thread, calling  S::drop. 
   // 2. The drop impl deref's a `Rc<String>` off the main thread. This is UB.

}

This example is somewhat contrived, but it's there to illustrate that the Send + Sync constraint exists because of how values are Droped by the collector.

For multi-threading support, we still need a way to intercept pthread_create calls in order to register each thread's call stack with the collector. Until this is in place it will kinda-sorta work, but it is unsound because eventually the GC will miss a pointer from a root on a non-registered thread's call stack.

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