Skip to content
This repository has been archived by the owner on Jul 29, 2024. It is now read-only.

Commit

Permalink
Don't block on data access (#159)
Browse files Browse the repository at this point in the history
* Impl DataHolder for Function

* Impl data holder for the rest

* Fix warnings
  • Loading branch information
gavrilikhin-d authored May 11, 2024
1 parent cb184c8 commit 74fdd0e
Show file tree
Hide file tree
Showing 26 changed files with 111 additions and 72 deletions.
23 changes: 23 additions & 0 deletions src/data_holder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use std::sync::{Arc, RwLock, RwLockReadGuard, RwLockWriteGuard, TryLockResult};

/// Trait for structs that hold some data
pub trait DataHolder {
/// The type of the data
type Data;

/// Create a holder for some data
fn new(data: Self::Data) -> Self;

/// Get a reference to the inner data
fn inner(&self) -> &Arc<RwLock<Self::Data>>;

/// Lock for reading
fn read(&self) -> TryLockResult<RwLockReadGuard<'_, Self::Data>> {
self.inner().try_read()
}

/// Lock for writing
fn write(&self) -> TryLockResult<RwLockWriteGuard<'_, Self::Data>> {
self.inner().try_write()
}
}
40 changes: 16 additions & 24 deletions src/hir/declarations/function.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::borrow::Cow;
use std::fmt::Display;
use std::ops::Range;
use std::sync::{Arc, LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::sync::{Arc, RwLock};

use derive_more::From;
use derive_visitor::DriveMut;
Expand All @@ -11,6 +11,7 @@ use crate::hir::{FunctionType, Generic, Statement, Type, TypeReference, Typed};
use crate::mutability::Mutable;
use crate::named::Named;
use crate::syntax::{Identifier, Keyword, Ranged};
use crate::DataHolder;

use super::Trait;

Expand All @@ -20,22 +21,17 @@ pub struct Parameter {
inner: Arc<RwLock<ParameterData>>,
}

impl Parameter {
/// Create a new parameter from its data
pub fn new(data: ParameterData) -> Self {
impl DataHolder for Parameter {
type Data = ParameterData;

fn new(data: Self::Data) -> Self {
Self {
inner: Arc::new(RwLock::new(data)),
}
}

/// Lock variable for reading
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, ParameterData>> {
self.inner.read()
}

/// Lock variable for writing
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, ParameterData>> {
self.inner.write()
fn inner(&self) -> &Arc<RwLock<Self::Data>> {
&self.inner
}
}

Expand Down Expand Up @@ -179,22 +175,17 @@ pub struct Function {
inner: Arc<RwLock<FunctionData>>,
}

impl Function {
/// Create a new function from its data
pub fn new(data: FunctionData) -> Self {
Function {
impl DataHolder for Function {
type Data = FunctionData;

fn new(data: Self::Data) -> Self {
Self {
inner: Arc::new(RwLock::new(data)),
}
}

/// Lock function for reading
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, FunctionData>> {
self.inner.read()
}

/// Lock function for writing
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, FunctionData>> {
self.inner.write()
fn inner(&self) -> &Arc<RwLock<Self::Data>> {
&self.inner
}
}

Expand Down Expand Up @@ -548,6 +539,7 @@ mod tests {
},
semantics::ToHIR,
syntax::{Identifier, Keyword},
DataHolder,
};

use pretty_assertions::assert_eq;
Expand Down
2 changes: 2 additions & 0 deletions src/hir/declarations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use derive_more::Display;

use crate::{named::Named, syntax::Ranged};

use crate::DataHolder;

/// Any PPL declaration
#[derive(Debug, Display, PartialEq, Eq, Clone, From, TryInto, DriveMut)]
pub enum Declaration {
Expand Down
20 changes: 8 additions & 12 deletions src/hir/declarations/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{
fmt::Display,
hash::{Hash, Hasher},
ops::Range,
sync::{Arc, LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard},
sync::{Arc, RwLock},
};

use derive_visitor::DriveMut;
Expand All @@ -17,29 +17,25 @@ use crate::{
};

use super::Function;
use crate::DataHolder;

/// Trait data holder
#[derive(Debug, Clone)]
pub struct Trait {
inner: Arc<RwLock<TraitData>>,
}

impl Trait {
/// Create a new function from its data
pub fn new(data: TraitData) -> Self {
impl DataHolder for Trait {
type Data = TraitData;

fn new(data: Self::Data) -> Self {
Self {
inner: Arc::new(RwLock::new(data)),
}
}

/// Lock function for reading
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, TraitData>> {
self.inner.read()
}

/// Lock function for writing
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, TraitData>> {
self.inner.write()
fn inner(&self) -> &Arc<RwLock<Self::Data>> {
&self.inner
}
}

Expand Down
40 changes: 17 additions & 23 deletions src/hir/declarations/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
hash::Hash,
ops::Range,
str::FromStr,
sync::{Arc, LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard},
sync::{Arc, RwLock},
};

use derive_visitor::DriveMut;
Expand All @@ -17,28 +17,25 @@ use crate::{
AddSourceLocation,
};

use crate::DataHolder;

/// Member data holder
#[derive(Debug, Clone)]
pub struct Member {
inner: Arc<RwLock<MemberData>>,
}

impl Member {
/// Create a new member from its data
pub fn new(data: MemberData) -> Self {
impl DataHolder for Member {
type Data = MemberData;

fn new(data: Self::Data) -> Self {
Self {
inner: Arc::new(RwLock::new(data)),
}
}

/// Lock Member for reading
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, MemberData>> {
self.inner.read()
}

/// Lock Member for writing
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, MemberData>> {
self.inner.write()
fn inner(&self) -> &Arc<RwLock<Self::Data>> {
&self.inner
}
}

Expand Down Expand Up @@ -187,24 +184,21 @@ pub struct Class {
inner: Arc<RwLock<ClassData>>,
}

impl Class {
/// Create a new class from its data
pub fn new(data: ClassData) -> Self {
impl DataHolder for Class {
type Data = ClassData;

fn new(data: Self::Data) -> Self {
Class {
inner: Arc::new(RwLock::new(data)),
}
}

/// Lock class for reading
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, ClassData>> {
self.inner.read()
}

/// Lock class for writing
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, ClassData>> {
self.inner.write()
fn inner(&self) -> &Arc<RwLock<Self::Data>> {
&self.inner
}
}

impl Class {
/// Is this a builtin type?
pub fn is_builtin(&self) -> bool {
self.read().unwrap().is_builtin()
Expand Down
23 changes: 11 additions & 12 deletions src/hir/declarations/variable.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::fmt::Display;
use std::sync::{Arc, LockResult, RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::sync::{Arc, RwLock};

use derive_visitor::DriveMut;

Expand All @@ -9,30 +9,29 @@ use crate::mutability::{Mutability, Mutable};
use crate::named::Named;
use crate::syntax::{Identifier, Keyword, Ranged};

use crate::DataHolder;

/// Variable data holder
#[derive(Debug, Clone)]
pub struct Variable {
inner: Arc<RwLock<VariableData>>,
}

impl Variable {
/// Create a new variable from its data
pub fn new(data: VariableData) -> Self {
impl DataHolder for Variable {
type Data = VariableData;

fn new(data: Self::Data) -> Self {
Self {
inner: Arc::new(RwLock::new(data)),
}
}

/// Lock variable for reading
pub fn read(&self) -> LockResult<RwLockReadGuard<'_, VariableData>> {
self.inner.read()
}

/// Lock variable for writing
pub fn write(&self) -> LockResult<RwLockWriteGuard<'_, VariableData>> {
self.inner.write()
fn inner(&self) -> &Arc<RwLock<Self::Data>> {
&self.inner
}
}

impl Variable {
/// Is this a temporary variable?
pub fn is_temporary(&self) -> bool {
self.read().unwrap().is_temporary()
Expand Down
2 changes: 2 additions & 0 deletions src/hir/expressions/call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use crate::syntax::Ranged;
use std::fmt::Display;
use std::ops::Range;

use crate::DataHolder;

/// AST for function call
#[derive(Debug, PartialEq, Eq, Clone, DriveMut)]
pub struct Call {
Expand Down
1 change: 1 addition & 0 deletions src/hir/expressions/variable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::hir::{Generic, Parameter, ParameterData, Type, Typed, Variable, Varia
use crate::mutability::Mutable;
use crate::named::Named;
use crate::syntax::Ranged;
use crate::DataHolder;
use std::borrow::Cow;
use std::fmt::Display;

Expand Down
1 change: 1 addition & 0 deletions src/hir/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use miette::NamedSource;

use crate::hir::{Statement, Variable};
use crate::named::Named;
use crate::DataHolder;
use crate::SourceFile;

use super::{Class, Function, Trait, Type};
Expand Down
2 changes: 2 additions & 0 deletions src/hir/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::collections::HashMap;

use super::{Class, ClassData, FunctionType, Generic, Member, MemberData, Type, Typed};

use crate::DataHolder;

/// Specialize type using given mapping
pub trait Specialize
where
Expand Down
2 changes: 2 additions & 0 deletions src/hir/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ use derive_more::{Display, From, TryInto};
use derive_visitor::DriveMut;
use enum_dispatch::enum_dispatch;

use crate::DataHolder;

use super::Expression;

/// PPL's Function type
Expand Down
1 change: 1 addition & 0 deletions src/ir/to_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use crate::ir::Initializer;
use crate::mutability::Mutable;
use crate::named::Named;
use crate::syntax::Ranged;
use crate::DataHolder;

use super::Context;
use super::FunctionContext;
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,8 @@ pub use reporter::*;
mod err_vec;
pub use err_vec::*;

mod data_holder;
pub use data_holder::*;

#[cfg(test)]
mod tests;
2 changes: 2 additions & 0 deletions src/semantics/contexts/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{

use super::BuiltinContext;

use crate::DataHolder;

/// Trait for various AST lowering contexts
pub trait Context: FindDeclaration + AddDeclaration + Display {
/// Get parent context
Expand Down
1 change: 1 addition & 0 deletions src/semantics/contexts/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{
hir::{Class, Function, ParameterOrVariable, Trait, Type, Variable},
named::Named,
semantics::{AddDeclaration, FindDeclaration, FindDeclarationHere},
DataHolder,
};

use super::Context;
Expand Down
2 changes: 2 additions & 0 deletions src/semantics/contexts/trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ use crate::{

use super::Context;

use crate::DataHolder;

/// Context for lowering body of trait
pub struct TraitContext<'p> {
/// Trait, which is being lowered
Expand Down
2 changes: 2 additions & 0 deletions src/semantics/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use super::{
Context, Implements, Implicit,
};

use crate::DataHolder;

/// Trait to check if one type is convertible to another
pub trait ConvertibleTo
where
Expand Down
2 changes: 2 additions & 0 deletions src/semantics/declare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use super::{
TraitContext,
};

use crate::DataHolder;

/// Trait to pre-declare something
pub trait Declare {
type Declaration;
Expand Down
Loading

0 comments on commit 74fdd0e

Please sign in to comment.