Prevent moving CanGc values between threads/tasks (#33902)

* Make CanGc non-sendable, and add documentation.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Update CanGc usage to fix usages that were moved between threads/tasks.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-10-18 13:26:54 -04:00 committed by GitHub
parent a58da5aa83
commit b85093ad74
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 69 additions and 89 deletions

View file

@ -1115,10 +1115,17 @@ impl Runnable {
}
#[derive(Clone, Copy, Debug)]
pub struct CanGc(());
/// A compile-time marker that there are operations that could trigger a JS garbage collection
/// operation within the current stack frame. It is trivially copyable, so it should be passed
/// as a function argument and reused when calling other functions whenever possible. Since it
/// is only meaningful within the current stack frame, it is impossible to move it to a different
/// thread or into a task that will execute asynchronously.
pub struct CanGc(std::marker::PhantomData<*mut ()>);
impl CanGc {
/// Create a new CanGc value, representing that a GC operation is possible within the
/// current stack frame.
pub fn note() -> CanGc {
CanGc(())
CanGc(std::marker::PhantomData)
}
}