@@ -12,10 +12,10 @@ use prelude::*;
12
12
13
13
use sync:: atomic:: { mod, AtomicUint } ;
14
14
use sync:: poison:: { mod, LockResult } ;
15
- use sync:: CondvarGuard ;
16
15
use sys_common:: condvar as sys;
17
16
use sys_common:: mutex as sys_mutex;
18
17
use time:: Duration ;
18
+ use sync:: { mutex, MutexGuard } ;
19
19
20
20
/// A Condition Variable
21
21
///
@@ -57,6 +57,7 @@ use time::Duration;
57
57
/// started = cvar.wait(started).unwrap();
58
58
/// }
59
59
/// ```
60
+ #[ stable]
60
61
pub struct Condvar { inner : Box < StaticCondvar > }
61
62
62
63
unsafe impl Send for Condvar { }
@@ -74,6 +75,7 @@ unsafe impl Sync for Condvar {}
74
75
///
75
76
/// static CVAR: StaticCondvar = CONDVAR_INIT;
76
77
/// ```
78
+ #[ unstable = "may be merged with Condvar in the future" ]
77
79
pub struct StaticCondvar {
78
80
inner : sys:: Condvar ,
79
81
mutex : AtomicUint ,
@@ -83,24 +85,16 @@ unsafe impl Send for StaticCondvar {}
83
85
unsafe impl Sync for StaticCondvar { }
84
86
85
87
/// Constant initializer for a statically allocated condition variable.
88
+ #[ unstable = "may be merged with Condvar in the future" ]
86
89
pub const CONDVAR_INIT : StaticCondvar = StaticCondvar {
87
90
inner : sys:: CONDVAR_INIT ,
88
91
mutex : atomic:: INIT_ATOMIC_UINT ,
89
92
} ;
90
93
91
- /// A trait for vaules which can be passed to the waiting methods of condition
92
- /// variables. This is implemented by the mutex guards in this module.
93
- ///
94
- /// Note that this trait should likely not be implemented manually unless you
95
- /// really know what you're doing.
96
- pub trait AsGuard {
97
- #[ allow( missing_docs) ]
98
- fn as_guard ( & self ) -> CondvarGuard ;
99
- }
100
-
101
94
impl Condvar {
102
95
/// Creates a new condition variable which is ready to be waited on and
103
96
/// notified.
97
+ #[ stable]
104
98
pub fn new ( ) -> Condvar {
105
99
Condvar {
106
100
inner : box StaticCondvar {
@@ -136,11 +130,12 @@ impl Condvar {
136
130
/// over time. Each condition variable is dynamically bound to exactly one
137
131
/// mutex to ensure defined behavior across platforms. If this functionality
138
132
/// is not desired, then unsafe primitives in `sys` are provided.
139
- pub fn wait < T : AsGuard > ( & self , mutex_guard : T )
140
- -> LockResult < T > {
133
+ #[ stable]
134
+ pub fn wait < ' a , T > ( & self , guard : MutexGuard < ' a , T > )
135
+ -> LockResult < MutexGuard < ' a , T > > {
141
136
unsafe {
142
137
let me: & ' static Condvar = & * ( self as * const _ ) ;
143
- me. inner . wait ( mutex_guard )
138
+ me. inner . wait ( guard )
144
139
}
145
140
}
146
141
@@ -164,11 +159,11 @@ impl Condvar {
164
159
// provide. There are also additional concerns about the unix-specific
165
160
// implementation which may need to be addressed.
166
161
#[ allow( dead_code) ]
167
- fn wait_timeout < T : AsGuard > ( & self , mutex_guard : T , dur : Duration )
168
- -> LockResult < ( T , bool ) > {
162
+ fn wait_timeout < ' a , T > ( & self , guard : MutexGuard < ' a , T > , dur : Duration )
163
+ -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
169
164
unsafe {
170
165
let me: & ' static Condvar = & * ( self as * const _ ) ;
171
- me. inner . wait_timeout ( mutex_guard , dur)
166
+ me. inner . wait_timeout ( guard , dur)
172
167
}
173
168
}
174
169
@@ -179,6 +174,7 @@ impl Condvar {
179
174
/// `notify_one` are not buffered in any way.
180
175
///
181
176
/// To wake up all threads, see `notify_one()`.
177
+ #[ stable]
182
178
pub fn notify_one ( & self ) { unsafe { self . inner . inner . notify_one ( ) } }
183
179
184
180
/// Wake up all blocked threads on this condvar.
@@ -188,6 +184,7 @@ impl Condvar {
188
184
/// way.
189
185
///
190
186
/// To wake up only one thread, see `notify_one()`.
187
+ #[ stable]
191
188
pub fn notify_all ( & self ) { unsafe { self . inner . inner . notify_all ( ) } }
192
189
}
193
190
@@ -202,17 +199,19 @@ impl StaticCondvar {
202
199
/// notification.
203
200
///
204
201
/// See `Condvar::wait`.
205
- pub fn wait < T : AsGuard > ( & ' static self , mutex_guard : T ) -> LockResult < T > {
202
+ #[ unstable = "may be merged with Condvar in the future" ]
203
+ pub fn wait < ' a , T > ( & ' static self , guard : MutexGuard < ' a , T > )
204
+ -> LockResult < MutexGuard < ' a , T > > {
206
205
let poisoned = unsafe {
207
- let cvar_guard = mutex_guard . as_guard ( ) ;
208
- self . verify ( cvar_guard . lock ) ;
209
- self . inner . wait ( cvar_guard . lock ) ;
210
- cvar_guard . poisoned . get ( )
206
+ let lock = mutex :: guard_lock ( & guard ) ;
207
+ self . verify ( lock) ;
208
+ self . inner . wait ( lock) ;
209
+ mutex :: guard_poison ( & guard ) . get ( )
211
210
} ;
212
211
if poisoned {
213
- Err ( poison:: new_poison_error ( mutex_guard ) )
212
+ Err ( poison:: new_poison_error ( guard ) )
214
213
} else {
215
- Ok ( mutex_guard )
214
+ Ok ( guard )
216
215
}
217
216
}
218
217
@@ -221,29 +220,31 @@ impl StaticCondvar {
221
220
///
222
221
/// See `Condvar::wait_timeout`.
223
222
#[ allow( dead_code) ] // may want to stabilize this later, see wait_timeout above
224
- fn wait_timeout < T : AsGuard > ( & ' static self , mutex_guard : T , dur : Duration )
225
- -> LockResult < ( T , bool ) > {
223
+ fn wait_timeout < ' a , T > ( & ' static self , guard : MutexGuard < ' a , T > , dur : Duration )
224
+ -> LockResult < ( MutexGuard < ' a , T > , bool ) > {
226
225
let ( poisoned, success) = unsafe {
227
- let cvar_guard = mutex_guard . as_guard ( ) ;
228
- self . verify ( cvar_guard . lock ) ;
229
- let success = self . inner . wait_timeout ( cvar_guard . lock , dur) ;
230
- ( cvar_guard . poisoned . get ( ) , success)
226
+ let lock = mutex :: guard_lock ( & guard ) ;
227
+ self . verify ( lock) ;
228
+ let success = self . inner . wait_timeout ( lock, dur) ;
229
+ ( mutex :: guard_poison ( & guard ) . get ( ) , success)
231
230
} ;
232
231
if poisoned {
233
- Err ( poison:: new_poison_error ( ( mutex_guard , success) ) )
232
+ Err ( poison:: new_poison_error ( ( guard , success) ) )
234
233
} else {
235
- Ok ( ( mutex_guard , success) )
234
+ Ok ( ( guard , success) )
236
235
}
237
236
}
238
237
239
238
/// Wake up one blocked thread on this condvar.
240
239
///
241
240
/// See `Condvar::notify_one`.
241
+ #[ unstable = "may be merged with Condvar in the future" ]
242
242
pub fn notify_one ( & ' static self ) { unsafe { self . inner . notify_one ( ) } }
243
243
244
244
/// Wake up all blocked threads on this condvar.
245
245
///
246
246
/// See `Condvar::notify_all`.
247
+ #[ unstable = "may be merged with Condvar in the future" ]
247
248
pub fn notify_all ( & ' static self ) { unsafe { self . inner . notify_all ( ) } }
248
249
249
250
/// Deallocate all resources associated with this static condvar.
@@ -252,6 +253,7 @@ impl StaticCondvar {
252
253
/// active users of the condvar, and this also doesn't prevent any future
253
254
/// users of the condvar. This method is required to be called to not leak
254
255
/// memory on all platforms.
256
+ #[ unstable = "may be merged with Condvar in the future" ]
255
257
pub unsafe fn destroy ( & ' static self ) {
256
258
self . inner . destroy ( )
257
259
}
0 commit comments