1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
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()
}
}