use std::sync as ssync;
use std::fmt;
mod mioco {
pub use super::super::*;
}
pub mod mpsc;
#[derive(Debug)]
pub struct RwLock<T: ?Sized> {
lock: ssync::RwLock<T>,
}
impl<T> RwLock<T> {
pub fn new(t: T) -> Self {
RwLock { lock: ssync::RwLock::new(t) }
}
}
impl<T: ?Sized> RwLock<T> {
pub fn native_lock(&self) -> &ssync::RwLock<T> {
&self.lock
}
pub fn read(&self) -> ssync::LockResult<ssync::RwLockReadGuard<T>> {
if mioco::in_coroutine() {
self.read_in_mioco()
} else {
self.lock.read()
}
}
fn read_in_mioco(&self) -> ssync::LockResult<ssync::RwLockReadGuard<T>> {
loop {
match self.lock.try_read() {
Ok(guard) => return Ok(guard),
Err(try_error) => {
match try_error {
ssync::TryLockError::Poisoned(p_err) => {
return Err(p_err);
}
ssync::TryLockError::WouldBlock => mioco::yield_now(),
}
}
}
}
}
pub fn try_read(&self) -> ssync::TryLockResult<ssync::RwLockReadGuard<T>> {
self.lock.try_read()
}
pub fn write(&self) -> ssync::LockResult<ssync::RwLockWriteGuard<T>> {
if mioco::in_coroutine() {
self.write_in_mioco()
} else {
self.lock.write()
}
}
fn write_in_mioco(&self) -> ssync::LockResult<ssync::RwLockWriteGuard<T>> {
loop {
match self.lock.try_write() {
Ok(guard) => return Ok(guard),
Err(try_error) => {
match try_error {
ssync::TryLockError::Poisoned(p_err) => {
return Err(p_err);
}
ssync::TryLockError::WouldBlock => mioco::yield_now(),
}
}
}
}
}
pub fn try_write(&self) -> ssync::TryLockResult<ssync::RwLockWriteGuard<T>> {
self.lock.try_write()
}
pub fn is_poisoned(&self) -> bool {
self.lock.is_poisoned()
}
}
pub struct Mutex<T: ?Sized> {
lock: ssync::Mutex<T>,
}
impl<T: ?Sized + fmt::Debug + 'static> fmt::Debug for Mutex<T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.lock.fmt(f)
}
}
impl<T> Mutex<T> {
pub fn new(t: T) -> Self {
Mutex { lock: ssync::Mutex::new(t) }
}
}
impl<T: ?Sized> Mutex<T> {
pub fn native_lock(&self) -> &ssync::Mutex<T> {
&self.lock
}
pub fn lock(&self) -> ssync::LockResult<ssync::MutexGuard<T>> {
if mioco::in_coroutine() {
self.lock_in_mioco()
} else {
self.lock.lock()
}
}
fn lock_in_mioco(&self) -> ssync::LockResult<ssync::MutexGuard<T>> {
loop {
match self.try_lock() {
Ok(guard) => return Ok(guard),
Err(try_error) => {
match try_error {
ssync::TryLockError::Poisoned(p_err) => {
return Err(p_err);
}
ssync::TryLockError::WouldBlock => mioco::yield_now(),
}
}
}
}
}
pub fn try_lock(&self) -> ssync::TryLockResult<ssync::MutexGuard<T>> {
self.lock.try_lock()
}
}