Skip to content

Commit

Permalink
Make Send and Sync impls an opt-in feature
Browse files Browse the repository at this point in the history
  • Loading branch information
ianks committed Nov 29, 2023
1 parent a6ff9e0 commit a4f59ca
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 15 deletions.
8 changes: 7 additions & 1 deletion ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ tokio = ["dep:tokio", "dep:async-timer"]
all-arch = ["wasmtime/all-arch"]
ruby-api = []
winch = ["wasmtime/winch"]
unsafe-impl-sync = []
unsafe-impl-send = []
unsafe-impl-send-sync = ["unsafe-impl-send", "unsafe-impl-sync"]

[dependencies]
lazy_static = "1.4.0"
magnus = { version = "0.6", features = ["rb-sys", "deprecated-send-sync-value"] }
magnus = { version = "0.6", features = [
"rb-sys",
"deprecated-send-sync-value",
] }
rb-sys = { version = "*", default-features = false, features = [
"stable-api-compiled-fallback",
] }
Expand Down
17 changes: 17 additions & 0 deletions ext/src/helpers/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,20 @@ macro_rules! define_rb_intern {
)*
};
}

/// Boilerplate for defining send and sync on a Magnus struct when the feature is enabled.
#[macro_export]
macro_rules! unsafe_impl_send_sync {
($struct:ident) => {
#[cfg(feature = "unsafe-impl-send")]
unsafe impl Send for $struct {}
#[cfg(feature = "unsafe-impl-sync")]
unsafe impl Sync for $struct {}
};
($struct:ident <'_>) => {
#[cfg(feature = "unsafe-impl-send")]
unsafe impl Send for $struct<'_> {}
#[cfg(feature = "unsafe-impl-sync")]
unsafe impl Sync for $struct<'_> {}
};
}
3 changes: 2 additions & 1 deletion ext/src/ruby_api/caller.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::{convert::WrapWasmtimeType, externals::Extern, root, store::StoreData};
use crate::{error, unsafe_impl_send_sync};
use magnus::{
class,
gc::{Compactor, Marker},
Expand Down Expand Up @@ -143,7 +144,7 @@ impl<'a> Caller<'a> {
}
}

unsafe impl Send for Caller<'_> {}
unsafe_impl_send_sync!(Caller);

pub fn init() -> Result<(), Error> {
let klass = root().define_class("Caller", class::object())?;
Expand Down
6 changes: 3 additions & 3 deletions ext/src/ruby_api/convert.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{define_rb_intern, err, error, helpers::SymbolEnum};
use crate::{define_rb_intern, err, error, helpers::SymbolEnum, unsafe_impl_send_sync};
use lazy_static::lazy_static;
use magnus::{prelude::*, Error, IntoValue, RArray, Ruby, Symbol, TryConvert, TypedData, Value};
use wasmtime::{ExternRef, Val, ValType};
Expand Down Expand Up @@ -95,8 +95,8 @@ impl From<Value> for ExternRefValue {
Self(v)
}
}
unsafe impl Send for ExternRefValue {}
unsafe impl Sync for ExternRefValue {}

unsafe_impl_send_sync!(ExternRefValue);

pub trait ToExtern {
fn to_extern(&self, ruby: &Ruby) -> Result<wasmtime::Extern, Error>;
Expand Down
4 changes: 2 additions & 2 deletions ext/src/ruby_api/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
root,
store::{Store, StoreContextValue, StoreData},
};
use crate::err;
use crate::{err, unsafe_impl_send_sync};
use magnus::{
class, function, gc::Marker, method, prelude::*, scan_args, typed_data::Obj, DataTypeFunctions,
Error, Object, RArray, RHash, RString, Ruby, TryConvert, TypedData, Value,
Expand All @@ -22,7 +22,7 @@ pub struct Instance {
store: Obj<Store>,
}

unsafe impl Send for Instance {}
unsafe_impl_send_sync!(Instance);

impl DataTypeFunctions for Instance {
fn mark(&self, marker: &Marker) {
Expand Down
4 changes: 2 additions & 2 deletions ext/src/ruby_api/linker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::{
root,
store::{Store, StoreContextValue, StoreData},
};
use crate::{define_rb_intern, err, error};
use crate::{define_rb_intern, err, error, unsafe_impl_send_sync};
use magnus::{
block::Proc, class, function, gc::Marker, method, prelude::*, scan_args, scan_args::scan_args,
typed_data::Obj, DataTypeFunctions, Error, Object, RArray, RHash, RString, Ruby, TypedData,
Expand All @@ -32,7 +32,7 @@ pub struct Linker {
has_wasi: bool,
}

unsafe impl Send for Linker {}
unsafe_impl_send_sync!(Linker);

impl DataTypeFunctions for Linker {
fn mark(&self, marker: &Marker) {
Expand Down
5 changes: 3 additions & 2 deletions ext/src/ruby_api/memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::{
root,
store::{Store, StoreContextValue},
};
use crate::{define_rb_intern, error};
use crate::{define_rb_intern, error, unsafe_impl_send_sync};
use magnus::{
class, function, gc::Marker, method, r_string::RString, scan_args, typed_data::Obj,
DataTypeFunctions, Error, Module as _, Object, Ruby, TypedData, Value,
Expand All @@ -31,12 +31,13 @@ pub struct Memory<'a> {
inner: ManuallyTracked<MemoryImpl>,
}

unsafe_impl_send_sync!(Memory<'_>);

impl DataTypeFunctions for Memory<'_> {
fn mark(&self, marker: &Marker) {
self.store.mark(marker)
}
}
unsafe impl Send for Memory<'_> {}

impl<'a> Memory<'a> {
/// @yard
Expand Down
2 changes: 1 addition & 1 deletion ext/src/ruby_api/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub struct Module {
_track_memory_usage: ManuallyTracked<()>,
}

// Needed for ManuallyTracked
#[cfg(feature = "unsafe-impl-send")]
unsafe impl Send for Module {}

impl Module {
Expand Down
5 changes: 2 additions & 3 deletions ext/src/ruby_api/store.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::errors::wasi_exit_error;
use super::{caller::Caller, engine::Engine, root, trap::Trap, wasi_ctx_builder::WasiCtxBuilder};
use crate::{define_rb_intern, error};
use crate::{define_rb_intern, error, unsafe_impl_send_sync};
use magnus::rb_sys::AsRawValue;
use magnus::Class;
use magnus::{
Expand Down Expand Up @@ -96,8 +96,7 @@ impl DataTypeFunctions for Store {
}
}

unsafe impl Send for Store {}
unsafe impl Send for StoreData {}
unsafe_impl_send_sync!(Store);

impl Store {
/// @yard
Expand Down

0 comments on commit a4f59ca

Please sign in to comment.