Use locked_read.

This commit is contained in:
Stephen Chung 2022-06-26 14:10:09 +08:00
parent 84b8e1ed87
commit 7068775f19
10 changed files with 35 additions and 92 deletions

View File

@ -1,7 +1,7 @@
//! Module implementing custom syntax for [`Engine`].
use crate::ast::Expr;
use crate::func::native::SendSync;
use crate::func::SendSync;
use crate::parser::ParseResult;
use crate::tokenizer::{is_valid_identifier, Token};
use crate::types::dynamic::Variant;

View File

@ -991,7 +991,7 @@ impl Engine {
if !name.contains(separator.as_ref()) {
if !module.is_indexed() {
// Index the module (making a clone copy if necessary) if it is not indexed
let mut module = crate::func::native::shared_take_or_clone(module);
let mut module = crate::func::shared_take_or_clone(module);
module.build_index();
root.insert(name.into(), module.into());
} else {
@ -1009,7 +1009,7 @@ impl Engine {
root.insert(sub_module.into(), m.into());
} else {
let m = root.remove(sub_module).expect("contains sub-module");
let mut m = crate::func::native::shared_take_or_clone(m);
let mut m = crate::func::shared_take_or_clone(m);
register_static_module_raw(m.sub_modules_mut(), remainder, module);
m.build_index();
root.insert(sub_module.into(), m.into());

View File

@ -615,8 +615,7 @@ impl AST {
#[cfg(not(feature = "no_function"))]
if !other.lib.is_empty() {
crate::func::native::shared_make_mut(&mut self.lib)
.merge_filtered(&other.lib, &_filter);
crate::func::shared_make_mut(&mut self.lib).merge_filtered(&other.lib, &_filter);
}
#[cfg(not(feature = "no_module"))]
@ -629,10 +628,8 @@ impl AST {
self.set_resolver(other.resolver.unwrap());
}
(_, _) => {
let resolver =
crate::func::native::shared_make_mut(self.resolver.as_mut().unwrap());
let other_resolver =
crate::func::native::shared_take_or_clone(other.resolver.unwrap());
let resolver = crate::func::shared_make_mut(self.resolver.as_mut().unwrap());
let other_resolver = crate::func::shared_take_or_clone(other.resolver.unwrap());
for (k, v) in other_resolver {
resolver.insert(k, crate::func::shared_take_or_clone(v));
}
@ -673,7 +670,7 @@ impl AST {
filter: impl Fn(FnNamespace, FnAccess, &str, usize) -> bool,
) -> &mut Self {
if !self.lib.is_empty() {
crate::func::native::shared_make_mut(&mut self.lib).retain_script_functions(filter);
crate::func::shared_make_mut(&mut self.lib).retain_script_functions(filter);
}
self
}

View File

@ -1222,7 +1222,7 @@ impl Engine {
let (mut target, _pos) =
self.search_namespace(scope, global, lib, this_ptr, first_expr, level)?;
if target.as_ref().is_read_only() {
if target.is_read_only() {
target = target.into_owned();
}

View File

@ -22,8 +22,8 @@ pub use hashing::{
combine_hashes, get_hasher,
};
pub use native::{
locked_read, locked_write, shared_make_mut, shared_take, shared_take_or_clone, shared_try_take,
FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,
locked_read, locked_write, shared_get_mut, shared_make_mut, shared_take, shared_take_or_clone,
shared_try_take, FnAny, FnPlugin, IteratorFn, Locked, NativeCallContext, SendSync, Shared,
};
pub use plugin::PluginFunction;
pub use register::RegisterNativeFunction;

View File

@ -307,12 +307,7 @@ impl FileModuleResolver {
let file_path = self.get_file_path(path, source_path);
if self.is_cache_enabled() {
#[cfg(not(feature = "sync"))]
let c = self.cache.borrow();
#[cfg(feature = "sync")]
let c = self.cache.read().unwrap();
if let Some(module) = c.get(&file_path) {
if let Some(module) = locked_read(&self.cache).get(&file_path) {
return Ok(module.clone());
}
}

View File

@ -1,5 +1,5 @@
use crate::eval::GlobalRuntimeState;
use crate::func::native::SendSync;
use crate::func::SendSync;
use crate::{Engine, Module, Position, RhaiResultOf, Shared, AST};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;

View File

@ -1343,7 +1343,7 @@ pub fn optimize_into_ast(
let lib2 = &[&lib2];
for fn_def in functions {
let mut fn_def = crate::func::native::shared_take_or_clone(fn_def);
let mut fn_def = crate::func::shared_take_or_clone(fn_def);
// Optimize the function body
let body = mem::take(&mut *fn_def.body);

View File

@ -1,7 +1,7 @@
//! Helper module which defines the [`Dynamic`] data type and the
//! [`Any`] trait to to allow custom type handling.
use crate::func::native::SendSync;
use crate::func::{locked_read, SendSync};
use crate::{reify, ExclusiveRange, FnPtr, ImmutableString, InclusiveRange, INT};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
@ -26,7 +26,7 @@ pub use instant::Instant;
const CHECKED: &str = "data type was checked";
mod private {
use crate::func::native::SendSync;
use crate::func::SendSync;
use std::any::Any;
/// A sealed trait that prevents other crates from implementing [`Variant`].
@ -384,12 +384,7 @@ impl Dynamic {
Union::Variant(ref v, ..) => (***v).type_id(),
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))]
Union::Shared(ref cell, ..) => (*cell.borrow()).type_id(),
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")]
Union::Shared(ref cell, ..) => (*cell.read().unwrap()).type_id(),
Union::Shared(ref cell, ..) => (*locked_read(cell)).type_id(),
}
}
/// Get the name of the type of the value held by this [`Dynamic`].
@ -463,12 +458,7 @@ impl Hash for Dynamic {
Union::FnPtr(ref f, ..) => f.hash(state),
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))]
Union::Shared(ref cell, ..) => (*cell.borrow()).hash(state),
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")]
Union::Shared(ref cell, ..) => (*cell.read().unwrap()).hash(state),
Union::Shared(ref cell, ..) => (*locked_read(cell)).hash(state),
Union::Variant(..) => unimplemented!("{} cannot be hashed", self.type_name()),
@ -1025,16 +1015,8 @@ impl Dynamic {
match self.0 {
Union::Shared(.., ReadOnly) => return true,
#[cfg(not(feature = "sync"))]
Union::Shared(ref cell, ..) => {
return match cell.borrow().access_mode() {
ReadWrite => false,
ReadOnly => true,
}
}
#[cfg(feature = "sync")]
Union::Shared(ref cell, ..) => {
return match cell.read().unwrap().access_mode() {
return match locked_read(cell).access_mode() {
ReadWrite => false,
ReadOnly => true,
}
@ -1066,12 +1048,7 @@ impl Dynamic {
Union::Map(..) => true,
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))]
Union::Shared(ref cell, ..) => cell.borrow().is_hashable(),
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")]
Union::Shared(ref cell, ..) => cell.read().unwrap().is_hashable(),
Union::Shared(ref cell, ..) => locked_read(cell).is_hashable(),
_ => false,
}
@ -1322,11 +1299,7 @@ impl Dynamic {
pub fn flatten_clone(&self) -> Self {
match self.0 {
#[cfg(not(feature = "no_closure"))]
#[cfg(not(feature = "sync"))]
Union::Shared(ref cell, ..) => cell.borrow().clone(),
#[cfg(not(feature = "no_closure"))]
#[cfg(feature = "sync")]
Union::Shared(ref cell, ..) => cell.read().unwrap().clone(),
Union::Shared(ref cell, ..) => locked_read(cell).clone(),
_ => self.clone(),
}
}
@ -1341,12 +1314,8 @@ impl Dynamic {
pub fn flatten(self) -> Self {
match self.0 {
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, ..) => crate::func::native::shared_try_take(cell).map_or_else(
#[cfg(not(feature = "sync"))]
|cell| cell.borrow().clone(),
#[cfg(feature = "sync")]
|cell| cell.read().unwrap().clone(),
#[cfg(not(feature = "sync"))]
Union::Shared(cell, ..) => crate::func::shared_try_take(cell).map_or_else(
|ref cell| locked_read(cell).clone(),
|value| value.into_inner(),
#[cfg(feature = "sync")]
|value| value.into_inner().unwrap(),
@ -1366,11 +1335,8 @@ impl Dynamic {
#[cfg(not(feature = "no_closure"))]
Union::Shared(ref mut cell, ..) => {
let cell = mem::take(cell);
*self = crate::func::native::shared_try_take(cell).map_or_else(
#[cfg(not(feature = "sync"))]
|cell| cell.borrow().clone(),
#[cfg(feature = "sync")]
|cell| cell.read().unwrap().clone(),
*self = crate::func::shared_try_take(cell).map_or_else(
|ref cell| locked_read(cell).clone(),
#[cfg(not(feature = "sync"))]
|value| value.into_inner(),
#[cfg(feature = "sync")]
@ -1422,10 +1388,7 @@ impl Dynamic {
match self.0 {
#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
let value = locked_read(cell);
if (*value).type_id() != TypeId::of::<T>()
&& TypeId::of::<Dynamic>() != TypeId::of::<T>()
@ -1457,7 +1420,7 @@ impl Dynamic {
match self.0 {
#[cfg(not(feature = "no_closure"))]
Union::Shared(ref cell, ..) => {
let guard = crate::func::native::locked_write(cell);
let guard = crate::func::locked_write(cell);
if (*guard).type_id() != TypeId::of::<T>()
&& TypeId::of::<Dynamic>() != TypeId::of::<T>()
@ -1772,11 +1735,8 @@ impl Dynamic {
match self.0 {
Union::Str(s, ..) => Ok(s),
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, ..) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
Union::Shared(ref cell, ..) => {
let value = locked_read(cell);
match value.0 {
Union::Str(ref s, ..) => Ok(s.clone()),
@ -1794,11 +1754,8 @@ impl Dynamic {
match self.0 {
Union::Array(a, ..) => Ok(*a),
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, ..) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
Union::Shared(ref cell, ..) => {
let value = locked_read(cell);
match value.0 {
Union::Array(ref a, ..) => Ok(a.as_ref().clone()),
@ -1832,11 +1789,8 @@ impl Dynamic {
.collect(),
Union::Blob(..) if TypeId::of::<T>() == TypeId::of::<u8>() => Ok(self.cast::<Vec<T>>()),
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, ..) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
Union::Shared(ref cell, ..) => {
let value = locked_read(cell);
match value.0 {
Union::Array(ref a, ..) => {
@ -1873,11 +1827,8 @@ impl Dynamic {
match self.0 {
Union::Blob(a, ..) => Ok(*a),
#[cfg(not(feature = "no_closure"))]
Union::Shared(cell, ..) => {
#[cfg(not(feature = "sync"))]
let value = cell.borrow();
#[cfg(feature = "sync")]
let value = cell.read().unwrap();
Union::Shared(ref cell, ..) => {
let value = locked_read(cell);
match value.0 {
Union::Blob(ref a, ..) => Ok(a.as_ref().clone()),

View File

@ -1,6 +1,6 @@
//! The `ImmutableString` type.
use crate::func::native::{shared_get_mut, shared_make_mut, shared_take};
use crate::func::{shared_get_mut, shared_make_mut, shared_take};
use crate::{Shared, SmartString};
#[cfg(feature = "no_std")]
use std::prelude::v1::*;