feat: let Shared: Send + Sync

This commit is contained in:
Shunsuke Shibayama 2023-05-27 20:41:28 +09:00
parent 4d9800716b
commit 0e42ab03ca
18 changed files with 197 additions and 174 deletions

View file

@ -2,7 +2,7 @@ use std::borrow::Borrow;
use std::fmt;
use std::hash::Hash;
use std::path::PathBuf;
use std::rc::Rc;
use std::sync::Arc;
use erg_common::config::ErgConfig;
use erg_common::dict::Dict;
@ -32,7 +32,7 @@ impl ModId {
pub struct ModuleEntry {
pub id: ModId, // builtin == 0, __main__ == 1
pub hir: Option<HIR>,
pub module: Rc<ModuleContext>,
pub module: Arc<ModuleContext>,
}
impl fmt::Display for ModuleEntry {
@ -50,7 +50,7 @@ impl ModuleEntry {
Self {
id,
hir,
module: Rc::new(ctx),
module: Arc::new(ctx),
}
}
@ -58,7 +58,7 @@ impl ModuleEntry {
Self {
id: ModId::builtin(),
hir: None,
module: Rc::new(ctx),
module: Arc::new(ctx),
}
}
@ -189,7 +189,7 @@ impl SharedModuleCache {
ref_.get_mut(path)
}
pub fn get_ctx<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<Rc<ModuleContext>>
pub fn get_ctx<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<Arc<ModuleContext>>
where
PathBuf: Borrow<Q>,
{
@ -236,7 +236,7 @@ impl SharedModuleCache {
for path in self.keys() {
self.remove(&path);
}
self.register(builtin_path, None, Rc::try_unwrap(builtin.module).unwrap());
self.register(builtin_path, None, Arc::try_unwrap(builtin.module).unwrap());
}
pub fn rename_path(&self, path: &PathBuf, new: PathBuf) {

View file

@ -4,7 +4,7 @@ use std::hash::Hash;
use erg_common::dict::Dict;
use erg_common::set::Set;
use erg_common::shared::Shared;
use erg_common::shared::{MappedRwLockWriteGuard, RwLockWriteGuard, Shared};
use erg_common::Str;
use crate::context::TraitImpl;
@ -84,12 +84,13 @@ impl SharedTraitImpls {
ref_.get(path)
}
pub fn get_mut<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> Option<&mut Set<TraitImpl>>
pub fn get_mut<Q: Eq + Hash + ?Sized>(&self, path: &Q) -> MappedRwLockWriteGuard<Set<TraitImpl>>
where
Str: Borrow<Q>,
{
let ref_ = unsafe { self.0.as_ptr().as_mut().unwrap() };
ref_.get_mut(path)
// let ref_ = unsafe { self.0.as_ptr().as_mut().unwrap() };
// ref_.get_mut(path)
RwLockWriteGuard::map(self.0.borrow_mut(), |tis| tis.get_mut(path).unwrap())
}
pub fn register(&self, name: Str, impls: Set<TraitImpl>) {