Skip to content

Commit

Permalink
Implement Sync for yksom VM types
Browse files Browse the repository at this point in the history
This is needed because their trait objects are passed to a GC so must be
FSA-safe.
  • Loading branch information
jacob-hughes committed Nov 20, 2023
1 parent 8e39124 commit 676db21
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 8 deletions.
11 changes: 8 additions & 3 deletions src/lib/vm/objects/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,15 @@ pub struct Block {
pub method_stack_base: usize,
}

// By default, `Block` is not `FinalizerSafe` because it has fields to other
// `Gc`s. These fields would be unsound to access inside a `drop` method, so
// this explicit impl is needed to tell the compiler we are not doing that.
// The following impls are needed to allow the GC to finalise `Block`. By
// default, `Block` is not `FinalizerSafe` because it has fields to other `Gc`s.
// These fields would be unsound to access inside a `drop` method, so this
// explicit impl is needed to tell the compiler we are not doing that. The only
// time a `Block` is shared between threads is inside a finaliser, which is
// Sync-safe because we guarantee it will not be accessed at the same time as
// the main-thread.
unsafe impl FinalizerSafe for Block {}
unsafe impl Sync for Block {}

impl Obj for Block {
fn dyn_objtype(self: Gc<Self>) -> ObjType {
Expand Down
1 change: 1 addition & 0 deletions src/lib/vm/objects/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Class {
// thread, it is guaranteed that the finalizer thread will be the only thread
// accessing its data.
unsafe impl FinalizerSafe for Class {}
unsafe impl Sync for Class {}

impl Obj for Class {
fn dyn_objtype(self: Gc<Self>) -> ObjType {
Expand Down
1 change: 1 addition & 0 deletions src/lib/vm/objects/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct Method {
// method. This explicit impl is needed to tell the compiler we are not doing
// that.
unsafe impl FinalizerSafe for Method {}
unsafe impl Sync for Method {}

impl Obj for Method {
fn dyn_objtype(self: Gc<Self>) -> ObjType {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/vm/objects/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub use string_::String_;

use natrob::narrowable_alloy;
use std::gc::Gc;
use std::gc::ReferenceFree;

use crate::vm::{
core::VM,
Expand Down Expand Up @@ -80,7 +81,7 @@ impl ObjType {
/// The main SOM Object trait. Notice that code should almost never call these functions directly:
/// you should instead call the equivalent function in the `Val` struct.
#[narrowable_alloy(ThinObj)]
pub trait Obj: std::fmt::Debug + FinalizerSafe {
pub trait Obj: std::fmt::Debug + Send + Sync + FinalizerSafe + ReferenceFree {
/// What `ObjType` does this `Val` represent?
fn dyn_objtype(self: Gc<Self>) -> ObjType;

Expand Down
1 change: 1 addition & 0 deletions src/lib/vm/objects/string_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ pub struct String_ {
// thread, it is guaranteed that the finalizer thread will be the only thread
// accessing its data.
unsafe impl FinalizerSafe for String_ {}
unsafe impl Sync for String_ {}

impl Obj for String_ {
fn dyn_objtype(self: Gc<Self>) -> ObjType {
Expand Down
4 changes: 0 additions & 4 deletions src/lib/vm/val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ use num_enum::{IntoPrimitive, UnsafeFromPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use std::gc::Gc;

use std::gc::NoTrace;

use super::{
core::VM,
error::{VMError, VMErrorKind},
Expand Down Expand Up @@ -62,8 +60,6 @@ pub struct Val {
pub val: usize,
}

impl !NoTrace for Val {}

impl Val {
/// Create a new `Val` from an object that should be allocated on the heap.
pub fn from_obj<T: Obj + 'static>(obj: T) -> Val {
Expand Down

0 comments on commit 676db21

Please sign in to comment.