Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to use Mutex / Condvar from std. #140

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ readme = "README.md"
keywords = ["database", "pool"]
edition = "2018"

[features]
default = ["parking_lot"]

[dependencies]
log = "0.4"
parking_lot = "0.12"
parking_lot = { version = "0.12", optional = true }
scheduled-thread-pool = "0.2"
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

use log::error;

use parking_lot::{Condvar, Mutex, MutexGuard};
use std::cmp;
use std::error;
use std::fmt;
Expand All @@ -56,10 +55,12 @@ use crate::config::Config;
use crate::event::{AcquireEvent, CheckinEvent, CheckoutEvent, ReleaseEvent, TimeoutEvent};
pub use crate::event::{HandleEvent, NopEventHandler};
pub use crate::extensions::Extensions;
use crate::sync::*;

mod config;
pub mod event;
mod extensions;
mod sync;

#[cfg(test)]
mod test;
Expand Down Expand Up @@ -395,7 +396,10 @@ where
let initial_size = self.0.config.min_idle.unwrap_or(self.0.config.max_size);

while internals.num_conns != initial_size {
if self.0.cond.wait_until(&mut internals, end).timed_out() {
let (guard, result) = self.0.cond.wait_until(internals, end);
internals = guard;

if result.timed_out() {
return Err(Error(internals.last_error.take()));
}
}
Expand Down Expand Up @@ -435,7 +439,10 @@ where

add_connection(&self.0, &mut internals);

if self.0.cond.wait_until(&mut internals, end).timed_out() {
let (guard, result) = self.0.cond.wait_until(internals, end);
internals = guard;

if result.timed_out() {
let event = TimeoutEvent { timeout };
self.0.config.event_handler.handle_timeout(event);

Expand Down
97 changes: 97 additions & 0 deletions src/sync.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#[cfg(feature = "parking_lot")]
mod parking_lot_mutex {
pub(crate) use parking_lot::{MutexGuard, WaitTimeoutResult};
use std::time::Instant;

pub(crate) struct Mutex<T>(parking_lot::Mutex<T>);

impl<T> Mutex<T> {
#[inline]
pub fn new(t: T) -> Self {
Self(parking_lot::Mutex::new(t))
}

#[inline]
pub fn lock(&self) -> MutexGuard<T> {
self.0.lock()
}
}

pub(crate) struct Condvar(parking_lot::Condvar);

impl Condvar {
#[inline]
pub fn new() -> Self {
Self(parking_lot::Condvar::new())
}

#[inline]
pub fn notify_one(&self) {
self.0.notify_one();
}

#[inline]
pub fn wait_until<'a, T>(
&self,
mut mutex_guard: MutexGuard<'a, T>,
timeout: Instant,
) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
let wait_result = self.0.wait_until(&mut mutex_guard, timeout);

(mutex_guard, wait_result)
}
}
}
#[cfg(feature = "parking_lot")]
pub(crate) use parking_lot_mutex::*;

#[cfg(not(feature = "parking_lot"))]
mod std_mutex {
pub(crate) use std::sync::{MutexGuard, WaitTimeoutResult};
use std::{sync::PoisonError, time::Instant};

pub(crate) struct Mutex<T>(std::sync::Mutex<T>);

impl<T> Mutex<T> {
#[inline]
pub fn new(t: T) -> Self {
Self(std::sync::Mutex::new(t))
}

#[inline]
pub fn lock(&self) -> MutexGuard<T> {
self.0.lock().unwrap_or_else(PoisonError::into_inner)
}
}

pub(crate) struct Condvar(std::sync::Condvar);

impl Condvar {
#[inline]
pub fn new() -> Self {
Self(std::sync::Condvar::new())
}

#[inline]
pub fn notify_one(&self) {
self.0.notify_one()
}

#[inline]
pub fn wait_until<'a, T>(
&self,
mutex_guard: MutexGuard<'a, T>,
timeout: Instant,
) -> (MutexGuard<'a, T>, WaitTimeoutResult) {
let timeout = timeout
.checked_duration_since(Instant::now())
.unwrap_or_default();

self.0
.wait_timeout(mutex_guard, timeout)
.unwrap_or_else(PoisonError::into_inner)
}
}
}
#[cfg(not(feature = "parking_lot"))]
pub(crate) use std_mutex::*;