From c9785e8cc5b6a9a722ba0aff1eb33c2dbf020f2e Mon Sep 17 00:00:00 2001 From: hozan23 Date: Thu, 23 May 2024 15:42:07 +0200 Subject: core: use mutex from parking_lot library --- core/Cargo.toml | 1 + core/src/async_util/condvar.rs | 11 ++++++----- core/src/async_util/task_group.rs | 12 +++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) (limited to 'core') diff --git a/core/Cargo.toml b/core/Cargo.toml index 66e7d55..dc13561 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -34,3 +34,4 @@ smol = { version = "2.0.0", optional = true } # tokio feature deps tokio = { version = "1.37.0", features = ["full"], optional = true } once_cell = "1.19.0" +parking_lot = "0.12.2" diff --git a/core/src/async_util/condvar.rs b/core/src/async_util/condvar.rs index c3f373d..8385982 100644 --- a/core/src/async_util/condvar.rs +++ b/core/src/async_util/condvar.rs @@ -2,10 +2,11 @@ use std::{ collections::HashMap, future::Future, pin::Pin, - sync::Mutex, task::{Context, Poll, Waker}, }; +use parking_lot::Mutex; + use crate::{async_runtime::lock::MutexGuard, util::random_16}; /// CondVar is an async version of @@ -80,12 +81,12 @@ impl CondVar { /// Wakes up one blocked task waiting on this condvar. pub fn signal(&self) { - self.inner.lock().unwrap().wake(true); + self.inner.lock().wake(true); } /// Wakes up all blocked tasks waiting on this condvar. pub fn broadcast(&self) { - self.inner.lock().unwrap().wake(false); + self.inner.lock().wake(false); } } @@ -115,7 +116,7 @@ impl<'a, T> Future for CondVarAwait<'a, T> { type Output = (); fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { - let mut inner = self.condvar.inner.lock().unwrap(); + let mut inner = self.condvar.inner.lock(); match self.guard.take() { Some(_) => { @@ -153,7 +154,7 @@ impl<'a, T> Future for CondVarAwait<'a, T> { impl<'a, T> Drop for CondVarAwait<'a, T> { fn drop(&mut self) { if let Some(id) = self.id { - let mut inner = self.condvar.inner.lock().unwrap(); + let mut inner = self.condvar.inner.lock(); if let Some(wk) = inner.wakers.get_mut(&id).unwrap().take() { wk.wake() } diff --git a/core/src/async_util/task_group.rs b/core/src/async_util/task_group.rs index 5af75ed..63c1541 100644 --- a/core/src/async_util/task_group.rs +++ b/core/src/async_util/task_group.rs @@ -1,4 +1,6 @@ -use std::{future::Future, sync::Arc, sync::Mutex}; +use std::{future::Future, sync::Arc}; + +use parking_lot::Mutex; use crate::async_runtime::{global_executor, Executor, Task}; @@ -67,17 +69,17 @@ impl TaskGroup { callback, self.stop_signal.clone(), ); - self.tasks.lock().unwrap().push(task); + self.tasks.lock().push(task); } /// Checks if the TaskGroup is empty. pub fn is_empty(&self) -> bool { - self.tasks.lock().unwrap().is_empty() + self.tasks.lock().is_empty() } /// Get the number of the tasks in the group. pub fn len(&self) -> usize { - self.tasks.lock().unwrap().len() + self.tasks.lock().len() } /// Cancels all tasks in the group. @@ -85,7 +87,7 @@ impl TaskGroup { self.stop_signal.broadcast().await; loop { - let task = self.tasks.lock().unwrap().pop(); + let task = self.tasks.lock().pop(); if let Some(t) = task { t.cancel().await } else { -- cgit v1.2.3