lang: current state for archival purposes
This commit is contained in:
parent
37651a83bc
commit
1c6180aabc
10 changed files with 775 additions and 59 deletions
|
@ -20,6 +20,9 @@ num_cpus = "1.16"
|
|||
dashmap = "5.5.3"
|
||||
crossbeam = "0.8.4"
|
||||
owo-colors = {version = "4", features = ["supports-colors"]}
|
||||
smol = "2"
|
||||
futures-lite = "2.3"
|
||||
easy-parallel = "3.3.1"
|
||||
|
||||
[lints]
|
||||
workspace = true
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
#![feature(type_alias_impl_trait, lint_reasons)]
|
||||
#![feature(type_alias_impl_trait, lint_reasons, box_into_inner)]
|
||||
|
||||
use crate::lst_parser::syntax_kind::SyntaxKind;
|
||||
|
||||
|
|
|
@ -209,4 +209,9 @@ impl Output {
|
|||
pub fn errors(&self) -> Vec<SyntaxError> {
|
||||
self.errors.clone()
|
||||
}
|
||||
|
||||
pub fn dissolve(self) -> (GreenNode, Vec<SyntaxError>) {
|
||||
let Self { green_node, errors } = self;
|
||||
(green_node, errors)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,11 +1,19 @@
|
|||
use std::{
|
||||
fmt::Display,
|
||||
path::{Path, PathBuf},
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use crate::world::world_creation_pool::WorldCreationPool;
|
||||
|
||||
use self::{files::Files, registry::Registry};
|
||||
use easy_parallel::Parallel;
|
||||
use futures_lite::future;
|
||||
use smol::{
|
||||
channel::{unbounded, Sender},
|
||||
Executor,
|
||||
};
|
||||
|
||||
use self::{error::Error, files::Files, registry::Registry};
|
||||
|
||||
pub mod error;
|
||||
pub mod files;
|
||||
|
|
|
@ -7,50 +7,55 @@ use std::{
|
|||
use dashmap::DashMap;
|
||||
use rowan::ast::{AstNode, AstPtr};
|
||||
|
||||
use crate::lst_parser::{
|
||||
error::SyntaxError, grammar::source_file, input::Input, output::Output, syntax_kind, Parser,
|
||||
use crate::{
|
||||
lst_parser::{
|
||||
error::SyntaxError, grammar::source_file, input::Input, output::Output, syntax_kind, Parser,
|
||||
},
|
||||
Lang,
|
||||
};
|
||||
|
||||
use super::{error::Error, modules::Module, nodes};
|
||||
|
||||
pub struct Loc<N: AstNode> {
|
||||
#[derive(Clone)]
|
||||
pub struct Loc<N: AstNode<Language = Lang>> {
|
||||
file: FileId,
|
||||
syntax: AstPtr<N>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Files(Arc<FilesInner>);
|
||||
|
||||
impl Files {
|
||||
pub fn new() -> Self {
|
||||
Self(Arc::new(FilesInner::new()))
|
||||
impl<N: AstNode<Language = Lang>> Loc<N> {
|
||||
pub fn new(node: N, file: FileId) -> Self {
|
||||
Self {
|
||||
file,
|
||||
syntax: AstPtr::new(&node),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn add_and_parse(&self, file: &Path) -> Result<(FileId, Vec<Error>), std::io::Error> {
|
||||
self.0.add_and_parse(file)
|
||||
pub fn file(&self) -> FileId {
|
||||
self.file
|
||||
}
|
||||
|
||||
pub fn get(&self, id: FileId) -> Arc<SourceFile> {
|
||||
self.0.get(id)
|
||||
pub fn syntax(&self) -> AstPtr<N> {
|
||||
self.syntax.clone()
|
||||
}
|
||||
}
|
||||
|
||||
/// global file store
|
||||
/// contains all known files etc.
|
||||
struct FilesInner {
|
||||
paths: Mutex<Vec<PathBuf>>,
|
||||
store: DashMap<PathBuf, Arc<SourceFile>>,
|
||||
#[derive(Clone)]
|
||||
pub struct Files {
|
||||
paths: Arc<Mutex<Vec<PathBuf>>>,
|
||||
store: Arc<DashMap<PathBuf, Arc<SourceFile>>>,
|
||||
}
|
||||
|
||||
impl FilesInner {
|
||||
fn new() -> Self {
|
||||
impl Files {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
paths: Mutex::new(Vec::new()),
|
||||
store: DashMap::new(),
|
||||
paths: Arc::new(Mutex::new(Vec::new())),
|
||||
store: Arc::new(DashMap::new()),
|
||||
}
|
||||
}
|
||||
|
||||
fn add_and_parse(&self, path: &Path) -> Result<(FileId, Vec<Error>), std::io::Error> {
|
||||
pub fn add_and_parse(&self, path: &Path) -> Result<(FileId, Vec<Error>), std::io::Error> {
|
||||
let (file, errors) = SourceFile::read_and_parse(&path)?;
|
||||
|
||||
// add file to paths and unlock again
|
||||
|
@ -73,7 +78,7 @@ impl FilesInner {
|
|||
))
|
||||
}
|
||||
|
||||
fn get(&self, id: FileId) -> Arc<SourceFile> {
|
||||
pub fn get(&self, id: FileId) -> Arc<SourceFile> {
|
||||
let path = {
|
||||
let paths = self.paths.lock().unwrap();
|
||||
paths[id.0].clone()
|
||||
|
@ -81,10 +86,19 @@ impl FilesInner {
|
|||
|
||||
self.store.get(&path).unwrap().clone()
|
||||
}
|
||||
|
||||
pub fn get_path(&self, id: FileId) -> PathBuf {
|
||||
let paths = self.paths.lock().unwrap();
|
||||
paths[id.0].clone()
|
||||
}
|
||||
|
||||
pub fn path_store(&self) -> Arc<Mutex<Vec<PathBuf>>> {
|
||||
self.paths.clone()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct SourceFile {
|
||||
pub lst: Mutex<nodes::Root>,
|
||||
pub lst: RwLock<rowan::GreenNode>,
|
||||
root_module: Option<Arc<Module>>,
|
||||
}
|
||||
|
||||
|
@ -100,14 +114,15 @@ impl SourceFile {
|
|||
|
||||
let events = parser.finish();
|
||||
let out = Output::from_parser_output(toks, events);
|
||||
let lst = out.syntax();
|
||||
// let lst = out.syntax();
|
||||
let (lst, errors) = out.dissolve();
|
||||
|
||||
Ok((
|
||||
Self {
|
||||
lst: Mutex::new(nodes::Root::cast(lst).unwrap()),
|
||||
lst: RwLock::new(lst),
|
||||
root_module: None,
|
||||
},
|
||||
out.errors(),
|
||||
errors,
|
||||
))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
use std::sync::Arc;
|
||||
use std::{
|
||||
string::String,
|
||||
sync::{Arc, Mutex},
|
||||
};
|
||||
|
||||
use dashmap::{DashMap, DashSet};
|
||||
use rowan::ast::AstNode;
|
||||
use rowan::ast::{AstNode, AstPtr};
|
||||
|
||||
use crate::SyntaxNode;
|
||||
|
||||
use super::{
|
||||
files::{FileId, Loc},
|
||||
nodes,
|
||||
registry::ItemPath,
|
||||
world_creation_pool::WorkerCtx,
|
||||
// world_creation_pool::WorkerCtx,
|
||||
world_creation_pool::{wait_blocker::WaitBlocker, Task, WorkerCtx},
|
||||
};
|
||||
|
||||
pub struct Module {
|
||||
|
@ -21,18 +25,151 @@ pub struct Module {
|
|||
}
|
||||
|
||||
impl Module {
|
||||
pub fn parse_file(ctx: WorkerCtx, file: FileId, decl: Option<Loc<nodes::Mod>>) {
|
||||
let f = ctx.files.get(file);
|
||||
pub fn parse_mod_body(
|
||||
ctx: &WorkerCtx,
|
||||
tree: SyntaxNode,
|
||||
path: ItemPath,
|
||||
file: FileId,
|
||||
) -> (DashMap<String, Arc<Mutex<Option<Arc<Module>>>>>, Vec<Task>) {
|
||||
let children: Vec<(String, Arc<Mutex<Option<Arc<Module>>>>, nodes::Mod)> = tree
|
||||
.children()
|
||||
.filter_map(|c| nodes::Mod::cast(c))
|
||||
.map(|m| {
|
||||
let name = nodes::ModName::cast(m.syntax().first_child().unwrap())
|
||||
.expect("ModName should always be first child of Mod");
|
||||
(
|
||||
name.syntax().text().to_string(),
|
||||
Arc::new(Mutex::new(None)),
|
||||
m,
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
let retv: DashMap<String, Arc<Mutex<Option<Arc<Module>>>>> = children
|
||||
.iter()
|
||||
.map(|(name, mod_r, _)| (name.to_owned(), mod_r.clone()))
|
||||
.collect();
|
||||
let tasks = children
|
||||
.into_iter()
|
||||
.map(|(name, mod_r, loc)| {
|
||||
let mut path = path.clone();
|
||||
path.push(name);
|
||||
Task::ParseMod(Loc::new(loc, file), mod_r, path)
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let tree = f.lst.lock().unwrap();
|
||||
(retv, tasks)
|
||||
}
|
||||
pub fn parse_file0(
|
||||
ctx: &mut WorkerCtx,
|
||||
file: FileId,
|
||||
path: ItemPath,
|
||||
decl: Option<Loc<nodes::Mod>>,
|
||||
) {
|
||||
let tree = ctx.get_tree(file);
|
||||
|
||||
let children = (&*tree).syntax().children();
|
||||
let (retv, tasks) = Self::parse_mod_body(ctx, tree, path.clone(), file);
|
||||
|
||||
ctx.send_tasks(
|
||||
Task::ParseFileMod1 {
|
||||
file,
|
||||
decl,
|
||||
ret: retv.into(),
|
||||
path,
|
||||
},
|
||||
tasks,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn parse_mod(
|
||||
ctx: &mut WorkerCtx,
|
||||
loc: Loc<nodes::Mod>,
|
||||
path: ItemPath,
|
||||
ret: Arc<Mutex<Option<Module>>>,
|
||||
blocker: WaitBlocker,
|
||||
) {
|
||||
let mod_decl = ctx.resolve_loc(loc);
|
||||
let children = mod_decl.syntax().children().collect::<Vec<_>>();
|
||||
|
||||
if children.len() == 1 {
|
||||
// TODO: file mod
|
||||
todo!()
|
||||
} else if children.len() == 2 {
|
||||
// inline mod
|
||||
}
|
||||
}
|
||||
|
||||
pub fn parse_inline_mod(
|
||||
ctx: &mut WorkerCtx,
|
||||
decl: Loc<nodes::Mod>,
|
||||
path: ItemPath,
|
||||
ret: Arc<Mutex<Option<Arc<Module>>>>,
|
||||
blocker: WaitBlocker,
|
||||
) {
|
||||
let mod_decl_children = ctx
|
||||
.resolve_loc(decl.clone())
|
||||
.syntax()
|
||||
.children()
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
assert_eq!(mod_decl_children.len(), 2);
|
||||
|
||||
let name = nodes::ModName::cast(mod_decl_children[0].clone()).unwrap();
|
||||
let body = nodes::ModBody::cast(mod_decl_children[1].clone()).unwrap();
|
||||
|
||||
let (retv, tasks) =
|
||||
Self::parse_mod_body(ctx, body.syntax().clone(), path.clone(), decl.file());
|
||||
|
||||
ctx.send_tasks(
|
||||
Task::CompleteMod {
|
||||
ret_self: ret,
|
||||
body: ModuleBody::Inline(Loc::new(body, decl.file())),
|
||||
decl: Some(decl),
|
||||
path,
|
||||
child_mods: retv.into(),
|
||||
blocker,
|
||||
},
|
||||
tasks,
|
||||
)
|
||||
}
|
||||
|
||||
pub fn complete_mod(
|
||||
ctx: &mut WorkerCtx,
|
||||
decl: Option<Loc<nodes::Mod>>,
|
||||
ret_self: Arc<Mutex<Option<Arc<Module>>>>,
|
||||
body: ModuleBody,
|
||||
path: ItemPath,
|
||||
child_mods: Arc<DashMap<String, Arc<Mutex<Option<Arc<Module>>>>>>,
|
||||
blocker: WaitBlocker,
|
||||
) {
|
||||
assert!(child_mods
|
||||
.iter()
|
||||
.all(|item| item.value().lock().unwrap().is_some()));
|
||||
|
||||
let module = Arc::new(Module {
|
||||
decl,
|
||||
body,
|
||||
own_path: path,
|
||||
child_modules: Arc::new(
|
||||
Arc::<_>::unwrap_or_clone(child_mods)
|
||||
.into_iter()
|
||||
.map(|(name, module)| {
|
||||
let module = module.lock().unwrap().take().unwrap();
|
||||
(name, module)
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
child_defs: Arc::default(),
|
||||
});
|
||||
|
||||
let mut ret_self = ret_self.lock().unwrap();
|
||||
*ret_self = Some(module);
|
||||
drop(blocker);
|
||||
}
|
||||
}
|
||||
|
||||
struct Def;
|
||||
|
||||
enum ModuleBody {
|
||||
InLine(Loc<nodes::ModBody>),
|
||||
pub enum ModuleBody {
|
||||
Inline(Loc<nodes::ModBody>),
|
||||
File(FileId),
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use rowan::Language;
|
|||
macro_rules! ast_nodes {
|
||||
($($ast:ident, $kind:ident);+) => {
|
||||
$(
|
||||
#[derive(Debug, PartialEq, Eq, Hash)]
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
|
||||
#[repr(transparent)]
|
||||
pub struct $ast(SyntaxNode);
|
||||
impl rowan::ast::AstNode for $ast {
|
||||
|
|
|
@ -1,14 +1,28 @@
|
|||
use std::{path::PathBuf, thread};
|
||||
use std::{
|
||||
collections::HashMap,
|
||||
path::PathBuf,
|
||||
sync::{Arc, Mutex},
|
||||
thread,
|
||||
};
|
||||
|
||||
use crossbeam::channel::{Receiver, Sender};
|
||||
use dashmap::DashMap;
|
||||
use rowan::ast::{AstNode, AstPtr};
|
||||
|
||||
use crate::{Lang, SyntaxNode};
|
||||
|
||||
use self::wait_blocker::WaitBlocker;
|
||||
|
||||
use super::{
|
||||
error::Error,
|
||||
files::{FileId, Files, Loc},
|
||||
modules::{Module, ModuleBody},
|
||||
nodes,
|
||||
registry::Registry,
|
||||
registry::{ItemPath, Registry},
|
||||
};
|
||||
|
||||
pub mod wait_blocker;
|
||||
|
||||
pub(super) struct WorldCreationPool {
|
||||
workers: Vec<Worker>,
|
||||
tx: Sender<Job>,
|
||||
|
@ -24,7 +38,7 @@ impl WorldCreationPool {
|
|||
i,
|
||||
files.clone(),
|
||||
reg.clone(),
|
||||
JobSender(tx.clone()),
|
||||
tx.clone(),
|
||||
rx.clone(),
|
||||
))
|
||||
}
|
||||
|
@ -33,12 +47,31 @@ impl WorldCreationPool {
|
|||
}
|
||||
}
|
||||
|
||||
pub struct JobSender(Sender<Job>);
|
||||
|
||||
enum Job {
|
||||
ParseFileMod {
|
||||
WaitingFor(WaitBlocker, Task),
|
||||
Awaited(WaitBlocker, Task),
|
||||
}
|
||||
|
||||
pub enum Task {
|
||||
ParseMod(Loc<nodes::Mod>, Arc<Mutex<Option<Arc<Module>>>>, ItemPath),
|
||||
ParseFileMod0 {
|
||||
file: FileId,
|
||||
decl: Option<Loc<nodes::Mod>>,
|
||||
path: ItemPath,
|
||||
},
|
||||
ParseFileMod1 {
|
||||
file: FileId,
|
||||
decl: Option<Loc<nodes::Mod>>,
|
||||
path: ItemPath,
|
||||
ret: Arc<DashMap<String, Arc<Mutex<Option<Arc<Module>>>>>>,
|
||||
},
|
||||
CompleteMod {
|
||||
ret_self: Arc<Mutex<Option<Arc<Module>>>>,
|
||||
decl: Option<Loc<nodes::Mod>>,
|
||||
body: ModuleBody,
|
||||
path: ItemPath,
|
||||
child_mods: Arc<DashMap<String, Arc<Mutex<Option<Arc<Module>>>>>>,
|
||||
blocker: WaitBlocker,
|
||||
},
|
||||
OpenFile(PathBuf),
|
||||
}
|
||||
|
@ -51,26 +84,104 @@ struct Worker {
|
|||
|
||||
pub struct WorkerCtx {
|
||||
errors: Vec<Error>,
|
||||
pub files: Files,
|
||||
files: Files,
|
||||
local_files: HashMap<PathBuf, SyntaxNode>,
|
||||
pub reg: Registry,
|
||||
pub tx: JobSender,
|
||||
tx: Sender<Job>,
|
||||
}
|
||||
|
||||
impl WorkerCtx {
|
||||
pub fn get_tree(&mut self, id: FileId) -> SyntaxNode {
|
||||
let p = self.files.get_path(id);
|
||||
if self.local_files.contains_key(&p) {
|
||||
self.local_files[&p].clone()
|
||||
} else {
|
||||
let f = self.files.get(id);
|
||||
let lst = SyntaxNode::new_root(f.lst.read().unwrap().clone());
|
||||
self.local_files.insert(p, lst.clone());
|
||||
lst
|
||||
}
|
||||
}
|
||||
|
||||
pub fn resolve_loc<N: AstNode<Language = Lang>>(&mut self, loc: Loc<N>) -> N {
|
||||
let f = self.get_tree(loc.file());
|
||||
|
||||
loc.syntax().to_node(&f)
|
||||
}
|
||||
|
||||
pub fn send_tasks(&self, task: Task, dependencies: Vec<Task>) {
|
||||
let blocker = WaitBlocker::new();
|
||||
for dep_task in dependencies {
|
||||
self.tx
|
||||
.send(Job::Awaited(blocker.clone(), dep_task))
|
||||
.unwrap();
|
||||
}
|
||||
self.tx.send(Job::WaitingFor(blocker, task)).unwrap();
|
||||
}
|
||||
}
|
||||
|
||||
impl Worker {
|
||||
fn new(id: usize, files: Files, reg: Registry, sender: JobSender, rx: Receiver<Job>) -> Self {
|
||||
let ctx = WorkerCtx {
|
||||
errors: Vec::new(),
|
||||
files,
|
||||
reg,
|
||||
tx: sender,
|
||||
};
|
||||
|
||||
fn new(id: usize, files: Files, reg: Registry, sender: Sender<Job>, rx: Receiver<Job>) -> Self {
|
||||
let thread_handle = thread::spawn(move || {
|
||||
for msg in rx {
|
||||
match msg {
|
||||
Job::ParseFileMod { file, decl } => todo!(),
|
||||
Job::OpenFile(path) => todo!(),
|
||||
let ctx = WorkerCtx {
|
||||
errors: Vec::new(),
|
||||
local_files: HashMap::new(),
|
||||
files,
|
||||
reg,
|
||||
tx: sender,
|
||||
};
|
||||
|
||||
for job in &rx {
|
||||
// if matches!(job, Job::WithCond(_, _)) {
|
||||
|
||||
// }
|
||||
match job {
|
||||
Job::WaitingFor(blocker, task) => {
|
||||
if blocker.is_ready() {
|
||||
Self::do_task(&ctx, task, None)
|
||||
} else if rx.is_empty() {
|
||||
if let Some(blocker) =
|
||||
blocker.wait_for(std::time::Duration::from_millis(50))
|
||||
{
|
||||
ctx.tx.send(Job::WaitingFor(blocker, task)).unwrap();
|
||||
} else {
|
||||
Self::do_task(&ctx, task, None)
|
||||
}
|
||||
} else {
|
||||
ctx.tx.send(Job::WaitingFor(blocker, task)).unwrap();
|
||||
}
|
||||
}
|
||||
Job::Awaited(blocker, task) => {
|
||||
Self::do_task(&ctx, task, Some(blocker.clone()));
|
||||
drop(blocker)
|
||||
}
|
||||
}
|
||||
|
||||
// if let Job::WithCond(blocker, job_inner) = job_msg {
|
||||
// if blocker.is_ready() {
|
||||
// job = Box::<Job>::into_inner(job_inner);
|
||||
// } else if rx.is_empty() {
|
||||
// if let Some(blocker) =
|
||||
// blocker.wait_for(std::time::Duration::from_millis(50))
|
||||
// {
|
||||
// job = Job::WithCond(blocker, job_inner);
|
||||
// } else {
|
||||
// job = Box::<Job>::into_inner(job_inner);
|
||||
// }
|
||||
// } else {
|
||||
// job = Job::WithCond(blocker, job_inner);
|
||||
// }
|
||||
// } else {
|
||||
// job = job_msg;
|
||||
// }
|
||||
|
||||
// match job {
|
||||
// Job::ParseFileMod { file, decl } => todo!(),
|
||||
// Job::OpenFile(path) => todo!(),
|
||||
// Job::WithCond(blocker, job) => {
|
||||
// ctx.tx.send(Job::WithCond(blocker, job)).unwrap()
|
||||
// }
|
||||
// }
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -79,4 +190,26 @@ impl Worker {
|
|||
thread: thread_handle,
|
||||
}
|
||||
}
|
||||
|
||||
fn do_task(ctx: &WorkerCtx, task: Task, blocker: Option<WaitBlocker>) {
|
||||
match task {
|
||||
Task::ParseMod(_, _, _) => todo!(),
|
||||
Task::ParseFileMod0 { file, decl, path } => todo!(),
|
||||
Task::ParseFileMod1 {
|
||||
file,
|
||||
decl,
|
||||
path,
|
||||
ret,
|
||||
} => todo!(),
|
||||
Task::CompleteMod {
|
||||
ret_self,
|
||||
decl,
|
||||
body,
|
||||
path,
|
||||
child_mods,
|
||||
blocker,
|
||||
} => todo!(),
|
||||
Task::OpenFile(_) => todo!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
87
crates/lang/src/world/world_creation_pool/wait_blocker.rs
Normal file
87
crates/lang/src/world/world_creation_pool/wait_blocker.rs
Normal file
|
@ -0,0 +1,87 @@
|
|||
use std::{
|
||||
sync::{Arc, Condvar, Mutex},
|
||||
time::Duration,
|
||||
};
|
||||
|
||||
/// like a WaitGroup from crossbeam, but can also just check if it's the last one
|
||||
pub struct WaitBlocker {
|
||||
inner: Arc<Inner>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
waiting: Mutex<usize>,
|
||||
cvar: Condvar,
|
||||
}
|
||||
|
||||
impl WaitBlocker {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
inner: Arc::new(Inner {
|
||||
waiting: Mutex::new(1),
|
||||
cvar: Condvar::new(),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn wait(self) {
|
||||
if *self.inner.waiting.lock().unwrap() == 1 {
|
||||
return;
|
||||
}
|
||||
|
||||
let inner = self.inner.clone();
|
||||
drop(self);
|
||||
|
||||
inner
|
||||
.cvar
|
||||
.wait_while(inner.waiting.lock().unwrap(), |w| *w > 0);
|
||||
}
|
||||
|
||||
pub fn wait_for(self, dur: Duration) -> Option<Self> {
|
||||
if *self.inner.waiting.lock().unwrap() == 1 {
|
||||
return None;
|
||||
}
|
||||
|
||||
let inner = self.inner.clone();
|
||||
drop(self);
|
||||
|
||||
let (_, timeout_res) = inner
|
||||
.cvar
|
||||
.wait_timeout_while(inner.waiting.lock().unwrap(), dur, |w| *w > 0)
|
||||
.unwrap();
|
||||
|
||||
if timeout_res.timed_out() {
|
||||
None
|
||||
} else {
|
||||
{
|
||||
let mut w = inner.waiting.lock().unwrap();
|
||||
*w += 1;
|
||||
}
|
||||
Some(Self { inner })
|
||||
}
|
||||
}
|
||||
|
||||
pub fn is_ready(&self) -> bool {
|
||||
*self.inner.waiting.lock().unwrap() == 1
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for WaitBlocker {
|
||||
fn clone(&self) -> Self {
|
||||
let mut w = self.inner.waiting.lock().unwrap();
|
||||
*w += 1;
|
||||
drop(w);
|
||||
Self {
|
||||
inner: self.inner.clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WaitBlocker {
|
||||
fn drop(&mut self) {
|
||||
let mut w = self.inner.waiting.lock().unwrap();
|
||||
*w -= 1;
|
||||
if *w == 0 {
|
||||
self.inner.cvar.notify_all()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue